From 4d948672cd741709ec29ee02313662b8af9eb0ae Mon Sep 17 00:00:00 2001 From: sandroroux Date: Wed, 6 Dec 2017 11:43:29 -0500 Subject: [PATCH] Brought SiteConfig checks back. --- lms/djangoapps/discussion/signals/handlers.py | 10 ++++++++ .../discussion/tests/test_signals.py | 25 +++++++++++++++++++ lms/djangoapps/discussion/tests/test_tasks.py | 4 +++ 3 files changed, 39 insertions(+) diff --git a/lms/djangoapps/discussion/signals/handlers.py b/lms/djangoapps/discussion/signals/handlers.py index 71ae8fea83..44941e91de 100644 --- a/lms/djangoapps/discussion/signals/handlers.py +++ b/lms/djangoapps/discussion/signals/handlers.py @@ -25,6 +25,16 @@ def send_discussion_email_notification(sender, user, post, **kwargs): 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 f8142dee10..d937a894fc 100644 --- a/lms/djangoapps/discussion/tests/test_signals.py +++ b/lms/djangoapps/discussion/tests/test_signals.py @@ -2,6 +2,7 @@ from django.test import TestCase import mock from django_comment_common import signals +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 @@ -18,6 +19,9 @@ class SendMessageHandlerTestCase(TestCase): @mock.patch('lms.djangoapps.discussion.signals.handlers.get_current_site') @mock.patch('lms.djangoapps.discussion.signals.handlers.send_message') 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 signals.comment_created.send(sender=self.sender, user=self.user, post=self.post) @@ -41,3 +45,24 @@ 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') + 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 + 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') + 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 + 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 be1b3768ed..05cb4d583d 100644 --- a/lms/djangoapps/discussion/tests/test_tasks.py +++ b/lms/djangoapps/discussion/tests/test_tasks.py @@ -17,6 +17,7 @@ from django_comment_common.signals import comment_created from edx_ace.recipient import Recipient from edx_ace.renderers import EmailRenderer from edx_ace.utils import date +from lms.djangoapps.discussion.signals.handlers import ENABLE_FORUM_NOTIFICATIONS_FOR_SITE_KEY from lms.djangoapps.discussion.tasks import _should_send_message from openedx.core.djangoapps.content.course_overviews.tests.factories import CourseOverviewFactory from openedx.core.djangoapps.ace_common.template_context import get_base_template_context @@ -182,6 +183,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 mock.patch('lms.djangoapps.discussion.signals.handlers.get_current_site', return_value=site): comment_created.send(sender=None, user=user, post=comment)