class AuthorizeLiveTests(TestCase): def setUp(self): # Random in testing feels gross, otherwise running the same test # suite in quick succession produces failures because Authorize.net # thinks the transactions are duplicates and rejects them self.amount1 = random.randrange(100, 100000) / 100.0 self.amount2 = random.randrange(100, 100000) / 100.0 self.amount3 = random.randrange(100, 1000) / 100.0 self.client = AuthorizeClient(TEST_LOGIN_ID, TEST_TRANSACTION_KEY) self.year = date.today().year + 10 self.credit_card = CreditCard('4111111111111111', self.year, 1, '911', 'Jeff', 'Schenck') self.bank_account = BankAccount(**dict(TEST_BANK_ACCOUNT)) self.address = Address('45 Rose Ave', 'Venice', 'CA', '90291') def test_credit_card(self): card = self.client.card(self.credit_card, self.address) transaction = card.auth(self.amount1) transaction.void() self.assertRaises(AuthorizeResponseError, transaction.settle) def test_saved_card(self): card = self.client.card(self.credit_card, self.address) saved = card.save() saved.auth(self.amount1).settle() saved.capture(self.amount1) saved_from_id = self.client.saved_card(saved.uid) saved_from_id.delete() def test_recurring(self): card = self.client.card(self.credit_card, self.address) start = date.today() + timedelta(days=7) recurring = card.recurring(self.amount1, start, months=1, occurrences=10) recurring.update(amount=self.amount2, trial_amount=self.amount2 - 0.5, trial_occurrences=3) recurring_from_id = self.client.recurring(recurring.uid) recurring_from_id.delete() def test_saved_bank_account_pass(self): check = self.client.check(self.bank_account, self.address) saved = check.save() saved.auth(self.amount3).settle() saved.capture(self.amount3) saved_from_id = self.client.saved_check(saved.uid) saved_from_id.delete()
class ClientTests(TestCase): def setUp(self): self.transaction_api_patcher = mock.patch( 'authorize.client.TransactionAPI') self.transaction_api = self.transaction_api_patcher.start() self.customer_api_patcher = mock.patch( 'authorize.client.CustomerAPI') self.customer_api = self.customer_api_patcher.start() self.recurring_api_patcher = mock.patch( 'authorize.client.RecurringAPI') self.recurring_api = self.recurring_api_patcher.start() self.client = AuthorizeClient('123', '456') self.year = date.today().year + 10 self.credit_card = CreditCard('4111111111111111', self.year, 1, '911', 'Jeff', 'Schenck') self.bank_account = BankAccount(**dict(TEST_BANK_ACCOUNT)) self.address = Address('45 Rose Ave', 'Venice', 'CA', '90291') def tearDown(self): self.transaction_api_patcher.stop() self.customer_api_patcher.stop() self.recurring_api_patcher.stop() def test_basic_authorize_client(self): self.transaction_api.reset_mock() self.customer_api.reset_mock() self.recurring_api.reset_mock() self.assertEqual(self.transaction_api.call_args, None) self.assertEqual(self.customer_api.call_args, None) self.assertEqual(self.recurring_api.call_args, None) client = AuthorizeClient('123', '456', False, False) self.assertEqual(self.transaction_api.call_args, (('123', '456', False, False), {})) self.assertEqual(self.customer_api.call_args, (('123', '456', False, False), {})) self.assertEqual(self.recurring_api.call_args, (('123', '456', False, False), {})) def test_authorize_client_payment_creators(self): self.assertTrue(isinstance( self.client.card(self.credit_card), AuthorizeCreditCard)) self.assertTrue(isinstance( self.client.card(self.credit_card, self.address), AuthorizeCreditCard)) self.assertTrue(isinstance( self.client.transaction('123'), AuthorizeTransaction)) self.assertTrue(isinstance( self.client.saved_card('123|456'), AuthorizeSavedCard)) self.assertTrue(isinstance( self.client.recurring('123'), AuthorizeRecurring)) self.assertTrue(isinstance( self.client.check(self.bank_account), AuthorizeBankAccount)) self.assertTrue(isinstance( self.client.check(self.bank_account, self.address), AuthorizeBankAccount)) def test_authorize_credit_card_basic(self): card = AuthorizeCreditCard(self.client, self.credit_card) card = AuthorizeCreditCard(self.client, self.credit_card, self.address) repr(card) def test_authorize_bank_account_basic(self): check = AuthorizeBankAccount(self.client, self.bank_account) check = AuthorizeBankAccount(self.client, self.bank_account, self.address) repr(check) def test_authorize_credit_card_auth(self): self.client._transaction.auth.return_value = TRANSACTION_RESULT card = AuthorizeCreditCard(self.client, self.credit_card) result = card.auth(10) self.assertEqual(self.client._transaction.auth.call_args, ((10, self.credit_card, None), {})) self.assertTrue(isinstance(result, AuthorizeTransaction)) self.assertEqual(result.uid, '2171062816') self.assertEqual(result.full_response, TRANSACTION_RESULT) def test_authorize_credit_card_capture(self): self.client._transaction.capture.return_value = TRANSACTION_RESULT card = AuthorizeCreditCard(self.client, self.credit_card) result = card.capture(10) self.assertEqual(self.client._transaction.capture.call_args, ((10, self.credit_card, None), {})) self.assertTrue(isinstance(result, AuthorizeTransaction)) self.assertEqual(result.uid, '2171062816') self.assertEqual(result.full_response, TRANSACTION_RESULT) def test_authorize_credit_card_save(self): self.client._customer.create_saved_profile.return_value = ('1', '2') card = AuthorizeCreditCard(self.client, self.credit_card) result = card.save() self.assertEqual(self.client._customer.create_saved_payment.call_args, ((), {'address': None, 'credit_card': self.credit_card})) self.assertTrue(isinstance( self.client._customer.create_saved_profile.call_args[0][0], str)) self.assertTrue(isinstance( self.client._customer.create_saved_profile.call_args[0][1], list)) self.assertTrue(isinstance(result, AuthorizeSavedCard)) self.assertEqual(result.uid, '1|2') def test_authorize_bank_account_save(self): self.client._customer.create_saved_profile.return_value = ('1', '2') check = AuthorizeBankAccount(self.client, self.bank_account) result = check.save() self.assertEqual(self.client._customer.create_saved_payment.call_args, ((), {'address': None, 'bank_account': self.bank_account})) self.assertTrue(isinstance( self.client._customer.create_saved_profile.call_args[0][0], str)) self.assertTrue(isinstance( self.client._customer.create_saved_profile.call_args[0][1], list)) self.assertTrue(isinstance(result, AuthorizeSavedAccount)) self.assertEqual(result.uid, '1|2') def test_authorize_credit_card_recurring(self): self.client._recurring.create_subscription.return_value = '1' card = AuthorizeCreditCard(self.client, self.credit_card) today = date.today() result = card.recurring(10, today, months=1) self.assertEqual(self.client._recurring.create_subscription.call_args, ((self.credit_card, 10, today), {'days': None, 'months': 1, 'occurrences': None, 'trial_amount': None, 'trial_occurrences': None})) self.assertTrue(isinstance(result, AuthorizeRecurring)) self.assertEqual(result.uid, '1') def test_authorize_transaction_basic(self): transaction = AuthorizeTransaction(self.client, '123') repr(transaction) def test_authorize_transaction_settle(self): self.client._transaction.settle.return_value = TRANSACTION_RESULT transaction = AuthorizeTransaction(self.client, '123') # Test without amount result = transaction.settle() self.assertEqual(self.client._transaction.settle.call_args, (('123',), {'amount': None})) self.assertTrue(isinstance(result, AuthorizeTransaction)) self.assertEqual(result.uid, '2171062816') # Test with amount result = transaction.settle(10) self.assertEqual(self.client._transaction.settle.call_args, (('123',), {'amount': 10})) self.assertTrue(isinstance(result, AuthorizeTransaction)) self.assertEqual(result.uid, '2171062816') def test_authorize_transaction_credit(self): self.client._transaction.credit.return_value = TRANSACTION_RESULT transaction = AuthorizeTransaction(self.client, '123') # Test with amount result = transaction.credit('1111', 10) self.assertEqual(self.client._transaction.credit.call_args, (('1111', '123', 10), {})) self.assertTrue(isinstance(result, AuthorizeTransaction)) self.assertEqual(result.uid, '2171062816') def test_authorize_transaction_void(self): self.client._transaction.void.return_value = TRANSACTION_RESULT transaction = AuthorizeTransaction(self.client, '123') result = transaction.void() self.assertEqual(self.client._transaction.void.call_args, (('123',), {})) self.assertTrue(isinstance(result, AuthorizeTransaction)) self.assertEqual(result.uid, '2171062816') def test_authorize_saved_card_basic(self): saved = AuthorizeSavedCard(self.client, '1|2') self.assertTrue(isinstance(saved, AuthorizeSavedCard)) repr(saved) def test_authorize_saved_check_basic(self): saved = AuthorizeSavedAccount(self.client, '1|2') self.assertTrue(isinstance(saved, AuthorizeSavedAccount)) repr(saved) def test_authorize_saved_card_auth(self): self.client._customer.auth.return_value = TRANSACTION_RESULT saved = AuthorizeSavedCard(self.client, '1|2') self.assertTrue(isinstance(saved, AuthorizeSavedCard)) result = saved.auth(10) self.assertEqual(self.client._customer.auth.call_args, (('1', '2', 10), {})) self.assertTrue(isinstance(result, AuthorizeTransaction)) self.assertEqual(result.uid, '2171062816') def test_authorize_saved_check_auth(self): self.client._customer.auth.return_value = TRANSACTION_RESULT saved = AuthorizeSavedAccount(self.client, '1|2') self.assertTrue(isinstance(saved, AuthorizeSavedAccount)) result = saved.auth(10) self.assertEqual(self.client._customer.auth.call_args, (('1', '2', 10), {})) self.assertTrue(isinstance(result, AuthorizeTransaction)) self.assertEqual(result.uid, '2171062816') def test_authorize_saved_card_capture(self): self.client._customer.capture.return_value = TRANSACTION_RESULT saved = AuthorizeSavedCard(self.client, '1|2') self.assertTrue(isinstance(saved, AuthorizeSavedCard)) result = saved.capture(10) self.assertEqual(self.client._customer.capture.call_args, (('1', '2', 10), {})) self.assertTrue(isinstance(result, AuthorizeTransaction)) self.assertEqual(result.uid, '2171062816') def test_authorize_saved_check_capture(self): self.client._customer.capture.return_value = TRANSACTION_RESULT saved = AuthorizeSavedAccount(self.client, '1|2') self.assertTrue(isinstance(saved, AuthorizeSavedAccount)) result = saved.capture(10) self.assertEqual(self.client._customer.capture.call_args, (('1', '2', 10), {})) self.assertTrue(isinstance(result, AuthorizeTransaction)) self.assertEqual(result.uid, '2171062816') def test_authorize_saved_card_delete(self): saved = AuthorizeSavedCard(self.client, '1|2') self.assertTrue(isinstance(saved, AuthorizeSavedCard)) result = saved.delete() self.assertEqual(self.client._customer.delete_saved_payment.call_args, (('1', '2'), {})) def test_authorize_saved_check_delete(self): saved = AuthorizeSavedAccount(self.client, '1|2') self.assertTrue(isinstance(saved, AuthorizeSavedAccount)) result = saved.delete() self.assertEqual(self.client._customer.delete_saved_payment.call_args, (('1', '2'), {})) def test_authorize_recurring_basic(self): recurring = AuthorizeRecurring(self.client, '123') repr(recurring) def test_authorize_recurring_update(self): recurring = AuthorizeRecurring(self.client, '123') recurring.update(occurrences=20) self.assertEqual(self.client._recurring.update_subscription.call_args, (('123',), {'amount': None, 'start': None, 'occurrences': 20, 'trial_amount': None, 'trial_occurrences': None})) def test_authorize_recurring_delete(self): recurring = AuthorizeRecurring(self.client, '123') recurring.delete() self.assertEqual(self.client._recurring.delete_subscription.call_args, (('123',), {}))