def test_post_on_error(self, create_card): create_card.side_effect = CardError("Bad card", "Param", "CODE") self.client.login(username=self.user.username, password="******") response = self.client.post( reverse("pinax_stripe_payment_method_create"), {}) self.assertEquals(response.status_code, 200) self.assertTrue("errors" in response.context_data)
def test_user_can_place_order_with_failure(self): # Mock call to Stripe. mock_create = patch( 'organizations.apis.stripe.Charge.create', side_effect=CardError(message='', param='', code='card_declined')).start() # Mock call to SparkPost. patch('organizations.apis.sparkpost.SparkPost').start() order = self.create_order(self.offer.id, token='tok_chargeDeclined') response = self.client.post(reverse('api:order_list'), data=order, format='json') assert_equal(400, response.status_code) mock_create.assert_called_once_with( amount=int(self.offer.discounted_value * 1 * 100), # 'customer': CustomerSerializer(Customer.objects.last()).data, currency='usd', source='tok_chargeDeclined', destination={ "account": self.offer.organization.stripe_organization_id, }) vouchers = Voucher.objects.filter( customer__email='*****@*****.**', offer_id=self.offer.id) assert_equal(0, vouchers.count())
def test_server_stripe_error_with_params(): expected = jsonify({ "message": "something", "params": "param1", "code": "500" }), 500 error = CardError("something", "param1", "500") actual = server_stripe_error_with_params(error) assert actual[0].json == expected[0].json assert actual[1] == expected[1]
def mock_stripe(success=True): stripe = MagicMock() if success: stripe.Charge.create = MagicMock(return_value=True) else: stripe.Charge.create = MagicMock(side_effect=CardError( message='testing', param='123', code='100500')) return stripe
def test_charge_card_declined(self, charge_create_mocked): card_error_json_body = { "error": { "charge": "ch_1F5C8nBszOVoiLmgPWC36cnI", "code": "card_declined", "decline_code": "generic_decline", "doc_url": "https://stripe.com/docs/error-codes/card-declined", "message": "Your card was declined.", "type": "card_error", } } charge_create_mocked.side_effect = CardError( message="a", param="b", code="c", json_body=card_error_json_body) call_command("charge_stripe") self.charge.refresh_from_db() self.assertFalse(self.success_signal_was_called) self.assertTrue(self.exception_signal_was_called) self.assertDictEqual(self.charge.stripe_response, card_error_json_body) self.assertEqual(self.charge.stripe_charge_id, "ch_1F5C8nBszOVoiLmgPWC36cnI")
def test_charges(self, charge_create_mocked): self.success_signal_was_called = False self.exception_signal_was_called = False def success_handler(sender, instance, **kwargs): self.success_signal_was_called = True def exception_handler(sender, instance, **kwargs): self.exception_signal_was_called = True stripe_charge_succeeded.connect(success_handler) stripe_charge_card_exception.connect(exception_handler) data = { "customer_id": "cus_AlSWz1ZQw7qG2z", "currency": "usd", "amount": 100, "description": "ABC" } charge_create_mocked.return_value = stripe.Charge(id="AA1") StripeCustomer.objects.create(user=self.user, stripe_customer_id="bum", stripe_js_response='"aa"') StripeCustomer.objects.create(user=self.user, stripe_customer_id=data["customer_id"], stripe_js_response='"foo"') customer = StripeCustomer.objects.create( user=self.user, stripe_customer_id=data["customer_id"], stripe_js_response='"foo"') self.assertTrue( customer, StripeCustomer.get_latest_active_customer_for_user(self.user)) charge = StripeCharge.objects.create(user=self.user, amount=data["amount"], customer=customer, description=data["description"]) manual_charge = StripeCharge.objects.create( user=self.user, amount=data["amount"], customer=customer, description=data["description"]) self.assertFalse(charge.is_charged) # test in case of an API error charge_create_mocked.side_effect = StripeError() with self.assertRaises(SystemExit): out = StringIO() sys.stdout = out call_command('charge_stripe') self.assertFalse(self.success_signal_was_called) charge.refresh_from_db() self.assertFalse(charge.is_charged) self.assertIn('Exception happened', out.getvalue()) charge_create_mocked.reset_mock() charge_create_mocked.side_effect = CardError(message="a", param="b", code="c") # test regular case call_command("charge_stripe") self.assertTrue(self.exception_signal_was_called) charge.refresh_from_db() self.assertFalse(charge.is_charged) self.assertTrue(charge.charge_attempt_failed) charge_create_mocked.reset_mock() charge_create_mocked.side_effect = None # test regular case # reset charge charge.charge_attempt_failed = False charge.save() call_command("charge_stripe") self.assertTrue(self.success_signal_was_called) charge.refresh_from_db() self.assertTrue(charge.is_charged) manual_charge.refresh_from_db() self.assertFalse(manual_charge.is_charged) self.assertEqual(charge.stripe_response["id"], "AA1") charge_create_mocked.assert_called_with( amount=charge.amount, currency=data["currency"], customer=data["customer_id"], description=data["description"]) # manual call manual_charge.charge() self.assertTrue(manual_charge.is_charged)
def test_server_stripe_card_error(): expected = jsonify({"message": "something", "code": "402"}), 402 error = CardError("something", "param1", "402") actual = server_stripe_card_error(error) assert actual[0].json == expected[0].json assert actual[1] == expected[1]
def paymentintent_create(**kwargs): raise CardError(message='Foo', param='foo', code=100)
def charge_create(**kwargs): raise CardError(message='Foo', param='foo', code=100)