From b35450decce155dd09fe56e4bf7290d2bd23bf97 Mon Sep 17 00:00:00 2001 From: Alex Dusenbery Date: Mon, 13 Nov 2017 12:34:07 -0500 Subject: [PATCH] EDUCATOR-1729 | Control forum notifications with SiteConfiguration, too. --- lms/djangoapps/discussion/signals/handlers.py | 18 ++++++++-- .../discussion/tests/test_signals.py | 33 +++++++++++++++++++ lms/djangoapps/discussion/tests/test_tasks.py | 5 +++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/lms/djangoapps/discussion/signals/handlers.py b/lms/djangoapps/discussion/signals/handlers.py index 99b6b70a78..7221b5ed6c 100644 --- a/lms/djangoapps/discussion/signals/handlers.py +++ b/lms/djangoapps/discussion/signals/handlers.py @@ -9,12 +9,16 @@ from opaque_keys.edx.keys import CourseKey from django_comment_common import signals from lms.djangoapps.discussion.config.waffle import waffle, FORUM_RESPONSE_NOTIFICATIONS, SEND_NOTIFICATIONS_FOR_COURSE from lms.djangoapps.discussion import tasks +from openedx.core.djangoapps.site_configuration.models import SiteConfiguration from openedx.core.djangoapps.theming.helpers import get_current_site log = logging.getLogger(__name__) +ENABLE_FORUM_NOTIFICATIONS_FOR_SITE_KEY = 'enable_forum_notifications' + + @receiver(signals.comment_created) def send_discussion_email_notification(sender, user, post, **kwargs): if not waffle().is_enabled(FORUM_RESPONSE_NOTIFICATIONS): @@ -22,12 +26,22 @@ def send_discussion_email_notification(sender, user, post, **kwargs): return if not SEND_NOTIFICATIONS_FOR_COURSE.is_enabled(CourseKey.from_string(post.thread.course_id)): - log.debug('Discussion: Response notifications not enabled for this course') + log.debug('Discussion: Response notifications not enabled for course: %s.', post.thread.course_id) return current_site = get_current_site() if current_site is None: - log.debug('Discussion: No current site, not sending notification') + log.info('Discussion: No current site, not sending notification about post: %s.', post.id) + return + + try: + if not current_site.configuration.get_value(ENABLE_FORUM_NOTIFICATIONS_FOR_SITE_KEY, False): + log_message = 'Discussion: notifications not enabled for site: %s. Not sending message about post: %s.' + log.info(log_message, current_site, post.id) + return + except SiteConfiguration.DoesNotExist: + log_message = 'Discussion: No SiteConfiguration for site %s. Not sending message about post: %s.' + log.info(log_message, current_site, post.id) return send_message(post, current_site) diff --git a/lms/djangoapps/discussion/tests/test_signals.py b/lms/djangoapps/discussion/tests/test_signals.py index 7b94ae31f4..60096a5c75 100644 --- a/lms/djangoapps/discussion/tests/test_signals.py +++ b/lms/djangoapps/discussion/tests/test_signals.py @@ -5,6 +5,8 @@ from opaque_keys.edx.keys import CourseKey from django_comment_common import signals from lms.djangoapps.discussion.config.waffle import waffle, FORUM_RESPONSE_NOTIFICATIONS, SEND_NOTIFICATIONS_FOR_COURSE +from lms.djangoapps.discussion.signals.handlers import ENABLE_FORUM_NOTIFICATIONS_FOR_SITE_KEY +from openedx.core.djangoapps.site_configuration.tests.factories import SiteFactory, SiteConfigurationFactory from openedx.core.djangoapps.waffle_utils.testutils import override_waffle_flag @@ -15,10 +17,16 @@ class SendMessageHandlerTestCase(TestCase): self.post = mock.Mock() self.post.thread.course_id = 'course-v1:edX+DemoX+Demo_Course' + self.site = SiteFactory.create() + @mock.patch('lms.djangoapps.discussion.signals.handlers.get_current_site') @mock.patch('lms.djangoapps.discussion.signals.handlers.send_message') @override_waffle_flag(SEND_NOTIFICATIONS_FOR_COURSE, True) def test_comment_created_signal_sends_message(self, mock_send_message, mock_get_current_site): + site_config = SiteConfigurationFactory.create(site=self.site) + site_config.values[ENABLE_FORUM_NOTIFICATIONS_FOR_SITE_KEY] = True + site_config.save() + mock_get_current_site.return_value = self.site with waffle().override(FORUM_RESPONSE_NOTIFICATIONS): signals.comment_created.send(sender=self.sender, user=self.user, post=self.post) @@ -47,3 +55,28 @@ class SendMessageHandlerTestCase(TestCase): signals.comment_created.send(sender=self.sender, user=self.user, post=self.post) self.assertFalse(mock_send_message.called) + + @mock.patch('lms.djangoapps.discussion.signals.handlers.get_current_site') + @mock.patch('lms.djangoapps.discussion.signals.handlers.send_message') + @override_waffle_flag(SEND_NOTIFICATIONS_FOR_COURSE, True) + def test_comment_created_signal_msg_not_sent_without_site_config(self, mock_send_message, mock_get_current_site): + mock_get_current_site.return_value = self.site + with waffle().override(FORUM_RESPONSE_NOTIFICATIONS): + signals.comment_created.send(sender=self.sender, user=self.user, post=self.post) + + self.assertFalse(mock_send_message.called) + + @mock.patch('lms.djangoapps.discussion.signals.handlers.get_current_site') + @mock.patch('lms.djangoapps.discussion.signals.handlers.send_message') + @override_waffle_flag(SEND_NOTIFICATIONS_FOR_COURSE, True) + def test_comment_created_signal_msg_not_sent_with_site_config_disabled( + self, mock_send_message, mock_get_current_site + ): + site_config = SiteConfigurationFactory.create(site=self.site) + site_config.values[ENABLE_FORUM_NOTIFICATIONS_FOR_SITE_KEY] = False + site_config.save() + mock_get_current_site.return_value = self.site + with waffle().override(FORUM_RESPONSE_NOTIFICATIONS): + signals.comment_created.send(sender=self.sender, user=self.user, post=self.post) + + self.assertFalse(mock_send_message.called) diff --git a/lms/djangoapps/discussion/tests/test_tasks.py b/lms/djangoapps/discussion/tests/test_tasks.py index e5ca35cf41..ccaf9d784e 100644 --- a/lms/djangoapps/discussion/tests/test_tasks.py +++ b/lms/djangoapps/discussion/tests/test_tasks.py @@ -17,10 +17,12 @@ from django_comment_common.signals import comment_created from edx_ace.recipient import Recipient from edx_ace.utils import date from lms.djangoapps.discussion.config.waffle import waffle, FORUM_RESPONSE_NOTIFICATIONS, SEND_NOTIFICATIONS_FOR_COURSE +from lms.djangoapps.discussion.signals.handlers import ENABLE_FORUM_NOTIFICATIONS_FOR_SITE_KEY from lms.djangoapps.discussion.tasks import _should_send_message, _generate_ga_pixel_url import lms.lib.comment_client as cc from openedx.core.djangoapps.content.course_overviews.tests.factories import CourseOverviewFactory from openedx.core.djangoapps.schedules.template_context import get_base_template_context +from openedx.core.djangoapps.site_configuration.tests.factories import SiteConfigurationFactory from openedx.core.djangoapps.waffle_utils.testutils import override_waffle_flag from student.tests.factories import CourseEnrollmentFactory, UserFactory from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase @@ -180,6 +182,9 @@ class TaskTestCase(ModuleStoreTestCase): user = mock.Mock() comment = cc.Comment.find(id=self.comment['id']).retrieve() site = Site.objects.get_current() + site_config = SiteConfigurationFactory.create(site=site) + site_config.values[ENABLE_FORUM_NOTIFICATIONS_FOR_SITE_KEY] = True + site_config.save() with waffle().override(FORUM_RESPONSE_NOTIFICATIONS): with mock.patch('lms.djangoapps.discussion.signals.handlers.get_current_site', return_value=site): comment_created.send(sender=None, user=user, post=comment)