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)
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))
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))
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)
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 !
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()))
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)
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)
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)
def test_get_post_count(self): # Start with 0 self.assertEqual(self.user1.get_post_count(), 0) # Post ! PostFactory(topic=self.forumtopic, author=self.user1.user, position=1) # Should be 1 self.assertEqual(self.user1.get_post_count(), 1)
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()))
def test_failure_post_karma_with_client_unauthenticated(self): profile = ProfileFactory() category, forum = create_category_and_forum() topic = create_topic_in_forum(forum, profile) post = PostFactory(topic=topic, author=profile.user, position=2) response = self.client.put(reverse("api:forum:post-karma", args=(post.pk,))) self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
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)))
def test_get_hidden_by_staff_posts_count(self): # Start with 0 self.assertEqual(self.user1.get_hidden_by_staff_posts_count(), 0) # Post and hide it by poster PostFactory(topic=self.forumtopic, author=self.user1.user, position=1, is_visible=False, editor=self.user1.user) # Should be 0 self.assertEqual(self.user1.get_hidden_by_staff_posts_count(), 0) # Post and hide it by staff PostFactory(topic=self.forumtopic, author=self.user1.user, position=1, is_visible=False, editor=self.staff.user) # Should be 1 self.assertEqual(self.user1.get_hidden_by_staff_posts_count(), 1)
def test_get_hidden_by_staff_posts_count_staff_poster(self): # Start with 0 self.assertEqual(self.staff.get_hidden_by_staff_posts_count(), 0) # Post and hide it by poster which is staff PostFactory(topic=self.forumtopic, author=self.staff.user, position=1, is_visible=False, editor=self.staff.user) # Should be 0 because even if poster is staff, he is the poster self.assertEqual(self.staff.get_hidden_by_staff_posts_count(), 0)
def test_get_posts(self): # Start with 0 self.assertEqual(len(self.user1.get_posts()), 0) # Post ! apost = PostFactory(topic=self.forumtopic, author=self.user1.user, position=1) # Should be 1 posts = self.user1.get_posts() self.assertEqual(len(posts), 1) self.assertEqual(apost, posts[0])
def test_success_post_karma_dislike(self): profile = ProfileFactory() category, forum = create_category_and_forum() topic = create_topic_in_forum(forum, profile) another_profile = ProfileFactory() post = PostFactory(topic=topic, author=another_profile.user, position=2) self.client.force_login(profile.user) response = self.client.put(reverse("api:forum:post-karma", args=(post.pk,)), {"vote": "dislike"}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(CommentVote.objects.filter(user=profile.user, comment=post, positive=False).exists())
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)
def test_get_active_alerts_count(self): # Start with 0 self.assertEqual(self.user1.get_active_alerts_count(), 0) # Post and Alert it ! post = PostFactory(topic=self.forumtopic, author=self.user1.user, position=1) Alert.objects.create(author=self.user1.user, comment=post, scope="FORUM", pubdate=datetime.now()) # Should be 1 self.assertEqual(self.user1.get_active_alerts_count(), 1)
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)
def test_failure_post_karma_with_sanctioned_user(self): profile = ProfileFactory() category, forum = create_category_and_forum() topic = create_topic_in_forum(forum, profile) another_profile = ProfileFactory() post = PostFactory(topic=topic, author=another_profile.user, position=2) profile = ProfileFactory() profile.can_read = False profile.can_write = False profile.save() self.client.force_login(profile.user) response = self.client.put(reverse("api:forum:post-karma", args=(post.pk,))) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
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))
def test_success_post_karma_like_already_disliked(self): profile = ProfileFactory() category, forum = create_category_and_forum() topic = create_topic_in_forum(forum, profile) another_profile = ProfileFactory() post = PostFactory(topic=topic, author=another_profile.user, position=2) vote = CommentVote(user=profile.user, comment=post, positive=False) vote.save() self.client.force_login(profile.user) response = self.client.put(reverse("api:forum:post-karma", args=(post.pk,)), {"vote": "like"}) vote.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(vote.positive)
def __generate_topic_and_post(cli, fake, nb_avg_posts_in_topic, nb_topics, nb_users, topics, tps1): profiles = list(Profile.objects.all()) for topic_index in range(0, nb_topics): nb_posts = randint(0, nb_avg_posts_in_topic * 2) + 1 for post_index in range(1, nb_posts): post = PostFactory( topic=topics[topic_index], author=profiles[post_index % nb_users].user, position=post_index + 1 ) post.text = fake.paragraph(nb_sentences=5, variable_nb_sentences=True) post.text_html = emarkdown(post.text) post.is_useful = int(nb_posts * 0.3) > 0 and post_index % int(nb_posts * 0.3) == 0 post.save() sys.stdout.write(f" Topic {topic_index + 1}/{nb_topics} \tPost {post_index + 1}/{nb_posts} \r") sys.stdout.flush() tps2 = time.time() cli.stdout.write(f"\nFait en {tps2 - tps1} sec")
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")
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
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
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)
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()
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)