-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_stripe.py
206 lines (151 loc) · 6.24 KB
/
test_stripe.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import stripe
import donate.vendor.stripe as stripe_utils
import pytest
import os
from unittest.mock import Mock, patch
def test_api_context_manager():
fake_key = "bananaboat"
old_val = os.environ['STRIPE_SECRET']
os.environ['STRIPE_SECRET'] = fake_key
with stripe_utils.stripe_api() as api:
assert api.api_key == fake_key
os.environ['STRIPE_SECRET'] = old_val
old_val = os.environ['STRIPE_KEY']
os.environ['STRIPE_KEY'] = fake_key
assert stripe_utils._get_stripe_key('PUBLIC') == fake_key
os.environ['STRIPE_KEY'] = old_val
def test_stringify_stripe_error():
error_json_body = {}
error = stripe.error.CardError(code='1',
param='params',
message='No',
json_body=error_json_body)
msg = stripe_utils.stringify_stripe_error(error)
assert msg == "Your card was not accepted"
msg = stripe_utils.stringify_stripe_error('wrong')
assert msg == "An unexpected error occured processing your payment request"
@patch('donate.vendor.stripe.stripe.Charge.create')
@patch('donate.vendor.stripe.stripe.Customer.list')
@patch('donate.vendor.stripe.stripe.Customer.create')
def test_create_onetime_charge(customer_create, customer_list, charge):
tok = "token"
email = "[email protected]"
amt = 10000
desc = "lasers"
customer_list.return_value = []
m = Mock
m.id = "test_customer"
customer_create.return_value = m
assert charge.called
assert customer_create.called
customer_create.called_with(source=tok, email=email)
charge.assert_called_with(amount=amt, currency='usd',
description=desc,
customer="test_customer")
customer_list.return_value = [Mock(data=m)]
assert charge.called
charge.assert_called_with(amount=amt, currency='usd',
description=desc,
customer="test_customer")
@patch('donate.vendor.stripe.create_plan')
@patch('donate.vendor.stripe.stripe.Plan')
def test_get_plan(plan, create_plan):
amt = 20000
ccy = "USD"
interval = "month"
plan_id = stripe_utils.get_plan(amt, ccy, interval)
assert plan.list.called
plan.list.assert_called_with(amount=amt,
limit=1,
active=True,
currency=ccy,
interval=interval)
plan.list.return_value = []
plan_id = stripe_utils.get_plan(amt, ccy, interval)
plan.list.assert_called_with(amount=amt,
limit=1,
active=True,
currency=ccy,
interval=interval)
assert create_plan.called
create_plan.assert_called_with(amt, ccy, interval)
plan.list.return_value = [1, 2]
with pytest.raises(ValueError):
plan_id = stripe_utils.get_plan(amt, ccy, interval)
m = Mock
m.id = 10
plan.list.return_value = {'data': [m]}
plan = stripe_utils.get_plan(amt, ccy, interval)
assert plan['plan_id'] == 10
@patch('donate.vendor.stripe.stripe.Plan')
def test_create_plan(plan):
amt = 10000
ccy = "USD",
interval = "monthly"
stripe_utils.create_plan(amt, ccy, interval)
plan.create.assert_called_with(name="${} / {}".format(amt / 100, interval),
amount=amt,
currency=ccy,
interval=interval)
@patch('donate.vendor.stripe.get_plan')
@patch('donate.vendor.stripe.stripe.Customer')
@patch('donate.vendor.stripe.stripe.Subscription')
def test_charge_monthly(sub, customer, plan):
tok = "abc123"
amt = 1000
email = "[email protected]"
desc = "A plan"
plan.return_value = {'plan_id': 10}
customer.create().id = 100
stripe_utils.charge_monthly(cc_token=tok,
amount_in_cents=amt,
email=email,
description=desc)
customer.create.assert_called_with(source=tok,
email=email)
sub.create.assert_called_with(customer=100, items=[{'plan': 10}])
@patch('donate.vendor.stripe.stripe.Customer')
@patch('donate.vendor.stripe.charge_monthly')
@patch('donate.vendor.stripe.charge_once')
def test_create_charge(once, monthly, customer):
customer.create().id = 10
recurring = True
cc_token = 'tok'
amt = 10000
email = "[email protected]"
stripe_utils.create_charge(recurring, cc_token, amt, email)
assert monthly.called
stripe_utils.create_charge(not recurring, cc_token, amt, email)
assert once.called
# @patch('donate.routes.redirect')
@patch('donate.routes.flash')
@patch('donate.routes.get_donation_params')
@patch('donate.routes.create_charge')
def test_donate_stripe_error(create_charge, get_donation_params,
flash, testapp):
genericerrmsg = '<div class="alert alert-danger" role="alert">Something went wrong. Please try a different <a href="https://www.noisebridge.net/wiki/Donate_or_Pay_Dues">payment method</a>.</div>'
CardError = stripe.error.CardError
StripeError = stripe.error.StripeError
RateLimitError = stripe.error.RateLimitError
# get_donation_params.return_value = {}
# get_donation_params.side_effect = None
# redirect.return_value =
msg = "test message"
ce = CardError(code='404', param={}, message=msg)
create_charge.side_effect = ce
response = testapp.post("/donation", data={})
assert flash.called
flash.assert_called_with(genericerrmsg)
json_body = {'error': {'message': msg}}
create_charge.side_effect = CardError(code='404', param={},
message=msg, json_body=json_body)
response = testapp.post("/donation", data={})
assert flash.called
flash.assert_called_with(genericerrmsg)
create_charge.side_effect = RateLimitError()
response = testapp.post("/donation", data={})
assert flash.called
flash.assert_called_with(genericerrmsg)
create_charge.side_effect = StripeError()
assert flash.called
flash.assert_called_with(genericerrmsg)