示例#1
0
    def test_mark_read_a_topic_from_view_list_posts(self):
        """
        To ensure we can subscribe to a topic, we first generate a new notification when
        another user posts a message, then mark the notification as read after displaying
        the list of messages.
        """
        topic = TopicFactory(forum=self.forum11, author=self.user2)
        PostFactory(topic=topic, author=self.user2, position=1)

        # Follow the topic.
        self.assertIsNone(TopicAnswerSubscription.objects.get_existing(self.user1, topic))
        subscription = TopicAnswerSubscription.objects.get_or_create_active(self.user1, topic)

        # Creates a new post in the topic to generate a new notification.
        PostFactory(topic=topic, author=self.user2, position=1)
        content_notification_type = ContentType.objects.get_for_model(topic.last_message)
        notification = Notification.objects.get(
            subscription=subscription,
            content_type__pk=content_notification_type.pk,
            object_id=topic.last_message.pk,
            is_read=False,
        )
        self.assertIsNotNone(notification)

        response = self.client.get(reverse("topic-posts-list", args=[topic.pk, topic.slug()]))
        self.assertEqual(response.status_code, 200)

        # Checks that the notification is reading now.
        notification = Notification.objects.get(
            subscription=subscription,
            content_type__pk=content_notification_type.pk,
            object_id=topic.last_message.pk,
            is_read=True,
        )
        self.assertIsNotNone(notification)
示例#2
0
    def test_remove_subscribed_tag(self):
        """
        When the topic is edited and a tag is added to which the user has subscribed
        """
        NewTopicSubscription.objects.toggle_follow(self.tag1, self.user2)

        topic = TopicFactory(forum=self.forum11, author=self.user1)
        topic.add_tags(["Linux"])
        PostFactory(topic=topic, author=self.user1, position=1)

        notifications = Notification.objects.filter(object_id=topic.pk,
                                                    is_read=False).all()
        self.assertEqual(1, len(notifications))

        self.client.post(
            reverse("topic-edit") + f"?topic={topic.pk}",
            {
                "title": "Un autre sujet",
                "subtitle": "Encore ces lombards en plein été",
                "text":
                "C'est tout simplement l'histoire de la ville de Paris que je voudrais vous conter ",
                "tags": "Windows",
            },
            follow=False,
        )

        self.assertEqual(
            1,
            len(
                Notification.objects.filter(object_id=topic.pk,
                                            is_read=False,
                                            is_dead=True).all()))
示例#3
0
    def test_move_topic_from_forum_followed_to_forum_followed_too(self):
        NewTopicSubscription.objects.toggle_follow(self.forum11, self.user1)
        NewTopicSubscription.objects.toggle_follow(self.forum12, self.user1)

        topic = TopicFactory(forum=self.forum11, author=self.user2)
        PostFactory(topic=topic, author=self.user2, position=1)
        self.assertEqual(1, len(Notification.objects.filter(object_id=topic.pk, is_read=False).all()))

        # Move the topic to another forum.
        self.client.logout()
        staff = StaffProfileFactory()
        self.client.force_login(staff.user)
        data = {"move": "", "forum": self.forum12.pk, "topic": topic.pk}
        response = self.client.post(reverse("topic-edit"), data, follow=False)
        self.assertEqual(302, response.status_code)

        topic = Topic.objects.get(pk=topic.pk)
        self.assertEqual(self.forum12, topic.forum)
        self.assertEqual(1, len(Notification.objects.filter(object_id=topic.pk, is_read=False, is_dead=False).all()))

        self.client.logout()
        self.client.force_login(self.user1)
        response = self.client.get(reverse("topic-posts-list", args=[topic.pk, topic.slug()]))
        self.assertEqual(200, response.status_code)

        self.assertEqual(1, len(Notification.objects.filter(object_id=topic.pk, is_read=True, is_dead=False).all()))
示例#4
0
    def test_pubdate_on_notification_updated(self):
        """
        When we update a notification, we should update its pubdate too.
        """
        topic = TopicFactory(forum=self.forum11, author=self.user1)
        PostFactory(topic=topic, author=self.user1, position=1)

        topics_followed = TopicAnswerSubscription.objects.get_objects_followed_by(
            self.user1)
        self.assertEqual(1, len(topics_followed))

        post = PostFactory(topic=topic, author=self.user2, position=2)

        old_notification = Notification.objects.get(
            subscription__user=self.user1, object_id=post.pk, is_read=False)
        old_notification.pubdate = datetime.now() - timedelta(days=1)
        old_notification.save()
        self.assertEqual(old_notification.object_id, post.pk)
        self.assertEqual(old_notification.subscription.object_id, topic.pk)

        # read it.
        old_notification.is_read = True
        old_notification.save()

        user3 = UserFactory()
        post2 = PostFactory(topic=topic, author=user3, position=3)

        new_notification = Notification.objects.get(
            subscription__user=self.user1, object_id=post2.pk, is_read=False)
        self.assertEqual(new_notification.object_id, post2.pk)
        self.assertEqual(new_notification.subscription.object_id, topic.pk)

        # Check that the pubdate is well updated.
        self.assertTrue(old_notification.pubdate < new_notification.pubdate)
示例#5
0
    def test_answer_topic(self):
        """
        When a user posts on a topic, a subscription to the said topic is created
        for this user.
        """
        topic1 = TopicFactory(forum=self.forum11, author=self.user2)
        PostFactory(topic=topic1, author=self.user2, position=1)

        result = self.client.post(
            reverse("post-new") + f"?sujet={topic1.pk}",
            {
                "last_post": topic1.last_message.pk,
                "text": "C'est tout simplement l'histoire de la ville de Paris que je voudrais vous conter ",
            },
            follow=False,
        )

        self.assertEqual(result.status_code, 302)

        # check that topic creator has been notified
        notification = Notification.objects.get(subscription__user=self.user2)
        subscription_content_type = ContentType.objects.get_for_model(topic1)

        self.assertEqual(notification.is_read, False)
        self.assertEqual(notification.subscription.content_type, subscription_content_type)
        self.assertEqual(notification.subscription.object_id, topic1.pk)

        # check that answerer has subscribed to the topic
        subscription = TopicAnswerSubscription.objects.get(
            object_id=topic1.pk, content_type__pk=subscription_content_type.pk, user=self.user1
        )
        self.assertTrue(subscription.is_active)
示例#6
0
    def test_hide_post_mark_notification_as_read(self):
        """
        Ensure a notification gets deleted when the corresponding post is hidden.
        """
        topic = TopicFactory(forum=self.forum11, author=self.user1)
        PostFactory(topic=topic, author=self.user1, position=1)
        PostFactory(topic=topic, author=self.user2, position=2)
        PostFactory(topic=topic, author=ProfileFactory().user, position=3)

        notifications = Notification.objects.filter(
            object_id=topic.last_message.pk, is_read=False).all()
        self.assertEqual(1, len(notifications))

        # hide last post
        data = {"delete_message": ""}
        self.client.force_login(StaffProfileFactory().user)
        response = self.client.post(reverse("post-edit") +
                                    f"?message={topic.last_message.pk}",
                                    data,
                                    follow=False)
        self.assertEqual(302, response.status_code)

        notifications = Notification.objects.filter(
            object_id=topic.last_message.pk, is_read=True).all()
        self.assertEqual(1, len(notifications))
示例#7
0
    def test_notification_read(self):
        """
        When we post on a topic, a notification is created for each subscriber. We can
        read a notification when we display the list of messages of the said topic.
        """
        topic1 = TopicFactory(forum=self.forum11, author=self.user2)
        PostFactory(topic=topic1, author=self.user2, position=1)

        result = self.client.post(
            reverse("post-new") + f"?sujet={topic1.pk}",
            {
                "last_post":
                topic1.last_message.pk,
                "text":
                "C'est tout simplement l'histoire de la ville de Paris que je voudrais vous conter ",
            },
            follow=False,
        )

        self.assertEqual(result.status_code, 302)

        notification = Notification.objects.get(subscription__user=self.user2)
        self.assertEqual(notification.is_read, False)

        self.client.logout()
        self.client.force_login(self.user2)

        result = self.client.get(reverse(
            "topic-posts-list", args=[topic1.pk,
                                      old_slugify(topic1.title)]),
                                 follow=True)
        self.assertEqual(result.status_code, 200)

        notification = Notification.objects.get(subscription__user=self.user2)
        self.assertEqual(notification.is_read, True)
示例#8
0
    def test_subscription_deactivated_and_notification_read_when_topic_moved(self):
        """
        When a topic is moved to a forum where subscribers can't read it, the subscriptions
        should be deactivated and notifications marked as read.
        """
        topic = TopicFactory(forum=self.forum11, author=self.user1)
        PostFactory(topic=topic, author=self.user1, position=1)
        other_user = ProfileFactory().user
        TopicAnswerSubscription.objects.toggle_follow(topic, other_user)
        PostFactory(topic=topic, author=ProfileFactory().user, position=2)

        self.assertIsNotNone(TopicAnswerSubscription.objects.get_existing(self.user1, topic, is_active=True))
        self.assertIsNotNone(Notification.objects.get(subscription__user=self.user1, is_read=False))

        forum_not_read = ForumFactory(category=self.category1, position_in_category=2)
        forum_not_read.groups.add(Group.objects.create(name="DummyGroup_1"))

        self.client.force_login(StaffProfileFactory().user)
        data = {"move": "", "forum": forum_not_read.pk, "topic": topic.pk}
        response = self.client.post(reverse("topic-edit"), data, follow=False)

        self.assertEqual(302, response.status_code)
        self.assertIsNotNone(TopicAnswerSubscription.objects.get_existing(self.user1, topic, is_active=False))
        self.assertIsNotNone(Notification.objects.get(subscription__user=self.user1, is_read=True))
        self.assertFalse(TopicAnswerSubscription.objects.get_existing(other_user, topic).is_active)
        self.assertIsNotNone(Notification.objects.get(subscription__user=other_user, is_read=True))
示例#9
0
 def test_get_topic_count(self):
     # Start with 0
     self.assertEqual(self.user1.get_topic_count(), 0)
     # Create Topic !
     TopicFactory(forum=self.forum, author=self.user1.user)
     # Should be 1
     self.assertEqual(self.user1.get_topic_count(), 1)
示例#10
0
    def test_mark_read_a_topic_of_a_forum_subscribed(self):
        """
        When a user has a notification on a topic, the notification should be marked as read.
        """
        NewTopicSubscription.objects.toggle_follow(self.forum11, self.user1)

        topic = TopicFactory(forum=self.forum11, author=self.user2)
        PostFactory(topic=topic, author=self.user2, position=1)
        notifications = Notification.objects.filter(object_id=topic.pk, is_read=False).all()
        self.assertEqual(1, len(notifications))

        response = self.client.get(reverse("topic-posts-list", args=[topic.pk, topic.slug()]))
        self.assertEqual(response.status_code, 200)

        notifications = Notification.objects.filter(object_id=topic.pk, is_read=False).all()
        self.assertEqual(0, len(notifications))
示例#11
0
    def test_get_similar_topics(self):
        """Get similar topics lists"""

        if not self.manager.connected_to_es:
            return

        text = "Clem ne se mange pas"

        topic_1 = TopicFactory(forum=self.forum, author=self.user, title=text)
        post_1 = PostFactory(topic=topic_1, author=self.user, position=1)
        post_1.text = post_1.text_html = text
        post_1.save()

        text = "Clem est la meilleure mascotte"

        topic_2 = TopicFactory(forum=self.forum, author=self.user, title=text)
        post_2 = PostFactory(topic=topic_2, author=self.user, position=1)
        post_2.text = post_1.text_html = text
        post_2.save()

        # 1. Should not get any result
        result = self.client.get(reverse("search:similar") + "?q=est",
                                 follow=False)
        self.assertEqual(result.status_code, 200)
        content = json_handler.loads(result.content.decode("utf-8"))
        self.assertEqual(len(content["results"]), 0)

        # index
        for model in self.indexable:
            if model is FakeChapter:
                continue
            self.manager.es_bulk_indexing_of_model(model)
        self.manager.refresh_index()

        # 2. Should get exactly one result
        result = self.client.get(reverse("search:similar") + "?q=mange",
                                 follow=False)
        self.assertEqual(result.status_code, 200)
        content = json_handler.loads(result.content.decode("utf-8"))
        self.assertEqual(len(content["results"]), 1)

        # 2. Should get exactly two results
        result = self.client.get(reverse("search:similar") + "?q=Clem",
                                 follow=False)
        self.assertEqual(result.status_code, 200)
        content = json_handler.loads(result.content.decode("utf-8"))
        self.assertEqual(len(content["results"]), 2)
示例#12
0
    def test_hidden_forums_give_no_results_if_user_not_allowed(self):
        """Long name, isn't ?"""

        if not self.manager.connected_to_es:
            return

        # 1. Create a hidden forum belonging to a hidden staff group.
        text = "test"

        group = Group.objects.create(name="Les illuminatis anonymes de ZdS")
        _, hidden_forum = create_category_and_forum(group)

        self.staff.groups.add(group)
        self.staff.save()

        topic_1 = TopicFactory(forum=hidden_forum,
                               author=self.staff,
                               title=text)
        post_1 = PostFactory(topic=topic_1, author=self.user, position=1)
        post_1.text = post_1.text_html = text
        post_1.save()

        self.manager.es_bulk_indexing_of_model(Topic)
        self.manager.es_bulk_indexing_of_model(Post)
        self.manager.refresh_index()

        self.assertEqual(
            len(
                self.manager.setup_search(Search().query(
                    MatchAll())).execute()), 2)  # indexing ok

        # 2. search without connection and get not result
        result = self.client.get(reverse("search:query") + "?q=" + text,
                                 follow=False)

        self.assertEqual(result.status_code, 200)
        response = result.context["object_list"].execute()
        self.assertEqual(response.hits.total, 0)

        # 3. Connect with user (not a member of the group), search, and get no result
        self.client.force_login(self.user)

        result = self.client.get(reverse("search:query") + "?q=" + text,
                                 follow=False)

        self.assertEqual(result.status_code, 200)
        response = result.context["object_list"].execute()
        self.assertEqual(response.hits.total, 0)

        # 4. Connect with staff, search, and get the topic and the post
        self.client.logout()
        self.client.force_login(self.staff)

        result = self.client.get(reverse("search:query") + "?q=" + text,
                                 follow=False)

        self.assertEqual(result.status_code, 200)
        response = result.context["object_list"].execute()
        self.assertEqual(response.hits.total, 2)  # ok !
示例#13
0
    def test_notifications_on_a_forum_subscribed(self):
        """
        When a user subscribes to a forum, they receive a notification for each topic created.
        """
        # Subscribe.
        NewTopicSubscription.objects.toggle_follow(self.forum11, self.user1)

        topic = TopicFactory(forum=self.forum11, author=self.user2)
        notifications = Notification.objects.filter(object_id=topic.pk, is_read=False).all()
        self.assertEqual(1, len(notifications))

        # Unsubscribe.
        NewTopicSubscription.objects.toggle_follow(self.forum11, self.user1)

        topic = TopicFactory(forum=self.forum11, author=self.user2)
        notifications = Notification.objects.filter(object_id=topic.pk, is_read=False).all()
        self.assertEqual(0, len(notifications))
示例#14
0
    def setUp(self):
        self.user1 = ProfileFactory()
        self.staff = StaffProfileFactory()

        # Create a forum for later test
        self.forumcat = ForumCategoryFactory()
        self.forum = ForumFactory(category=self.forumcat)
        self.forumtopic = TopicFactory(forum=self.forum,
                                       author=self.staff.user)
示例#15
0
    def test_topics_followed_by_a_user(self):
        """
        Check that we correctly retrieve all topics followed by a user.
        """
        user = UserFactory()

        topics_followed = TopicAnswerSubscription.objects.get_objects_followed_by(user)
        self.assertEqual(0, len(topics_followed))

        first = TopicFactory(forum=self.forum11, author=user)
        second = TopicFactory(forum=self.forum11, author=user)
        third = TopicFactory(forum=self.forum11, author=user)

        # Subscribes to all topics.
        TopicAnswerSubscription.objects.get_or_create_active(user, second)
        TopicAnswerSubscription.objects.get_or_create_active(user, first)
        TopicAnswerSubscription.objects.get_or_create_active(user, third)

        topics_followed = TopicAnswerSubscription.objects.get_objects_followed_by(user)
        self.assertEqual(3, len(topics_followed))
示例#16
0
    def _generate(cls, create, attrs):
        # This parameter is only used inside _generate() and won't be saved in the database,
        # which is why we use attrs.pop() (it is removed from attrs).
        beta_forum = attrs.pop("forum", None)

        # Creates the PublishableContent (see PublishableContentFactory._generate() for more info)
        publishable_content = super()._generate(create, attrs)

        if publishable_content.authors.count() > 0 and beta_forum is not None:
            beta_topic = TopicFactory(
                title="[beta]" + publishable_content.title,
                author=publishable_content.authors.first(),
                forum=beta_forum)
            publishable_content.sha_beta = publishable_content.sha_draft
            publishable_content.beta_topic = beta_topic
            publishable_content.save()
            PostFactory(topic=beta_topic,
                        position=1,
                        author=publishable_content.authors.first())
            beta_topic.save()
        return publishable_content
示例#17
0
    def setUp(self):
        # prepare a user and 2 Topic (with and without tags)

        settings.EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend"

        self.category1 = ForumCategoryFactory(position=1)
        self.forum = ForumFactory(category=self.category1,
                                  position_in_category=1)
        self.forum2 = ForumFactory(category=self.category1,
                                   position_in_category=2)

        self.user = ProfileFactory().user
        self.client.force_login(self.user)

        self.tag = TagFactory()
        self.topic1 = TopicFactory(forum=self.forum, author=self.user)
        self.topic2 = TopicFactory(forum=self.forum2, author=self.user)
        self.topic2.tags.add(self.tag)
        self.topic2.save()

        self.topicfeed = LastTopicsFeedRSS()
示例#18
0
    def test_no_duplicate_subscription(self):
        """
        Creating two same subscriptions is rejected by the database.
        """
        category = ForumCategoryFactory(position=1)
        forum = ForumFactory(category=category, position_in_category=1)
        topic = TopicFactory(forum=forum, author=self.user1)
        TopicAnswerSubscription.objects.toggle_follow(topic, self.user1, True)

        subscription = TopicAnswerSubscription(user=self.user1, content_object=topic)
        with self.assertRaises(IntegrityError):
            subscription.save()
示例#19
0
    def test_no_notification_on_a_tag_subscribed_in_hidden_forum(self):
        """
        When a user subscribes to a tag and a topic is created using that tag in a hidden forum, no notification is sent
        """
        # Subscribe.
        user1 = ProfileFactory().user
        user2 = ProfileFactory().user

        group = Group.objects.create(name="Restricted")
        user2.groups.add(group)
        user2.save()
        category, forum = create_category_and_forum(group)

        tag1 = TagFactory(title="MyTagInHiddenForum")
        NewTopicSubscription.objects.toggle_follow(tag1, user1)

        topic1 = TopicFactory(forum=forum, author=user2)
        topic1.add_tags([tag1.title])

        notifications = Notification.objects.filter(object_id=topic1.pk,
                                                    is_read=False).all()
        self.assertEqual(0, len(notifications))
示例#20
0
    def setUp(self):
        # prepare a user and 2 Topic (with and without tags)

        settings.EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend"

        self.category1 = ForumCategoryFactory(position=1)
        self.forum = ForumFactory(category=self.category1,
                                  position_in_category=1)
        self.forum2 = ForumFactory(category=self.category1,
                                   position_in_category=2)
        self.forum3 = ForumFactory(category=self.category1,
                                   position_in_category=3)

        self.user = ProfileFactory().user
        self.client.force_login(self.user)

        self.tag = TagFactory()
        self.topic1 = TopicFactory(forum=self.forum, author=self.user)
        self.topic2 = TopicFactory(forum=self.forum2, author=self.user)
        self.topic2.tags.add(self.tag)
        self.topic2.save()

        # create 2 posts un each forum
        PostFactory(topic=self.topic1, author=self.user, position=1)
        PostFactory(topic=self.topic1, author=self.user, position=2)
        PostFactory(topic=self.topic2, author=self.user, position=1)
        PostFactory(topic=self.topic2, author=self.user, position=2)

        # and last topic + post alone
        self.tag2 = TagFactory()
        self.topic3 = TopicFactory(forum=self.forum3, author=self.user)
        self.post3 = PostFactory(topic=self.topic3,
                                 author=self.user,
                                 position=1)
        self.topic3.tags.add(self.tag2)
        self.topic3.save()

        self.postfeed = LastPostsFeedRSS()
示例#21
0
 def test_success_initial_content_topic(self):
     author = ProfileFactory().user
     category = ForumCategoryFactory(position=1)
     forum = ForumFactory(category=category, position_in_category=1)
     topic = TopicFactory(forum=forum, author=author)
     staff = StaffProfileFactory()
     self.client.force_login(staff.user)
     response = self.client.get(
         "{}?content_type=topic&content_id={}".format(reverse("featured-resource-create"), topic.id)
     )
     initial_dict = response.context["form"].initial
     self.assertEqual(initial_dict["title"], topic.title)
     self.assertEqual(initial_dict["authors"], str(author))
     self.assertEqual(initial_dict["type"], _("Un sujet"))
     self.assertEqual(initial_dict["url"], f"http://testserver{topic.get_absolute_url()}")
示例#22
0
def load_topics(cli, size, fake, *_, **__):
    """
    Load topics
    """
    nb_topics = size * 10
    cli.stdout.write(f"Nombres de Topics à créer : {nb_topics}")
    tps1 = time.time()
    nb_forums = Forum.objects.count()
    if nb_forums == 0:
        cli.stdout.write(
            "Il n'y a aucun forum actuellement. " "Vous devez rajouter les forums dans vos fixtures (forum)"
        )
        return
    forums = list(Forum.objects.all())
    nb_users = User.objects.count()
    if nb_users == 0:
        cli.stdout.write(
            "Il n'y a aucun membre actuellement. " "Vous devez rajouter les membres dans vos fixtures (member)"
        )
        return
    profiles = list(Profile.objects.all())
    nb_tags = Tag.objects.count()
    if nb_tags == 0:
        cli.stdout.write("Il n'y a aucun tag actuellement. " "Vous devez rajouter les tags dans vos fixtures (tag)")
        return
    for i in range(0, nb_topics):
        with contextlib.suppress(IntegrityError):
            topic = TopicFactory(forum=forums[i % nb_forums], author=profiles[i % nb_users].user)
            topic.solved_by = profiles[i % nb_users].user if i % 5 else None
            topic.is_locked = i % 10 == 0
            topic.is_sticky = i % 15 == 0
            nb_rand_tags = random.randint(0, 5)
            add_generated_tags_to_topic(nb_rand_tags, nb_tags, topic)
            topic.title = fake.text(max_nb_chars=80)
            topic.subtitle = fake.text(max_nb_chars=200)
            topic.save()
            PostFactory(topic=topic, author=topic.author, position=1)
        sys.stdout.write(f" Topic {i + 1}/{nb_topics}  \r")
        sys.stdout.flush()
    tps2 = time.time()
    cli.stdout.write(f"\nFait en {tps2 - tps1} sec")
示例#23
0
    def test_hidden_post_are_not_result(self):
        """Hidden posts should not show up in the search results"""

        if not self.manager.connected_to_es:
            return

        # 1. Index and test search:
        text = "test"

        topic_1 = TopicFactory(forum=self.forum, author=self.user, title=text)
        post_1 = PostFactory(topic=topic_1, author=self.user, position=1)
        post_1.text = post_1.text_html = text
        post_1.save()

        self.manager.es_bulk_indexing_of_model(Topic)
        self.manager.es_bulk_indexing_of_model(Post)
        self.manager.refresh_index()

        self.assertEqual(
            len(
                self.manager.setup_search(Search().query(
                    MatchAll())).execute()), 2)  # indexing ok

        post_1 = Post.objects.get(pk=post_1.pk)

        result = self.client.get(reverse("search:query") + "?q=" + text +
                                 "&models=" + Post.get_es_document_type(),
                                 follow=False)

        self.assertEqual(result.status_code, 200)

        response = result.context["object_list"].execute()

        self.assertEqual(response.hits.total, 1)
        self.assertEqual(response[0].meta.id, post_1.es_id)

        # 2. Hide, reindex and search again:
        post_1.hide_comment_by_user(self.staff,
                                    "Un abus de pouvoir comme un autre ;)")
        self.manager.refresh_index()

        result = self.client.get(reverse("search:query") + "?q=" + text +
                                 "&models=" + Post.get_es_document_type(),
                                 follow=False)

        self.assertEqual(result.status_code, 200)
        response = result.context["object_list"].execute()
        self.assertEqual(response.hits.total, 0)  # nothing in the results
示例#24
0
    def test_post_unread(self):
        """
        When a post is marked unread, a notification is generated.
        """
        topic1 = TopicFactory(forum=self.forum11, author=self.user2)
        PostFactory(topic=topic1, author=self.user2, position=1)
        PostFactory(topic=topic1, author=self.user1, position=2)
        post = PostFactory(topic=topic1, author=self.user2, position=3)

        result = self.client.get(reverse("post-unread") + f"?message={post.pk}", follow=False)

        self.assertEqual(result.status_code, 302)

        notification = Notification.objects.get(subscription__user=self.user1, object_id=post.pk, is_read=False)
        self.assertEqual(notification.object_id, post.pk)
        self.assertEqual(notification.subscription.object_id, topic1.pk)
示例#25
0
    def test_update(self):
        # create topic and content and toggle request
        author = ProfileFactory().user
        category = ForumCategoryFactory(position=1)
        forum = ForumFactory(category=category, position_in_category=1)
        topic = TopicFactory(forum=forum, author=author)

        FeaturedRequested.objects.toogle_request(topic, author)

        # ignore
        staff = StaffProfileFactory()
        self.client.force_login(staff.user)

        content_type = ContentType.objects.get_for_model(topic)
        q = FeaturedRequested.objects.get(object_id=topic.pk, content_type__pk=content_type.pk)
        self.assertFalse(q.rejected)

        response = self.client.post(
            reverse("featured-resource-request-update", kwargs={"pk": q.pk}), {"operation": "REJECT"}, follow=False
        )
        self.assertEqual(200, response.status_code)

        q = FeaturedRequested.objects.get(pk=q.pk)
        self.assertTrue(q.rejected)
        self.assertFalse(q.rejected_for_good)

        response = self.client.post(
            reverse("featured-resource-request-update", kwargs={"pk": q.pk}), {"operation": "CONSIDER"}, follow=False
        )
        self.assertEqual(200, response.status_code)

        q = FeaturedRequested.objects.get(pk=q.pk)
        self.assertFalse(q.rejected)

        response = self.client.post(
            reverse("featured-resource-request-update", kwargs={"pk": q.pk}),
            {"operation": "REJECT_FOR_GOOD"},
            follow=False,
        )
        self.assertEqual(200, response.status_code)

        q = FeaturedRequested.objects.get(pk=q.pk)
        self.assertTrue(q.rejected)
        self.assertTrue(q.rejected_for_good)
示例#26
0
    def test_mark_all_notifications_as_read_when_toggle_follow(self):
        """
        When a user unsubscribes from a content, we mark all notifications for
        this content as read.
        """
        category = ForumCategoryFactory(position=1)
        forum = ForumFactory(category=category, position_in_category=1)
        topic = TopicFactory(forum=forum, author=self.user1)
        PostFactory(topic=topic, author=self.user1, position=1)
        PostFactory(topic=topic, author=self.user2, position=2)

        notifications = Notification.objects.get_unread_notifications_of(self.user1)
        self.assertEqual(1, len(notifications))
        self.assertIsNotNone(notifications.first())
        self.assertEqual(topic.last_message, notifications.first().content_object)

        TopicAnswerSubscription.objects.toggle_follow(topic, self.user1)

        self.assertEqual(0, len(Notification.objects.get_unread_notifications_of(self.user1)))
示例#27
0
    def test_mark_notifications_as_read(self):
        category = ForumCategoryFactory(position=1)
        forum = ForumFactory(category=category, position_in_category=1)
        topic = TopicFactory(forum=forum, author=self.user1)
        PostFactory(topic=topic, author=self.user1, position=1)
        PostFactory(topic=topic, author=self.user2, position=2)

        self.client.force_login(self.user1)

        notifications = Notification.objects.get_unread_notifications_of(self.user1)
        self.assertEqual(1, len(notifications))

        self.assertFalse(topic.is_read)

        result = self.client.post(reverse("mark-notifications-as-read"), follow=False)
        self.assertEqual(result.status_code, 302)

        notifications = Notification.objects.get_unread_notifications_of(self.user1)
        self.assertEqual(0, len(notifications))

        self.assertTrue(Topic.objects.get(pk=topic.pk).is_read)
示例#28
0
    def setUp(self):
        self.staff = StaffProfileFactory()
        self.dummy_author = ProfileFactory()

        self.category = ForumCategoryFactory(position=1)
        self.forum = ForumFactory(category=self.category,
                                  position_in_category=1)
        self.topic = TopicFactory(forum=self.forum,
                                  author=self.dummy_author.user)
        self.post = PostFactory(topic=self.topic,
                                author=self.dummy_author.user,
                                position=1)

        self.alerts = []
        for i in range(20):
            alert = Alert(
                author=self.dummy_author.user,
                comment=self.post,
                scope="FORUM",
                text=f"pouet-{i}",
                pubdate=(datetime.now() + timedelta(minutes=i)),
            )
            alert.save()
            self.alerts.append(alert)
示例#29
0
    def test_upercase_and_lowercase_search_give_same_results(self):
        """Pretty self-explanatory function name, isn't it ?"""

        if not self.manager.connected_to_es:
            return

        # 1. Index lowercase stuffs
        text_lc = "test"

        topic_1_lc = TopicFactory(forum=self.forum,
                                  author=self.user,
                                  title=text_lc)

        tag_lc = TagFactory(title=text_lc)
        topic_1_lc.tags.add(tag_lc)
        topic_1_lc.subtitle = text_lc
        topic_1_lc.save()

        post_1_lc = PostFactory(topic=topic_1_lc, author=self.user, position=1)
        post_1_lc.text = post_1_lc.text_html = text_lc
        post_1_lc.save()

        tuto_lc = PublishableContentFactory(type="TUTORIAL")
        tuto_draft_lc = tuto_lc.load_version()

        tuto_lc.title = text_lc
        tuto_lc.authors.add(self.user)
        subcategory_lc = SubCategoryFactory(title=text_lc)
        tuto_lc.subcategory.add(subcategory_lc)
        tuto_lc.tags.add(tag_lc)
        tuto_lc.save()

        tuto_draft_lc.description = text_lc
        tuto_draft_lc.repo_update_top_container(text_lc, tuto_lc.slug, text_lc,
                                                text_lc)

        chapter1_lc = ContainerFactory(parent=tuto_draft_lc, db_object=tuto_lc)
        extract_lc = ExtractFactory(container=chapter1_lc, db_object=tuto_lc)
        extract_lc.repo_update(text_lc, text_lc)

        published_lc = publish_content(tuto_lc,
                                       tuto_draft_lc,
                                       is_major_update=True)

        tuto_lc.sha_public = tuto_draft_lc.current_version
        tuto_lc.sha_draft = tuto_draft_lc.current_version
        tuto_lc.public_version = published_lc
        tuto_lc.save()

        # 2. Index uppercase stuffs
        text_uc = "TEST"

        topic_1_uc = TopicFactory(forum=self.forum,
                                  author=self.user,
                                  title=text_uc)

        topic_1_uc.tags.add(
            tag_lc)  # Note: a constraint forces tags title to be unique
        topic_1_uc.subtitle = text_uc
        topic_1_uc.save()

        post_1_uc = PostFactory(topic=topic_1_uc, author=self.user, position=1)
        post_1_uc.text = post_1_uc.text_html = text_uc
        post_1_uc.save()

        tuto_uc = PublishableContentFactory(type="TUTORIAL")
        tuto_draft_uc = tuto_uc.load_version()

        tuto_uc.title = text_uc
        tuto_uc.authors.add(self.user)
        tuto_uc.subcategory.add(subcategory_lc)
        tuto_uc.tags.add(tag_lc)
        tuto_uc.save()

        tuto_draft_uc.description = text_uc
        tuto_draft_uc.repo_update_top_container(text_uc, tuto_uc.slug, text_uc,
                                                text_uc)

        chapter1_uc = ContainerFactory(parent=tuto_draft_uc, db_object=tuto_uc)
        extract_uc = ExtractFactory(container=chapter1_uc, db_object=tuto_uc)
        extract_uc.repo_update(text_uc, text_uc)

        published_uc = publish_content(tuto_uc,
                                       tuto_draft_uc,
                                       is_major_update=True)

        tuto_uc.sha_public = tuto_draft_uc.current_version
        tuto_uc.sha_draft = tuto_draft_uc.current_version
        tuto_uc.public_version = published_uc
        tuto_uc.save()

        # 3. Index and search:
        self.assertEqual(
            len(
                self.manager.setup_search(Search().query(
                    MatchAll())).execute()), 0)

        # index
        for model in self.indexable:
            if model is FakeChapter:
                continue
            self.manager.es_bulk_indexing_of_model(model)
        self.manager.refresh_index()

        result = self.client.get(reverse("search:query") + "?q=" + text_lc,
                                 follow=False)
        self.assertEqual(result.status_code, 200)

        response_lc = result.context["object_list"].execute()
        self.assertEqual(response_lc.hits.total, 8)

        result = self.client.get(reverse("search:query") + "?q=" + text_uc,
                                 follow=False)
        self.assertEqual(result.status_code, 200)

        response_uc = result.context["object_list"].execute()
        self.assertEqual(response_uc.hits.total, 8)

        for responses in zip(
                response_lc,
                response_uc):  # we should get results in the same order!
            self.assertEqual(responses[0].meta.id, responses[1].meta.id)
示例#30
0
    def test_change_topic_impacts_posts(self):

        if not self.manager.connected_to_es:
            return

        # 1. Create a hidden forum belonging to a hidden group and add staff in it.
        text = "test"

        group = Group.objects.create(name="Les illuminatis anonymes de ZdS")
        _, hidden_forum = create_category_and_forum(group)

        self.staff.groups.add(group)
        self.staff.save()

        # 2. Create a normal topic and index it
        topic_1 = TopicFactory(forum=self.forum, author=self.user, title=text)
        post_1 = PostFactory(topic=topic_1, author=self.user, position=1)
        post_1.text = post_1.text_html = text
        post_1.save()

        self.manager.es_bulk_indexing_of_model(Topic)
        self.manager.es_bulk_indexing_of_model(Post)
        self.manager.refresh_index()

        self.assertEqual(
            len(
                self.manager.setup_search(Search().query(
                    MatchAll())).execute()), 2)  # indexing ok

        result = self.client.get(reverse("search:query") + "?q=" + text +
                                 "&models=" + Post.get_es_document_type(),
                                 follow=False)

        self.assertEqual(result.status_code, 200)
        response = result.context["object_list"].execute()
        self.assertEqual(response.hits.total, 1)  # ok
        self.assertEqual(response[0].meta.doc_type,
                         Post.get_es_document_type())
        self.assertEqual(response[0].forum_pk, self.forum.pk)
        self.assertEqual(response[0].topic_pk, topic_1.pk)
        self.assertEqual(response[0].topic_title, topic_1.title)

        # 3. Change topic title and reindex
        topic_1.title = "new title"
        topic_1.save()

        self.manager.es_bulk_indexing_of_model(Topic)
        self.manager.es_bulk_indexing_of_model(Post)
        self.manager.refresh_index()

        result = self.client.get(reverse("search:query") + "?q=" + text +
                                 "&models=" + Post.get_es_document_type(),
                                 follow=False)

        self.assertEqual(result.status_code, 200)
        response = result.context["object_list"].execute()
        self.assertEqual(response.hits.total, 1)  # ok

        self.assertEqual(response[0].topic_title,
                         topic_1.title)  # title was changed

        # 4. connect with staff and move topic
        self.client.force_login(self.staff)

        data = {"move": "", "forum": hidden_forum.pk, "topic": topic_1.pk}
        response = self.client.post(reverse("topic-edit"), data, follow=False)

        self.assertEqual(302, response.status_code)

        self.manager.es_bulk_indexing_of_model(Topic)
        self.manager.es_bulk_indexing_of_model(Post)
        self.manager.refresh_index()

        result = self.client.get(reverse("search:query") + "?q=" + text +
                                 "&models=" + Post.get_es_document_type(),
                                 follow=False)

        self.assertEqual(result.status_code, 200)
        response = result.context["object_list"].execute()
        self.assertEqual(
            response.hits.total,
            1)  # Note: without staff, would not get any results (see below)

        self.assertEqual(response[0].forum_pk,
                         hidden_forum.pk)  # post was updated with new forum

        # 5. Topic is now hidden
        self.client.logout()

        result = self.client.get(reverse("search:query") + "?q=" + text +
                                 "&models=" + Post.get_es_document_type(),
                                 follow=False)

        self.assertEqual(result.status_code, 200)
        response = result.context["object_list"].execute()
        self.assertEqual(response.hits.total, 0)  # ok