def test_http_headers(self): data = { 'name': 'testing', 'header': { 'My-Header': 'testing' } } resource = Resource(data) self.assertEqual(resource.header, {'My-Header': 'testing'}) self.assertEqual(resource.http_headers(), {'PayPal-Request-Id': resource.request_id, 'My-Header': 'testing'})
def test_passing_api(self): """ Check that api objects are passed on to new resources when given """ class DummyAPI(object): post = lambda s, *a, **k: {} get = lambda s, *a, **k: {} api = DummyAPI() # Conversion resource = Resource({ 'name': 'testing', }, api=api) self.assertEqual(resource.api, api) convert_ret = resource.convert('test', {}) self.assertEqual(convert_ret.api, api) class TestResource(Find, List, Post): path = '/' # Find find = TestResource.find('resourceid', api=api) self.assertEqual(find.api, api) # List list_ = TestResource.all(api=api) self.assertEqual(list_.api, api) # Post post = TestResource({'id': 'id'}, api=api) post_ret = post.post('test') self.assertEqual(post_ret.api, api)
def test_to_dict(self): data = { "intent": "sale", "payer": { "payment_method": "credit_card", "funding_instruments": [{ "credit_card": { "type": "visa", "number": "4417119669820331", "expire_month": "11", "expire_year": "2018", "cvv2": "874", "first_name": "Joe", "last_name": "Shopper"}}]}, "transactions": [{ "item_list": { "items": [{ "name": "item", "sku": "item", "price": "1.00", "currency": "USD", "quantity": 1}]}, "amount": { "total": "1.00", "currency": "USD"}, "description": "This is the payment transaction description."}]} resource = Resource(data) self.assertEqual(resource.to_dict(), data)
def test_request_id(self): data = { 'name': 'testing', 'request_id': 1234} resource = Resource(data) self.assertEqual(resource.to_dict(), {'name': 'testing'}) self.assertEqual(resource.request_id, 1234) self.assertEqual(resource.http_headers(), {'PayPal-Request-Id': 1234})
def test_setter(self): data = {'name': 'testing'} resource = Resource(data) self.assertEqual(resource.name, 'testing') resource.name = 'changed' self.assertEqual(resource.name, 'changed') resource['name'] = 'again-changed' self.assertEqual(resource.name, 'again-changed') resource.transaction = {'description': 'testing'} self.assertEqual(resource.transaction.__class__, Resource) self.assertEqual(resource.transaction.description, 'testing')
def search_transactions(self, start_date, end_date, api=None): if not start_date or not end_date: raise exceptions.MissingParam( "Search transactions needs valid start_date and end_date.") api = api or default_api() # Construct url similar to # /billing-agreements/I-HT38K76XPMGJ/transactions?start-date=2014-04-13&end-date=2014-04-30 endpoint = util.join_url(self.path, str(self['id']), 'transaction') date_range = [('start-date', start_date), ('end-date', end_date)] url = util.join_url_params(endpoint, date_range) return Resource(self.api.get(url), api=api)
def test_to_dict(self): data = { "intent": "sale", "payer": { "payment_method": "credit_card", "funding_instruments": [{ "credit_card": { "type": "visa", "number": "4417119669820331", "expire_month": "11", "expire_year": "2018", "cvv2": "874", "first_name": "Joe", "last_name": "Shopper" } }] }, "transactions": [{ "item_list": { "items": [{ "name": "item", "sku": "item", "price": "1.00", "currency": "USD", "quantity": 1 }] }, "amount": { "total": "1.00", "currency": "USD" }, "description": "This is the payment transaction description." }] } resource = Resource(data) self.assertEqual(resource.to_dict(), data)
def test_issue_credit(self): """ Tests issuing credit/refund with Paypal processor """ refund = self.create_refund(self.processor_name) order = refund.order basket = order.basket amount = refund.total_credit_excl_tax currency = refund.currency source = order.sources.first() transaction_id = 'PAY-REFUND-1' paypal_refund = paypalrestsdk.Refund({'id': transaction_id}) payment = paypalrestsdk.Payment({ 'transactions': [ Resource({ 'related_resources': [ Resource( {'sale': paypalrestsdk.Sale({'id': 'PAY-SALE-1'})}) ] }) ] }) with mock.patch.object(paypalrestsdk.Payment, 'find', return_value=payment): with mock.patch.object(paypalrestsdk.Sale, 'refund', return_value=paypal_refund): actual_transaction_id = self.processor.issue_credit( order.number, order.basket, source.reference, amount, currency) self.assertEqual(actual_transaction_id, transaction_id) # Verify PaymentProcessorResponse created self.assert_processor_response_recorded(self.processor.NAME, transaction_id, {'id': transaction_id}, basket)
def test_issue_credit(self): refund = self.create_refund(self.processor_name) order = refund.order basket = order.basket amount = refund.total_credit_excl_tax currency = refund.currency source = order.sources.first() transaction_id = 'PAY-REFUND-1' paypal_refund = paypalrestsdk.Refund({'id': transaction_id}) payment = Payment({ 'transactions': [ Resource({ 'related_resources': [Resource({'sale': Sale({'id': 'PAY-SALE-1'})})] }) ] }) with mock.patch.object(Payment, 'find', return_value=payment): with mock.patch.object(Sale, 'refund', return_value=paypal_refund): self.processor.issue_credit(source, amount, currency) # Verify PaymentProcessorResponse created self.assert_processor_response_recorded(self.processor.NAME, transaction_id, {'id': transaction_id}, basket) # Verify Source updated self.assertEqual(source.amount_refunded, amount) # Verify PaymentEvent created paid_type = PaymentEventType.objects.get(code=u'refunded') order = basket.order_set.first() payment_event = order.payment_events.first() self.assert_valid_payment_event_fields(payment_event, amount, paid_type, self.processor.NAME, transaction_id)
def test_issue_credit_error(self): """ Tests issue credit fails in case of erroneous response or exceptions """ refund = self.create_refund(self.processor_name) order = refund.order basket = order.basket amount = refund.total_credit_excl_tax currency = refund.currency source = order.sources.first() transaction_id = 'PAY-REFUND-FAIL-1' expected_response = {'debug_id': transaction_id} paypal_refund = paypalrestsdk.Refund({'error': expected_response}) payment = paypalrestsdk.Payment({ 'transactions': [ Resource({'related_resources': [Resource({'sale': paypalrestsdk.Sale({'id': 'PAY-SALE-1'})})]}) ] }) # Test general exception with mock.patch.object(paypalrestsdk.Payment, 'find', return_value=payment): with mock.patch.object(paypalrestsdk.Sale, 'refund', side_effect=ValueError): self.assertRaises(GatewayError, self.processor.issue_credit, source, amount, currency) self.assertEqual(source.amount_refunded, 0) # Test error response with mock.patch.object(paypalrestsdk.Payment, 'find', return_value=payment): with mock.patch.object(paypalrestsdk.Sale, 'refund', return_value=paypal_refund): self.assertRaises(GatewayError, self.processor.issue_credit, source, amount, currency) # Verify PaymentProcessorResponse created self.assert_processor_response_recorded(self.processor.NAME, transaction_id, expected_response, basket) # Verify Source unchanged self.assertEqual(source.amount_refunded, 0)
def test_getter(self): data = { 'name': 'testing', 'amount': 10.0, 'transaction': { 'description': 'testing' }, 'items': [ { 'name': 'testing' } ] } resource = Resource(data) self.assertEqual(resource.name, 'testing') self.assertEqual(resource['name'], 'testing') self.assertEqual(resource.amount, 10.0) self.assertEqual(resource.items[0].__class__, Resource) self.assertEqual(resource.items[0].name, 'testing') self.assertEqual(resource.items[0]['name'], 'testing') self.assertEqual(resource.unknown, None) self.assertRaises(KeyError, lambda: resource['unknown'])
def get_event_types(self, api=None): """Get the list of events types that are subscribed to a webhook """ api = api or default_api() url = util.join_url(self.path, str(self['id']), 'event-types') return Resource(self.api.get(url), api=api)
def next_invoice_number(cls, api=None): api = api or default_api() url = util.join_url(cls.path, 'next-invoice-number') return Resource(api.post(url), api=api)
def delete_external_refund(self, transactionId): # /invoicing/invoices/<INVOICE-ID>/refund-records/<TRANSACTION-ID> endpoint = util.join_url(self.path, str(self['id']), 'refund-records', str(transactionId)) return Resource(self.api.delete(endpoint), api=self.api)
def test_request_id(self): data = {'name': 'testing', 'request_id': 1234} resource = Resource(data) self.assertEqual(resource.to_dict(), {'name': 'testing'}) self.assertEqual(resource.request_id, 1234) self.assertEqual(resource.http_headers(), {'PayPal-Request-Id': 1234})
def test_contains(self): data = {'name': 'testing'} resource = Resource(data) self.assertEqual('name' in resource, True) self.assertEqual('testing' in resource, False)