From dedf8f4ecd49ec007ee4595e2fe39c4a158f51e9 Mon Sep 17 00:00:00 2001 From: Alie Langston Date: Fri, 20 Sep 2024 14:40:53 -0400 Subject: [PATCH] fix: allow course staff role to be updated with email change --- edx_exams/apps/core/signals/handlers.py | 5 +---- edx_exams/apps/core/tests/test_handlers.py | 26 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/edx_exams/apps/core/signals/handlers.py b/edx_exams/apps/core/signals/handlers.py index 8d5e0345..b4af85b1 100644 --- a/edx_exams/apps/core/signals/handlers.py +++ b/edx_exams/apps/core/signals/handlers.py @@ -23,10 +23,7 @@ def listen_for_course_access_role_added(sender, signal, **kwargs): # pylint: di if role not in COURSE_STAFF_ROLES: return - user, _ = User.objects.get_or_create( - username=user_data.pii.username, - email=user_data.pii.email, - ) + user, _ = User.objects.get_or_create(username=user_data.pii.username) CourseStaffRole.objects.get_or_create( user=user, course_id=course_key, diff --git a/edx_exams/apps/core/tests/test_handlers.py b/edx_exams/apps/core/tests/test_handlers.py index 9d81adb5..e1925fd0 100644 --- a/edx_exams/apps/core/tests/test_handlers.py +++ b/edx_exams/apps/core/tests/test_handlers.py @@ -155,3 +155,29 @@ def test_course_access_role_remove_single_role(self): roles, ['instructor'] ) + + def test_course_access_role_email_change(self): + """ + Test that if a user updates their email, additional course staff roles are able to be added. + """ + role_event_data = self._get_event_data(self.course_id, self.existing_user.username, 'staff') + event_metadata = self._get_event_metadata(COURSE_ACCESS_ROLE_ADDED) + event_kwargs = { + 'course_access_role_data': role_event_data, + 'metadata': event_metadata, + } + listen_for_course_access_role_added(None, COURSE_ACCESS_ROLE_ADDED, **event_kwargs) + + self.existing_user.email = 'updated_email@example.com' + self.existing_user.save() + + other_course = 'course-v1:another-course-2024' + role_event_data = self._get_event_data(other_course, self.existing_user.username, 'staff') + event_metadata = self._get_event_metadata(COURSE_ACCESS_ROLE_ADDED) + event_kwargs = { + 'course_access_role_data': role_event_data, + 'metadata': event_metadata, + } + listen_for_course_access_role_added(None, COURSE_ACCESS_ROLE_ADDED, **event_kwargs) + + self.assertEqual(len(CourseStaffRole.objects.filter(user=self.existing_user)), 2)