forked from dj-stripe/dj-stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
194 lines (158 loc) · 6.32 KB
/
conftest.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
"""
Module for creating re-usable fixtures to be used across the test suite
"""
import os
import pytest
import stripe
from django.conf import settings
from django.contrib.auth import get_user_model
from stripe import InvalidRequestError, PermissionError
from djstripe import models
from . import FAKE_CUSTOMER, FAKE_PLATFORM_ACCOUNT
pytestmark = pytest.mark.django_db
class CreateAccountMixin:
@pytest.fixture(autouse=True)
def create_account(self, monkeypatch):
"""
Fixture to automatically create and assign the default testing keys to the Platform Account
"""
def mock_account_retrieve(*args, **kwargs):
return FAKE_PLATFORM_ACCOUNT
monkeypatch.setattr(stripe.Account, "retrieve", mock_account_retrieve)
# create a Stripe Platform Account
FAKE_PLATFORM_ACCOUNT.create()
def pytest_collection_modifyitems(items, config):
"""Override Pytest config at run-time to run tests using Stripe API only if explictly specified using `-m stripe_api`"""
# get passed in markers
markexpr = config.getoption("markexpr")
if markexpr:
# allow passed in markers to run
config.option.markexpr = markexpr
else:
# skip running `stripe_api` marked tests unless explictly specified
for item in items:
if "stripe_api" in item.keywords:
# add message to let user know how to run tests using Stripe API
item.add_marker(
pytest.mark.skip(reason="need -m stripe_api option to run")
)
@pytest.fixture
def configure_settings(settings):
settings.STRIPE_TEST_SECRET_KEY = settings.STRIPE_SECRET_KEY = os.environ.get(
"STRIPE_TEST_SECRET_KEY"
)
def pytest_configure(config):
markexpr = config.getoption("markexpr")
if markexpr == "stripe_api":
key = os.environ.get("STRIPE_TEST_SECRET_KEY")
if not key:
pytest.exit(
f"Expected Real Stripe Account Testing key to be provided. Got {key}."
" Please pass it like so 'STRIPE_TEST_SECRET_KEY=<STRIPE_KEY> pytest"
" -m stripe_api'"
)
@pytest.fixture
def fake_user():
user = get_user_model().objects.create_user(
username="testuser", email="[email protected]"
)
return user
@pytest.fixture
def fake_customer(fake_user):
customer = FAKE_CUSTOMER.create_for_user(fake_user)
return customer
# Stripe Account Fixtures
@pytest.fixture
def standard_account_fixture(django_db_setup, django_db_blocker, configure_settings):
"""Create Standard Stripe Account."""
# See: https://pytest-django.readthedocs.io/en/latest/database.html#populate-the-test-database-if-you-don-t-use-transactional-or-live-server
with django_db_blocker.unblock():
# setup_stuff
account_json = models.Account._api_create(
type="standard",
country="US",
email="[email protected]",
api_key=settings.STRIPE_SECRET_KEY,
)
account_instance = models.Account.sync_from_stripe_data(
account_json,
api_key=settings.STRIPE_SECRET_KEY,
)
yield account_json, account_instance
# teardown_stuff
try:
# try to delete
account_instance._api_delete(api_key=settings.STRIPE_SECRET_KEY)
except (InvalidRequestError, PermissionError):
pass
@pytest.fixture
def custom_account_fixture(django_db_setup, django_db_blocker, configure_settings):
"""Create Custom Stripe Account."""
# See: https://pytest-django.readthedocs.io/en/latest/database.html#populate-the-test-database-if-you-don-t-use-transactional-or-live-server
with django_db_blocker.unblock():
# setup_stuff
account_json = models.Account._api_create(
type="custom",
country="US",
email="[email protected]",
capabilities={
"card_payments": {"requested": True},
"transfers": {"requested": True},
},
api_key=settings.STRIPE_SECRET_KEY,
)
account_instance = models.Account.sync_from_stripe_data(
account_json,
api_key=settings.STRIPE_SECRET_KEY,
)
yield account_json, account_instance
# teardown_stuff
try:
# try to delete
account_instance._api_delete(api_key=settings.STRIPE_SECRET_KEY)
except (InvalidRequestError, PermissionError):
pass
@pytest.fixture
def custom_account_func_fixture(django_db_setup, django_db_blocker, configure_settings):
"""
Same as custom_account_fixture but functional.
This is useful for tests involving rejecting and deleting instances.
"""
# See: https://pytest-django.readthedocs.io/en/latest/database.html#populate-the-test-database-if-you-don-t-use-transactional-or-live-server
with django_db_blocker.unblock():
# setup_stuff
account_json = models.Account._api_create(
type="custom",
country="US",
email="[email protected]",
capabilities={
"card_payments": {"requested": True},
"transfers": {"requested": True},
},
api_key=settings.STRIPE_SECRET_KEY,
)
account_instance = models.Account.sync_from_stripe_data(
account_json,
api_key=settings.STRIPE_SECRET_KEY,
)
yield account_json, account_instance
# teardown_stuff
try:
# try to delete
account_instance._api_delete(api_key=settings.STRIPE_SECRET_KEY)
except (InvalidRequestError, PermissionError):
pass
@pytest.fixture
def platform_account_fixture(django_db_setup, django_db_blocker, configure_settings):
"""Retrieve Platform Stripe Account."""
# See: https://pytest-django.readthedocs.io/en/latest/database.html#populate-the-test-database-if-you-don-t-use-transactional-or-live-server
with django_db_blocker.unblock():
# setup_stuff
account_json = stripe.Account.retrieve(
api_key=settings.STRIPE_SECRET_KEY,
)
account_instance = models.Account.sync_from_stripe_data(
account_json,
api_key=settings.STRIPE_SECRET_KEY,
)
yield account_json, account_instance