forked from dj-stripe/dj-stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_middleware.py
176 lines (138 loc) · 5.56 KB
/
test_middleware.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
"""
dj-stripe Middleware Tests.
"""
from copy import deepcopy
from unittest.mock import patch
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AnonymousUser
from django.test import TestCase
from django.test.client import RequestFactory
from django.test.utils import modify_settings, override_settings
from djstripe.middleware import SubscriptionPaymentMiddleware
from djstripe.models import Customer, Subscription
from . import (
FAKE_CUSTOMER,
FAKE_PRODUCT,
FAKE_SUBSCRIPTION,
FAKE_SUBSCRIPTION_NOT_PERIOD_CURRENT,
FUTURE_DATE,
)
class MiddlewareURLTest(TestCase):
urlconf = "tests.urls"
def setUp(self):
self.settings(ROOT_URLCONF=self.urlconf)
self.factory = RequestFactory()
self.user = get_user_model().objects.create_user(
username="pydanny", email="[email protected]"
)
self.middleware = SubscriptionPaymentMiddleware()
def test_appname(self):
request = self.factory.get("/admin/")
request.user = self.user
request.urlconf = self.urlconf
response = self.middleware.process_request(request)
self.assertEqual(response, None)
def test_namespace(self):
request = self.factory.get("/djstripe/webhook/")
request.user = self.user
request.urlconf = self.urlconf
response = self.middleware.process_request(request)
self.assertEqual(response, None)
def test_namespace_and_url(self):
request = self.factory.get("/testapp_namespaced/")
request.user = self.user
request.urlconf = self.urlconf
response = self.middleware.process_request(request)
self.assertEqual(response, None)
def test_url(self):
request = self.factory.get("/testapp/")
request.user = self.user
request.urlconf = self.urlconf
response = self.middleware.process_request(request)
self.assertEqual(response, None)
@override_settings(DEBUG=True)
def test_djdt(self):
request = self.factory.get("/__debug__/")
request.user = self.user
request.urlconf = self.urlconf
response = self.middleware.process_request(request)
self.assertEqual(response, None)
def test_fnmatch(self):
request = self.factory.get("/test_fnmatch/extra_text/")
request.user = self.user
request.urlconf = self.urlconf
response = self.middleware.process_request(request)
self.assertEqual(response, None)
@override_settings(DEBUG=True)
@modify_settings(
MIDDLEWARE={"append": ["djstripe.middleware.SubscriptionPaymentMiddleware"]}
)
def test_middleware_loads(self):
"""Check that the middleware can be loaded by django's
middleware handlers. This is to check for compatibility across
the change to django's middleware class structure. See
https://docs.djangoproject.com/en/1.10/topics/http/middleware/#upgrading-pre-django-1-10-style-middleware
"""
self.client.get("/__debug__")
class MiddlewareLogicTest(TestCase):
urlconf = "tests.urls"
def setUp(self):
self.settings(ROOT_URLCONF=self.urlconf)
self.factory = RequestFactory()
self.user = get_user_model().objects.create_user(
username="pydanny", email="[email protected]"
)
self.customer = Customer.sync_from_stripe_data(FAKE_CUSTOMER)
self.customer.subscriber = self.user
self.customer.save()
with patch(
"stripe.Product.retrieve",
return_value=deepcopy(FAKE_PRODUCT),
autospec=True,
):
self.subscription = Subscription.sync_from_stripe_data(FAKE_SUBSCRIPTION)
self.middleware = SubscriptionPaymentMiddleware()
def test_anonymous(self):
request = self.factory.get("/djstripe/webhook/")
request.user = AnonymousUser()
request.urlconf = self.urlconf
response = self.middleware.process_request(request)
self.assertEqual(response, None)
def test_is_staff(self):
self.user.is_staff = True
self.user.save()
request = self.factory.get("/djstripe/webhook/")
request.user = self.user
request.urlconf = self.urlconf
response = self.middleware.process_request(request)
self.assertEqual(response, None)
def test_is_superuser(self):
self.user.is_superuser = True
self.user.save()
request = self.factory.get("/djstripe/webhook/")
request.user = self.user
request.urlconf = self.urlconf
response = self.middleware.process_request(request)
self.assertEqual(response, None)
def test_customer_has_inactive_subscription(self):
with patch(
"stripe.Product.retrieve",
return_value=deepcopy(FAKE_PRODUCT),
autospec=True,
):
self.subscription = Subscription.sync_from_stripe_data(
FAKE_SUBSCRIPTION_NOT_PERIOD_CURRENT
)
request = self.factory.get("/testapp_content/")
request.user = self.user
request.urlconf = self.urlconf
response = self.middleware.process_request(request)
self.assertEqual(response.status_code, 302)
def test_customer_has_active_subscription(self):
self.subscription.current_period_end = FUTURE_DATE
self.subscription.save()
request = self.factory.get("/testapp_content/")
request.user = self.user
request.urlconf = self.urlconf
response = self.middleware.process_request(request)
self.assertEqual(response, None)