|
| 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