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)
Example #2
0
    def test_marking_read_state(self):
        """
        Verify proper behavior when marking notfications as read/unread
        """

        msg = NotificationMessage(namespace="test-runner", msg_type=self.msg_type, payload={"foo": "bar"})

        # now do happy path
        sent_user_msg = publish_notification_to_user(self.test_user_id, msg)

        # now mark msg as read by this user
        mark_notification_read(self.test_user_id, sent_user_msg.msg.id)

        # shouldn't be counted in unread counts
        self.assertEquals(
            get_notifications_count_for_user(self.test_user_id, filters={"read": False, "unread": True}), 0
        )

        # Should be counted in read counts
        self.assertEquals(
            get_notifications_count_for_user(self.test_user_id, filters={"read": True, "unread": False}), 1
        )

        # now mark msg as unread by this user
        mark_notification_read(self.test_user_id, sent_user_msg.msg.id, read=False)

        # Should be counted in unread counts
        self.assertEquals(
            get_notifications_count_for_user(self.test_user_id, filters={"read": False, "unread": True}), 1
        )

        # Shouldn't be counted in read counts
        self.assertEquals(
            get_notifications_count_for_user(self.test_user_id, filters={"read": True, "unread": False}), 0
        )
Example #3
0
    def test_marking_read_state(self):
        """
        Verify proper behavior when marking notfications as read/unread
        """

        msg = NotificationMessage(namespace='test-runner',
                                  msg_type=self.msg_type,
                                  payload={'foo': 'bar'})

        # now do happy path
        sent_user_msg = publish_notification_to_user(self.test_user_id, msg)

        # now mark msg as read by this user
        mark_notification_read(self.test_user_id, sent_user_msg.msg.id)

        # shouldn't be counted in unread counts
        self.assertEquals(
            get_notifications_count_for_user(
                self.test_user_id,
                filters={
                    'read': False,
                    'unread': True,
                },
            ), 0)

        # Should be counted in read counts
        self.assertEquals(
            get_notifications_count_for_user(
                self.test_user_id,
                filters={
                    'read': True,
                    'unread': False,
                },
            ), 1)

        # now mark msg as unread by this user
        mark_notification_read(self.test_user_id,
                               sent_user_msg.msg.id,
                               read=False)

        # Should be counted in unread counts
        self.assertEquals(
            get_notifications_count_for_user(
                self.test_user_id,
                filters={
                    'read': False,
                    'unread': True,
                },
            ), 1)

        # Shouldn't be counted in read counts
        self.assertEquals(
            get_notifications_count_for_user(
                self.test_user_id,
                filters={
                    'read': True,
                    'unread': False,
                },
            ), 0)
Example #4
0
    def test_notification_count(self):
        """
        Simple test to make sure that we get the right count back after
        publishing a notification to this test user
        """

        msg = NotificationMessage(namespace='test-runner',
                                  msg_type=self.msg_type,
                                  payload={'foo': 'bar'})

        # publish
        user_msg = publish_notification_to_user(self.user.id, msg)
        self.assertIsNotNone(user_msg)

        url = reverse('edx_notifications.consumer.notifications.count')

        # now query API
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

        results = json.loads(response.content.decode('utf-8'))
        self.assertIn('count', results)
        self.assertEqual(results['count'], 1)

        # query just the unread
        response = self.client.get(url, {'read': False, 'unread': True})
        self.assertEqual(response.status_code, 200)

        results = json.loads(response.content.decode('utf-8'))
        self.assertIn('count', results)
        self.assertEqual(results['count'], 1)

        # query just the read, which should be 0
        response = self.client.get(url, {'read': True, 'unread': False})
        self.assertEqual(response.status_code, 200)

        results = json.loads(response.content.decode('utf-8'))
        self.assertIn('count', results)
        self.assertEqual(results['count'], 0)

        # now mark the message as read
        mark_notification_read(self.user.id, user_msg.msg.id)

        # query just the unread, should be 0
        response = self.client.get(url, {'read': False, 'unread': True})
        self.assertEqual(response.status_code, 200)

        results = json.loads(response.content.decode('utf-8'))
        self.assertIn('count', results)
        self.assertEqual(results['count'], 0)

        # query just the read, which should be 1
        response = self.client.get(url, {'read': True, 'unread': False})
        self.assertEqual(response.status_code, 200)

        results = json.loads(response.content.decode('utf-8'))
        self.assertIn('count', results)
        self.assertEqual(results['count'], 1)
Example #5
0
    def test_marking_invalid_msg_read(self):
        """
        Makes sure that we can't mark an invalid notification, e.g. someone elses
        """

        msg = NotificationMessage(namespace="test-runner", msg_type=self.msg_type, payload={"foo": "bar"})

        # publish that
        sent_user_msg = publish_notification_to_user(self.test_user_id, msg)

        with self.assertRaises(ItemNotFoundError):
            # this user doesn't have this notification!
            mark_notification_read(100, sent_user_msg.msg.id)
Example #6
0
    def test_marking_invalid_msg_read(self):
        """
        Makes sure that we can't mark an invalid notification, e.g. someone elses
        """

        msg = NotificationMessage(namespace='test-runner',
                                  msg_type=self.msg_type,
                                  payload={'foo': 'bar'})

        # publish that
        sent_user_msg = publish_notification_to_user(self.test_user_id, msg)

        with self.assertRaises(ItemNotFoundError):
            # this user doesn't have this notification!
            mark_notification_read(100, sent_user_msg.msg.id)
    def post(self, request, msg_id):
        """
        HTTP POST Handler which is used for such use-cases as 'mark as read'
        and 'mark as unread'
        """

        # make sure we only have expected parameter names and values
        if not self.validate_post_parameters(request):
            return Response({}, status.HTTP_400_BAD_REQUEST)

        if 'mark_as' in request.data:
            mark_as_read = request.data['mark_as'] == 'read'
            try:
                # this will raise an ItemNotFoundError if the user_id/msg_id combo
                # cannot be found
                mark_notification_read(int(request.user.id), int(msg_id), read=mark_as_read)
            except ItemNotFoundError:
                raise Http404()

        return Response([], status.HTTP_200_OK)
Example #8
0
    def post(self, request, msg_id):
        """
        HTTP POST Handler which is used for such use-cases as 'mark as read'
        and 'mark as unread'
        """

        # make sure we only have expected parameter names and values
        if not self.validate_post_parameters(request):
            return Response({}, status.HTTP_400_BAD_REQUEST)

        if 'mark_as' in request.DATA:
            mark_as_read = request.DATA['mark_as'] == 'read'
            try:
                # this will raise an ItemNotFoundError if the user_id/msg_id combo
                # cannot be found
                mark_notification_read(int(request.user.id), int(msg_id), read=mark_as_read)
            except ItemNotFoundError:
                raise Http404()

        return Response([], status.HTTP_200_OK)
    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_notification_count(self):
        """
        Simple test to make sure that we get the right count back after
        publishing a notification to this test user
        """

        msg = NotificationMessage(
            namespace='test-runner',
            msg_type=self.msg_type,
            payload={
                'foo': 'bar'
            }
        )

        # publish
        user_msg = publish_notification_to_user(self.user.id, msg)
        self.assertIsNotNone(user_msg)

        url = reverse('edx_notifications.consumer.notifications.count')

        # now query API
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

        results = json.loads(response.content)
        self.assertIn('count', results)
        self.assertEqual(results['count'], 1)

        # query just the unread
        response = self.client.get(url, {'read': False, 'unread': True})
        self.assertEqual(response.status_code, 200)

        results = json.loads(response.content)
        self.assertIn('count', results)
        self.assertEqual(results['count'], 1)

        # query just the read, which should be 0
        response = self.client.get(url, {'read': True, 'unread': False})
        self.assertEqual(response.status_code, 200)

        results = json.loads(response.content)
        self.assertIn('count', results)
        self.assertEqual(results['count'], 0)

        # now mark the message as read
        mark_notification_read(self.user.id, user_msg.msg.id)

        # query just the unread, should be 0
        response = self.client.get(url, {'read': False, 'unread': True})
        self.assertEqual(response.status_code, 200)

        results = json.loads(response.content)
        self.assertIn('count', results)
        self.assertEqual(results['count'], 0)

        # query just the read, which should be 1
        response = self.client.get(url, {'read': True, 'unread': False})
        self.assertEqual(response.status_code, 200)

        results = json.loads(response.content)
        self.assertIn('count', results)
        self.assertEqual(results['count'], 1)