diff --git a/openedx/core/djangoapps/notifications/tests/test_views.py b/openedx/core/djangoapps/notifications/tests/test_views.py index 4ade0c2cad5f..c4a28bbf0461 100644 --- a/openedx/core/djangoapps/notifications/tests/test_views.py +++ b/openedx/core/djangoapps/notifications/tests/test_views.py @@ -11,6 +11,7 @@ from django.core.cache import cache from django.test.utils import override_settings from django.urls import reverse +from edx_toggles.toggles.testutils import override_waffle_flag from rest_framework import status from rest_framework.test import APIClient, APITestCase @@ -31,6 +32,7 @@ from xmodule.modulestore.tests.factories import CourseFactory from ..base_notification import COURSE_NOTIFICATION_APPS, COURSE_NOTIFICATION_TYPES, get_default_values_of_preferences +from ..config.waffle import DISABLE_EMAIL_NOTIFICATIONS from ..utils import exclude_inaccessible_preferences, get_notification_types_with_visibility_settings User = get_user_model() @@ -742,6 +744,7 @@ def test_if_data_is_correctly_aggregated(self): data = { "status": "success", "show_preferences": True, + "show_email_preferences": True, "message": "Notification preferences retrieved successfully.", "data": { "discussion": { @@ -888,6 +891,24 @@ def test_update_preferences(self): ) self.assertEqual(preference.web, True) # noqa: PT009 + def test_show_email_preferences_flag_disabled(self): + """ + Test: show_email_preferences is True when DISABLE_EMAIL_NOTIFICATIONS flag is off. + """ + with override_waffle_flag(DISABLE_EMAIL_NOTIFICATIONS, active=False): + response = self.client.get(self.url) + self.assertEqual(response.status_code, status.HTTP_200_OK) # noqa: PT009 + self.assertTrue(response.data['show_email_preferences']) # noqa: PT009 + + def test_show_email_preferences_flag_enabled(self): + """ + Test: show_email_preferences is False when DISABLE_EMAIL_NOTIFICATIONS flag is on. + """ + with override_waffle_flag(DISABLE_EMAIL_NOTIFICATIONS, active=True): + response = self.client.get(self.url) + self.assertEqual(response.status_code, status.HTTP_200_OK) # noqa: PT009 + self.assertFalse(response.data['show_email_preferences']) # noqa: PT009 + def test_update_preferences_non_grouped_email(self): """ Test case: Update notification preferences for the authenticated user diff --git a/openedx/core/djangoapps/notifications/utils.py b/openedx/core/djangoapps/notifications/utils.py index 75996d904749..3c4f08c8e084 100644 --- a/openedx/core/djangoapps/notifications/utils.py +++ b/openedx/core/djangoapps/notifications/utils.py @@ -5,7 +5,7 @@ from common.djangoapps.student.models import CourseAccessRole from openedx.core.djangoapps.django_comment_common.models import Role -from openedx.core.djangoapps.notifications.config.waffle import DISABLE_NOTIFICATIONS +from openedx.core.djangoapps.notifications.config.waffle import DISABLE_EMAIL_NOTIFICATIONS, DISABLE_NOTIFICATIONS from openedx.core.djangoapps.notifications.models import NotificationPreference, create_notification_preference from openedx.core.lib.cache_utils import request_cached @@ -17,6 +17,13 @@ def get_show_notifications_tray(): return not DISABLE_NOTIFICATIONS.is_enabled() +def get_show_email_preferences(): + """ + Returns whether email notification preferences are enabled via waffle flag + """ + return not DISABLE_EMAIL_NOTIFICATIONS.is_enabled() + + def get_list_in_batches(input_list, batch_size): """ Divides the list of objects into list of list of objects each of length batch_size. diff --git a/openedx/core/djangoapps/notifications/views.py b/openedx/core/djangoapps/notifications/views.py index 4901b42c9931..3d786da14d7f 100644 --- a/openedx/core/djangoapps/notifications/views.py +++ b/openedx/core/djangoapps/notifications/views.py @@ -35,6 +35,7 @@ from .utils import ( create_account_notification_pref_if_not_exists, exclude_inaccessible_preferences, + get_show_email_preferences, get_show_notifications_tray, ) @@ -305,6 +306,7 @@ def get(self, request): 'status': 'success', 'message': 'Notification preferences retrieved successfully.', 'show_preferences': get_show_notifications_tray(), + 'show_email_preferences': get_show_email_preferences(), 'data': structured_preferences }, status=status.HTTP_200_OK)