From dc548edeca9c1f187a60442540fa590d5e6c1c47 Mon Sep 17 00:00:00 2001 From: Hassan Raza Date: Thu, 27 Mar 2025 16:15:40 +0500 Subject: [PATCH] fix: user unsub preference removal on email enable through account preferences (#36451) --- .../notifications/tests/test_views.py | 18 ++++++++++++++++++ openedx/core/djangoapps/notifications/views.py | 5 +++++ 2 files changed, 23 insertions(+) diff --git a/openedx/core/djangoapps/notifications/tests/test_views.py b/openedx/core/djangoapps/notifications/tests/test_views.py index 3d1a11e713..3e68ff2ce8 100644 --- a/openedx/core/djangoapps/notifications/tests/test_views.py +++ b/openedx/core/djangoapps/notifications/tests/test_views.py @@ -1059,6 +1059,7 @@ def remove_notifications_with_visibility_settings(expected_response): return expected_response +@ddt.ddt class UpdateAllNotificationPreferencesViewTests(APITestCase): """ Tests for the UpdateAllNotificationPreferencesView. @@ -1313,6 +1314,23 @@ class UpdateAllNotificationPreferencesViewTests(APITestCase): response = self.client.post(self.url, test_case, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + @ddt.data(*itertools.product(('email', 'web'), (True, False))) + @ddt.unpack + def test_unsub_user_preferences_removal_on_account_email_enabled(self, channel, value): + """ + Test one click unsub user preference should be removed on email enable for any app through account preferences + """ + UserPreference.objects.create(user=self.user, key=ONE_CLICK_EMAIL_UNSUB_KEY) + payload = { + 'notification_app': 'grading', + 'notification_type': 'core', + 'notification_channel': channel, + 'value': value + } + self.client.post(self.url, payload, format='json') + result = 0 if channel == 'email' and value else 1 + self.assertEqual(UserPreference.objects.count(), result) + class GetAggregateNotificationPreferencesTest(APITestCase): """ diff --git a/openedx/core/djangoapps/notifications/views.py b/openedx/core/djangoapps/notifications/views.py index 4271c019c7..b5a120e305 100644 --- a/openedx/core/djangoapps/notifications/views.py +++ b/openedx/core/djangoapps/notifications/views.py @@ -537,6 +537,11 @@ class UpdateAllNotificationPreferencesView(APIView): 'course_id': str(preference.course_id), 'error': str(e) }) + if channel == 'email' and value: + UserPreference.objects.filter( + user_id=request.user, + key=ONE_CLICK_EMAIL_UNSUB_KEY + ).delete() response_data = { 'status': 'success' if updated_courses else 'partial_success' if errors else 'error', 'message': 'Notification preferences update completed',