def test_customer_create_call(self, mock_customer_module): mock_customer = Mock() mock_customer.id = 'cus_%s' % uuid().hex mock_customer_module.create.return_value = mock_customer token = uuid().hex stripe_customer = payment.StripeCustomer(None) stripe_user_id = stripe_customer.token_for_customer(token, self.user) exp_kwargs = dict(description='Poold user: %s' % self.user.id, card=token, email=self.user.email) mock_customer_module.create.assert_called_once_with(api_key=None, **exp_kwargs) assert_equal(mock_customer.id, stripe_user_id) self.patched_logger.transaction.assert_called_once_with( 'New Stripe Customer Created', **exp_kwargs)
def test_api_error(self, mock_customer): msg = 'Test message' def exception(*args, **kwargs): raise stripe.APIError(msg) mock_customer.create.side_effect = exception token = uuid().hex kwargs = dict(card=token, description='Poold user: %s' % self.user.id, email=self.user.email) meta = dict(user=str(self.user), request_args=kwargs) data = dict(error=stripe.APIError.__name__, message=msg) stripe_customer = payment.StripeCustomer(None) try: stripe_customer.token_for_customer(token, self.user) except: msg = 'An unknown error occurred while interacting with the Stripe API.' self.patched_logger.error.assert_called_once_with(msg, data=data, **meta) raise
def test_invalid_request_error(self, mock_customer): msg = 'Test message' def exception(*args, **kwargs): raise stripe.InvalidRequestError(msg, None) mock_customer.create.side_effect = exception token = uuid().hex kwargs = dict(card=token, description='Poold user: %s' % self.user.id, email=self.user.email) meta = dict(user=str(self.user), request_args=kwargs) data = dict(error=stripe.InvalidRequestError.__name__, message=msg) stripe_customer = payment.StripeCustomer(None) try: stripe_customer.token_for_customer(token, self.user) except: msg = 'Stripe Invalid Request Error.' self.patched_logger.error.assert_called_once_with(msg, data=data, **meta) raise
def test_token_exchange(self): # This is a functional test which depends on the stripe api. # To run all stripe api related tests run: $ make tests-stripe # To run all tests which utilize external services run: $ make # NOTE :: this test depends on the following environment variables # NOTE :: being defined: TEST_CARD_VISA_NUMBER, TEST_CARD_VISA_CVC, STRIPE_SECRET_KEY exp = datetime.now() + timedelta(days=365) # Get a token from stripe for tests card = dict(number=config.TEST_CARD_VISA_NUMBER, cvc=config.TEST_CARD_VISA_CVC, exp_month=exp.month, exp_year=exp.year) token = stripe.Token.create(api_key=config.STRIPE_SECRET_KEY, card=card) stripe_customer = payment.StripeCustomer(config.STRIPE_SECRET_KEY) stripe_user_id = stripe_customer.token_for_customer( token.id, self.user) # We don't know what the token will be, but we know it should be a # string, and start with 'cus_' assert_true(isinstance(stripe_user_id, basestring)) assert_true(stripe_user_id.startswith('cus_'))