def test_read_notifications(self): """ Test to make sure that we can generate a notification for read notifications as well """ register_namespace_resolver(TestNamespaceResolver()) set_user_notification_preference( self.test_user_id, const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'true') # mark the two test notifications as read mark_notification_read(self.test_user_id, self.notification1.id) mark_notification_read(self.test_user_id, self.notification2.id) # make sure read notifications do not generate digests when we only want unread notifications self.assertEqual( send_notifications_digest( self.from_timestamp, self.to_timestamp, const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'subject', '*****@*****.**', unread_only=True), 0) # make sure read notifications do generate digests when we want all notifications self.assertEqual( send_notifications_digest( self.from_timestamp, self.to_timestamp, const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'subject', '*****@*****.**', unread_only=False), 2)
def send_digest(request, digest_email): # just send to logged in user register_namespace_resolver(TestNotificationNamespaceResolver(request.user.email if not digest_email else digest_email)) send_notifications_digest( datetime.now(pytz.UTC) - timedelta(days=1) if const.NOTIFICATION_DIGEST_SEND_TIMEFILTERED else None, datetime.now(pytz.UTC), const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, const.NOTIFICATION_DAILY_DIGEST_SUBJECT, const.NOTIFICATION_EMAIL_FROM_ADDRESS )
def test_wildcard_group_mapping(self): """ Test that adds the default notification type mapping """ msg_type = self.store.save_notification_type( NotificationType( name='open-edx.lms.discussions.new-discussion-added', renderer='open-edx.lms.discussions.new-discussion-added', ) ) # create cohort notification msg = self.store.save_notification_message( NotificationMessage( msg_type=msg_type, namespace='cohort-thread-added', payload={'subject': 'foo', 'body': 'bar'}, ) ) publish_notification_to_user(self.test_user_id, msg) register_namespace_resolver(TestNamespaceResolver()) set_user_notification_preference(self.test_user_id, const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'true') self.assertEqual( send_notifications_digest( self.from_timestamp, self.to_timestamp, const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'subject', '*****@*****.**' ), 3 )
def test_user_preference_undefined(self): """ Make sure we don't send a digest if the digest preference name is not found """ register_namespace_resolver(TestNamespaceResolver()) self.assertEqual( send_notifications_digest( self.from_timestamp, self.to_timestamp, const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'subject', '*****@*****.**'), 0)
def test_no_user_resolver(self): """ Asserts no digests were sent if we don't have a user resolver as part of the namespace """ register_namespace_resolver(TestNamespaceResolverNoUsers()) self.assertEqual( send_notifications_digest( self.from_timestamp, self.to_timestamp, const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'subject', '*****@*****.**'), 0)
def test_default_namespace_resolver(self): """ Assert no digests were sent if we just use the DefaultNotificationNamespaceResolver """ register_namespace_resolver(DefaultNotificationNamespaceResolver()) self.assertEqual( send_notifications_digest( self.from_timestamp, self.to_timestamp, const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'subject', '*****@*****.**'), 0)
def test_no_namespace_resolver(self): """ Assert no digests were sent if we don't have a namespace resolver """ register_namespace_resolver(None) self.assertEqual( send_notifications_digest( self.from_timestamp, self.to_timestamp, const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'subject', '*****@*****.**'), 0)
def test_read_notifications(self): """ Test to make sure that we can generate a notification for read notifications as well """ register_namespace_resolver(TestNamespaceResolver()) set_user_notification_preference(self.test_user_id, const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'true') # mark the two test notifications as read mark_notification_read(self.test_user_id, self.notification1.id) mark_notification_read(self.test_user_id, self.notification2.id) # make sure read notifications do not generate digests when we only want unread notifications self.assertEqual( send_notifications_digest( self.from_timestamp, self.to_timestamp, const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'subject', '*****@*****.**', unread_only=True ), 0 ) # make sure read notifications do generate digests when we want all notifications self.assertEqual( send_notifications_digest( self.from_timestamp, self.to_timestamp, const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'subject', '*****@*****.**', unread_only=False ), 2 )
def test_user_preference_false(self): """ Make sure we don't send a digest to a user that does not want digests """ register_namespace_resolver(TestNamespaceResolver()) set_user_notification_preference( self.test_user_id, const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'false') self.assertEqual( send_notifications_digest( self.from_timestamp, self.to_timestamp, const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'subject', '*****@*****.**'), 0)
def test_user_preference_undefined(self): """ Make sure we don't send a digest if the digest preference name is not found """ register_namespace_resolver(TestNamespaceResolver()) self.assertEqual( send_notifications_digest( self.from_timestamp, self.to_timestamp, const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'subject', '*****@*****.**' ), 0 )
def test_happy_path_weekly(self): """ If all is good and enabled, in this test case, we should get two digests sent, one for each namespace """ register_namespace_resolver(TestNamespaceResolver()) set_user_notification_preference( self.test_user_id, const.NOTIFICATION_WEEKLY_DIGEST_PREFERENCE_NAME, 'true') self.assertEqual( send_notifications_digest( self.weekly_from_timestamp, self.to_timestamp, const.NOTIFICATION_WEEKLY_DIGEST_PREFERENCE_NAME, 'subject', '*****@*****.**'), 2)
def test_no_user_resolver(self): """ Asserts no digests were sent if we don't have a user resolver as part of the namespace """ register_namespace_resolver(TestNamespaceResolverNoUsers()) self.assertEqual( send_notifications_digest( self.from_timestamp, self.to_timestamp, const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'subject', '*****@*****.**' ), 0 )
def test_default_namespace_resolver(self): """ Assert no digests were sent if we just use the DefaultNotificationNamespaceResolver """ register_namespace_resolver(DefaultNotificationNamespaceResolver()) self.assertEqual( send_notifications_digest( self.from_timestamp, self.to_timestamp, const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'subject', '*****@*****.**' ), 0 )
def test_user_preference_false(self): """ Make sure we don't send a digest to a user that does not want digests """ register_namespace_resolver(TestNamespaceResolver()) set_user_notification_preference(self.test_user_id, const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'false') self.assertEqual( send_notifications_digest( self.from_timestamp, self.to_timestamp, const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'subject', '*****@*****.**' ), 0 )
def test_no_namespace_resolver(self): """ Assert no digests were sent if we don't have a namespace resolver """ register_namespace_resolver(None) self.assertEqual( send_notifications_digest( self.from_timestamp, self.to_timestamp, const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'subject', '*****@*****.**' ), 0 )
def _send_digest(self, subject, preference_name, day_delta, namespace): """ Sends a digest """ if const.NOTIFICATION_DIGEST_SEND_TIMEFILTERED: from_timestamp = datetime.datetime.now(pytz.UTC) - datetime.timedelta(days=day_delta) else: from_timestamp = None to_timestamp = datetime.datetime.now(pytz.UTC) from_email = const.NOTIFICATION_EMAIL_FROM_ADDRESS if namespace == "All": digests_sent = send_notifications_digest(from_timestamp, to_timestamp, preference_name, subject, from_email) else: digests_sent = send_notifications_namespace_digest( namespace, from_timestamp, to_timestamp, preference_name, subject, from_email ) return digests_sent
def test_happy_path_weekly(self): """ If all is good and enabled, in this test case, we should get two digests sent, one for each namespace """ register_namespace_resolver(TestNamespaceResolver()) set_user_notification_preference(self.test_user_id, const.NOTIFICATION_WEEKLY_DIGEST_PREFERENCE_NAME, 'true') self.assertEqual( send_notifications_digest( self.weekly_from_timestamp, self.to_timestamp, const.NOTIFICATION_WEEKLY_DIGEST_PREFERENCE_NAME, 'subject', '*****@*****.**' ), 2 )
def test_to_not_send_digest(self): """ If there are no unread notifications for the user for given timestamps Then don't send digest emails to the user. """ register_namespace_resolver(TestNamespaceResolver()) set_user_notification_preference( self.test_user_id, const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'true') # there will be no unread notifications to send for the digest. self.assertEqual( send_notifications_digest( self.from_timestamp + datetime.timedelta(days=2), self.to_timestamp + datetime.timedelta(days=1), const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'subject', '*****@*****.**'), 0)
def test_happy_path_without_styling(self): """ If all is good and enabled, but the css and image are not supplied, in this test case, we should still get two digests sent, one for each namespace, but the resulting emails would not have any css or images. """ register_namespace_resolver(TestNamespaceResolver()) const.NOTIFICATION_DIGEST_EMAIL_CSS = 'bad.css.file' const.NOTIFICATION_BRANDED_DEFAULT_LOGO = 'bad.image.file' set_user_notification_preference( self.test_user_id, const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'true') self.assertEqual( send_notifications_digest( self.from_timestamp, self.to_timestamp, const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'subject', '*****@*****.**'), 2)
def test_to_not_send_digest(self): """ If there are no unread notifications for the user for given timestamps Then don't send digest emails to the user. """ register_namespace_resolver(TestNamespaceResolver()) set_user_notification_preference(self.test_user_id, const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'true') # there will be no unread notifications to send for the digest. self.assertEqual( send_notifications_digest( self.from_timestamp + datetime.timedelta(days=2), self.to_timestamp + datetime.timedelta(days=1), const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'subject', '*****@*****.**' ), 0 )
def test_happy_path_without_styling(self): """ If all is good and enabled, but the css and image are not supplied, in this test case, we should still get two digests sent, one for each namespace, but the resulting emails would not have any css or images. """ register_namespace_resolver(TestNamespaceResolver()) const.NOTIFICATION_DIGEST_EMAIL_CSS = 'bad.css.file' const.NOTIFICATION_BRANDED_DEFAULT_LOGO = 'bad.image.file' set_user_notification_preference(self.test_user_id, const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'true') self.assertEqual( send_notifications_digest( self.from_timestamp, self.to_timestamp, const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME, 'subject', '*****@*****.**' ), 2 )