sanic-security 1.16.11__py3-none-any.whl → 1.17.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,547 +1,547 @@
1
- import json
2
- import os
3
- from unittest import TestCase
4
-
5
- import httpx
6
-
7
- from sanic_security.configuration import Config
8
-
9
- """
10
- Copyright (c) 2020-present Nicholas Aidan Stewart
11
-
12
- Permission is hereby granted, free of charge, to any person obtaining a copy
13
- of this software and associated documentation files (the "Software"), to deal
14
- in the Software without restriction, including without limitation the rights
15
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
- copies of the Software, and to permit persons to whom the Software is
17
- furnished to do so, subject to the following conditions:
18
-
19
- The above copyright notice and this permission notice shall be included in all
20
- copies or substantial portions of the Software.
21
-
22
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
- SOFTWARE.
29
- """
30
-
31
-
32
- class RegistrationTest(TestCase):
33
- """Registration tests."""
34
-
35
- def setUp(self):
36
- self.client = httpx.Client()
37
-
38
- def tearDown(self):
39
- self.client.close()
40
-
41
- def register(
42
- self,
43
- email: str,
44
- username: str,
45
- disabled: bool,
46
- verified: bool,
47
- phone: str = None,
48
- ):
49
- registration_response = self.client.post(
50
- "http://127.0.0.1:8000/api/test/auth/register",
51
- data={
52
- "username": username,
53
- "email": email,
54
- "password": "Pas$word1",
55
- "disabled": disabled,
56
- "verified": verified,
57
- "phone": phone,
58
- },
59
- )
60
- return registration_response
61
-
62
- def test_registration(self):
63
- """Account registration and login."""
64
- registration_response = self.register(
65
- "account_registration@register.test",
66
- "account_registration",
67
- False,
68
- True,
69
- "6172818371",
70
- )
71
- assert registration_response.status_code == 200, registration_response.text
72
- login_response = self.client.post(
73
- "http://127.0.0.1:8000/api/test/auth/login",
74
- auth=("account_registration@register.test", "Pas$word1"),
75
- )
76
- assert login_response.status_code == 200, login_response.text
77
-
78
- def test_invalid_registration(self):
79
- """Registration with an intentionally invalid email, username, and phone."""
80
- invalid_email_registration_response = self.register(
81
- "invalid_register.test", "invalid_register", False, True
82
- )
83
- assert (
84
- invalid_email_registration_response.status_code == 400
85
- ), invalid_email_registration_response.text
86
- invalid_phone_registration_response = self.register(
87
- "invalidnum@register.test", "invalid_num", False, True, phone="617261746"
88
- )
89
- assert (
90
- invalid_phone_registration_response.status_code == 400
91
- ), invalid_phone_registration_response.text
92
- too_many_characters_registration_response = self.register(
93
- "too_long_user@register.test",
94
- "this_username_is_too_long_to_be_registered_with",
95
- False,
96
- True,
97
- )
98
- assert (
99
- too_many_characters_registration_response.status_code == 400
100
- ), too_many_characters_registration_response.text
101
-
102
- def test_registration_disabled(self):
103
- """Registration and login with a disabled account."""
104
- registration_response = self.register(
105
- "disabled@register.test", "disabled", True, True
106
- )
107
- assert registration_response.status_code == 200, registration_response.text
108
- login_response = self.client.post(
109
- "http://127.0.0.1:8000/api/test/auth/login",
110
- auth=("disabled@register.test", "Pas$word1"),
111
- )
112
- assert "DisabledError" in login_response.text, login_response.text
113
-
114
- def test_registration_unverified(self):
115
- """Registration and login with an unverified account."""
116
- registration_response = self.register(
117
- "unverified@register.test", "unverified", False, False
118
- )
119
- assert registration_response.status_code == 200, registration_response.text
120
- login_response = self.client.post(
121
- "http://127.0.0.1:8000/api/test/auth/login",
122
- auth=("unverified@register.test", "Pas$word1"),
123
- )
124
- assert "UnverifiedError" in login_response.text, login_response.text
125
-
126
- def test_registration_unverified_disabled(self):
127
- """Registration and login with an unverified and disabled account."""
128
- registration_response = self.register(
129
- "unverified_disabled@register.test", "unverified_disabled", True, False
130
- )
131
- assert registration_response.status_code == 200, registration_response.text
132
- login_response = self.client.post(
133
- "http://127.0.0.1:8000/api/test/auth/login",
134
- auth=("unverified_disabled@register.test", "Pas$word1"),
135
- )
136
- assert "UnverifiedError" in login_response.text, login_response.text
137
-
138
-
139
- class LoginTest(TestCase):
140
- """Login tests."""
141
-
142
- def setUp(self):
143
- self.client = httpx.Client()
144
-
145
- def tearDown(self):
146
- self.client.close()
147
-
148
- def test_login(self):
149
- """Login with an email and password."""
150
- self.client.post(
151
- "http://127.0.0.1:8000/api/test/account",
152
- data={"email": "email_pass@login.test", "username": "email_pass"},
153
- )
154
- login_response = self.client.post(
155
- "http://127.0.0.1:8000/api/test/auth/login",
156
- auth=("email_pass@login.test", "password"),
157
- )
158
- assert login_response.status_code == 200, login_response.text
159
- authenticate_response = self.client.post(
160
- "http://127.0.0.1:8000/api/test/auth",
161
- )
162
- assert authenticate_response.status_code == 200, authenticate_response.text
163
-
164
- def test_login_with_username(self):
165
- """Login with a username instead of an email and password."""
166
- self.client.post(
167
- "http://127.0.0.1:8000/api/test/account",
168
- data={"email": "user_pass@login.test", "username": "user_pass"},
169
- )
170
- login_response = self.client.post(
171
- "http://127.0.0.1:8000/api/test/auth/login",
172
- auth=("user_pass", "password"),
173
- )
174
- assert login_response.status_code == 200, login_response.text
175
- authenticate_response = self.client.post(
176
- "http://127.0.0.1:8000/api/test/auth",
177
- )
178
- assert authenticate_response.status_code == 200, authenticate_response.text
179
-
180
- def test_invalid_login(self):
181
- """Login with an intentionally incorrect password and into a non-existent account."""
182
- self.client.post(
183
- "http://127.0.0.1:8000/api/test/account",
184
- data={"email": "incorrect_pass@login.test", "username": "incorrect_pass"},
185
- )
186
- incorrect_password_login_response = self.client.post(
187
- "http://127.0.0.1:8000/api/test/auth/login",
188
- auth=("incorrect_pass@login.test", "incorrect_password"),
189
- )
190
- assert (
191
- incorrect_password_login_response.status_code == 401
192
- ), incorrect_password_login_response.text
193
- unavailable_account_login_response = self.client.post(
194
- "http://127.0.0.1:8000/api/test/auth/login",
195
- auth=("unavailable@login.test", "password"),
196
- )
197
- assert (
198
- unavailable_account_login_response.status_code == 404
199
- ), unavailable_account_login_response
200
-
201
- def test_logout(self):
202
- """Logout of logged in account and attempt to authenticate."""
203
- self.client.post(
204
- "http://127.0.0.1:8000/api/test/account",
205
- data={"email": "logout@login.test", "username": "logout"},
206
- )
207
- self.client.post(
208
- "http://127.0.0.1:8000/api/test/auth/login",
209
- auth=("logout@login.test", "password"),
210
- )
211
- logout_response = self.client.post("http://127.0.0.1:8000/api/test/auth/logout")
212
- assert logout_response.status_code == 200, logout_response.text
213
- authenticate_response = self.client.post(
214
- "http://127.0.0.1:8000/api/test/auth",
215
- )
216
- assert authenticate_response.status_code == 401, authenticate_response.text
217
-
218
- def test_initial_admin_login(self):
219
- """Initial admin account login and authorization."""
220
- login_response = self.client.post(
221
- "http://127.0.0.1:8000/api/test/auth/login",
222
- auth=("admin@login.test", "admin123"),
223
- )
224
- assert login_response.status_code == 200, login_response.text
225
- permitted_authorization_response = self.client.post(
226
- "http://127.0.0.1:8000/api/test/auth/roles",
227
- data={
228
- "role": "Root",
229
- "permissions_required": ["perm1:create,add", "perm2:*"],
230
- },
231
- )
232
- assert (
233
- permitted_authorization_response.status_code == 200
234
- ), permitted_authorization_response.text
235
-
236
- def test_two_factor_login(self):
237
- """Test login with two-factor authentication requirement."""
238
- self.client.post(
239
- "http://127.0.0.1:8000/api/test/account",
240
- data={"email": "two-factor@login.test", "username": "two-factor"},
241
- )
242
- login_response = self.client.post(
243
- "http://127.0.0.1:8000/api/test/auth/login?two-factor-authentication=true",
244
- auth=("two-factor@login.test", "password"),
245
- )
246
- assert login_response.status_code == 200, login_response.text
247
- authentication_invalid_response = self.client.post(
248
- "http://127.0.0.1:8000/api/test/auth",
249
- )
250
- assert (
251
- authentication_invalid_response.status_code == 401
252
- ), authentication_invalid_response.text
253
- two_factor_authentication_attempt_response = self.client.post(
254
- "http://127.0.0.1:8000/api/test/auth/validate-2fa",
255
- data={"code": json.loads(login_response.text)["data"]},
256
- )
257
- assert (
258
- two_factor_authentication_attempt_response.status_code == 200
259
- ), two_factor_authentication_attempt_response.text
260
- authenticate_response = self.client.post(
261
- "http://127.0.0.1:8000/api/test/auth",
262
- )
263
- assert authenticate_response.status_code == 200, authenticate_response.text
264
-
265
- def test_anonymous_login(self):
266
- """Test login of anonymous user."""
267
- anon_login_response = self.client.post(
268
- "http://127.0.0.1:8000/api/test/auth/login/anon"
269
- )
270
- assert anon_login_response.status_code == 200, anon_login_response.text
271
- authenticate_response = self.client.post(
272
- "http://127.0.0.1:8000/api/test/auth",
273
- )
274
- assert authenticate_response.status_code == 200, authenticate_response.text
275
- logout_response = self.client.post("http://127.0.0.1:8000/api/test/auth/logout")
276
- assert logout_response.status_code == 200, logout_response.text
277
-
278
-
279
- class VerificationTest(TestCase):
280
- """Two-step verification and captcha tests."""
281
-
282
- def setUp(self):
283
- self.client = httpx.Client()
284
-
285
- def tearDown(self):
286
- self.client.close()
287
-
288
- def test_captcha(self):
289
- """Captcha request and attempt."""
290
- captcha_request_response = self.client.get(
291
- "http://127.0.0.1:8000/api/test/capt/request"
292
- )
293
- assert (
294
- captcha_request_response.status_code == 200
295
- ), captcha_request_response.text
296
- captcha_image_response = self.client.get(
297
- "http://127.0.0.1:8000/api/test/capt/image"
298
- )
299
- assert captcha_image_response.status_code == 200, captcha_image_response.text
300
- captcha_attempt_response = self.client.post(
301
- "http://127.0.0.1:8000/api/test/capt",
302
- data={"captcha": json.loads(captcha_request_response.text)["data"]},
303
- )
304
- assert (
305
- captcha_attempt_response.status_code == 200
306
- ), captcha_attempt_response.text
307
-
308
- def test_two_step_verification(self):
309
- """Two-step verification request and attempt."""
310
- self.client.post(
311
- "http://127.0.0.1:8000/api/test/account",
312
- data={"email": "two_step@verification.test", "username": "two_step"},
313
- )
314
- two_step_verification_request_response = self.client.post(
315
- "http://127.0.0.1:8000/api/test/two-step/request",
316
- data={"email": "two_step@verification.test"},
317
- )
318
- assert (
319
- two_step_verification_request_response.status_code == 200
320
- ), two_step_verification_request_response.text
321
- two_step_verification_invalid_attempt_response = self.client.post(
322
- "http://127.0.0.1:8000/api/test/two-step",
323
- data={"code": "123xyz"},
324
- )
325
- assert (
326
- two_step_verification_invalid_attempt_response.status_code == 401
327
- ), two_step_verification_invalid_attempt_response.text
328
- two_step_verification_no_email_request_response = self.client.post(
329
- "http://127.0.0.1:8000/api/test/two-step/request",
330
- )
331
- assert (
332
- two_step_verification_no_email_request_response.status_code == 200
333
- ), two_step_verification_no_email_request_response.text
334
- two_step_verification_attempt_response = self.client.post(
335
- "http://127.0.0.1:8000/api/test/two-step",
336
- data={
337
- "code": json.loads(
338
- two_step_verification_no_email_request_response.text
339
- )["data"]
340
- },
341
- )
342
- assert (
343
- two_step_verification_attempt_response.status_code == 200
344
- ), two_step_verification_attempt_response.text
345
-
346
- def test_account_verification(self):
347
- """Account registration and verification process with successful login."""
348
- registration_response = self.client.post(
349
- "http://127.0.0.1:8000/api/test/auth/register",
350
- data={
351
- "username": "account_verification",
352
- "email": "account_verification@verification.test",
353
- "password": "Pa$sword1",
354
- "disabled": False,
355
- "verified": False,
356
- },
357
- )
358
- assert registration_response.status_code == 200, registration_response.text
359
- verify_account_response = self.client.post(
360
- "http://127.0.0.1:8000/api/test/auth/verify",
361
- data={"code": json.loads(registration_response.text)["data"]},
362
- )
363
- assert verify_account_response.status_code == 200, verify_account_response.text
364
-
365
-
366
- class AuthorizationTest(TestCase):
367
- """Role and permissions based authorization tests."""
368
-
369
- def setUp(self):
370
- self.client = httpx.Client()
371
-
372
- def tearDown(self):
373
- self.client.close()
374
-
375
- def test_permissions_authorization(self):
376
- """Authorization with permissions."""
377
- self.client.post(
378
- "http://127.0.0.1:8000/api/test/account",
379
- data={"email": "permissions@authorization.test", "username": "permissions"},
380
- )
381
- self.client.post(
382
- "http://127.0.0.1:8000/api/test/auth/login",
383
- auth=("permissions@authorization.test", "password"),
384
- )
385
- self.client.post(
386
- "http://127.0.0.1:8000/api/test/auth/roles/assign",
387
- data={
388
- "name": "AuthTestPerms",
389
- "permissions": "perm1:create,update, perm2:delete,retrieve, perm3:*",
390
- },
391
- )
392
- permitted_authorization_response = self.client.post(
393
- "http://127.0.0.1:8000/api/test/auth/roles",
394
- data={
395
- "role": "AuthTestPerms",
396
- "permissions_required": "perm1:create,update, perm3:retrieve",
397
- },
398
- )
399
- assert (
400
- permitted_authorization_response.status_code == 200
401
- ), permitted_authorization_response.text
402
- permitted_authorization_response = self.client.post(
403
- "http://127.0.0.1:8000/api/test/auth/roles",
404
- data={
405
- "role": "AuthTestPerms",
406
- "permissions_required": "perm1:retrieve, perm2:delete",
407
- },
408
- )
409
- assert (
410
- permitted_authorization_response.status_code == 200
411
- ), permitted_authorization_response.text
412
-
413
- prohibited_authorization_response = self.client.post(
414
- "http://127.0.0.1:8000/api/test/auth/roles",
415
- data={
416
- "role": "AuthTestPerms",
417
- "permissions_required": "perm1:create,retrieve",
418
- },
419
- )
420
- assert (
421
- prohibited_authorization_response.status_code == 403
422
- ), prohibited_authorization_response.text
423
- prohibited_authorization_response = self.client.post(
424
- "http://127.0.0.1:8000/api/test/auth/roles",
425
- data={
426
- "role": "AuthTestPerms",
427
- "permissions_required": "perm1:delete, perm2:create",
428
- },
429
- )
430
- assert (
431
- prohibited_authorization_response.status_code == 403
432
- ), prohibited_authorization_response.text
433
-
434
- def test_roles_authorization(self):
435
- """Authorization with roles."""
436
- self.client.post(
437
- "http://127.0.0.1:8000/api/test/account",
438
- data={"email": "roles@authorization.test", "username": "roles"},
439
- )
440
- self.client.post(
441
- "http://127.0.0.1:8000/api/test/auth/login",
442
- auth=("roles@authorization.test", "password"),
443
- )
444
- self.client.post(
445
- "http://127.0.0.1:8000/api/test/auth/roles/assign",
446
- data={"name": "AuthTestRole"},
447
- )
448
- permitted_authorization_response = self.client.post(
449
- "http://127.0.0.1:8000/api/test/auth/roles",
450
- data={
451
- "role": "AuthTestRole",
452
- },
453
- )
454
- assert (
455
- permitted_authorization_response.status_code == 200
456
- ), permitted_authorization_response.text
457
- prohibited_authorization_response = self.client.post(
458
- "http://127.0.0.1:8000/api/test/auth/roles",
459
- data={"role": "InvalidRole"},
460
- )
461
- assert (
462
- prohibited_authorization_response.status_code == 403
463
- ), prohibited_authorization_response.text
464
-
465
- def test_anonymous_authorization(self):
466
- """Authorization with anonymous client."""
467
- anon_login_response = self.client.post(
468
- "http://127.0.0.1:8000/api/test/auth/login/anon"
469
- )
470
- assert anon_login_response.status_code == 200, anon_login_response.text
471
- authenticate_response = self.client.post(
472
- "http://127.0.0.1:8000/api/test/auth",
473
- )
474
- assert authenticate_response.status_code == 200, authenticate_response.text
475
- prohibited_authorization_response = self.client.post(
476
- "http://127.0.0.1:8000/api/test/auth/roles",
477
- data={"role": "AuthTestPerms"},
478
- )
479
- assert (
480
- prohibited_authorization_response.status_code == 403
481
- ), prohibited_authorization_response.text
482
-
483
-
484
- class MiscTest(TestCase):
485
- """Miscellaneous tests that cannot be categorized."""
486
-
487
- def setUp(self):
488
- self.client = httpx.Client()
489
-
490
- def tearDown(self):
491
- self.client.close()
492
-
493
- def test_environment_variable_load(self):
494
- """Config loads environment variables."""
495
- os.environ["SANIC_SECURITY_SECRET"] = "test-secret"
496
- security_config = Config()
497
- security_config.load_environment_variables()
498
- assert security_config.SECRET == "test-secret"
499
-
500
- def test_get_associated_sessions(self):
501
- """Retrieve sessions associated to logged in account."""
502
- self.client.post(
503
- "http://127.0.0.1:8000/api/test/account",
504
- data={
505
- "email": "get_associated_sessions@misc.test",
506
- "username": "get_associated",
507
- },
508
- )
509
- login_response = self.client.post(
510
- "http://127.0.0.1:8000/api/test/auth/login",
511
- auth=("get_associated_sessions@misc.test", "password"),
512
- )
513
- assert login_response.status_code == 200, login_response.text
514
- retrieve_associated_response = self.client.get(
515
- "http://127.0.0.1:8000/api/test/auth/associated"
516
- )
517
- assert (
518
- retrieve_associated_response.status_code == 200
519
- ), retrieve_associated_response.text
520
-
521
- def test_authentication_refresh(self):
522
- """Test automatic authentication refresh."""
523
- self.client.post(
524
- "http://127.0.0.1:8000/api/test/account",
525
- data={
526
- "email": "refreshed@misc.test",
527
- "username": "refreshed",
528
- },
529
- )
530
- login_response = self.client.post(
531
- "http://127.0.0.1:8000/api/test/auth/login",
532
- auth=("refreshed@misc.test", "password"),
533
- )
534
- assert login_response.status_code == 200, login_response.text
535
- expire_response = self.client.post("http://127.0.0.1:8000/api/test/auth/expire")
536
- assert expire_response.status_code == 200, expire_response.text
537
- authenticate_refresh_response = self.client.post(
538
- "http://127.0.0.1:8000/api/test/auth",
539
- )
540
- assert (
541
- authenticate_refresh_response.status_code == 200
542
- ), authenticate_refresh_response.text
543
- assert json.loads(authenticate_refresh_response.text)["data"]["refresh"] is True
544
- authenticate_response = self.client.post(
545
- "http://127.0.0.1:8000/api/test/auth",
546
- ) # Since session refresh handling is complete, it will be returned as a regular session now.
547
- assert authenticate_response.status_code == 200, authenticate_response.text
1
+ import json
2
+ import os
3
+ from unittest import TestCase
4
+
5
+ import httpx
6
+
7
+ from sanic_security.configuration import Config
8
+
9
+ """
10
+ Copyright (c) 2020-present Nicholas Aidan Stewart
11
+
12
+ Permission is hereby granted, free of charge, to any person obtaining a copy
13
+ of this software and associated documentation files (the "Software"), to deal
14
+ in the Software without restriction, including without limitation the rights
15
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
+ copies of the Software, and to permit persons to whom the Software is
17
+ furnished to do so, subject to the following conditions:
18
+
19
+ The above copyright notice and this permission notice shall be included in all
20
+ copies or substantial portions of the Software.
21
+
22
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
+ SOFTWARE.
29
+ """
30
+
31
+
32
+ class RegistrationTest(TestCase):
33
+ """Registration tests."""
34
+
35
+ def setUp(self):
36
+ self.client = httpx.Client()
37
+
38
+ def tearDown(self):
39
+ self.client.close()
40
+
41
+ def register(
42
+ self,
43
+ email: str,
44
+ username: str,
45
+ disabled: bool,
46
+ verified: bool,
47
+ phone: str = None,
48
+ ):
49
+ registration_response = self.client.post(
50
+ "http://127.0.0.1:8000/api/test/auth/register",
51
+ data={
52
+ "username": username,
53
+ "email": email,
54
+ "password": "Pas$word1",
55
+ "disabled": disabled,
56
+ "verified": verified,
57
+ "phone": phone,
58
+ },
59
+ )
60
+ return registration_response
61
+
62
+ def test_registration(self):
63
+ """Account registration and login."""
64
+ registration_response = self.register(
65
+ "account_registration@register.test",
66
+ "account_registration",
67
+ False,
68
+ True,
69
+ "6172818371",
70
+ )
71
+ assert registration_response.status_code == 200, registration_response.text
72
+ login_response = self.client.post(
73
+ "http://127.0.0.1:8000/api/test/auth/login",
74
+ auth=("account_registration@register.test", "Pas$word1"),
75
+ )
76
+ assert login_response.status_code == 200, login_response.text
77
+
78
+ def test_invalid_registration(self):
79
+ """Registration with an intentionally invalid email, username, and phone."""
80
+ invalid_email_registration_response = self.register(
81
+ "invalid_register.test", "invalid_register", False, True
82
+ )
83
+ assert (
84
+ invalid_email_registration_response.status_code == 400
85
+ ), invalid_email_registration_response.text
86
+ invalid_phone_registration_response = self.register(
87
+ "invalidnum@register.test", "invalid_num", False, True, phone="617261746"
88
+ )
89
+ assert (
90
+ invalid_phone_registration_response.status_code == 400
91
+ ), invalid_phone_registration_response.text
92
+ too_many_characters_registration_response = self.register(
93
+ "too_long_user@register.test",
94
+ "this_username_is_too_long_to_be_registered_with",
95
+ False,
96
+ True,
97
+ )
98
+ assert (
99
+ too_many_characters_registration_response.status_code == 400
100
+ ), too_many_characters_registration_response.text
101
+
102
+ def test_registration_disabled(self):
103
+ """Registration and login with a disabled account."""
104
+ registration_response = self.register(
105
+ "disabled@register.test", "disabled", True, True
106
+ )
107
+ assert registration_response.status_code == 200, registration_response.text
108
+ login_response = self.client.post(
109
+ "http://127.0.0.1:8000/api/test/auth/login",
110
+ auth=("disabled@register.test", "Pas$word1"),
111
+ )
112
+ assert "DisabledError" in login_response.text, login_response.text
113
+
114
+ def test_registration_unverified(self):
115
+ """Registration and login with an unverified account."""
116
+ registration_response = self.register(
117
+ "unverified@register.test", "unverified", False, False
118
+ )
119
+ assert registration_response.status_code == 200, registration_response.text
120
+ login_response = self.client.post(
121
+ "http://127.0.0.1:8000/api/test/auth/login",
122
+ auth=("unverified@register.test", "Pas$word1"),
123
+ )
124
+ assert "UnverifiedError" in login_response.text, login_response.text
125
+
126
+ def test_registration_unverified_disabled(self):
127
+ """Registration and login with an unverified and disabled account."""
128
+ registration_response = self.register(
129
+ "unverified_disabled@register.test", "unverified_disabled", True, False
130
+ )
131
+ assert registration_response.status_code == 200, registration_response.text
132
+ login_response = self.client.post(
133
+ "http://127.0.0.1:8000/api/test/auth/login",
134
+ auth=("unverified_disabled@register.test", "Pas$word1"),
135
+ )
136
+ assert "UnverifiedError" in login_response.text, login_response.text
137
+
138
+
139
+ class LoginTest(TestCase):
140
+ """Login tests."""
141
+
142
+ def setUp(self):
143
+ self.client = httpx.Client()
144
+
145
+ def tearDown(self):
146
+ self.client.close()
147
+
148
+ def test_login(self):
149
+ """Login with an email and password."""
150
+ self.client.post(
151
+ "http://127.0.0.1:8000/api/test/account",
152
+ data={"email": "email_pass@login.test", "username": "email_pass"},
153
+ )
154
+ login_response = self.client.post(
155
+ "http://127.0.0.1:8000/api/test/auth/login",
156
+ auth=("email_pass@login.test", "password"),
157
+ )
158
+ assert login_response.status_code == 200, login_response.text
159
+ authenticate_response = self.client.post(
160
+ "http://127.0.0.1:8000/api/test/auth",
161
+ )
162
+ assert authenticate_response.status_code == 200, authenticate_response.text
163
+
164
+ def test_login_with_username(self):
165
+ """Login with a username instead of an email and password."""
166
+ self.client.post(
167
+ "http://127.0.0.1:8000/api/test/account",
168
+ data={"email": "user_pass@login.test", "username": "user_pass"},
169
+ )
170
+ login_response = self.client.post(
171
+ "http://127.0.0.1:8000/api/test/auth/login",
172
+ auth=("user_pass", "password"),
173
+ )
174
+ assert login_response.status_code == 200, login_response.text
175
+ authenticate_response = self.client.post(
176
+ "http://127.0.0.1:8000/api/test/auth",
177
+ )
178
+ assert authenticate_response.status_code == 200, authenticate_response.text
179
+
180
+ def test_invalid_login(self):
181
+ """Login with an intentionally incorrect password and into a non-existent account."""
182
+ self.client.post(
183
+ "http://127.0.0.1:8000/api/test/account",
184
+ data={"email": "incorrect_pass@login.test", "username": "incorrect_pass"},
185
+ )
186
+ incorrect_password_login_response = self.client.post(
187
+ "http://127.0.0.1:8000/api/test/auth/login",
188
+ auth=("incorrect_pass@login.test", "incorrect_password"),
189
+ )
190
+ assert (
191
+ incorrect_password_login_response.status_code == 401
192
+ ), incorrect_password_login_response.text
193
+ unavailable_account_login_response = self.client.post(
194
+ "http://127.0.0.1:8000/api/test/auth/login",
195
+ auth=("unavailable@login.test", "password"),
196
+ )
197
+ assert (
198
+ unavailable_account_login_response.status_code == 404
199
+ ), unavailable_account_login_response
200
+
201
+ def test_logout(self):
202
+ """Logout of logged in account and attempt to authenticate."""
203
+ self.client.post(
204
+ "http://127.0.0.1:8000/api/test/account",
205
+ data={"email": "logout@login.test", "username": "logout"},
206
+ )
207
+ self.client.post(
208
+ "http://127.0.0.1:8000/api/test/auth/login",
209
+ auth=("logout@login.test", "password"),
210
+ )
211
+ logout_response = self.client.post("http://127.0.0.1:8000/api/test/auth/logout")
212
+ assert logout_response.status_code == 200, logout_response.text
213
+ authenticate_response = self.client.post(
214
+ "http://127.0.0.1:8000/api/test/auth",
215
+ )
216
+ assert authenticate_response.status_code == 401, authenticate_response.text
217
+
218
+ def test_initial_admin_login(self):
219
+ """Initial admin account login and authorization."""
220
+ login_response = self.client.post(
221
+ "http://127.0.0.1:8000/api/test/auth/login",
222
+ auth=("admin@login.test", "admin123"),
223
+ )
224
+ assert login_response.status_code == 200, login_response.text
225
+ permitted_authorization_response = self.client.post(
226
+ "http://127.0.0.1:8000/api/test/auth/roles",
227
+ data={
228
+ "role": "Root",
229
+ "permissions_required": ["perm1:create,add", "perm2:*"],
230
+ },
231
+ )
232
+ assert (
233
+ permitted_authorization_response.status_code == 200
234
+ ), permitted_authorization_response.text
235
+
236
+ def test_two_factor_login(self):
237
+ """Test login with two-factor authentication requirement."""
238
+ self.client.post(
239
+ "http://127.0.0.1:8000/api/test/account",
240
+ data={"email": "two-factor@login.test", "username": "two-factor"},
241
+ )
242
+ login_response = self.client.post(
243
+ "http://127.0.0.1:8000/api/test/auth/login?two-factor-authentication=true",
244
+ auth=("two-factor@login.test", "password"),
245
+ )
246
+ assert login_response.status_code == 200, login_response.text
247
+ authentication_invalid_response = self.client.post(
248
+ "http://127.0.0.1:8000/api/test/auth",
249
+ )
250
+ assert (
251
+ authentication_invalid_response.status_code == 401
252
+ ), authentication_invalid_response.text
253
+ two_factor_authentication_attempt_response = self.client.post(
254
+ "http://127.0.0.1:8000/api/test/auth/validate-2fa",
255
+ data={"code": json.loads(login_response.text)["data"]},
256
+ )
257
+ assert (
258
+ two_factor_authentication_attempt_response.status_code == 200
259
+ ), two_factor_authentication_attempt_response.text
260
+ authenticate_response = self.client.post(
261
+ "http://127.0.0.1:8000/api/test/auth",
262
+ )
263
+ assert authenticate_response.status_code == 200, authenticate_response.text
264
+
265
+ def test_anonymous_login(self):
266
+ """Test login of anonymous user."""
267
+ anon_login_response = self.client.post(
268
+ "http://127.0.0.1:8000/api/test/auth/login/anon"
269
+ )
270
+ assert anon_login_response.status_code == 200, anon_login_response.text
271
+ authenticate_response = self.client.post(
272
+ "http://127.0.0.1:8000/api/test/auth",
273
+ )
274
+ assert authenticate_response.status_code == 200, authenticate_response.text
275
+ logout_response = self.client.post("http://127.0.0.1:8000/api/test/auth/logout")
276
+ assert logout_response.status_code == 200, logout_response.text
277
+
278
+
279
+ class VerificationTest(TestCase):
280
+ """Two-step verification and captcha tests."""
281
+
282
+ def setUp(self):
283
+ self.client = httpx.Client()
284
+
285
+ def tearDown(self):
286
+ self.client.close()
287
+
288
+ def test_captcha(self):
289
+ """Captcha request and attempt."""
290
+ captcha_request_response = self.client.get(
291
+ "http://127.0.0.1:8000/api/test/capt/request"
292
+ )
293
+ assert (
294
+ captcha_request_response.status_code == 200
295
+ ), captcha_request_response.text
296
+ captcha_image_response = self.client.get(
297
+ "http://127.0.0.1:8000/api/test/capt/image"
298
+ )
299
+ assert captcha_image_response.status_code == 200, captcha_image_response.text
300
+ captcha_attempt_response = self.client.post(
301
+ "http://127.0.0.1:8000/api/test/capt",
302
+ data={"captcha": json.loads(captcha_request_response.text)["data"]},
303
+ )
304
+ assert (
305
+ captcha_attempt_response.status_code == 200
306
+ ), captcha_attempt_response.text
307
+
308
+ def test_two_step_verification(self):
309
+ """Two-step verification request and attempt."""
310
+ self.client.post(
311
+ "http://127.0.0.1:8000/api/test/account",
312
+ data={"email": "two_step@verification.test", "username": "two_step"},
313
+ )
314
+ two_step_verification_request_response = self.client.post(
315
+ "http://127.0.0.1:8000/api/test/two-step/request",
316
+ data={"email": "two_step@verification.test"},
317
+ )
318
+ assert (
319
+ two_step_verification_request_response.status_code == 200
320
+ ), two_step_verification_request_response.text
321
+ two_step_verification_invalid_attempt_response = self.client.post(
322
+ "http://127.0.0.1:8000/api/test/two-step",
323
+ data={"code": "123xyz"},
324
+ )
325
+ assert (
326
+ two_step_verification_invalid_attempt_response.status_code == 401
327
+ ), two_step_verification_invalid_attempt_response.text
328
+ two_step_verification_no_email_request_response = self.client.post(
329
+ "http://127.0.0.1:8000/api/test/two-step/request",
330
+ )
331
+ assert (
332
+ two_step_verification_no_email_request_response.status_code == 200
333
+ ), two_step_verification_no_email_request_response.text
334
+ two_step_verification_attempt_response = self.client.post(
335
+ "http://127.0.0.1:8000/api/test/two-step",
336
+ data={
337
+ "code": json.loads(
338
+ two_step_verification_no_email_request_response.text
339
+ )["data"]
340
+ },
341
+ )
342
+ assert (
343
+ two_step_verification_attempt_response.status_code == 200
344
+ ), two_step_verification_attempt_response.text
345
+
346
+ def test_account_verification(self):
347
+ """Account registration and verification process with successful login."""
348
+ registration_response = self.client.post(
349
+ "http://127.0.0.1:8000/api/test/auth/register",
350
+ data={
351
+ "username": "account_verification",
352
+ "email": "account_verification@verification.test",
353
+ "password": "Pa$sword1",
354
+ "disabled": False,
355
+ "verified": False,
356
+ },
357
+ )
358
+ assert registration_response.status_code == 200, registration_response.text
359
+ verify_account_response = self.client.post(
360
+ "http://127.0.0.1:8000/api/test/auth/verify",
361
+ data={"code": json.loads(registration_response.text)["data"]},
362
+ )
363
+ assert verify_account_response.status_code == 200, verify_account_response.text
364
+
365
+
366
+ class AuthorizationTest(TestCase):
367
+ """Role and permissions based authorization tests."""
368
+
369
+ def setUp(self):
370
+ self.client = httpx.Client()
371
+
372
+ def tearDown(self):
373
+ self.client.close()
374
+
375
+ def test_permissions_authorization(self):
376
+ """Authorization with permissions."""
377
+ self.client.post(
378
+ "http://127.0.0.1:8000/api/test/account",
379
+ data={"email": "permissions@authorization.test", "username": "permissions"},
380
+ )
381
+ self.client.post(
382
+ "http://127.0.0.1:8000/api/test/auth/login",
383
+ auth=("permissions@authorization.test", "password"),
384
+ )
385
+ self.client.post(
386
+ "http://127.0.0.1:8000/api/test/auth/roles/assign",
387
+ data={
388
+ "name": "AuthTestPerms",
389
+ "permissions": "perm1:create,update, perm2:delete,retrieve, perm3:*",
390
+ },
391
+ )
392
+ permitted_authorization_response = self.client.post(
393
+ "http://127.0.0.1:8000/api/test/auth/roles",
394
+ data={
395
+ "role": "AuthTestPerms",
396
+ "permissions_required": "perm1:create,update, perm3:retrieve",
397
+ },
398
+ )
399
+ assert (
400
+ permitted_authorization_response.status_code == 200
401
+ ), permitted_authorization_response.text
402
+ permitted_authorization_response = self.client.post(
403
+ "http://127.0.0.1:8000/api/test/auth/roles",
404
+ data={
405
+ "role": "AuthTestPerms",
406
+ "permissions_required": "perm1:retrieve, perm2:delete",
407
+ },
408
+ )
409
+ assert (
410
+ permitted_authorization_response.status_code == 200
411
+ ), permitted_authorization_response.text
412
+
413
+ prohibited_authorization_response = self.client.post(
414
+ "http://127.0.0.1:8000/api/test/auth/roles",
415
+ data={
416
+ "role": "AuthTestPerms",
417
+ "permissions_required": "perm1:create,retrieve",
418
+ },
419
+ )
420
+ assert (
421
+ prohibited_authorization_response.status_code == 403
422
+ ), prohibited_authorization_response.text
423
+ prohibited_authorization_response = self.client.post(
424
+ "http://127.0.0.1:8000/api/test/auth/roles",
425
+ data={
426
+ "role": "AuthTestPerms",
427
+ "permissions_required": "perm1:delete, perm2:create",
428
+ },
429
+ )
430
+ assert (
431
+ prohibited_authorization_response.status_code == 403
432
+ ), prohibited_authorization_response.text
433
+
434
+ def test_roles_authorization(self):
435
+ """Authorization with roles."""
436
+ self.client.post(
437
+ "http://127.0.0.1:8000/api/test/account",
438
+ data={"email": "roles@authorization.test", "username": "roles"},
439
+ )
440
+ self.client.post(
441
+ "http://127.0.0.1:8000/api/test/auth/login",
442
+ auth=("roles@authorization.test", "password"),
443
+ )
444
+ self.client.post(
445
+ "http://127.0.0.1:8000/api/test/auth/roles/assign",
446
+ data={"name": "AuthTestRole"},
447
+ )
448
+ permitted_authorization_response = self.client.post(
449
+ "http://127.0.0.1:8000/api/test/auth/roles",
450
+ data={
451
+ "role": "AuthTestRole",
452
+ },
453
+ )
454
+ assert (
455
+ permitted_authorization_response.status_code == 200
456
+ ), permitted_authorization_response.text
457
+ prohibited_authorization_response = self.client.post(
458
+ "http://127.0.0.1:8000/api/test/auth/roles",
459
+ data={"role": "InvalidRole"},
460
+ )
461
+ assert (
462
+ prohibited_authorization_response.status_code == 403
463
+ ), prohibited_authorization_response.text
464
+
465
+ def test_anonymous_authorization(self):
466
+ """Authorization with anonymous client."""
467
+ anon_login_response = self.client.post(
468
+ "http://127.0.0.1:8000/api/test/auth/login/anon"
469
+ )
470
+ assert anon_login_response.status_code == 200, anon_login_response.text
471
+ authenticate_response = self.client.post(
472
+ "http://127.0.0.1:8000/api/test/auth",
473
+ )
474
+ assert authenticate_response.status_code == 200, authenticate_response.text
475
+ prohibited_authorization_response = self.client.post(
476
+ "http://127.0.0.1:8000/api/test/auth/roles",
477
+ data={"role": "AuthTestPerms"},
478
+ )
479
+ assert (
480
+ prohibited_authorization_response.status_code == 403
481
+ ), prohibited_authorization_response.text
482
+
483
+
484
+ class MiscTest(TestCase):
485
+ """Miscellaneous tests that cannot be categorized."""
486
+
487
+ def setUp(self):
488
+ self.client = httpx.Client()
489
+
490
+ def tearDown(self):
491
+ self.client.close()
492
+
493
+ def test_environment_variable_load(self):
494
+ """Config loads environment variables."""
495
+ os.environ["SANIC_SECURITY_SECRET"] = "test-secret"
496
+ security_config = Config()
497
+ security_config.load_environment_variables()
498
+ assert security_config.SECRET == "test-secret"
499
+
500
+ def test_get_associated_sessions(self):
501
+ """Retrieve sessions associated to logged in account."""
502
+ self.client.post(
503
+ "http://127.0.0.1:8000/api/test/account",
504
+ data={
505
+ "email": "get_associated_sessions@misc.test",
506
+ "username": "get_associated",
507
+ },
508
+ )
509
+ login_response = self.client.post(
510
+ "http://127.0.0.1:8000/api/test/auth/login",
511
+ auth=("get_associated_sessions@misc.test", "password"),
512
+ )
513
+ assert login_response.status_code == 200, login_response.text
514
+ retrieve_associated_response = self.client.get(
515
+ "http://127.0.0.1:8000/api/test/auth/associated"
516
+ )
517
+ assert (
518
+ retrieve_associated_response.status_code == 200
519
+ ), retrieve_associated_response.text
520
+
521
+ def test_authentication_refresh(self):
522
+ """Test automatic authentication refresh."""
523
+ self.client.post(
524
+ "http://127.0.0.1:8000/api/test/account",
525
+ data={
526
+ "email": "refreshed@misc.test",
527
+ "username": "refreshed",
528
+ },
529
+ )
530
+ login_response = self.client.post(
531
+ "http://127.0.0.1:8000/api/test/auth/login",
532
+ auth=("refreshed@misc.test", "password"),
533
+ )
534
+ assert login_response.status_code == 200, login_response.text
535
+ expire_response = self.client.post("http://127.0.0.1:8000/api/test/auth/expire")
536
+ assert expire_response.status_code == 200, expire_response.text
537
+ authenticate_refresh_response = self.client.post(
538
+ "http://127.0.0.1:8000/api/test/auth",
539
+ )
540
+ assert (
541
+ authenticate_refresh_response.status_code == 200
542
+ ), authenticate_refresh_response.text
543
+ assert json.loads(authenticate_refresh_response.text)["data"]["refresh"] is True
544
+ authenticate_response = self.client.post(
545
+ "http://127.0.0.1:8000/api/test/auth",
546
+ ) # Since session refresh handling is complete, it will be returned as a regular session now.
547
+ assert authenticate_response.status_code == 200, authenticate_response.text