-
Notifications
You must be signed in to change notification settings - Fork 84
/
test_register.py
324 lines (291 loc) · 13.3 KB
/
test_register.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import math
import time
from unittest.mock import patch
from django.test.utils import override_settings
from rest_framework import status
from rest_registration.api.views.register import RegisterSigner
from rest_registration.settings import registration_settings
from .base import APIViewTestCase
REGISTER_VERIFICATION_URL = '/verify-account/'
VERIFICATION_FROM_EMAIL = 'no-reply@example.com'
REST_REGISTRATION_WITH_VERIFICATION = {
'REGISTER_VERIFICATION_ENABLED': True,
'REGISTER_VERIFICATION_URL': REGISTER_VERIFICATION_URL,
'VERIFICATION_FROM_EMAIL': VERIFICATION_FROM_EMAIL,
}
REST_REGISTRATION_WITH_VERIFICATION_NO_PASSWORD = {
'REGISTER_VERIFICATION_ENABLED': True,
'REGISTER_VERIFICATION_URL': REGISTER_VERIFICATION_URL,
'VERIFICATION_FROM_EMAIL': VERIFICATION_FROM_EMAIL,
'REGISTER_SERIALIZER_PASSWORD_CONFIRM': False,
}
REST_REGISTRATION_WITHOUT_VERIFICATION = {
'REGISTER_VERIFICATION_ENABLED': False,
}
REST_REGISTRATION_WITH_HTML_EMAIL_VERIFICATION = {
'REGISTER_VERIFICATION_ENABLED': True,
'REGISTER_VERIFICATION_URL': REGISTER_VERIFICATION_URL,
'REGISTER_VERIFICATION_EMAIL_TEMPLATES': {
'subject': 'rest_registration/register/subject.txt',
'html_body': 'rest_registration/register/body.html',
},
'VERIFICATION_FROM_EMAIL': VERIFICATION_FROM_EMAIL,
}
@override_settings(REST_REGISTRATION=REST_REGISTRATION_WITH_VERIFICATION)
class RegisterViewTestCase(APIViewTestCase):
VIEW_NAME = 'register'
def test_register_serializer_ok(self):
serializer_class = registration_settings.REGISTER_SERIALIZER_CLASS
serializer = serializer_class(data={})
field_names = {f for f in serializer.get_fields()}
self.assertEqual(
field_names,
{'id', 'username', 'first_name', 'last_name', 'email',
'password', 'password_confirm'},
)
@override_settings(
REST_REGISTRATION=REST_REGISTRATION_WITH_VERIFICATION_NO_PASSWORD,
)
def test_register_serializer_no_password_ok(self):
serializer_class = registration_settings.REGISTER_SERIALIZER_CLASS
serializer = serializer_class(data={})
field_names = {f for f in serializer.get_fields()}
self.assertEqual(
field_names,
{'id', 'username', 'first_name', 'last_name', 'email', 'password'},
)
def test_register_ok(self):
data = self._get_register_user_data(password='testpassword')
request = self.create_post_request(data)
time_before = math.floor(time.time())
with self.assert_one_mail_sent() as sent_emails:
response = self.view_func(request)
time_after = math.ceil(time.time())
self.assert_valid_response(response, status.HTTP_201_CREATED)
user_id = response.data['id']
# Check database state.
user = self.user_class.objects.get(id=user_id)
self.assertEqual(user.username, data['username'])
self.assertTrue(user.check_password(data['password']))
self.assertFalse(user.is_active)
# Check verification e-mail.
sent_email = sent_emails[0]
self.assertEqual(sent_email.from_email, VERIFICATION_FROM_EMAIL)
self.assertListEqual(sent_email.to, [data['email']])
url = self.assert_one_url_line_in_text(sent_email.body)
verification_data = self.assert_valid_verification_url(
url,
expected_path=REGISTER_VERIFICATION_URL,
expected_query_keys={'signature', 'user_id', 'timestamp'},
)
url_user_id = int(verification_data['user_id'])
self.assertEqual(url_user_id, user_id)
url_sig_timestamp = int(verification_data['timestamp'])
self.assertGreaterEqual(url_sig_timestamp, time_before)
self.assertLessEqual(url_sig_timestamp, time_after)
signer = RegisterSigner(verification_data)
signer.verify()
# TODO: unskip this test when × entity problem will be fixed.
@override_settings(
REST_REGISTRATION=REST_REGISTRATION_WITH_HTML_EMAIL_VERIFICATION,
)
def test_register_with_html_email_ok(self):
data = self._get_register_user_data(password='testpassword')
request = self.create_post_request(data)
time_before = math.floor(time.time())
with self.assert_one_mail_sent() as sent_emails:
response = self.view_func(request)
time_after = math.ceil(time.time())
self.assert_valid_response(response, status.HTTP_201_CREATED)
user_id = response.data['id']
# Check database state.
user = self.user_class.objects.get(id=user_id)
self.assertEqual(user.username, data['username'])
self.assertTrue(user.check_password(data['password']))
self.assertFalse(user.is_active)
# Check verification e-mail.
sent_email = sent_emails[0]
self.assertEqual(sent_email.from_email, VERIFICATION_FROM_EMAIL)
self.assertListEqual(sent_email.to, [data['email']])
url = self.assert_one_url_in_brackets_in_text(sent_email.body)
verification_data = self.assert_valid_verification_url(
url,
expected_path=REGISTER_VERIFICATION_URL,
expected_query_keys={'signature', 'user_id', 'timestamp'},
)
url_user_id = int(verification_data['user_id'])
self.assertEqual(url_user_id, user_id)
url_sig_timestamp = int(verification_data['timestamp'])
self.assertGreaterEqual(url_sig_timestamp, time_before)
self.assertLessEqual(url_sig_timestamp, time_after)
signer = RegisterSigner(verification_data)
signer.verify()
@override_settings(
REST_REGISTRATION=REST_REGISTRATION_WITH_VERIFICATION_NO_PASSWORD,
)
def test_register_no_password_confirm_ok(self):
data = self._get_register_user_data(password='testpassword')
data.pop('password_confirm')
request = self.create_post_request(data)
time_before = math.floor(time.time())
with self.assert_one_mail_sent() as sent_emails:
response = self.view_func(request)
self.assert_valid_response(response, status.HTTP_201_CREATED)
time_after = math.ceil(time.time())
user_id = response.data['id']
# Check database state.
user = self.user_class.objects.get(id=user_id)
self.assertEqual(user.username, data['username'])
self.assertTrue(user.check_password(data['password']))
self.assertFalse(user.is_active)
# Check verification e-mail.
sent_email = sent_emails[0]
self.assertEqual(sent_email.from_email, VERIFICATION_FROM_EMAIL)
self.assertListEqual(sent_email.to, [data['email']])
url = self.assert_one_url_line_in_text(sent_email.body)
verification_data = self.assert_valid_verification_url(
url,
expected_path=REGISTER_VERIFICATION_URL,
expected_query_keys={'signature', 'user_id', 'timestamp'},
)
url_user_id = int(verification_data['user_id'])
self.assertEqual(url_user_id, user_id)
url_sig_timestamp = int(verification_data['timestamp'])
self.assertGreaterEqual(url_sig_timestamp, time_before)
self.assertLessEqual(url_sig_timestamp, time_after)
signer = RegisterSigner(verification_data)
signer.verify()
def test_register_same_username(self):
self.create_test_user(username='testusername')
data = self._get_register_user_data(
username='testusername', password='testpassword')
request = self.create_post_request(data)
with self.assert_no_mail_sent():
response = self.view_func(request)
self.assert_invalid_response(response, status.HTTP_400_BAD_REQUEST)
@override_settings(
REST_REGISTRATION=REST_REGISTRATION_WITHOUT_VERIFICATION,
)
def test_register_without_verification_ok(self):
data = self._get_register_user_data(password='testpassword')
request = self.create_post_request(data)
with self.assert_no_mail_sent():
response = self.view_func(request)
self.assert_valid_response(response, status.HTTP_201_CREATED)
user_id = response.data['id']
user = self.user_class.objects.get(id=user_id)
self.assertEqual(user.username, data['username'])
self.assertTrue(user.check_password(data['password']))
self.assertTrue(user.is_active)
def test_register_missing_email(self):
data = self._get_register_user_data(password='testpassword')
del data['email']
request = self.create_post_request(data)
with self.assert_no_mail_sent():
response = self.view_func(request)
self.assert_invalid_response(response, status.HTTP_400_BAD_REQUEST)
def test_register_empty_email(self):
data = self._get_register_user_data(password='testpassword', email='')
request = self.create_post_request(data)
with self.assert_no_mail_sent():
response = self.view_func(request)
self.assert_response_is_bad_request(response)
def test_register_short_password(self):
data = self._get_register_user_data(password='a')
request = self.create_post_request(data)
with self.assert_no_mail_sent():
response = self.view_func(request)
self.assert_response_is_bad_request(response)
def test_register_password_numeric(self):
data = self._get_register_user_data(password='4321332211113322')
request = self.create_post_request(data)
with self.assert_no_mail_sent():
response = self.view_func(request)
self.assert_response_is_bad_request(response)
def test_register_password_same_as_username(self):
username = 'testusername'
data = self._get_register_user_data(
username=username, password=username)
request = self.create_post_request(data)
with self.assert_no_mail_sent():
response = self.view_func(request)
self.assert_response_is_bad_request(response)
def test_register_not_matching_password(self):
data = self._get_register_user_data(
password='testpassword1',
password_confirm='testpassword2')
request = self.create_post_request(data)
with self.assert_no_mail_sent():
response = self.view_func(request)
self.assert_response_is_bad_request(response)
def _get_register_user_data(
self, password, password_confirm=None, **options):
username = 'testusername'
email = 'testusername@example.com'
if password_confirm is None:
password_confirm = password
data = {
'username': username,
'password': password,
'password_confirm': password_confirm,
'email': email,
}
data.update(options)
return data
class VerifyRegistrationViewTestCase(APIViewTestCase):
VIEW_NAME = 'verify-registration'
@override_settings(REST_REGISTRATION=REST_REGISTRATION_WITH_VERIFICATION)
def test_verify_ok(self):
user = self.create_test_user(is_active=False)
self.assertFalse(user.is_active)
signer = RegisterSigner({'user_id': user.pk})
data = signer.get_signed_data()
request = self.create_post_request(data)
response = self.view_func(request)
self.assert_valid_response(response, status.HTTP_200_OK)
user.refresh_from_db()
self.assertTrue(user.is_active)
@override_settings(REST_REGISTRATION=REST_REGISTRATION_WITH_VERIFICATION)
def test_verify_tampered_timestamp(self):
user = self.create_test_user(is_active=False)
self.assertFalse(user.is_active)
signer = RegisterSigner({'user_id': user.pk})
data = signer.get_signed_data()
data['timestamp'] += 1
request = self.create_post_request(data)
response = self.view_func(request)
self.assert_invalid_response(response, status.HTTP_400_BAD_REQUEST)
user.refresh_from_db()
self.assertFalse(user.is_active)
@override_settings(REST_REGISTRATION=REST_REGISTRATION_WITH_VERIFICATION)
def test_verify_expired(self):
timestamp = int(time.time())
user = self.create_test_user(is_active=False)
self.assertFalse(user.is_active)
with patch('time.time',
side_effect=lambda: timestamp):
signer = RegisterSigner({'user_id': user.pk})
data = signer.get_signed_data()
request = self.create_post_request(data)
with patch('time.time',
side_effect=lambda: timestamp + 3600 * 24 * 8):
response = self.view_func(request)
self.assert_invalid_response(response, status.HTTP_400_BAD_REQUEST)
user.refresh_from_db()
self.assertFalse(user.is_active)
@override_settings(
REST_REGISTRATION={
'REGISTER_VERIFICATION_ENABLED': False,
'REGISTER_VERIFICATION_URL': REGISTER_VERIFICATION_URL,
}
)
def test_verify_disabled(self):
user = self.create_test_user(is_active=False)
self.assertFalse(user.is_active)
signer = RegisterSigner({'user_id': user.pk})
data = signer.get_signed_data()
request = self.create_post_request(data)
response = self.view_func(request)
self.assert_invalid_response(response, status.HTTP_404_NOT_FOUND)
user.refresh_from_db()
self.assertFalse(user.is_active)