-
Notifications
You must be signed in to change notification settings - Fork 21
fix: cleanup of toggle and few custom attributes #405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d6c96a0
7c9f209
54927df
c26ddfb
6a79f2a
6550570
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| import datetime | ||
| import json | ||
| from calendar import timegm | ||
| from unittest.mock import patch, call | ||
| from unittest.mock import patch | ||
|
|
||
| import ddt | ||
| import jwt | ||
|
|
@@ -14,7 +14,6 @@ | |
| from django.contrib.sessions.middleware import SessionMiddleware | ||
| from django.core.cache import cache | ||
| from django.test import RequestFactory | ||
| from django.test.utils import override_settings | ||
| from social_core.tests.backends.oauth import OAuth2Test | ||
|
|
||
| User = get_user_model() | ||
|
|
@@ -127,54 +126,38 @@ def test_login(self): | |
| self.do_login() | ||
|
|
||
| @pytest.mark.django_db | ||
| @ddt.data(True, False) # Test session cleanup with both toggle enabled and disabled | ||
| @ddt.data(True, False) # Test with and without authenticated user | ||
| @patch('auth_backends.backends.set_custom_attribute') | ||
| @patch('auth_backends.backends.logger') | ||
| def test_start_with_session_cleanup(self, toggle_enabled, mock_logger, mock_set_attr): | ||
| """Test start method for session cleanup of existing user with toggle variation.""" | ||
| with override_settings(ENABLE_OAUTH_SESSION_CLEANUP=toggle_enabled): | ||
| existing_user = User.objects.create_user(username='existing_user', email='[email protected]') | ||
| def test_start_with_session_cleanup(self, user_authenticated, mock_logger, mock_set_attr): | ||
| """Test start method for session cleanup with and without authenticated user.""" | ||
| request = RequestFactory().get('/auth/login/edx-oauth2/') | ||
|
|
||
| request = RequestFactory().get('/auth/login/edx-oauth2/') | ||
| if user_authenticated: | ||
| existing_user = User.objects.create_user(username='existing_user', email='[email protected]') | ||
| request.user = existing_user | ||
|
|
||
| middleware = SessionMiddleware(lambda req: None) | ||
| middleware.process_request(request) | ||
| request.session.save() | ||
|
|
||
| initial_session_key = request.session.session_key | ||
|
|
||
| self.backend.strategy.request = request | ||
|
|
||
| self.do_start() | ||
|
|
||
| if toggle_enabled: | ||
| self.assertNotEqual(request.session.session_key, initial_session_key) | ||
|
|
||
| self.assertTrue(request.user.is_anonymous) | ||
| middleware = SessionMiddleware(lambda req: None) | ||
| middleware.process_request(request) | ||
| request.session.save() | ||
|
|
||
| mock_set_attr.assert_has_calls([ | ||
| call('session_cleanup.toggle_enabled', True), | ||
| call('session_cleanup.logout_performed', True), | ||
| call('session_cleanup.logged_out_username', 'existing_user') | ||
| ], any_order=True) | ||
| initial_session_key = request.session.session_key | ||
|
|
||
| mock_logger.info.assert_called_with( | ||
| "OAuth start: Performing session cleanup for user '%s'", | ||
| 'existing_user' | ||
| ) | ||
| else: | ||
| self.assertEqual(request.session.session_key, initial_session_key) | ||
| self.backend.strategy.request = request | ||
|
|
||
| self.assertEqual(request.user, existing_user) | ||
| self.assertFalse(request.user.is_anonymous) | ||
| self.do_start() | ||
|
|
||
| mock_set_attr.assert_has_calls([ | ||
| call('session_cleanup.toggle_enabled', False), | ||
| call('session_cleanup.logout_performed', False) | ||
| ], any_order=True) | ||
| mock_set_attr.assert_called_once_with('session_cleanup.logout_required', user_authenticated) | ||
|
|
||
| mock_logger.info.assert_not_called() | ||
| if user_authenticated: | ||
| self.assertNotEqual(request.session.session_key, initial_session_key) | ||
| self.assertTrue(request.user.is_anonymous) | ||
| mock_logger.info.assert_called_with( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is the only assertion needed in the conditional. You should be able to remove the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed. |
||
| "OAuth start: Performing session cleanup for user '%s'", | ||
| 'existing_user' | ||
| ) | ||
| else: | ||
| mock_logger.info.assert_not_called() | ||
|
|
||
| def test_partial_pipeline(self): | ||
| self.do_partial_pipeline() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,11 +44,9 @@ def setUp(self): | |
| self.user = User.objects.create(username='test_user') | ||
| self.strategy = load_strategy() | ||
|
|
||
| @patch('auth_backends.pipeline.SKIP_UPDATE_EMAIL_ON_USERNAME_MISMATCH.is_enabled') | ||
| @patch('auth_backends.pipeline.set_custom_attribute') | ||
| def test_update_email(self, mock_set_attribute, mock_toggle): | ||
| def test_update_email(self, mock_set_attribute): | ||
| """ Verify that user email is updated upon changing email when usernames match. """ | ||
| mock_toggle.return_value = False | ||
| updated_email = '[email protected]' | ||
| self.assertNotEqual(self.user.email, updated_email) | ||
|
|
||
|
|
@@ -60,33 +58,24 @@ def test_update_email(self, mock_set_attribute, mock_toggle): | |
| self.assertEqual(updated_user.email, updated_email) | ||
| self.assertNotEqual(updated_user.email, initial_email) | ||
|
|
||
| mock_set_attribute.assert_any_call('update_email.username_mismatch', False) | ||
| mock_set_attribute.assert_any_call('update_email.rollout_toggle_enabled', False) | ||
| self.assert_attribute_was_set(mock_set_attribute, 'update_email.email_updated', should_exist=True) | ||
|
|
||
| @patch('auth_backends.pipeline.SKIP_UPDATE_EMAIL_ON_USERNAME_MISMATCH.is_enabled') | ||
| @patch('auth_backends.pipeline.set_custom_attribute') | ||
| def test_update_email_with_none(self, mock_set_attribute, mock_toggle): | ||
| def test_update_email_with_none(self, mock_set_attribute): | ||
| """ Verify that user email is not updated if email value is None. """ | ||
| mock_toggle.return_value = False | ||
| old_email = self.user.email | ||
|
|
||
| update_email(self.strategy, {'email': None, 'username': 'test_user'}, user=self.user) | ||
|
|
||
| updated_user = User.objects.get(pk=self.user.pk) | ||
| self.assertEqual(updated_user.email, old_email) | ||
|
|
||
| mock_set_attribute.assert_any_call('update_email.username_mismatch', False) | ||
| mock_set_attribute.assert_any_call('update_email.rollout_toggle_enabled', False) | ||
| self.assert_attribute_was_set(mock_set_attribute, 'update_email.email_updated', should_exist=False) | ||
|
|
||
| @patch('auth_backends.pipeline.SKIP_UPDATE_EMAIL_ON_USERNAME_MISMATCH.is_enabled') | ||
| @patch('auth_backends.pipeline.logger') | ||
| @patch('auth_backends.pipeline.set_custom_attribute') | ||
| def test_username_mismatch_no_update_toggle_enabled(self, mock_set_attribute, mock_logger, mock_toggle): | ||
| """ Verify that email is not updated when usernames don't match and toggle is enabled. """ | ||
| mock_toggle.return_value = True | ||
|
|
||
| def test_username_mismatch_no_update(self, mock_set_attribute, mock_logger): | ||
| """ Verify that email is not updated when usernames don't match. """ | ||
| old_email = self.user.email | ||
| updated_email = '[email protected]' | ||
|
|
||
|
|
@@ -95,49 +84,16 @@ def test_username_mismatch_no_update_toggle_enabled(self, mock_set_attribute, mo | |
| updated_user = User.objects.get(pk=self.user.pk) | ||
| self.assertEqual(updated_user.email, old_email) | ||
|
|
||
| self.assertEqual(mock_logger.warning.call_count, 2) | ||
| mock_logger.warning.assert_any_call( | ||
| "Username mismatch during email update. User username: %s, Details username: %s", | ||
| 'test_user', 'different_user' | ||
| ) | ||
| mock_logger.warning.assert_any_call( | ||
| "Skipping email update for user %s due to username mismatch and " | ||
| "SKIP_UPDATE_EMAIL_ON_USERNAME_MISMATCH toggle enabled", | ||
| 'test_user' | ||
| mock_logger.warning.assert_called_once_with( | ||
| "Unexpected username mismatch during email update. Skipping email update for user %s. " | ||
| "User username: %s, Details username: %s", | ||
| 'test_user', | ||
| 'test_user', | ||
| 'different_user' | ||
| ) | ||
|
|
||
| mock_set_attribute.assert_any_call('update_email.username_mismatch', True) | ||
| mock_set_attribute.assert_any_call('update_email.rollout_toggle_enabled', True) | ||
| mock_set_attribute.assert_any_call('update_email.details_username', 'different_user') | ||
| mock_set_attribute.assert_any_call('update_email.user_username', 'test_user') | ||
| mock_set_attribute.assert_any_call('update_email.details_has_email', True) | ||
| self.assert_attribute_was_set(mock_set_attribute, 'update_email.email_updated', should_exist=False) | ||
|
|
||
| @patch('auth_backends.pipeline.SKIP_UPDATE_EMAIL_ON_USERNAME_MISMATCH.is_enabled') | ||
| @patch('auth_backends.pipeline.logger') | ||
| @patch('auth_backends.pipeline.set_custom_attribute') | ||
| def test_username_mismatch_with_update_toggle_disabled(self, mock_set_attribute, mock_logger, mock_toggle): | ||
| """ Verify that email is updated when usernames don't match but toggle is disabled. """ | ||
| mock_toggle.return_value = False | ||
|
|
||
| old_email = self.user.email | ||
| updated_email = '[email protected]' | ||
|
|
||
| update_email(self.strategy, {'email': updated_email, 'username': 'different_user'}, user=self.user) | ||
|
|
||
| updated_user = User.objects.get(pk=self.user.pk) | ||
| self.assertEqual(updated_user.email, updated_email) | ||
| self.assertNotEqual(updated_user.email, old_email) | ||
|
|
||
| mock_logger.warning.assert_called_once() | ||
|
|
||
| mock_set_attribute.assert_any_call('update_email.username_mismatch', True) | ||
| mock_set_attribute.assert_any_call('update_email.rollout_toggle_enabled', False) | ||
| mock_set_attribute.assert_any_call('update_email.details_username', 'different_user') | ||
| mock_set_attribute.assert_any_call('update_email.user_username', 'test_user') | ||
| mock_set_attribute.assert_any_call('update_email.details_has_email', True) | ||
| self.assert_attribute_was_set(mock_set_attribute, 'update_email.email_updated', should_exist=True) | ||
|
|
||
| def assert_attribute_was_set(self, mock_set_attribute, attribute_name, should_exist=True): | ||
| """ | ||
| Assert that a specific attribute was or was not set via set_custom_attribute. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also remove
session_cleanup.logout_performed. I thinksession_cleanup.logout_requiredis close enough and has a simpler implementation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed. Removed
session_cleanup.logout_performedand updated test file.