-
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 4 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 |
|---|---|---|
|
|
@@ -4,24 +4,11 @@ | |
| """ | ||
| import logging | ||
| from django.contrib.auth import get_user_model | ||
| from edx_toggles.toggles import SettingToggle | ||
| from edx_django_utils.monitoring import set_custom_attribute | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
| User = get_user_model() | ||
|
|
||
| # .. toggle_name: SKIP_UPDATE_EMAIL_ON_USERNAME_MISMATCH | ||
| # .. toggle_implementation: SettingToggle | ||
| # .. toggle_default: False | ||
| # .. toggle_description: Determines whether to block email updates when usernames don't match. | ||
| # When enabled (True), email updates will be blocked when the username in social auth details | ||
| # doesn't match the user's username. When disabled (False), email updates will proceed regardless | ||
| # of username mismatches. This will be used for a temporary rollout. | ||
| # .. toggle_use_cases: temporary | ||
| # .. toggle_creation_date: 2025-06-18 | ||
| # .. toggle_target_removal_date: 2025-08-18 | ||
| SKIP_UPDATE_EMAIL_ON_USERNAME_MISMATCH = SettingToggle("SKIP_UPDATE_EMAIL_ON_USERNAME_MISMATCH", default=False) | ||
|
|
||
|
|
||
| # pylint: disable=unused-argument | ||
| # The function parameters must be named exactly as they are below. | ||
|
|
@@ -56,49 +43,18 @@ def update_email(strategy, details, user=None, *args, **kwargs): # pylint: disa | |
| # Check if usernames don't match | ||
| username_mismatch = details_username != user_username | ||
|
|
||
| # .. custom_attribute_name: update_email.username_mismatch | ||
| # .. custom_attribute_description: Tracks whether there's a mismatch between | ||
| # the username in the social details and the user's actual username. | ||
| # True if usernames don't match, False if they match. | ||
| set_custom_attribute('update_email.username_mismatch', username_mismatch) | ||
|
|
||
| # .. custom_attribute_name: update_email.rollout_toggle_enabled | ||
| # .. custom_attribute_description: Tracks whether the SKIP_UPDATE_EMAIL_ON_USERNAME_MISMATCH | ||
| # toggle is enabled during this pipeline execution. | ||
| set_custom_attribute('update_email.rollout_toggle_enabled', SKIP_UPDATE_EMAIL_ON_USERNAME_MISMATCH.is_enabled()) | ||
|
|
||
| if username_mismatch: | ||
| # Log warning and set additional custom attributes for mismatches | ||
| # Log warning about the mismatch | ||
|
||
| logger.warning( | ||
| "Username mismatch during email update. User username: %s, Details username: %s", | ||
| "Unexpected username mismatch during email update. Skipping email update for user %s. " | ||
| "User username: %s, Details username: %s", | ||
| user_username, | ||
| user_username, | ||
| details_username | ||
| ) | ||
| # .. custom_attribute_name: update_email.details_username | ||
| # .. custom_attribute_description: Records the username provided in the | ||
| # social details when a mismatch occurs with the user's username. | ||
| set_custom_attribute('update_email.details_username', details_username) | ||
|
|
||
| # .. custom_attribute_name: update_email.user_username | ||
| # .. custom_attribute_description: Records the actual username of the user | ||
| # when a mismatch occurs with the social details username. | ||
| set_custom_attribute('update_email.user_username', user_username) | ||
|
|
||
| # .. custom_attribute_name: update_email.details_has_email | ||
| # .. custom_attribute_description: Records whether the details contain an email | ||
| # when a username mismatch occurs, to identify potential edge cases. | ||
| set_custom_attribute('update_email.details_has_email', bool(details.get('email'))) | ||
|
|
||
| # Only exit if the toggle is enabled | ||
| if SKIP_UPDATE_EMAIL_ON_USERNAME_MISMATCH.is_enabled(): | ||
| logger.warning( | ||
| "Skipping email update for user %s due to username mismatch and " | ||
| "SKIP_UPDATE_EMAIL_ON_USERNAME_MISMATCH toggle enabled", | ||
| user_username | ||
| ) | ||
| return # Exit without updating email | ||
| return # Exit without updating email | ||
|
|
||
| # Proceed with email update only if usernames match or toggle is disabled | ||
| # Proceed with email update only if usernames match | ||
| email = details.get('email') | ||
| if email and user.email != email: | ||
| user.email = email | ||
|
|
||
| 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,41 @@ 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='existing@example.com') | ||
| 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='existing@example.com') | ||
| 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() | ||
| middleware = SessionMiddleware(lambda req: None) | ||
| middleware.process_request(request) | ||
| request.session.save() | ||
|
|
||
| if toggle_enabled: | ||
| self.assertNotEqual(request.session.session_key, initial_session_key) | ||
| initial_session_key = request.session.session_key | ||
|
|
||
| self.assertTrue(request.user.is_anonymous) | ||
| self.backend.strategy.request = request | ||
|
|
||
| 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) | ||
| self.do_start() | ||
|
|
||
| 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) | ||
| if user_authenticated: | ||
| self.assertNotEqual(request.session.session_key, initial_session_key) | ||
| self.assertTrue(request.user.is_anonymous) | ||
|
|
||
| self.assertEqual(request.user, existing_user) | ||
| self.assertFalse(request.user.is_anonymous) | ||
| mock_set_attr.assert_called_once_with('session_cleanup.logout_required', True) | ||
|
|
||
| mock_set_attr.assert_has_calls([ | ||
| call('session_cleanup.toggle_enabled', False), | ||
| call('session_cleanup.logout_performed', False) | ||
| ], any_order=True) | ||
| 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_set_attr.assert_called_once_with('session_cleanup.logout_required', False) | ||
|
|
||
| mock_logger.info.assert_not_called() | ||
| mock_logger.info.assert_not_called() | ||
|
|
||
| def test_partial_pipeline(self): | ||
| self.do_partial_pipeline() | ||
|
|
||
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.