Skip to content

Commit 8c160c3

Browse files
committed
adding tests for stripe wrappers
1 parent 6cd89e3 commit 8c160c3

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

donate/vendor/stripe.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def get_plan(amount, currency='USD', interval='month'):
7272
return plan.id
7373

7474
if len(plans) == 1:
75-
return plans[0].id
75+
return plans['data'][0].id
7676

7777
if len(plans) > 1:
7878
raise ValueError("Noisebridge should only have 1 plan for amt: {},"

tests/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
22

33

4-
os.environ['STRIPE_SECRET'] = "asdf"
4+
os.environ['STRIPE_KEY'] = "pk_test_vA6045AyiPb5hvuWUELt1TRB"
5+
os.environ['STRIPE_SECRET'] = "sk_test_1uEl9bjB2jRgCWPJtHEjV0nP"

tests/test_stripe.py

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import stripe
2+
import donate.vendor.stripe as stripe_utils
3+
import pytest
4+
import os
5+
from unittest.mock import Mock
6+
7+
8+
def test_api_context_manager():
9+
fake_key = "bananaboat"
10+
11+
old_val = os.environ['STRIPE_SECRET']
12+
os.environ['STRIPE_SECRET'] = fake_key
13+
14+
with stripe_utils.stripe_api() as api:
15+
assert api.api_key == fake_key
16+
17+
os.environ['STRIPE_SECRET'] = old_val
18+
19+
old_val = os.environ['STRIPE_KEY']
20+
os.environ['STRIPE_KEY'] = fake_key
21+
22+
assert stripe_utils._get_stripe_key('PUBLIC') == fake_key
23+
24+
os.environ['STRIPE_KEY'] = old_val
25+
26+
27+
def test_stringify_stripe_error():
28+
error_json_body = {}
29+
error = stripe.error.CardError(code='1',
30+
param='params',
31+
message='No',
32+
json_body=error_json_body)
33+
34+
msg = stripe_utils.stringify_stripe_error(error)
35+
assert msg == "Your card was not accepted"
36+
37+
msg = stripe_utils.stringify_stripe_error('wrong')
38+
assert msg == "An unexpected error occured processing your payment request"
39+
40+
41+
def test_create_onetime_charge():
42+
43+
with pytest.raises(stripe.error.CardError):
44+
stripe_utils.charge_once(
45+
"tok_chargeDeclinedExpiredCard",
46+
10000,
47+
"lasers")
48+
49+
charge_id = stripe_utils.charge_once(
50+
"tok_visa",
51+
10000,
52+
"lasers")
53+
54+
assert charge_id is not None
55+
56+
with pytest.raises(stripe.error.InvalidRequestError):
57+
charge_id = stripe_utils.charge_once(
58+
"tok_visa",
59+
-100,
60+
"lasers")
61+
62+
63+
def test_get_plan():
64+
amt = 20000
65+
ccy = "USD"
66+
interval = "month"
67+
68+
plan_id = stripe_utils.get_plan(amt, ccy, interval)
69+
assert plan_id is not None
70+
71+
72+
def test_create_charge():
73+
74+
token = "tok_visa"
75+
amount = 12500
76+
77+
desc = "for some bananas"
78+
79+
# Test recurring
80+
result = stripe_utils.create_charge(True, token, amount, email, desc)

0 commit comments

Comments
 (0)