-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
test_login.py
1203 lines (1057 loc) · 50.7 KB
/
test_login.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Tests for student activation and login
"""
import datetime
import hashlib
import json
import unicodedata
from unittest.mock import Mock, patch
import ddt
from django.conf import settings
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
from django.core import mail
from django.core.cache import cache
from django.http import HttpResponse
from django.test.client import Client
from django.test.utils import override_settings
from django.urls import NoReverseMatch, reverse
from edx_toggles.toggles.testutils import override_waffle_switch
from freezegun import freeze_time
from common.djangoapps.student.tests.factories import RegistrationFactory, UserFactory, UserProfileFactory
from openedx_events.tests.utils import OpenEdxEventsTestMixin # lint-amnesty, pylint: disable=wrong-import-order
from openedx.core.djangoapps.password_policy.compliance import (
NonCompliantPasswordException,
NonCompliantPasswordWarning
)
from openedx.core.djangoapps.password_policy.hibp import PwnedPasswordsAPI
from openedx.core.djangoapps.user_api.accounts import EMAIL_MIN_LENGTH, EMAIL_MAX_LENGTH
from openedx.core.djangoapps.user_authn.config.waffle import ENABLE_PWNED_PASSWORD_API
from openedx.core.djangoapps.user_authn.cookies import jwt_cookies
from openedx.core.djangoapps.user_authn.tests.utils import setup_login_oauth_client
from openedx.core.djangoapps.user_authn.views.login import (
ENABLE_LOGIN_USING_THIRDPARTY_AUTH_ONLY,
AllowedAuthUser,
_check_user_auth_flow
)
from openedx.core.djangolib.testing.utils import CacheIsolationTestCase, skip_unless_lms
from openedx.core.djangoapps.site_configuration.tests.mixins import SiteMixin
from openedx.core.lib.api.test_utils import ApiTestCase
from openedx.features.enterprise_support.tests.factories import EnterpriseCustomerUserFactory
from common.djangoapps.student.models import LoginFailures
from common.djangoapps.util.password_policy_validators import DEFAULT_MAX_PASSWORD_LENGTH
@ddt.ddt
class LoginTest(SiteMixin, CacheIsolationTestCase, OpenEdxEventsTestMixin):
"""
Test login_user() view
"""
ENABLED_OPENEDX_EVENTS = []
ENABLED_CACHES = ['default']
LOGIN_FAILED_WARNING = 'Email or password is incorrect'
ACTIVATE_ACCOUNT_WARNING = 'In order to sign in, you need to activate your account'
username = 'test'
user_email = 'test@edx.org'
password = 'test_password'
@classmethod
def setUpClass(cls):
"""
Set up class method for the Test class.
This method starts manually events isolation. Explanation here:
openedx/core/djangoapps/user_authn/views/tests/test_events.py#L44
"""
super().setUpClass()
cls.start_events_isolation()
def setUp(self):
"""Setup a test user along with its registration and profile"""
super().setUp()
self.user = self._create_user(self.username, self.user_email)
RegistrationFactory(user=self.user)
UserProfileFactory(user=self.user)
self.client = Client()
cache.clear()
self.url = reverse('login_api')
def _create_user(self, username, user_email):
user = UserFactory.create(username=username, email=user_email)
user.set_password(self.password)
user.save()
return user
def test_login_success(self):
response, mock_audit_log = self._login_response(
self.user_email, self.password, patched_audit_log='common.djangoapps.student.models.user.AUDIT_LOG'
)
self._assert_response(response, success=True)
self._assert_audit_log(mock_audit_log, 'info', ['Login success', self.user_email])
FEATURES_WITH_AUTHN_MFE_ENABLED = settings.FEATURES.copy()
FEATURES_WITH_AUTHN_MFE_ENABLED['ENABLE_AUTHN_MICROFRONTEND'] = True
@override_settings(MARKETING_EMAILS_OPT_IN=True)
def test_login_success_with_opt_in_flag_enabled(self):
self.user.is_active = False
self.user.save()
response, mock_audit_log = self._login_response(
self.user_email, self.password, patched_audit_log='common.djangoapps.student.models.user.AUDIT_LOG'
)
self._assert_response(response, success=True)
self._assert_audit_log(mock_audit_log, 'info', ['Login success', self.user_email])
@override_settings(MARKETING_EMAILS_OPT_IN=False)
def test_login_failed_with_opt_in_flag_disabled(self):
self.user.is_active = False
self.user.save()
response, mock_audit_log = self._login_response(self.user_email, self.password)
self._assert_audit_log(
mock_audit_log, 'warning', ['Login failed - Account not active for user.id: 1, resending activation']
)
@patch.dict(settings.FEATURES, {
"ENABLE_THIRD_PARTY_AUTH": True
})
@patch(
'openedx.core.djangoapps.user_authn.views.login.is_require_third_party_auth_enabled',
Mock(return_value=True)
)
@skip_unless_lms
def test_public_login_failure_with_only_third_part_auth_enabled(self):
response, _ = self._login_response(
self.user_email,
self.password,
)
assert response.status_code == 403
assert response.content == b'Third party authentication is required to login.' \
b' Username and password were received instead.'
@ddt.data(
# Default redirect is dashboard.
{
'next_url': None,
'course_id': None,
'expected_redirect': settings.LMS_ROOT_URL + '/dashboard',
},
# Added root url in next .
{
'next_url': '/harmless-relative-page',
'course_id': None,
'expected_redirect': settings.LMS_ROOT_URL + '/harmless-relative-page',
},
# An absolute URL to a non-whitelisted domain is not an acceptable redirect.
{
'next_url': 'https://evil.sketchysite',
'course_id': None,
'expected_redirect': settings.LMS_ROOT_URL + '/dashboard',
},
# An absolute URL to a whitelisted domain is acceptable.
{
'next_url': 'https://openedx.service/coolpage',
'course_id': None,
'expected_redirect': 'https://openedx.service/coolpage',
},
# If course_id is provided, redirect to finish_auth with dashboard as next.
{
'next_url': None,
'course_id': 'coursekey',
'expected_redirect': (
'{root_url}/account/finish_auth?course_id=coursekey&next=%2Fdashboard'.
format(root_url=settings.LMS_ROOT_URL)
),
},
# If valid course_id AND next_url are provided, redirect to finish_auth with
# provided next URL.
{
'next_url': 'freshpage',
'course_id': 'coursekey',
'expected_redirect': (
settings.LMS_ROOT_URL + '/account/finish_auth?course_id=coursekey&next=freshpage'
)
},
# If course_id is provided with invalid next_url, redirect to finish_auth with
# course_id and dashboard as next URL.
{
'next_url': 'http://scam.scam',
'course_id': 'coursekey',
'expected_redirect': (
'{root_url}/account/finish_auth?course_id=coursekey&next=%2Fdashboard'.
format(root_url=settings.LMS_ROOT_URL)
),
},
)
@ddt.unpack
@override_settings(LOGIN_REDIRECT_WHITELIST=['openedx.service'])
@override_settings(FEATURES=FEATURES_WITH_AUTHN_MFE_ENABLED)
@skip_unless_lms
def test_login_success_with_redirect(self, next_url, course_id, expected_redirect):
post_params = {}
if next_url:
post_params['next'] = next_url
if course_id:
post_params['course_id'] = course_id
response, _ = self._login_response(
self.user_email,
self.password,
extra_post_params=post_params,
HTTP_ACCEPT='*/*',
)
self._assert_response(response, success=True)
self._assert_redirect_url(response, expected_redirect)
@ddt.data(('/dashboard', False), ('/enterprise/select/active/?success_url=/dashboard', True))
@ddt.unpack
@patch.dict(settings.FEATURES, {'ENABLE_AUTHN_MICROFRONTEND': True, 'ENABLE_ENTERPRISE_INTEGRATION': True})
@override_settings(LOGIN_REDIRECT_WHITELIST=['openedx.service'])
@patch('openedx.features.enterprise_support.api.EnterpriseApiClient')
@patch('openedx.core.djangoapps.user_authn.views.login.reverse')
@skip_unless_lms
def test_login_success_for_multiple_enterprises(
self, expected_redirect, user_has_multiple_enterprises, reverse_mock, mock_api_client_class
):
"""
Test that if multiple enterprise feature is enabled, user is redirected
to correct page
"""
api_response = {'results': []}
enterprise = EnterpriseCustomerUserFactory(user_id=self.user.id).enterprise_customer
api_response['results'].append(
{
"enterprise_customer": {
"uuid": enterprise.uuid,
"name": enterprise.name,
"active": enterprise.active,
}
}
)
if user_has_multiple_enterprises:
enterprise = EnterpriseCustomerUserFactory(user_id=self.user.id).enterprise_customer
api_response['results'].append(
{
"enterprise_customer": {
"uuid": enterprise.uuid,
"name": enterprise.name,
"active": enterprise.active,
}
}
)
mock_client = mock_api_client_class.return_value
mock_client.fetch_enterprise_learner_data.return_value = api_response
reverse_mock.return_value = '/enterprise/select/active'
response, _ = self._login_response(
self.user.email,
self.password,
HTTP_ACCEPT='*/*',
)
self._assert_response(response, success=True)
self._assert_redirect_url(response, settings.LMS_ROOT_URL + expected_redirect)
@ddt.data(('', True), ('/enterprise/select/active/?success_url=', False))
@ddt.unpack
@patch.dict(settings.FEATURES, {'ENABLE_AUTHN_MICROFRONTEND': True, 'ENABLE_ENTERPRISE_INTEGRATION': True})
@patch('openedx.features.enterprise_support.api.EnterpriseApiClient')
@patch('openedx.core.djangoapps.user_authn.views.login.activate_learner_enterprise')
@patch('openedx.core.djangoapps.user_authn.views.login.reverse')
@skip_unless_lms
def test_enterprise_in_url(
self, expected_redirect, is_activated, reverse_mock, mock_activate_learner_enterprise, mock_api_client_class
):
"""
If user has multiple enterprises and the enterprise is present in url,
activate that url
"""
api_response = {}
enterprise_1 = EnterpriseCustomerUserFactory(user_id=self.user.id).enterprise_customer
enterprise_2 = EnterpriseCustomerUserFactory(user_id=self.user.id).enterprise_customer
api_response['results'] = [
{
"enterprise_customer": {
"uuid": enterprise_1.uuid,
"name": enterprise_1.name,
"active": enterprise_1.active,
}
},
{
"enterprise_customer": {
"uuid": enterprise_2.uuid,
"name": enterprise_2.name,
"active": enterprise_2.active,
}
}
]
next_url = '/enterprise/{}/course/{}/enroll/?catalog=catalog_uuid&utm_medium=enterprise'.format(
enterprise_1.uuid,
'course-v1:testX+test101+2T2020'
)
mock_client = mock_api_client_class.return_value
mock_client.fetch_enterprise_learner_data.return_value = api_response
mock_activate_learner_enterprise.return_value = is_activated
reverse_mock.return_value = '/enterprise/select/active'
response, _ = self._login_response(
self.user.email,
self.password,
extra_post_params={'next': next_url},
HTTP_ACCEPT='*/*',
)
self._assert_response(response, success=True)
self._assert_redirect_url(response, settings.LMS_ROOT_URL + expected_redirect + next_url)
@patch.dict("django.conf.settings.FEATURES", {'SQUELCH_PII_IN_LOGS': True})
def test_login_success_no_pii(self):
response, mock_audit_log = self._login_response(
self.user_email, self.password, patched_audit_log='common.djangoapps.student.models.user.AUDIT_LOG'
)
self._assert_response(response, success=True)
self._assert_audit_log(mock_audit_log, 'info', ['Login success'])
self._assert_not_in_audit_log(mock_audit_log, 'info', [self.user_email])
def test_login_success_unicode_email(self):
unicode_email = 'test' + chr(40960) + '@edx.org'
self.user.email = unicode_email
self.user.save()
response, mock_audit_log = self._login_response(
unicode_email, self.password, patched_audit_log='common.djangoapps.student.models.user.AUDIT_LOG'
)
self._assert_response(response, success=True)
self._assert_audit_log(mock_audit_log, 'info', ['Login success', unicode_email])
def test_login_fail_no_user_exists(self):
nonexistent_email = 'not_a_user@edx.org'
email_hash = hashlib.shake_128(nonexistent_email.encode('utf-8')).hexdigest(16)
response, mock_audit_log = self._login_response(
nonexistent_email,
self.password,
)
self._assert_response(
response, success=False, value=self.LOGIN_FAILED_WARNING, status_code=400
)
self._assert_audit_log(mock_audit_log, 'warning', ['Login failed', 'Unknown user email', email_hash])
@patch.dict("django.conf.settings.FEATURES", {'SQUELCH_PII_IN_LOGS': True})
def test_login_fail_no_user_exists_no_pii(self):
nonexistent_email = 'not_a_user@edx.org'
response, mock_audit_log = self._login_response(
nonexistent_email,
self.password,
)
self._assert_response(response, success=False, value=self.LOGIN_FAILED_WARNING)
self._assert_audit_log(mock_audit_log, 'warning', ['Login failed', 'Unknown user email'])
self._assert_not_in_audit_log(mock_audit_log, 'warning', [nonexistent_email])
def test_login_fail_wrong_password(self):
response, mock_audit_log = self._login_response(
self.user_email,
'wrong_password',
)
self._assert_response(response, success=False, value=self.LOGIN_FAILED_WARNING)
self._assert_audit_log(mock_audit_log, 'warning',
['Login failed', 'password for', str(self.user.id), 'invalid'])
@patch.dict("django.conf.settings.FEATURES", {'SQUELCH_PII_IN_LOGS': True})
def test_login_fail_wrong_password_no_pii(self):
response, mock_audit_log = self._login_response(self.user_email, 'wrong_password')
self._assert_response(response, success=False, value=self.LOGIN_FAILED_WARNING)
self._assert_audit_log(
mock_audit_log, 'warning', ['Login failed', 'password for', str(self.user.id), 'invalid']
)
self._assert_not_in_audit_log(mock_audit_log, 'warning', [self.user_email])
@override_settings(ENABLE_AUTHN_LOGIN_BLOCK_HIBP_POLICY=True)
@override_waffle_switch(ENABLE_PWNED_PASSWORD_API, True)
def test_password_compliance_block_error(self):
"""
Test that if HIBP Block flag is set to True and user's password lies
within block threshold, then login fails and user is not authenticated.
"""
password = hashlib.sha1(self.password.encode('utf-8')).hexdigest().upper()
api_response = {password[5:]: 1000000}
with patch.object(PwnedPasswordsAPI, 'range', return_value=api_response):
response, _ = self._login_response(self.user_email, self.password)
self._assert_response(response, success=False, error_code='require-password-change')
@override_settings(ENABLE_AUTHN_LOGIN_NUDGE_HIBP_POLICY=True)
@override_waffle_switch(ENABLE_PWNED_PASSWORD_API, True)
def test_password_compliance_nudge_error(self):
"""
Test that if HIBP Nudge flag is set to True and user's password lies
within nudge threshold, then user is authenticated and response contains
proper error code.
"""
password = hashlib.sha1(self.password.encode('utf-8')).hexdigest().upper()
api_response = {password[5:]: 10}
with patch.object(PwnedPasswordsAPI, 'range', return_value=api_response):
response, _ = self._login_response(self.user_email, self.password)
self._assert_response(response, success=False, error_code='nudge-password-change')
def test_login_not_activated_no_pii(self):
# De-activate the user
self.user.is_active = False
self.user.save()
# Should now be unable to login
response, mock_audit_log = self._login_response(
self.user_email,
self.password
)
self._assert_response(response, success=False, error_code="inactive-user")
self._assert_audit_log(mock_audit_log, 'warning', ['Login failed', 'Account not active for user'])
self._assert_not_in_audit_log(mock_audit_log, 'warning', ['test'])
def test_login_not_activated_with_correct_credentials(self):
"""
Tests that when user login with the correct credentials but with an inactive
account, the system, send account activation email notification to the user.
"""
self.user.is_active = False
self.user.save()
response, mock_audit_log = self._login_response(
self.user_email,
self.password,
)
self._assert_response(response, success=False, error_code="inactive-user")
self._assert_audit_log(mock_audit_log, 'warning', ['Login failed', 'Account not active for user'])
@patch('openedx.core.djangoapps.user_authn.views.login._log_and_raise_inactive_user_auth_error')
def test_login_inactivated_user_with_incorrect_credentials(self, mock_inactive_user_email_and_error):
"""
Tests that when user login with incorrect credentials and an inactive account,
the system does *not* send account activation email notification to the user.
"""
nonexistent_email = 'incorrect@email.com'
email_hash = hashlib.shake_128(nonexistent_email.encode('utf-8')).hexdigest(16)
self.user.is_active = False
self.user.save()
response, mock_audit_log = self._login_response(nonexistent_email, 'incorrect_password')
assert not mock_inactive_user_email_and_error.called
self._assert_response(response, success=False, value=self.LOGIN_FAILED_WARNING)
self._assert_audit_log(mock_audit_log, 'warning', ['Login failed', 'Unknown user email', email_hash])
def test_login_unicode_email(self):
unicode_email = self.user_email + chr(40960)
email_hash = hashlib.shake_128(unicode_email.encode('utf-8')).hexdigest(16)
response, mock_audit_log = self._login_response(
unicode_email,
self.password,
)
self._assert_response(response, success=False)
self._assert_audit_log(mock_audit_log, 'warning', ['Login failed', email_hash])
def test_login_unicode_password(self):
unicode_password = self.password + chr(1972)
response, mock_audit_log = self._login_response(
self.user_email,
unicode_password,
)
self._assert_response(response, success=False)
self._assert_audit_log(mock_audit_log, 'warning',
['Login failed', 'password for', str(self.user.id), 'invalid'])
def test_logout_logging(self):
response, _ = self._login_response(self.user_email, self.password)
self._assert_response(response, success=True)
logout_url = reverse('logout')
with patch('common.djangoapps.student.models.user.AUDIT_LOG') as mock_audit_log:
response = self.client.post(logout_url)
assert response.status_code == 200
self._assert_audit_log(mock_audit_log, 'info', ['Logout', 'test'])
def test_login_user_info_cookie(self):
response, _ = self._login_response(self.user_email, self.password)
self._assert_response(response, success=True)
# Verify the format of the "user info" cookie set on login
cookie = self.client.cookies[settings.EDXMKTG_USER_INFO_COOKIE_NAME]
user_info = json.loads(cookie.value)
assert user_info['version'] == settings.EDXMKTG_USER_INFO_COOKIE_VERSION
assert user_info['username'] == self.user.username
# Check that the URLs are absolute
for url in user_info["header_urls"].values():
assert 'http://testserver/' in url
def test_logout_deletes_mktg_cookies(self):
response, _ = self._login_response(self.user_email, self.password)
self._assert_response(response, success=True)
# Check that the marketing site cookies have been set
assert settings.EDXMKTG_LOGGED_IN_COOKIE_NAME in self.client.cookies
assert settings.EDXMKTG_USER_INFO_COOKIE_NAME in self.client.cookies
# Log out
logout_url = reverse('logout')
response = self.client.post(logout_url)
# Check that the marketing site cookies have been deleted
# (cookies are deleted by setting an expiration date in 1970)
for cookie_name in [settings.EDXMKTG_LOGGED_IN_COOKIE_NAME, settings.EDXMKTG_USER_INFO_COOKIE_NAME]:
cookie = self.client.cookies[cookie_name]
assert '01 Jan 1970' in cookie.get('expires').replace('-', ' ')
@override_settings(
EDXMKTG_LOGGED_IN_COOKIE_NAME="unicode-logged-in",
EDXMKTG_USER_INFO_COOKIE_NAME="unicode-user-info",
)
def test_unicode_mktg_cookie_names(self):
# When logged in cookie names are loaded from JSON files, they may
# have type `unicode` instead of `str`, which can cause errors
# when calling Django cookie manipulation functions.
response, _ = self._login_response(self.user_email, self.password)
self._assert_response(response, success=True)
response = self.client.post(reverse('logout'))
expected = {
'target': '/',
}
self.assertDictContainsSubset(expected, response.context_data)
@patch.dict("django.conf.settings.FEATURES", {'SQUELCH_PII_IN_LOGS': True})
def test_logout_logging_no_pii(self):
response, _ = self._login_response(self.user_email, self.password)
self._assert_response(response, success=True)
logout_url = reverse('logout')
with patch('common.djangoapps.student.models.user.AUDIT_LOG') as mock_audit_log:
response = self.client.post(logout_url)
assert response.status_code == 200
self._assert_audit_log(mock_audit_log, 'info', ['Logout'])
self._assert_not_in_audit_log(mock_audit_log, 'info', ['test'])
@override_settings(RATELIMIT_ENABLE=False)
def test_excessive_login_attempts_success(self):
# Try (and fail) logging in with fewer attempts than the limit of 30
# and verify that you can still successfully log in afterwards.
for i in range(20):
password = f'test_password{i}'
response, _audit_log = self._login_response(self.user_email, password)
self._assert_response(response, success=False)
# now try logging in with a valid password
response, _audit_log = self._login_response(self.user_email, self.password)
self._assert_response(response, success=True)
@patch('openedx.core.djangoapps.util.ratelimit.real_ip')
def test_excessive_login_attempts_by_email(self, real_ip_mock):
# try logging in 6 times, the defalutlimit for the number of failed
# login attempts in one 5 minute period before the rate gets limited
# for a specific e-mail address.
# We freeze time to deal with the fact that rate limit time boundaries
# are not predictable and we don't want the test to be flaky.
with freeze_time():
for i in range(6):
password = f'test_password{i}'
# Provide unique IPs so we don't get ip rate limited.
real_ip_mock.return_value = f'192.168.1.{i}'
self._login_response(self.user_email, password)
# check to see if this response indicates that this was ratelimited
response, _audit_log = self._login_response(self.user_email, 'wrong_password')
self._assert_response(response, success=False, value='Too many failed login attempts')
@patch('openedx.core.djangoapps.util.ratelimit.real_ip')
def test_excessive_login_attempts_by_username(self, real_ip_mock):
# try logging in 6 times, the defalutlimit for the number of failed
# login attempts in one 5 minute period before the rate gets limited
# for a specific username.
# We freeze time to deal with the fact that rate limit time boundaries
# are not predictable and we don't want the test to be flaky.
with freeze_time():
for i in range(6):
password = f'test_password{i}'
# Provide unique IPs so we don't get ip rate limited.
real_ip_mock.return_value = f'192.168.1.{i}'
self._login_response(self.username, password)
# check to see if this response indicates that this was ratelimited
response, _audit_log = self._login_response(self.username, 'wrong_password')
self._assert_response(response, success=False, value='Too many failed login attempts')
def test_excessive_login_attempts_by_ip(self):
# try logging in 5 times, the default limit for the number of failed
# login attempts in one 5 minute period before the rate gets limited
# from a specific ip address.
# We freeze time to deal with the fact that rate limit time boundaries
# are not predictable and we don't want the test to be flaky.
with freeze_time():
for i in range(5):
# provide different e-mail addresses so we don't get rate-limited
# by e-mail.
email = f'test_email{i}@example.com'
password = f'test_password{i}'
self._login_response(email, password)
# check to see if this response indicates that this was ratelimited
response, _audit_log = self._login_response(self.user_email, 'wrong_password')
self._assert_response(response, success=False, value='Too many failed login attempts')
@patch.dict("django.conf.settings.FEATURES", {"DISABLE_SET_JWT_COOKIES_FOR_TESTS": False})
def test_login_refresh(self):
def _assert_jwt_cookie_present(response):
assert response.status_code == 200
assert jwt_cookies.jwt_cookie_header_payload_name() in self.client.cookies
setup_login_oauth_client()
response, _ = self._login_response(self.user_email, self.password)
_assert_jwt_cookie_present(response)
response = self.client.post(reverse('login_refresh'))
_assert_jwt_cookie_present(response)
@patch.dict("django.conf.settings.FEATURES", {"DISABLE_SET_JWT_COOKIES_FOR_TESTS": False})
def test_login_refresh_anonymous_user(self):
response = self.client.post(reverse('login_refresh'))
assert response.status_code == 401
assert jwt_cookies.jwt_cookie_header_payload_name() not in self.client.cookies
@patch.dict("django.conf.settings.FEATURES", {'PREVENT_CONCURRENT_LOGINS': True})
def test_single_session(self):
creds = {'email': self.user_email, 'password': self.password}
client1 = Client()
client2 = Client()
response = client1.post(self.url, creds)
self._assert_response(response, success=True)
# Reload the user from the database
self.user = User.objects.get(pk=self.user.pk)
assert self.user.profile.get_meta()['session_id'] == client1.session.session_key
# second login should log out the first
response = client2.post(self.url, creds)
self._assert_response(response, success=True)
try:
# this test can be run with either lms or studio settings
# since studio does not have a dashboard url, we should
# look for another url that is login_required, in that case
url = reverse('dashboard')
except NoReverseMatch:
url = reverse('upload_transcripts')
response = client1.get(url)
# client1 will be logged out
assert response.status_code == 302
@patch.dict("django.conf.settings.FEATURES", {'PREVENT_CONCURRENT_LOGINS': True})
def test_single_session_with_no_user_profile(self):
"""
Assert that user login with cas (Central Authentication Service) is
redirect to dashboard in case of lms or upload_transcripts in case of
cms
"""
user = UserFactory.build(username='tester', email='tester@edx.org')
user.set_password(self.password)
user.save()
# Assert that no profile is created.
assert not hasattr(user, 'profile')
creds = {'email': 'tester@edx.org', 'password': self.password}
client1 = Client()
client2 = Client()
response = client1.post(self.url, creds)
self._assert_response(response, success=True)
# Reload the user from the database
user = User.objects.get(pk=user.pk)
# Assert that profile is created.
assert hasattr(user, 'profile')
# second login should log out the first
response = client2.post(self.url, creds)
self._assert_response(response, success=True)
try:
# this test can be run with either lms or studio settings
# since studio does not have a dashboard url, we should
# look for another url that is login_required, in that case
url = reverse('dashboard')
except NoReverseMatch:
url = reverse('upload_transcripts')
response = client1.get(url)
# client1 will be logged out
assert response.status_code == 302
@patch.dict("django.conf.settings.FEATURES", {'PREVENT_CONCURRENT_LOGINS': True})
def test_single_session_with_url_not_having_login_required_decorator(self):
# accessing logout url as it does not have login-required decorator it will avoid redirect
# and go inside the enforce_single_login
creds = {'email': self.user_email, 'password': self.password}
client1 = Client()
client2 = Client()
response = client1.post(self.url, creds)
self._assert_response(response, success=True)
# Reload the user from the database
self.user = User.objects.get(pk=self.user.pk)
assert self.user.profile.get_meta()['session_id'] == client1.session.session_key
# second login should log out the first
response = client2.post(self.url, creds)
self._assert_response(response, success=True)
url = reverse('logout')
response = client1.get(url)
assert response.status_code == 200
@override_settings(PASSWORD_POLICY_COMPLIANCE_ROLLOUT_CONFIG={'ENFORCE_COMPLIANCE_ON_LOGIN': True})
def test_check_password_policy_compliance(self):
"""
Tests _enforce_password_policy_compliance succeeds when no exception is thrown
"""
enforce_compliance_path = 'openedx.core.djangoapps.password_policy.compliance.enforce_compliance_on_login'
with patch(enforce_compliance_path) as mock_check_password_policy_compliance:
mock_check_password_policy_compliance.return_value = HttpResponse()
response, _ = self._login_response(self.user_email, self.password)
response_content = json.loads(response.content.decode('utf-8'))
assert response_content.get('success')
@patch.dict(settings.FEATURES, {"ENABLE_MAX_FAILED_LOGIN_ATTEMPTS": True})
@override_settings(PASSWORD_POLICY_COMPLIANCE_ROLLOUT_CONFIG={'ENFORCE_COMPLIANCE_ON_LOGIN': True})
def test_check_password_policy_compliance_exception(self):
"""
Tests _enforce_password_policy_compliance fails with an exception thrown
"""
assert not LoginFailures.objects.filter(user=self.user).exists()
enforce_compliance_on_login = 'openedx.core.djangoapps.password_policy.compliance.enforce_compliance_on_login'
with patch(enforce_compliance_on_login) as mock_enforce_compliance_on_login:
mock_enforce_compliance_on_login.side_effect = NonCompliantPasswordException()
response, _ = self._login_response(
self.user_email,
self.password
)
response_content = json.loads(response.content.decode('utf-8'))
assert not response_content.get('success')
assert len(mail.outbox) == 1
assert 'Password reset' in mail.outbox[0].subject
failure_record = LoginFailures.objects.get(user=self.user)
assert failure_record.failure_count == 1
LoginFailures.clear_lockout_counter(user=self.user)
@override_settings(PASSWORD_POLICY_COMPLIANCE_ROLLOUT_CONFIG={'ENFORCE_COMPLIANCE_ON_LOGIN': True})
def test_check_password_policy_compliance_warning(self):
"""
Tests _enforce_password_policy_compliance succeeds with a warning thrown
"""
enforce_compliance_on_login = 'openedx.core.djangoapps.password_policy.compliance.enforce_compliance_on_login'
with patch(enforce_compliance_on_login) as mock_enforce_compliance_on_login:
mock_enforce_compliance_on_login.side_effect = NonCompliantPasswordWarning('Test warning')
response, _ = self._login_response(self.user_email, self.password)
response_content = json.loads(response.content.decode('utf-8'))
assert 'Test warning' in self.client.session['_messages']
assert response_content.get('success')
@ddt.data(
('test_password', 'test_password', True),
(unicodedata.normalize('NFKD', 'Ṗŕệṿïệẅ Ṯệẍt'),
unicodedata.normalize('NFKC', 'Ṗŕệṿïệẅ Ṯệẍt'), False),
(unicodedata.normalize('NFKC', 'Ṗŕệṿïệẅ Ṯệẍt'),
unicodedata.normalize('NFKD', 'Ṗŕệṿïệẅ Ṯệẍt'), True),
(unicodedata.normalize('NFKD', 'Ṗŕệṿïệẅ Ṯệẍt'),
unicodedata.normalize('NFKD', 'Ṗŕệṿïệẅ Ṯệẍt'), False),
)
@ddt.unpack
def test_password_unicode_normalization_login(self, password, password_entered, login_success):
"""
Tests unicode normalization on user's passwords on login.
"""
self.user.set_password(password)
self.user.save()
response, _ = self._login_response(self.user.email, password_entered)
self._assert_response(response, success=login_success)
def _login_response(
self, email, password, patched_audit_log=None, extra_post_params=None, **extra
):
"""
Post the login info
"""
if patched_audit_log is None:
patched_audit_log = 'openedx.core.djangoapps.user_authn.views.login.AUDIT_LOG'
post_params = {'email': email, 'password': password}
if extra_post_params is not None:
post_params.update(extra_post_params)
with patch(patched_audit_log) as mock_audit_log:
result = self.client.post(self.url, post_params, **extra)
return result, mock_audit_log
def _assert_response(self, response, success=None, value=None, status_code=None, error_code=None):
"""
Assert that the response has the expected status code and returned a valid
JSON-parseable dict.
If success is provided, assert that the response had that
value for 'success' in the JSON dict.
If value is provided, assert that the response contained that
value for 'value' in the JSON dict.
"""
expected_status_code = status_code or (400 if success is False else 200)
assert response.status_code == expected_status_code
try:
response_dict = json.loads(response.content.decode('utf-8'))
except ValueError:
self.fail("Could not parse response content as JSON: %s"
% str(response.content))
if success is not None:
assert response_dict['success'] == success
if error_code is not None:
assert response_dict['error_code'] == error_code
if value is not None:
msg = ("'%s' did not contain '%s'" %
(str(response_dict['value']), str(value)))
assert value in response_dict['value'], msg
def _assert_redirect_url(self, response, expected_redirect_url):
"""
Assert that the redirect URL is in the response and has the expected value.
Assumes that response content is well-formed JSON
(you can call `_assert_response` first to assert this).
"""
response_dict = json.loads(response.content.decode('utf-8'))
assert 'redirect_url' in response_dict, (
"Response JSON unexpectedly does not have redirect_url: {!r}".format(
response_dict
)
)
assert response_dict['redirect_url'] == expected_redirect_url
def _assert_audit_log(self, mock_audit_log, level, log_strings):
"""
Check that the audit log has received the expected call as its last call.
"""
method_calls = mock_audit_log.method_calls
name, args, _kwargs = method_calls[-1]
assert name == level
assert len(args) == 1
format_string = args[0]
for log_string in log_strings:
assert log_string in format_string
def _assert_not_in_audit_log(self, mock_audit_log, level, log_strings):
"""
Check that the audit log has received the expected call as its last call.
"""
method_calls = mock_audit_log.method_calls
name, args, _kwargs = method_calls[-1]
assert name == level
assert len(args) == 1
format_string = args[0]
for log_string in log_strings:
assert log_string not in format_string
@ddt.data(
{
'switch_enabled': False,
'whitelisted': False,
'allowed_domain': 'edx.org',
'user_domain': 'edx.org',
'success': True,
'is_third_party_authenticated': False
},
{
'switch_enabled': False,
'whitelisted': True,
'allowed_domain': 'edx.org',
'user_domain': 'edx.org',
'success': True,
'is_third_party_authenticated': False
},
{
'switch_enabled': True,
'whitelisted': False,
'allowed_domain': 'edx.org',
'user_domain': 'edx.org',
'success': False,
'is_third_party_authenticated': False
},
{
'switch_enabled': True,
'whitelisted': False,
'allowed_domain': 'fake.org',
'user_domain': 'edx.org',
'success': True,
'is_third_party_authenticated': False
},
{
'switch_enabled': True,
'whitelisted': True,
'allowed_domain': 'edx.org',
'user_domain': 'edx.org',
'success': True,
'is_third_party_authenticated': False
},
{
'switch_enabled': True,
'whitelisted': False,
'allowed_domain': 'batman.gotham',
'user_domain': 'batman.gotham',
'success': False,
'is_third_party_authenticated': False
},
{
'switch_enabled': True,
'whitelisted': True,
'allowed_domain': 'edx.org',
'user_domain': 'edx.org',
'success': True,
'is_third_party_authenticated': True
},
{
'switch_enabled': False,
'whitelisted': False,
'allowed_domain': 'edx.org',
'user_domain': 'fake.org',
'success': True,
'is_third_party_authenticated': True
},
)
@ddt.unpack
@skip_unless_lms
def test_login_for_user_auth_flow(
self,
switch_enabled,
whitelisted,
allowed_domain,
user_domain,
success,
is_third_party_authenticated
):
"""
Verify that `login._check_user_auth_flow` works as expected.
"""
provider = 'Google'
provider_tpa_hint = 'saml-test'
username = 'batman'
user_email = f'{username}@{user_domain}'
user = self._create_user(username, user_email)
default_site_configuration_values = {
'SITE_NAME': allowed_domain,
'THIRD_PARTY_AUTH_ONLY_DOMAIN': allowed_domain,
'THIRD_PARTY_AUTH_ONLY_PROVIDER': provider,
'THIRD_PARTY_AUTH_ONLY_HINT': provider_tpa_hint,
}
with override_waffle_switch(ENABLE_LOGIN_USING_THIRDPARTY_AUTH_ONLY, switch_enabled):
if not is_third_party_authenticated:
site = self.set_up_site(allowed_domain, default_site_configuration_values)
if whitelisted:
AllowedAuthUser.objects.create(site=site, email=user.email)
else:
AllowedAuthUser.objects.filter(site=site, email=user.email).delete()
if success:
value = None
else:
value = 'As {0} user, You must login with your {0} <a href=\'{1}\'>{2} account</a>.'.format(
allowed_domain,
'{}?tpa_hint={}'.format(reverse("dashboard"), provider_tpa_hint),
provider,
)
response, __ = self._login_response(user.email, self.password)
self._assert_response(
response,
success=success,
value=value,
)
else:
default_site_configuration_values.update({'ENABLE_THIRD_PARTY_AUTH': True})
self.set_up_site(allowed_domain, default_site_configuration_values)
with patch('openedx.core.djangoapps.user_authn.views.login.pipeline'):
with patch(
'openedx.core.djangoapps.user_authn.views.login._check_user_auth_flow'
) as mock_check_user_auth_flow:
# user is already authenticated by third_party_auth then
# we should by-pass _check_user_auth_flow function
response, __ = self._login_response(user.email, self.password)