def test_create_payment_profile(self, PaymentProfile, CreateRequest): payment_profile = Mock() PaymentProfile.return_value = payment_profile _request = Mock() _request.make_request.return_value = self.payment_profile_id CreateRequest.return_value = _request # Scenario: call is successful, no validationMode is passed return_value = create_payment_profile(self.customer_id, 'address', 'credit_card') PaymentProfile.assert_called_once_with(billTo='address', card='credit_card') CreateRequest.assert_called_once_with( customerProfileId=self.customer_id, paymentProfile=payment_profile, validationMode=None) self.assertTrue(_request.make_request.called) self.assertEqual(return_value, self.payment_profile_id) # Scenario: call is successful, validationMode is passed create_payment_profile(self.customer_id, 'address', 'credit_card', 'liveMode') CreateRequest.assert_called_with(customerProfileId=self.customer_id, paymentProfile=payment_profile, validationMode='liveMode') # Scenario: call raises AuthorizeNetException _request.make_request.side_effect = AuthorizeNetException('') self.assertRaises(AuthorizeNetException, create_payment_profile, self.customer_id, 'address', 'credit_card')
def test_create_customer_profile(self, Profile, CreateRequest): merchant_customer_id = 99 description = 'some description' # Set up profile mock profile = Mock() Profile.return_value = profile # Set up request mock _request = MagicMock() _request.make_request.return_value = self.customer_id CreateRequest.return_value = _request # Scenario: successful call return_value = create_customer_profile(merchant_customer_id, description) Profile.assert_called_once_with( description=description, merchantCustomerId=merchant_customer_id, paymentProfiles=None, customerProfileId=None) CreateRequest.assert_called_once_with(profile=profile) self.assertTrue(_request.make_request.called) self.assertEqual(return_value, self.customer_id) # Scenario: call raises AuthorizeNetException _request.make_request.side_effect = AuthorizeNetException('') return_value = create_customer_profile(merchant_customer_id, description) self.assertEqual(return_value, None)
def test_exception_message(self): from r2.lib.authorize.api import AuthorizeNetException card_number = "<cardNumber>1111222233334444</cardNumber>" expected = "<cardNumber>...4444</cardNumber>" full_msg = "Wrong Card %s was given" exp = AuthorizeNetException(full_msg % (card_number)) self.assertNotEqual(str(exp), (full_msg % card_number)) self.assertEqual(str(exp), (full_msg % expected))
def refund_transaction(user, trans_id, campaign_id, amount, test=None): # refund will only work if charge has settled bid = Bid.one(transaction=trans_id, campaign=campaign_id) if trans_id < 0: bid.refund(amount) return True else: success, res = _make_transaction(ProfileTransRefund, amount, user, bid.pay_id, trans_id=trans_id, test=test) if success: bid.refund(amount) elif success == False: msg = "Refund failed, response: %r" % res raise AuthorizeNetException(msg) return True
def test_get_customer_profile(self, GetRequest): profile_mock = Mock() _request = Mock() _request.make_request.return_value = profile_mock GetRequest.return_value = _request # Scenario: call is successful return_value = get_customer_profile(self.customer_id) GetRequest.assert_called_once_with(customerProfileId=self.customer_id) self.assertTrue(_request.make_request.called) self.assertEqual(return_value, profile_mock) # Scenario: call raises AuthorizeNetException _request.make_request.side_effect = AuthorizeNetException('') return_value = get_customer_profile(self.customer_id) self.assertEqual(return_value, None)
def test_delete_payment_profile(self, DeleteRequest): _request = Mock() DeleteRequest.return_value = _request # Scenario: call is successful return_value = delete_payment_profile(self.customer_id, self.payment_profile_id) DeleteRequest.assert_called_once_with(customerProfileId=self.customer_id, customerPaymentProfileId=self.payment_profile_id) self.assertTrue(return_value) # Scenario: call raises AuthorizeNetException _request.make_request.side_effect = AuthorizeNetException('') return_value = delete_payment_profile(self.customer_id, self.payment_profile_id) self.assertFalse(return_value)
def test_update_payment_profile(self, PaymentProfile, UpdateRequest): _request = Mock() _request.make_request.return_value = self.payment_profile_id UpdateRequest.return_value = _request # Scenario: call is successful return_value = update_payment_profile(self.customer_id, self.payment_profile_id, 'address', 1234) self.assertTrue(UpdateRequest.called) self.assertTrue(_request.make_request.called) self.assertEqual(return_value, self.payment_profile_id) # Scenario: call raises AuthorizeNetException _request.make_request.side_effect = AuthorizeNetException('') self.assertRaises(AuthorizeNetException, update_payment_profile, self.customer_id, self.payment_profile_id, 'address', 1234)