コード例 #1
0
    def setUp(self):
        utils.cache_clear()
        self.user = utils.create_user()
        self.user2 = utils.create_user()
        self.category = utils.create_category()
        self.topic = utils.create_topic(self.category)
        self.comment = utils.create_comment(topic=self.topic)

        # comment notification
        self.topic_notification = TopicNotification.objects.create(
            user=self.user,
            topic=self.topic,
            comment=self.comment,
            is_active=True,
            action=COMMENT)
        self.topic_notification2 = TopicNotification.objects.create(
            user=self.user2,
            topic=self.topic,
            comment=self.comment,
            is_active=True,
            action=COMMENT)

        # subscription to topic
        self.topic2 = utils.create_topic(self.category)
        self.comment2 = utils.create_comment(topic=self.topic2)
        self.topic_subscrption = TopicNotification.objects.create(
            user=self.user,
            topic=self.topic2,
            comment=self.comment2,
            is_active=True)
コード例 #2
0
 def test_topic_notification_mark_all_as_read(self):
     TopicNotification.objects.all().delete()
     user2 = utils.create_user()
     topic = utils.create_topic(self.category)
     comment = utils.create_comment(topic=topic)
     TopicNotification.objects.create(user=user2,
                                      topic=topic,
                                      comment=comment,
                                      is_active=True,
                                      action=MENTION)
     topic0 = utils.create_topic(self.category)
     comment0 = utils.create_comment(topic=topic0)
     TopicNotification.objects.create(user=self.user,
                                      topic=topic0,
                                      comment=comment0,
                                      is_active=True,
                                      action=COMMENT)
     topic1 = utils.create_topic(self.category)
     comment1 = utils.create_comment(topic=topic1)
     TopicNotification.objects.create(user=self.user,
                                      topic=topic1,
                                      comment=comment1,
                                      is_active=True,
                                      action=COMMENT)
     topic2 = utils.create_topic(self.category)
     comment2 = utils.create_comment(topic=topic2)
     TopicNotification.objects.create(user=self.user,
                                      topic=topic2,
                                      comment=comment2,
                                      is_active=True,
                                      action=MENTION)
     self.assertEqual(
         TopicNotification.objects.filter(user=self.user,
                                          is_read=False).count(), 3)
     self.assertEqual(
         TopicNotification.objects.filter(user=user2,
                                          is_read=False).count(), 1)
     utils.login(self)
     response = self.client.post(
         reverse('spirit:topic:notification:mark-all-as-read'))
     self.assertRedirects(response,
                          reverse('spirit:topic:notification:index'),
                          status_code=302)
     self.assertEqual(
         TopicNotification.objects.filter(user=self.user,
                                          is_read=False).count(), 0)
     self.assertEqual(
         TopicNotification.objects.filter(user=user2,
                                          is_read=False).count(), 1)
コード例 #3
0
    def test_topic_notification_ajax_escape(self):
        """
        The receive username and topic title should be escaped
        """
        user = utils.create_user(username="******")
        topic = utils.create_topic(self.category,
                                   title="<tag>Have you met Ted?</tag>")
        notification = TopicNotification.objects.create(
            user=self.user,
            topic=topic,
            comment=utils.create_comment(topic=topic, user=user),
            is_active=True,
            action=COMMENT)

        utils.login(self)
        response = self.client.get(
            reverse('spirit:topic:notification:index-ajax'),
            HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        res = json.loads(response.content.decode('utf-8'))
        expected = {
            'user': "******",
            'action': notification.action,
            'title': "&lt;tag&gt;Have you met Ted?&lt;/tag&gt;",
            'url': notification.get_absolute_url(),
            'is_read': notification.is_read
        }
        self.assertDictEqual(res['n'][0], expected)
コード例 #4
0
    def test_topic_notification_ajax_order(self):
        """
        order by is_read=False first then by date
        """
        user = utils.create_user()

        for _ in range(10):
            topic = utils.create_topic(self.category, user=user)
            comment = utils.create_comment(topic=topic, user=user)
            TopicNotification.objects.create(user=self.user,
                                             topic=topic,
                                             comment=comment,
                                             is_active=True,
                                             action=COMMENT)

        TopicNotification.objects.filter(user=self.user).update(is_read=True)
        old_date = timezone.now() - datetime.timedelta(days=10)
        (TopicNotification.objects.filter(
            pk=self.topic_notification.pk).update(is_read=False,
                                                  date=old_date))

        utils.login(self)
        response = self.client.get(
            reverse('spirit:topic:notification:index-ajax'),
            HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        res = json.loads(response.content.decode('utf-8'))
        self.assertFalse(res['n'][0]['is_read'])
        self.assertTrue(res['n'][1]['is_read'])
コード例 #5
0
    def test_topic_viewed(self):
        """
        * Should update/create the comment bookmark
        * Should mark the topic notification as read
        * Should create or mark the topic (unread) as read
        * Should increase the view_counter
        """
        req = RequestFactory().get('/?page=1')
        req.user = self.user

        category = utils.create_category()
        topic = utils.create_topic(category=category, user=self.user)
        comment = utils.create_comment(topic=topic)
        notification = TopicNotification.objects.create(user=topic.user,
                                                        topic=topic,
                                                        comment=comment,
                                                        is_read=False)
        unread = TopicUnread.objects.create(user=topic.user,
                                            topic=topic,
                                            is_read=False)
        utils_topic.topic_viewed(req, topic)
        self.assertEqual(
            len(CommentBookmark.objects.filter(user=self.user, topic=topic)),
            1)
        self.assertTrue(
            TopicNotification.objects.get(pk=notification.pk).is_read)
        self.assertTrue(TopicUnread.objects.get(pk=unread.pk).is_read)
        self.assertEqual(Topic.objects.get(pk=topic.pk).view_count, 1)
コード例 #6
0
    def test_topic_notification_list_unread(self):
        """
        topic notification list
        """
        topic = utils.create_topic(self.category, user=self.user2)
        comment = utils.create_comment(topic=topic, user=self.user2)
        topic_notification = TopicNotification.objects.create(user=self.user,
                                                              topic=topic,
                                                              comment=comment,
                                                              is_active=True,
                                                              action=COMMENT)

        utils.login(self)
        response = self.client.get(
            reverse('spirit:topic:notification:index-unread'))
        self.assertEqual(list(response.context['page']),
                         [topic_notification, self.topic_notification])

        # fake next page
        response = self.client.get(
            reverse('spirit:topic:notification:index-unread'), {
                'p':
                to_page_key(value=topic_notification.date,
                            pk=topic_notification.pk)
            })
        self.assertEqual(list(response.context['page']), [
            self.topic_notification,
        ])
コード例 #7
0
    def test_notification_creation(self):
        """
        create notification
        """
        # Should be ready to suscribe (true)
        form = NotificationCreationForm()
        self.assertEqual(form.fields['is_active'].initial, True)

        category = utils.create_category()
        topic = utils.create_topic(category)
        comment = utils.create_comment(topic=topic)
        form_data = {
            'is_active': True,
        }
        form = NotificationCreationForm(data=form_data)
        form.user = self.user
        form.topic = topic
        self.assertEqual(form.is_valid(), True)

        TopicNotification.objects.create(user=self.user,
                                         topic=topic,
                                         comment=comment,
                                         is_active=True,
                                         action=COMMENT)
        form = NotificationCreationForm(data=form_data)
        form.user = self.user
        form.topic = topic
        self.assertEqual(form.is_valid(), False)
コード例 #8
0
    def test_topic_notification_notify_new_comment(self):
        """
        Should set is_read=False to all notifiers/users
        """
        creator = utils.create_user()
        subscriber = utils.create_user()
        topic = utils.create_topic(self.category)
        comment = utils.create_comment(user=creator, topic=topic)
        TopicNotification.objects.create(user=creator,
                                         topic=topic,
                                         comment=comment,
                                         is_active=True,
                                         is_read=True)
        TopicNotification.objects.create(user=subscriber,
                                         topic=topic,
                                         comment=comment,
                                         is_active=True,
                                         is_read=True)

        TopicNotification.notify_new_comment(comment)
        notification = TopicNotification.objects.get(user=subscriber,
                                                     topic=topic)
        self.assertTrue(notification.is_active)
        self.assertFalse(notification.is_read)
        self.assertEqual(notification.action, COMMENT)

        # Author should not be notified of its own comment
        notification2 = TopicNotification.objects.get(user=creator,
                                                      topic=topic)
        self.assertTrue(notification2.is_read)
コード例 #9
0
    def test_topic_notification_create_has_access(self):
        """
        create notification for private topic if user has access
        """
        TopicNotification.objects.all().delete()
        private = utils.create_private_topic(user=self.user)
        utils.create_comment(topic=private.topic)

        utils.login(self)
        form_data = {
            'is_active': True,
        }
        response = self.client.post(
            reverse('spirit:topic:notification:create',
                    kwargs={
                        'topic_id': private.topic.pk,
                    }), form_data)
        self.assertRedirects(response,
                             private.topic.get_absolute_url(),
                             status_code=302)
        self.assertEqual(len(TopicNotification.objects.all()), 1)
コード例 #10
0
    def test_topic_detail_view_paginate(self):
        """
        should display topic with comments, page 1
        """
        utils.login(self)
        category = utils.create_category()

        topic = utils.create_topic(category=category)

        comment1 = utils.create_comment(topic=topic)
        comment2 = utils.create_comment(topic=topic)
        utils.create_comment(topic=topic)  # comment3

        response = self.client.get(
            reverse('spirit:topic:detail',
                    kwargs={
                        'pk': topic.pk,
                        'slug': topic.slug
                    }))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(list(response.context['comments']),
                         [comment1, comment2])
コード例 #11
0
    def setUp(self):
        utils.cache_clear()
        self.user = utils.create_user()
        self.category = utils.create_category()
        self.topic = utils.create_topic(self.category)
        self.comment = utils.create_comment(topic=self.topic)

        self.topic_notification = TopicNotification.objects.create(
            user=self.user,
            topic=self.topic,
            comment=self.comment,
            is_active=True,
            action=COMMENT)
コード例 #12
0
 def test_topic_notification_mark_as_read(self):
     """
     Mark notification as read
     """
     private = utils.create_private_topic()
     comment = utils.create_comment(topic=private.topic)
     TopicNotification.objects.create(user=private.user,
                                      topic=private.topic,
                                      comment=comment,
                                      is_read=False)
     TopicNotification.mark_as_read(user=private.user, topic=private.topic)
     notification = TopicNotification.objects.get(user=private.user,
                                                  topic=private.topic)
     self.assertTrue(notification.is_read)
コード例 #13
0
 def test_topic_notification_notify_new_mentions_unactive(self):
     """
     set is_read=False when user gets mentioned
     even if is_active=False
     """
     (TopicNotification.objects.filter(
         pk=self.topic_notification.pk).update(is_active=False))
     mentions = {self.user.username: self.user}
     comment = utils.create_comment(topic=self.topic_notification.topic)
     TopicNotification.notify_new_mentions(comment=comment,
                                           mentions=mentions)
     self.assertEqual(
         TopicNotification.objects.get(
             pk=self.topic_notification.pk).action, MENTION)
     self.assertFalse(
         TopicNotification.objects.get(
             pk=self.topic_notification.pk).is_read)
コード例 #14
0
    def test_topic_private_bulk_create(self):
        """
        Create notifications for a bunch of users
        """
        TopicNotification.objects.all().delete()
        user = utils.create_user()
        user2 = utils.create_user()
        topic = utils.create_topic(self.category)
        comment = utils.create_comment(topic=topic)
        TopicNotification.bulk_create(users=[user, user2], comment=comment)
        self.assertEqual(len(TopicNotification.objects.all()), 2)

        notification = TopicNotification.objects.get(user=user,
                                                     topic=comment.topic)
        self.assertTrue(notification.is_active)
        self.assertFalse(notification.is_read)
        self.assertEqual(notification.comment, comment)
コード例 #15
0
    def test_topic_notification_list_paginate(self):
        """
        topic notification list paginated
        """
        topic2 = utils.create_topic(self.category)
        comment2 = utils.create_comment(topic=topic2)
        topic_notification2 = TopicNotification.objects.create(
            user=self.user,
            topic=topic2,
            comment=comment2,
            is_active=True,
            action=COMMENT)

        utils.login(self)
        response = self.client.get(reverse('spirit:topic:notification:index'))
        self.assertEqual(list(response.context['notifications']), [
            topic_notification2,
        ])
コード例 #16
0
 def test_topic_notification_notify_new_mentions(self):
     """
     Should notify mentions
     """
     topic = utils.create_topic(self.category)
     mentions = {self.user.username: self.user}
     comment = utils.create_comment(topic=topic)
     TopicNotification.notify_new_mentions(comment=comment,
                                           mentions=mentions)
     self.assertEqual(
         TopicNotification.objects.get(user=self.user,
                                       comment=comment).action, MENTION)
     self.assertFalse(
         TopicNotification.objects.get(user=self.user,
                                       comment=comment).is_read)
     self.assertTrue(
         TopicNotification.objects.get(user=self.user,
                                       comment=comment).is_active)
コード例 #17
0
    def test_topic_notification_notify_new_comment_unactive(self):
        """
        Should do nothing if notification is unactive
        """
        creator = utils.create_user()
        subscriber = utils.create_user()
        topic = utils.create_topic(self.category)
        comment = utils.create_comment(user=creator, topic=topic)
        TopicNotification.objects.create(user=subscriber,
                                         topic=topic,
                                         comment=comment,
                                         is_active=False,
                                         is_read=True)

        TopicNotification.notify_new_comment(comment)
        notification = TopicNotification.objects.get(user=subscriber,
                                                     topic=topic)
        self.assertTrue(notification.is_read)
コード例 #18
0
    def test_notification(self):
        """
        update notification
        """
        category = utils.create_category()
        topic = utils.create_topic(category)
        comment = utils.create_comment(topic=topic)
        notification = TopicNotification.objects.create(user=self.user,
                                                        topic=topic,
                                                        comment=comment,
                                                        is_active=True,
                                                        action=COMMENT)

        form_data = {
            'is_active': True,
        }
        form = NotificationForm(data=form_data, instance=notification)
        self.assertEqual(form.is_valid(), True)
コード例 #19
0
    def test_topic_notification_create_maybe(self):
        """
        Should create a notification if does not exists
        """
        user = utils.create_user()
        topic = utils.create_topic(self.category)
        comment = utils.create_comment(topic=topic)
        TopicNotification.create_maybe(user=user, comment=comment)
        notification = TopicNotification.objects.get(user=user, topic=topic)
        self.assertTrue(notification.is_active)
        self.assertTrue(notification.is_read)
        self.assertEqual(notification.action, COMMENT)

        # Creating it again should do nothing
        (TopicNotification.objects.filter(user=user,
                                          topic=topic).update(is_active=False))
        TopicNotification.create_maybe(user=user, comment=comment)
        self.assertFalse(
            TopicNotification.objects.get(user=user, topic=topic).is_active)
コード例 #20
0
    def test_topic_notification_ajax_limit(self):
        """
        get first N notifications
        """
        user = utils.create_user()
        topic = utils.create_topic(self.category, user=user)
        comment = utils.create_comment(topic=topic, user=user)
        TopicNotification.objects.create(user=self.user,
                                         topic=topic,
                                         comment=comment,
                                         is_active=True,
                                         action=COMMENT)

        utils.login(self)
        response = self.client.get(
            reverse('spirit:topic:notification:index-ajax'),
            HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        res = json.loads(response.content.decode('utf-8'))
        self.assertGreater(
            TopicNotification.objects.filter(user=self.user).count(), 1)
        self.assertEqual(len(res['n']), 1)