forked from dj-stripe/dj-stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_api_keys.py
159 lines (143 loc) · 5.54 KB
/
test_api_keys.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
from copy import deepcopy
from unittest.mock import patch
from django.conf import settings
from django.test import TestCase
from django.test.utils import override_settings
from djstripe import models
from djstripe.enums import APIKeyType
from djstripe.settings import djstripe_settings
from . import FAKE_ACCOUNT, FAKE_FILEUPLOAD_LOGO
class TestCheckApiKeySettings(TestCase):
@override_settings(
STRIPE_LIVE_SECRET_KEY="sk_live_foo",
STRIPE_LIVE_PUBLIC_KEY="sk_live_foo",
STRIPE_LIVE_MODE=True,
)
@patch("stripe.Account.retrieve")
@patch(
"stripe.File.retrieve",
return_value=deepcopy(FAKE_FILEUPLOAD_LOGO),
autospec=True,
)
def test_global_api_keys_live_mode(
self,
fileupload_retrieve_mock,
account_retrieve_mock,
):
fake_account = deepcopy(FAKE_ACCOUNT)
fake_account["settings"]["branding"]["icon"] = None
account_retrieve_mock.return_value = fake_account
with patch.object(
models.api,
"get_api_key_details_by_prefix",
return_value=(APIKeyType.secret, True),
):
account = models.Account.sync_from_stripe_data(
fake_account, api_key=djstripe_settings.STRIPE_SECRET_KEY
)
self.assertEqual(account.default_api_key, "sk_live_foo")
self.assertEqual(djstripe_settings.STRIPE_LIVE_MODE, True)
self.assertEqual(djstripe_settings.STRIPE_SECRET_KEY, "sk_live_foo")
self.assertEqual(djstripe_settings.LIVE_API_KEY, "sk_live_foo")
@override_settings(
STRIPE_TEST_SECRET_KEY="sk_test_foo",
STRIPE_TEST_PUBLIC_KEY="pk_test_foo",
STRIPE_LIVE_MODE=False,
)
@patch("stripe.Account.retrieve")
@patch(
"stripe.File.retrieve",
return_value=deepcopy(FAKE_FILEUPLOAD_LOGO),
autospec=True,
)
def test_global_api_keys_test_mode(
self,
fileupload_retrieve_mock,
account_retrieve_mock,
):
fake_account = deepcopy(FAKE_ACCOUNT)
fake_account["settings"]["branding"]["icon"] = None
account_retrieve_mock.return_value = fake_account
with patch.object(
models.api,
"get_api_key_details_by_prefix",
return_value=(APIKeyType.secret, False),
):
account = models.Account.sync_from_stripe_data(
fake_account, api_key=djstripe_settings.STRIPE_SECRET_KEY
)
self.assertEqual(account.default_api_key, "sk_test_foo")
self.assertEqual(djstripe_settings.STRIPE_LIVE_MODE, False)
self.assertEqual(djstripe_settings.STRIPE_SECRET_KEY, "sk_test_foo")
self.assertEqual(djstripe_settings.TEST_API_KEY, "sk_test_foo")
@override_settings(
STRIPE_TEST_SECRET_KEY="sk_test_foo",
STRIPE_LIVE_SECRET_KEY="sk_live_foo",
STRIPE_TEST_PUBLIC_KEY="pk_test_foo",
STRIPE_LIVE_PUBLIC_KEY="pk_live_foo",
STRIPE_LIVE_MODE=True,
)
@patch("stripe.Account.retrieve")
@patch(
"stripe.File.retrieve",
return_value=deepcopy(FAKE_FILEUPLOAD_LOGO),
autospec=True,
)
def test_api_key_live_mode(
self,
fileupload_retrieve_mock,
account_retrieve_mock,
):
fake_account = deepcopy(FAKE_ACCOUNT)
fake_account["settings"]["branding"]["icon"] = None
account_retrieve_mock.return_value = fake_account
with patch.object(
models.api,
"get_api_key_details_by_prefix",
return_value=(APIKeyType.secret, True),
):
account = models.Account.sync_from_stripe_data(
fake_account, api_key=djstripe_settings.STRIPE_SECRET_KEY
)
self.assertEqual(account.default_api_key, "sk_live_foo")
del settings.STRIPE_SECRET_KEY, settings.STRIPE_TEST_SECRET_KEY
del settings.STRIPE_PUBLIC_KEY, settings.STRIPE_TEST_PUBLIC_KEY
self.assertEqual(djstripe_settings.STRIPE_LIVE_MODE, True)
self.assertEqual(djstripe_settings.STRIPE_SECRET_KEY, "sk_live_foo")
self.assertEqual(djstripe_settings.STRIPE_PUBLIC_KEY, "pk_live_foo")
@override_settings(
STRIPE_TEST_SECRET_KEY="sk_test_foo",
STRIPE_LIVE_SECRET_KEY="sk_live_foo",
STRIPE_TEST_PUBLIC_KEY="pk_test_foo",
STRIPE_LIVE_PUBLIC_KEY="pk_live_foo",
STRIPE_LIVE_MODE=False,
)
@patch("stripe.Account.retrieve")
@patch(
"stripe.File.retrieve",
return_value=deepcopy(FAKE_FILEUPLOAD_LOGO),
autospec=True,
)
def test_secret_key_test_mode(
self,
fileupload_retrieve_mock,
account_retrieve_mock,
):
fake_account = deepcopy(FAKE_ACCOUNT)
fake_account["settings"]["branding"]["icon"] = None
account_retrieve_mock.return_value = fake_account
with patch.object(
models.api,
"get_api_key_details_by_prefix",
return_value=(APIKeyType.secret, False),
):
account = models.Account.sync_from_stripe_data(
fake_account, api_key=djstripe_settings.STRIPE_SECRET_KEY
)
self.assertEqual(account.default_api_key, "sk_test_foo")
del settings.STRIPE_SECRET_KEY
del settings.STRIPE_PUBLIC_KEY
self.assertEqual(djstripe_settings.STRIPE_LIVE_MODE, False)
self.assertEqual(djstripe_settings.STRIPE_SECRET_KEY, "sk_test_foo")
self.assertEqual(djstripe_settings.STRIPE_PUBLIC_KEY, "pk_test_foo")
self.assertEqual(djstripe_settings.TEST_API_KEY, "sk_test_foo")