forked from stripe-archive/stripe-payments-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
61 lines (49 loc) · 2.27 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
tests.py
Stripe Payments Demo. Created by Adrienne Dreyfus (@adrind).
This is a test suite for our webhooks and /pay endpoints.
It uses the data mocked in test_data.py to create sample requests and responses.
"""
import json
import os
from unittest import mock, TestCase, main
from dotenv import load_dotenv, find_dotenv
from test_data import *
from stripe.error import CardError
import sys
sys.path.append('../')
from app import app as flask_app, stripe
from inventory import Inventory
class AppTestCase(TestCase):
def setUp(self):
load_dotenv(find_dotenv())
flask_app.testing = True
# See https://github.com/pallets/flask/issues/2549
flask_app.config['JSONIFY_PRETTYPRINT_REGULAR'] = False
self.app = flask_app.test_client()
def test_config(self):
response = self.app.get('/config')
self.assertListEqual(list(json.loads(response.data).keys()),
['country', 'currency', 'paymentMethods', 'shippingOptions', 'stripeCountry', 'stripePublishableKey'])
self.assertEqual(response.status_code, 200)
def test_create_payment_intent(self):
"""
We should be able to create a PaymentIntent with the amount calculated by calculate_payment_amount.
"""
Inventory.calculate_payment_amount = mock.MagicMock(return_value=500)
response = self.app.post(f'payment_intents', data=json.dumps({'items': [], 'currency': 'eur'}))
self.assertEqual(response.status_code, 200)
self.assertEqual(json.loads(response.data)['paymentIntent']['amount'], 500)
def test_payment_intent_updated_on_shipping(self):
"""
We should not be able to update a PaymentIntent with new shipping amount.
"""
order_amount = 500
shipping_amount = 500
Inventory.calculate_payment_amount = mock.MagicMock(return_value=order_amount)
stripe.PaymentIntent.modify = mock.MagicMock(return_value={})
response = self.app.post(f'payment_intents/pi_1234/shipping_change', data=json.dumps({'items': [], 'shippingOption': { 'id': 'express' }}))
stripe.PaymentIntent.modify.assert_called_once_with('pi_1234', amount=order_amount + shipping_amount)
self.assertEqual(response.status_code, 200)
if __name__ == '__main__':
main()