def send_discussion_email_notification(sender, user, post, **kwargs): if not waffle().is_enabled(FORUM_RESPONSE_NOTIFICATIONS): log.debug( 'Discussion: Response notifications waffle switch not enabled') return if not SEND_NOTIFICATIONS_FOR_COURSE.is_enabled( CourseKey.from_string(post.thread.course_id)): 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.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)
def send_discussion_email_notification(sender, user, post, **kwargs): if not waffle().is_enabled(FORUM_RESPONSE_NOTIFICATIONS): log.debug('Discussion: Response notifications waffle switch not enabled') return if not SEND_NOTIFICATIONS_FOR_COURSE.is_enabled(CourseKey.from_string(post.thread.course_id)): 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.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)
def test_comment_created_signal_message_not_sent_without_site( self, mock_send_message, mock_get_current_site): with waffle().override(FORUM_RESPONSE_NOTIFICATIONS, active=True): signals.comment_created.send(sender=self.sender, user=self.user, post=self.post) self.assertFalse(mock_send_message.called)
def test_comment_created_signal_sends_message(self, mock_send_message): with waffle().override(FORUM_RESPONSE_NOTIFICATIONS): sender = mock.Mock() user = mock.Mock() post = mock.Mock() signals.comment_created.send(sender=sender, user=user, post=post) mock_send_message.assert_called_once_with(post)
def test_comment_created_signal_message_not_sent_without_waffle_switch(self, mock_send_message): with waffle().override(FORUM_RESPONSE_NOTIFICATIONS, active=False): sender = mock.Mock() user = mock.Mock() post = mock.Mock() signals.comment_created.send(sender=sender, user=user, post=post) self.assertFalse(mock_send_message.called)
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) mock_send_message.assert_called_once_with(self.post, mock_get_current_site.return_value)
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)
def test_comment_created_signal_message_not_sent_without_waffle_switch( self, mock_send_message): with waffle().override(FORUM_RESPONSE_NOTIFICATIONS, active=False): sender = mock.Mock() user = mock.Mock() post = mock.Mock() signals.comment_created.send(sender=sender, user=user, post=post) self.assertFalse(mock_send_message.called)
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)
def test_send_discussion_email_notification(self, user_subscribed): if user_subscribed: non_matching_id = 'not-a-match' # with per_page left with a default value of 1, this ensures # that we test a multiple page result when calling # comment_client.User.subscribed_threads() subscribed_thread_ids = [non_matching_id, self.discussion_id] else: subscribed_thread_ids = [] self.mock_request.side_effect = make_mock_responder( subscribed_thread_ids=subscribed_thread_ids, comment_data=self.comment, thread_data=self.thread, ) 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) if user_subscribed: expected_message_context = get_base_template_context(site) expected_message_context.update({ 'comment_author_id': self.comment_author.id, 'comment_body': self.comment['body'], 'comment_created_at': ONE_HOUR_AGO, 'comment_id': self.comment['id'], 'comment_username': self.comment_author.username, 'course_id': self.course.id, 'thread_author_id': self.thread_author.id, 'thread_created_at': TWO_HOURS_AGO, 'thread_id': self.discussion_id, 'thread_title': 'thread-title', 'thread_username': self.thread_author.username, 'thread_commentable_id': self.thread['commentable_id'], 'post_link': self.mock_permalink.return_value, 'site': site, 'site_id': site.id }) expected_recipient = Recipient(self.thread_author.username, self.thread_author.email) actual_message = self.mock_ace_send.call_args_list[0][0][0] self.assertEqual(expected_message_context, actual_message.context) self.assertEqual(expected_recipient, actual_message.recipient) self.assertEqual(self.course.language, actual_message.language) self._assert_rendered_email(actual_message) else: self.assertFalse(self.mock_ace_send.called)
def send_discussion_email_notification(sender, user, post, **kwargs): if not waffle().is_enabled(FORUM_RESPONSE_NOTIFICATIONS): log.debug( 'Discussion: Response notifications waffle switch not enabled') 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') return current_site = get_current_site() if current_site is None: log.debug('Discussion: No current site, not sending notification') return send_message(post, current_site)
def run_should_not_send_email_test(self, comment_dict): self.mock_request.side_effect = make_mock_responder( subscribed_thread_ids=[self.discussion_id], comment_data=comment_dict, thread_data=self.thread, ) user = mock.Mock() comment = cc.Comment.find(id=comment_dict['id']).retrieve() with waffle().override(FORUM_RESPONSE_NOTIFICATIONS): comment_created.send(sender=None, user=user, post=comment) actual_result = _should_send_message({ 'thread_author_id': self.thread_author.id, 'course_id': self.course.id, 'comment_id': comment_dict['id'], 'thread_id': self.thread['id'], }) self.assertEqual(actual_result, False) self.assertFalse(self.mock_ace_send.called)
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)
def test_comment_created_signal_message_not_sent_without_site(self, mock_send_message, mock_get_current_site): with waffle().override(FORUM_RESPONSE_NOTIFICATIONS, active=True): signals.comment_created.send(sender=self.sender, user=self.user, post=self.post) self.assertFalse(mock_send_message.called)
def send_discussion_email_notification(sender, user, post, **kwargs): if waffle().is_enabled(FORUM_RESPONSE_NOTIFICATIONS): send_message(post)
def test_send_discussion_email_notification(self, user_subscribed): with mock_the_things() as mocked_items: mock_request, mock_ace_send, mock_permalink = mocked_items if user_subscribed: non_matching_id = 'not-a-match' # with per_page left with a default value of 1, this ensures # that we test a multiple page result when calling # comment_client.User.subscribed_threads() mock_request.side_effect = make_mock_responder([non_matching_id, self.discussion_id]) else: mock_request.side_effect = make_mock_responder([]) now = datetime.utcnow() one_hour_ago = now - timedelta(hours=1) thread = mock.Mock( id=self.discussion_id, course_id=self.course.id, created_at=one_hour_ago, title='thread-title', user_id=self.thread_author.id, username=self.thread_author.username, commentable_id='thread-commentable-id' ) comment = mock.Mock( id='comment-id', body='comment-body', created_at=now, thread=thread, user_id=self.comment_author.id, username=self.comment_author.username ) user = mock.Mock() with waffle().override(FORUM_RESPONSE_NOTIFICATIONS): comment_created.send(sender=None, user=user, post=comment) if user_subscribed: expected_message_context = get_base_template_context(Site.objects.get_current()) expected_message_context.update({ 'comment_author_id': self.comment_author.id, 'comment_body': 'comment-body', 'comment_created_at': now, 'comment_id': 'comment-id', 'comment_username': self.comment_author.username, 'course_id': self.course.id, 'thread_author_id': self.thread_author.id, 'thread_created_at': one_hour_ago, 'thread_id': self.discussion_id, 'thread_title': 'thread-title', 'thread_username': self.thread_author.username, 'thread_commentable_id': 'thread-commentable-id', 'post_link': urljoin(Site.objects.get_current().domain, mock_permalink.return_value), 'site': Site.objects.get_current(), 'site_id': Site.objects.get_current().id, }) ga_tracking_pixel_url = _generate_ga_pixel_url(expected_message_context) expected_message_context.update({'ga_tracking_pixel_url': ga_tracking_pixel_url}) expected_recipient = Recipient(self.thread_author.username, self.thread_author.email) actual_message = mock_ace_send.call_args_list[0][0][0] self.assertEqual(expected_message_context, actual_message.context) self.assertEqual(expected_recipient, actual_message.recipient) self.assertEqual(self.course.language, actual_message.language) else: self.assertFalse(mock_ace_send.called)