예제 #1
0
    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
        )
예제 #2
0
    def post(self, request, name):
        """
        HTTP POST Handler
        """
        if 'value' not in request.DATA:
            raise Http404()

        try:
            # this will raise an ItemNotFoundError
            # if the notification_preference cannot be found
            value = request.DATA.get('value')
            set_user_notification_preference(int(request.user.id), name, value)

            if const.NOTIFICATION_ENFORCE_SINGLE_DIGEST_PREFERENCE:
                # If the user sets one digest preference, make sure
                # only one of them is enabled, i.e. turn off the other one
                is_digest_setting = (
                    name in [
                        const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME,
                        const.NOTIFICATION_WEEKLY_DIGEST_PREFERENCE_NAME
                    ]
                )

                if is_digest_setting and value.lower() == 'true':
                    other_setting = const.NOTIFICATION_DAILY_DIGEST_PREFERENCE_NAME if name \
                        == const.NOTIFICATION_WEEKLY_DIGEST_PREFERENCE_NAME else \
                        const.NOTIFICATION_WEEKLY_DIGEST_PREFERENCE_NAME

                    # turn off the other setting
                    set_user_notification_preference(int(request.user.id), other_setting, "false")

        except ItemNotFoundError:
            raise Http404()

        return Response([], status.HTTP_200_OK)
예제 #3
0
    def test_get_specific_user_preferences(self):
        """
        Test specific preference setting for the user
        """
        # test bad preference name send 400 error response
        response = self.client.get(
            reverse('edx_notifications.consumer.user_preferences.detail', args=['bad-name']))
        self.assertEqual(response.status_code, 404)

        notification_preference_daily = NotificationPreference(
            name='daily-digest-emails',
            display_name='Daily Emails',
            display_description='Daily Digestion Email ',
            default_value='false'
        )
        notification_preference = set_notification_preference(notification_preference_daily)

        user_preference = set_user_notification_preference(
            1,
            notification_preference.name,
            'Test User 1'
        )

        # hit the api with the valid preference name
        response = self.client.get(
            reverse('edx_notifications.consumer.user_preferences.detail', args=['daily-digest-emails']))
        self.assertEqual(response.status_code, 200)

        results = json.loads(response.content)
        self.assertEqual(len(results), 1)

        self.assertEqual(user_preference.user_id, results[0]['user_id'])
        self.assertEqual(user_preference.value, results[0]['value'])
        self._compare_notification_preference_to_result(user_preference.preference, results[0]['preference'])
예제 #4
0
    def test_user_preferences_list(self):
        """
        Test User Preferences List
        """
        notification_preference_daily = NotificationPreference(
            name='daily-digest-emails',
            display_name='Daily Emails',
            display_description='Daily Digestion Email ',
            default_value='false'
        )
        notification_preference = set_notification_preference(notification_preference_daily)

        user_preference = set_user_notification_preference(
            1,
            notification_preference.name,
            'Test User 1'
        )

        response = self.client.get(reverse('edx_notifications.consumer.user_preferences'))
        self.assertEqual(response.status_code, 200)

        results = json.loads(response.content)
        self.assertEqual(len(results), 1)

        self.assertEqual(user_preference.user_id, results[0]['user_id'])
        self.assertEqual(user_preference.value, results[0]['value'])
        self._compare_notification_preference_to_result(user_preference.preference, results[0]['preference'])
예제 #5
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
        )
예제 #6
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
        )
예제 #7
0
    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)
예제 #8
0
    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
        )
예제 #9
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)
예제 #10
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
        )
예제 #11
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
        )
예제 #12
0
    def test_default_group_mapping(self):
        """
        Test that adds the default notification type mapping
        """
        msg_type = self.store.save_notification_type(
            NotificationType(
                name='open-edx.lms.discussions.cohorted-thread-added',
                renderer=
                'edx_notifications.openedx.forums.CohortedThreadAddedRenderer',
            ))
        # create cohort notification
        with freeze_time(self.to_timestamp):
            msg = self.store.save_notification_message(
                NotificationMessage(
                    msg_type=msg_type,
                    namespace='cohort-thread-added',
                    payload={
                        'subject': 'foo',
                        'body': 'bar',
                        'thread_title':
                        'A demo posting to the discussion forums'
                    },
                ))
            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)