From 64094da239951b63818ef54d708ae71efe5e2480 Mon Sep 17 00:00:00 2001 From: Hassan Raza Date: Fri, 11 Jul 2025 12:04:25 +0500 Subject: [PATCH] chore: Update notification preferences for non-editable push notifications (#37003) * chore: Update notification preferences for non-editable push notifications * fix: unit tests --- .../notifications/base_notification.py | 14 ++-- .../notifications/tests/test_views.py | 71 ++++++++++++++----- 2 files changed, 59 insertions(+), 26 deletions(-) diff --git a/openedx/core/djangoapps/notifications/base_notification.py b/openedx/core/djangoapps/notifications/base_notification.py index 7d505c592c..5264053ace 100644 --- a/openedx/core/djangoapps/notifications/base_notification.py +++ b/openedx/core/djangoapps/notifications/base_notification.py @@ -61,7 +61,7 @@ COURSE_NOTIFICATION_TYPES = { 'email': False, 'email_cadence': EmailCadence.DAILY, 'push': False, - 'non_editable': [], + 'non_editable': ['push'], 'content_template': _('<{p}><{strong}>{username} posted <{strong}>{post_title}'), 'grouped_content_template': _('<{p}><{strong}>{replier_name} and others started new discussions' ''), @@ -81,7 +81,7 @@ COURSE_NOTIFICATION_TYPES = { 'email': False, 'email_cadence': EmailCadence.DAILY, 'push': False, - 'non_editable': [], + 'non_editable': ['push'], 'content_template': _('<{p}><{strong}>{username} asked <{strong}>{post_title}'), 'content_context': { 'post_title': 'Post title', @@ -131,7 +131,7 @@ COURSE_NOTIFICATION_TYPES = { 'email': True, 'email_cadence': EmailCadence.DAILY, 'push': False, - 'non_editable': [], + 'non_editable': ['push'], 'content_template': _('

{username}’s {content_type} has been reported {' 'content}

'), @@ -181,7 +181,7 @@ COURSE_NOTIFICATION_TYPES = { 'email': False, 'push': False, 'email_cadence': EmailCadence.DAILY, - 'non_editable': [], + 'non_editable': ['push'], 'content_template': _('<{p}><{strong}>{course_update_content}'), 'content_context': { 'course_update_content': 'Course update', @@ -198,7 +198,7 @@ COURSE_NOTIFICATION_TYPES = { 'email': False, 'push': False, 'email_cadence': EmailCadence.DAILY, - 'non_editable': [], + 'non_editable': ['push'], 'content_template': _('<{p}>You have a new open response submission awaiting review for ' '<{strong}>{ora_name}'), 'grouped_content_template': _('<{p}>You have multiple submissions awaiting review for ' @@ -219,7 +219,7 @@ COURSE_NOTIFICATION_TYPES = { 'email': True, 'push': False, 'email_cadence': EmailCadence.DAILY, - 'non_editable': [], + 'non_editable': ['push'], 'content_template': _('<{p}>You have received {points_earned} out of {points_possible} on your assessment: ' '<{strong}>{ora_name}'), 'content_context': { @@ -239,7 +239,7 @@ COURSE_NOTIFICATION_TYPES = { 'email': False, 'email_cadence': EmailCadence.DAILY, 'push': False, - 'non_editable': [], + 'non_editable': ['push'], 'content_template': _('<{p}>Your instructor posted <{strong}>{post_title}'), 'grouped_content_template': '', 'content_context': { diff --git a/openedx/core/djangoapps/notifications/tests/test_views.py b/openedx/core/djangoapps/notifications/tests/test_views.py index 0b36b5f042..9d5e3cdaab 100644 --- a/openedx/core/djangoapps/notifications/tests/test_views.py +++ b/openedx/core/djangoapps/notifications/tests/test_views.py @@ -290,12 +290,10 @@ class UserNotificationPreferenceAPITest(ModuleStoreTestCase): enrollment=enrollment_data ) - def _expected_api_response(self, course=None): + def _expected_api_response(self, is_staff=False): """ Helper method to return expected API response. """ - if course is None: - course = self.course response = { 'id': 1, 'course_name': 'course-v1:testorg+testcourse+testrun Course', @@ -343,7 +341,10 @@ class UserNotificationPreferenceAPITest(ModuleStoreTestCase): 'email_cadence': 'Daily', }, }, - 'non_editable': {} + 'non_editable': { + 'new_discussion_post': ['push'], + 'new_question_post': ['push'], + } }, 'updates': { 'enabled': True, @@ -364,7 +365,9 @@ class UserNotificationPreferenceAPITest(ModuleStoreTestCase): 'info': 'Notifications for new announcements and updates from the course team.' } }, - 'non_editable': {} + 'non_editable': { + 'course_updates': ['push'] + } }, 'grading': { 'enabled': True, @@ -393,10 +396,17 @@ class UserNotificationPreferenceAPITest(ModuleStoreTestCase): 'info': '' }, }, - 'non_editable': {} + 'non_editable': { + 'ora_grade_assigned': ['push'] + } } } } + if is_staff: + response['notification_preference_config']['grading']['non_editable'] = { + 'ora_staff_notifications': ['push'], + 'ora_grade_assigned': ['push'] + } return response def test_get_user_notification_preference_without_login(self): @@ -452,11 +462,10 @@ class UserNotificationPreferenceAPITest(ModuleStoreTestCase): response = self.client.get(self.path) self.assertEqual(response.status_code, status.HTTP_200_OK) - expected_response = self._expected_api_response() + expected_response = self._expected_api_response(is_staff=bool(role)) if not role: expected_response = remove_notifications_with_visibility_settings(expected_response) - self.assertEqual(response.data, expected_response) event_name, event_data = mock_emit.call_args[0] self.assertEqual(event_name, 'edx.notifications.preferences.viewed') @@ -1191,13 +1200,12 @@ class UpdateAllNotificationPreferencesViewTests(APITestCase): """ data = { 'notification_app': 'discussion', - 'notification_type': 'content_reported', - 'notification_channel': 'push', + 'notification_type': 'core', + 'notification_channel': 'web', 'value': False } response = self.client.post(self.url, data, format='json') - self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['status'], 'success') self.assertEqual(response.data['data']['total_updated'], 3) @@ -1205,7 +1213,8 @@ class UpdateAllNotificationPreferencesViewTests(APITestCase): # Verify database updates for pref in CourseNotificationPreference.objects.filter(is_active=True): self.assertFalse( - pref.notification_preference_config['discussion']['notification_types']['content_reported']['push'] + pref.notification_preference_config['discussion'][ + 'notification_types']['core']['web'] ) def test_update_non_editable_field(self): @@ -1414,7 +1423,11 @@ class GetAggregateNotificationPreferencesTest(APITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) prefs = response.data['data'] self.assertDictEqual(prefs['updates']['non_editable'], {'course_updates': ['email']}) - self.assertDictEqual(prefs['discussion']['non_editable'], {'core': ['web']}) + self.assertDictEqual(prefs['discussion']['non_editable'], { + 'new_discussion_post': ['push'], + 'new_question_post': ['push'], + 'core': ['web'] + }) class TestNotificationPreferencesView(APITestCase): @@ -1471,7 +1484,12 @@ class TestNotificationPreferencesView(APITestCase): "email_cadence": "Daily" } }, - "non_editable": {} + "non_editable": { + "new_discussion_post": ["push"], + "new_question_post": ["push"], + "content_reported": ["push"], + "new_instructor_all_learners_post": ["push"] + } }, "updates": { "enabled": True, @@ -1490,7 +1508,9 @@ class TestNotificationPreferencesView(APITestCase): "email_cadence": "Daily" } }, - "non_editable": {} + "non_editable": { + "course_updates": ["push"], + } }, "grading": { "enabled": True, @@ -1515,7 +1535,10 @@ class TestNotificationPreferencesView(APITestCase): "email_cadence": "Daily" } }, - "non_editable": {} + "non_editable": { + "ora_grade_assigned": ["push"], + "ora_staff_notifications": ["push"] + } } } } @@ -1596,7 +1619,12 @@ class TestNotificationPreferencesView(APITestCase): "email_cadence": "Daily" } }, - "non_editable": {} + "non_editable": { + "new_discussion_post": ["push"], + "new_question_post": ["push"], + "content_reported": ["push"], + "new_instructor_all_learners_post": ["push"] + } }, "updates": { "enabled": True, @@ -1615,7 +1643,9 @@ class TestNotificationPreferencesView(APITestCase): "email_cadence": "Daily" } }, - "non_editable": {} + "non_editable": { + "course_updates": ["push"], + } }, "grading": { "enabled": True, @@ -1640,7 +1670,10 @@ class TestNotificationPreferencesView(APITestCase): "email_cadence": "Daily" } }, - "non_editable": {} + "non_editable": { + "ora_grade_assigned": ["push"], + "ora_staff_notifications": ["push"] + } } } }