-
Notifications
You must be signed in to change notification settings - Fork 22
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='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() | ||
|
|
||
| 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() | ||
|
|
||
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.