From fe968a488c5d6dafce2996a719d5610d0023a917 Mon Sep 17 00:00:00 2001 From: Syed Sajjad Hussain Shah Date: Wed, 17 Jan 2024 12:51:30 +0500 Subject: [PATCH] fix: tests --- CHANGELOG.rst | 8 ++++++++ edx_rest_framework_extensions/__init__.py | 2 +- .../auth/jwt/tests/test_authentication.py | 19 +++++++------------ edx_rest_framework_extensions/settings.py | 1 - 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 7bb4142a..669985f8 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -12,6 +12,14 @@ Change Log Unreleased ---------- +[10.1.0] - 2024-01-26 +--------------------- + +* Added toggle EDX_DRF_EXTENSIONS[ENABLE_JWT_AND_LMS_USER_EMAIL_MATCH]: + + * If enabled, makes sure that the user email in JWT cookies and LMS user email matches + * If email matches, it allows authentication otherwise raise JwtUserEmailMismatchError error. + [10.0.0] - 2023-11-30 --------------------- diff --git a/edx_rest_framework_extensions/__init__.py b/edx_rest_framework_extensions/__init__.py index 53051dbc..2917331f 100644 --- a/edx_rest_framework_extensions/__init__.py +++ b/edx_rest_framework_extensions/__init__.py @@ -1,3 +1,3 @@ """ edx Django REST Framework extensions. """ -__version__ = '10.0.0' # pragma: no cover +__version__ = '10.1.0' # pragma: no cover diff --git a/edx_rest_framework_extensions/auth/jwt/tests/test_authentication.py b/edx_rest_framework_extensions/auth/jwt/tests/test_authentication.py index c50800f1..eb7279cb 100644 --- a/edx_rest_framework_extensions/auth/jwt/tests/test_authentication.py +++ b/edx_rest_framework_extensions/auth/jwt/tests/test_authentication.py @@ -36,7 +36,6 @@ ENABLE_JWT_AND_LMS_USER_EMAIL_MATCH, ENABLE_SET_REQUEST_USER_FOR_JWT_COOKIE, ) -from edx_rest_framework_extensions.config import ENABLE_SET_REQUEST_USER_FOR_JWT_COOKIE from edx_rest_framework_extensions.tests import factories @@ -251,11 +250,6 @@ def test_authenticate_with_correct_jwt_cookie_and_django_request( mock_set_custom_attribute.assert_any_call('jwt_auth_with_django_request', True) mock_set_custom_attribute.assert_any_call('jwt_auth_result', 'success-cookie') - @override_settings( - EDX_DRF_EXTENSIONS={ - ENABLE_JWT_AND_LMS_USER_EMAIL_MATCH: False - } - ) @mock.patch('edx_rest_framework_extensions.auth.jwt.authentication.set_custom_attribute') def test_authenticate_csrf_protected(self, mock_set_custom_attribute): """ @@ -551,12 +545,11 @@ def test_authenticate_jwt_and_no_session_and_set_request_user(self, mock_set_cus ), ROOT_URLCONF='edx_rest_framework_extensions.auth.jwt.tests.test_authentication', ) - def test_authenticate_user_lms_and_jwt_email_mismatch_not_checked(self): + def test_authenticate_user_lms_and_jwt_email_mismatch_toggle_disabled(self): """ - Test that lms and jwt user email is not checked for match if ENABLE_JWT_AND_LMS_USER_EMAIL_MATCH is false. + Test success for JwtAuthentication when ENABLE_JWT_AND_LMS_USER_EMAIL_MATCH is disabled. """ - user_email = 'old@example.com' - user = factories.UserFactory(email=user_email) + user = factories.UserFactory(email='old@example.com') jwt_header_payload, jwt_signature = self._get_test_jwt_token_payload_and_signature(user=user) # Cookie parts will be recombined by JwtAuthCookieMiddleware @@ -591,7 +584,8 @@ def test_authenticate_user_lms_and_jwt_email_mismatch_not_checked(self): @mock.patch('edx_rest_framework_extensions.auth.jwt.authentication.set_custom_attribute') def test_authenticate_user_lms_and_jwt_email_match_failure(self, mock_set_custom_attribute): """ - Tests failure when lms and jwt user email do not match if ENABLE_JWT_AND_LMS_USER_EMAIL_MATCH is True. + Test failure for JwtAuthentication when ENABLE_JWT_AND_LMS_USER_EMAIL_MATCH + is enabled and the lms and jwt user email do not match. """ user_email = 'old@example.com' user = factories.UserFactory(email=user_email) @@ -634,7 +628,8 @@ def test_authenticate_user_lms_and_jwt_email_match_failure(self, mock_set_custom @mock.patch('edx_rest_framework_extensions.auth.jwt.authentication.set_custom_attribute') def test_authenticate_user_lms_and_jwt_email_match_success(self, mock_set_custom_attribute): """ - Tests success when lms and jwt user email match if ENABLE_JWT_AND_LMS_USER_EMAIL_MATCH is True. + Test success for JwtAuthentication when ENABLE_JWT_AND_LMS_USER_EMAIL_MATCH + is enabled and the lms and jwt user email match. """ user_email = 'old@example.com' user = factories.UserFactory(email=user_email) diff --git a/edx_rest_framework_extensions/settings.py b/edx_rest_framework_extensions/settings.py index a17681cb..3d16890f 100644 --- a/edx_rest_framework_extensions/settings.py +++ b/edx_rest_framework_extensions/settings.py @@ -27,7 +27,6 @@ DEFAULT_SETTINGS = { ENABLE_JWT_AND_LMS_USER_EMAIL_MATCH: False, ENABLE_SET_REQUEST_USER_FOR_JWT_COOKIE: False, - ENABLE_JWT_AND_LMS_USER_EMAIL_MATCH: False, 'JWT_PAYLOAD_MERGEABLE_USER_ATTRIBUTES': (), # Map JWT claims to user attributes.