|
| 1 | +import json |
1 | 2 | from unittest import mock
|
2 | 3 |
|
3 | 4 | from django.conf import settings
|
@@ -88,6 +89,100 @@ def test_valid_login_with_disallowed_external_redirect(self):
|
88 | 89 | 'login_view-current_step': 'auth'})
|
89 | 90 | self.assertRedirects(response, reverse('two_factor:profile'), fetch_redirect_response=False)
|
90 | 91 |
|
| 92 | + @mock.patch('two_factor.views.core.time') |
| 93 | + def test_valid_login_primary_key_stored(self, mock_time): |
| 94 | + mock_time.time.return_value = 12345.12 |
| 95 | + user = self.create_user() |
| 96 | + user.totpdevice_set.create(name='default', |
| 97 | + key=random_hex_str()) |
| 98 | + |
| 99 | + response = self. _post({ 'auth-username': '[email protected]', |
| 100 | + 'auth-password': 'secret', |
| 101 | + 'login_view-current_step': 'auth'}) |
| 102 | + self.assertContains(response, 'Token:') |
| 103 | + |
| 104 | + self.assertEqual(self.client.session['wizard_login_view']['user_pk'], str(user.pk)) |
| 105 | + self.assertEqual( |
| 106 | + self.client.session['wizard_login_view']['user_backend'], |
| 107 | + 'django.contrib.auth.backends.ModelBackend') |
| 108 | + self.assertEqual(self.client.session['wizard_login_view']['authentication_time'], 12345) |
| 109 | + |
| 110 | + @mock.patch('two_factor.views.core.time') |
| 111 | + def test_valid_login_post_auth_session_clear_of_form_data(self, mock_time): |
| 112 | + mock_time.time.return_value = 12345.12 |
| 113 | + user = self.create_user() |
| 114 | + user.totpdevice_set.create(name='default', |
| 115 | + key=random_hex_str()) |
| 116 | + |
| 117 | + response = self. _post({ 'auth-username': '[email protected]', |
| 118 | + 'auth-password': 'secret', |
| 119 | + 'login_view-current_step': 'auth'}) |
| 120 | + self.assertContains(response, 'Token:') |
| 121 | + |
| 122 | + self.assertEqual(self.client.session['wizard_login_view']['user_pk'], str(user.pk)) |
| 123 | + self.assertEqual(self.client.session['wizard_login_view']['step'], 'token') |
| 124 | + self.assertEqual(self.client.session['wizard_login_view']['step_data'], {'auth': None}) |
| 125 | + self.assertEqual(self.client.session['wizard_login_view']['step_files'], {'auth': {}}) |
| 126 | + self.assertEqual(self.client.session['wizard_login_view']['validated_step_data'], {}) |
| 127 | + |
| 128 | + @mock.patch('two_factor.views.core.logger') |
| 129 | + @mock.patch('two_factor.views.core.time') |
| 130 | + def test_valid_login_expired(self, mock_time, mock_logger): |
| 131 | + mock_time.time.return_value = 12345.12 |
| 132 | + user = self.create_user() |
| 133 | + device = user.totpdevice_set.create(name='default', |
| 134 | + key=random_hex_str()) |
| 135 | + |
| 136 | + response = self. _post({ 'auth-username': '[email protected]', |
| 137 | + 'auth-password': 'secret', |
| 138 | + 'login_view-current_step': 'auth'}) |
| 139 | + self.assertContains(response, 'Token:') |
| 140 | + |
| 141 | + self.assertEqual(self.client.session['wizard_login_view']['user_pk'], str(user.pk)) |
| 142 | + self.assertEqual( |
| 143 | + self.client.session['wizard_login_view']['user_backend'], |
| 144 | + 'django.contrib.auth.backends.ModelBackend') |
| 145 | + self.assertEqual(self.client.session['wizard_login_view']['authentication_time'], 12345) |
| 146 | + |
| 147 | + mock_time.time.return_value = 20345.12 |
| 148 | + |
| 149 | + response = self._post({'token-otp_token': totp(device.bin_key), |
| 150 | + 'login_view-current_step': 'token'}) |
| 151 | + self.assertEqual(response.status_code, 200) |
| 152 | + self.assertNotContains(response, 'Token:') |
| 153 | + self.assertContains(response, 'Password:') |
| 154 | + self.assertContains(response, 'Your session has timed out. Please login again.') |
| 155 | + |
| 156 | + # Check that a message was logged. |
| 157 | + mock_logger.info.assert_called_with( |
| 158 | + "User's authentication flow has timed out. The user " |
| 159 | + "has been redirected to the initial auth form.") |
| 160 | + |
| 161 | + @override_settings(TWO_FACTOR_LOGIN_TIMEOUT=0) |
| 162 | + @mock.patch('two_factor.views.core.time') |
| 163 | + def test_valid_login_no_timeout(self, mock_time): |
| 164 | + mock_time.time.return_value = 12345.12 |
| 165 | + user = self.create_user() |
| 166 | + device = user.totpdevice_set.create(name='default', |
| 167 | + key=random_hex_str()) |
| 168 | + |
| 169 | + response = self. _post({ 'auth-username': '[email protected]', |
| 170 | + 'auth-password': 'secret', |
| 171 | + 'login_view-current_step': 'auth'}) |
| 172 | + self.assertContains(response, 'Token:') |
| 173 | + |
| 174 | + self.assertEqual(self.client.session['wizard_login_view']['user_pk'], str(user.pk)) |
| 175 | + self.assertEqual( |
| 176 | + self.client.session['wizard_login_view']['user_backend'], |
| 177 | + 'django.contrib.auth.backends.ModelBackend') |
| 178 | + self.assertEqual(self.client.session['wizard_login_view']['authentication_time'], 12345) |
| 179 | + |
| 180 | + mock_time.time.return_value = 20345.12 |
| 181 | + |
| 182 | + response = self._post({'token-otp_token': totp(device.bin_key), |
| 183 | + 'login_view-current_step': 'token'}) |
| 184 | + self.assertRedirects(response, resolve_url(settings.LOGIN_REDIRECT_URL)) |
| 185 | + self.assertEqual(self.client.session['_auth_user_id'], str(user.pk)) |
91 | 186 |
|
92 | 187 | def test_valid_login_with_redirect_authenticated_user(self):
|
93 | 188 | user = self.create_user()
|
@@ -251,35 +346,6 @@ def test_with_backup_token(self, mock_signal):
|
251 | 346 | # Check that the signal was fired.
|
252 | 347 | mock_signal.assert_called_with(sender=mock.ANY, request=mock.ANY, user=user, device=device)
|
253 | 348 |
|
254 |
| - @mock.patch('two_factor.views.utils.logger') |
255 |
| - def test_change_password_in_between(self, mock_logger): |
256 |
| - """ |
257 |
| - When the password of the user is changed while trying to login, should |
258 |
| - not result in errors. Refs #63. |
259 |
| - """ |
260 |
| - user = self.create_user() |
261 |
| - self.enable_otp() |
262 |
| - |
263 |
| - response = self. _post({ 'auth-username': '[email protected]', |
264 |
| - 'auth-password': 'secret', |
265 |
| - 'login_view-current_step': 'auth'}) |
266 |
| - self.assertContains(response, 'Token:') |
267 |
| - |
268 |
| - # Now, the password is changed. When the form is submitted, the |
269 |
| - # credentials should be checked again. If that's the case, the |
270 |
| - # login form should note that the credentials are invalid. |
271 |
| - user.set_password('secret2') |
272 |
| - user.save() |
273 |
| - response = self._post({'login_view-current_step': 'token'}) |
274 |
| - self.assertContains(response, 'Please enter a correct') |
275 |
| - self.assertContains(response, 'and password.') |
276 |
| - |
277 |
| - # Check that a message was logged. |
278 |
| - mock_logger.warning.assert_called_with( |
279 |
| - "Current step '%s' is no longer valid, returning to last valid " |
280 |
| - "step in the wizard.", |
281 |
| - 'token') |
282 |
| - |
283 | 349 | @mock.patch('two_factor.views.utils.logger')
|
284 | 350 | def test_reset_wizard_state(self, mock_logger):
|
285 | 351 | self.create_user()
|
@@ -332,6 +398,19 @@ def test_missing_management_data(self):
|
332 | 398 | # view should return HTTP 400 Bad Request
|
333 | 399 | self.assertEqual(response.status_code, 400)
|
334 | 400 |
|
| 401 | + def test_no_password_in_session(self): |
| 402 | + self.create_user() |
| 403 | + self.enable_otp() |
| 404 | + |
| 405 | + response = self. _post({ 'auth-username': '[email protected]', |
| 406 | + 'auth-password': 'secret', |
| 407 | + 'login_view-current_step': 'auth'}) |
| 408 | + self.assertContains(response, 'Token:') |
| 409 | + |
| 410 | + session_contents = json.dumps(list(self.client.session.items())) |
| 411 | + |
| 412 | + self.assertNotIn('secret', session_contents) |
| 413 | + |
335 | 414 |
|
336 | 415 | class BackupTokensTest(UserMixin, TestCase):
|
337 | 416 | def setUp(self):
|
|
0 commit comments