def test_generate_markdown(self): tuto = PublishedContentFactory( type="TUTORIAL") # generate and publish a tutorial published = PublishedContent.objects.get(content_pk=tuto.pk) tuto2 = PublishedContentFactory( type="TUTORIAL") # generate and publish a second tutorial published2 = PublishedContent.objects.get(content_pk=tuto2.pk) self.assertTrue(published.has_md()) self.assertTrue(published2.has_md()) os.remove( str( Path(published.get_extra_contents_directory(), published.content_public_slug + ".md"))) os.remove( str( Path(published2.get_extra_contents_directory(), published2.content_public_slug + ".md"))) self.assertFalse(published.has_md()) self.assertFalse(published2.has_md()) # test command with param call_command("generate_markdown", published.content.pk) self.assertTrue(published.has_md()) self.assertFalse(published2.has_md()) os.remove( str( Path(published.get_extra_contents_directory(), published.content_public_slug + ".md"))) # test command without param call_command("generate_markdown") self.assertTrue(published.has_md()) self.assertTrue(published2.has_md())
def setUp(self): # Create users self.staff = StaffProfileFactory().user self.author = ProfileFactory().user # Createcontents self.content = PublishableContentFactory(author_list=[self.author]) self.suggestable_content_1 = PublishedContentFactory() self.suggestable_content_2 = PublishedContentFactory() self.unpublished_content = PublishableContentFactory() self.not_picked_opinion = PublishedContentFactory() self.not_picked_opinion.type = "OPINION" self.not_picked_opinion.save() # Get information to be reused in tests self.form_url = reverse("content:add-suggestion", kwargs={"pk": self.content.pk}) self.success_message_fragment = _("a été ajouté dans les suggestions") self.error_message_fragment_unpublished = _( "un contenu qui n'a pas été publié") self.error_message_fragment_already_suggested = _( "fait déjà partie des suggestions de") self.error_message_fragment_self = _( "en tant que suggestion pour lui même") self.error_messge_fragment_not_picked = _( "un billet qui n'a pas été mis en avant") # Log in with an authorized user to perform the tests self.client.force_login(self.staff)
def test_accessible_ui_for_author(self): opinion = PublishedContentFactory(author_list=[self.user_author], type="OPINION") subcategory = SubCategoryFactory() opinion.subcategory.add(subcategory) opinion.save() self.client.force_login(self.user_author) resp = self.client.get(reverse("opinion:view", kwargs={"pk": opinion.pk, "slug": opinion.slug})) self.assertContains(resp, "Version brouillon", msg_prefix="Author must access their draft directly") self.assertNotContains(resp, "{}?subcategory=".format(reverse("publication:list"))) self.assertContains(resp, "{}?category=".format(reverse("opinion:list")))
def test_content_exports_list(self): author = ProfileFactory() content = PublishedContentFactory(author_list=[author.user]).public_version # Anonymous usage should be allowed # We check that no extraneous SQL query is executed, as this API is used # for live updates. # Here we expect one request, as there are no records. For other calls, # we expect two requests: one for the exports list and one for the # PublishedContent prefetch (to get the export's URL). with self.assertNumQueries(1): response = self.client.get(reverse("api:content:list_exports", args=[content.content.pk]), type="json") self.assertEqual(response.status_code, status.HTTP_200_OK) # At this point, no export was generated so this should be empty self.assertEqual(response.data, []) # Let's request some self.client.force_login(author.user) response = self.client.post(reverse("api:content:generate_export", args=[content.content.pk])) self.assertEqual(response.status_code, status.HTTP_201_CREATED) requests_count = PublicationEvent.objects.filter(published_object=content).count() self.client.logout() with self.assertNumQueries(2): response = self.client.get(reverse("api:content:list_exports", args=[content.content.pk]), type="json") self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data), requests_count) # Let's request some more. The API should only return the latest ones, so # even if there are some more records in the database, the count should stay # the same. self.client.force_login(author.user) response = self.client.post(reverse("api:content:generate_export", args=[content.content.pk])) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.client.logout() with self.assertNumQueries(2): response = self.client.get(reverse("api:content:list_exports", args=[content.content.pk]), type="json") self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data), requests_count) # We create another content. Even if there are some records in the database, # they should not be returned for this new content. other_content = PublishedContentFactory(author_list=[author.user]) # One request as there are no export: no prefetch needed. with self.assertNumQueries(1): response = self.client.get(reverse("api:content:list_exports", args=[other_content.pk]), type="json") self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, [])
def test_generate_pdf(self): """ensure the behavior of the `python manage.py generate_pdf` commmand""" self.overridden_zds_app["content"][ "build_pdf_when_published"] = True # this test need PDF build, if any tuto = PublishedContentFactory( type="TUTORIAL") # generate and publish a tutorial published = PublishedContent.objects.get(content_pk=tuto.pk) tuto2 = PublishedContentFactory( type="TUTORIAL") # generate and publish a second tutorial published2 = PublishedContent.objects.get(content_pk=tuto2.pk) # ensure that PDF exists in the first place self.assertTrue(published.has_pdf()) self.assertTrue(published2.has_pdf()) pdf_path = os.path.join(published.get_extra_contents_directory(), published.content_public_slug + ".pdf") pdf_path2 = os.path.join(published2.get_extra_contents_directory(), published2.content_public_slug + ".pdf") self.assertTrue(os.path.exists(pdf_path)) self.assertTrue(os.path.exists(pdf_path2)) # 1. re-generate (all) PDFs os.remove(pdf_path) os.remove(pdf_path2) self.assertFalse(os.path.exists(pdf_path)) self.assertFalse(os.path.exists(pdf_path2)) call_command("generate_pdf") self.assertTrue(os.path.exists(pdf_path)) self.assertTrue(os.path.exists(pdf_path2)) # both PDFs are generated # 2. re-generate a given PDF os.remove(pdf_path) os.remove(pdf_path2) self.assertFalse(os.path.exists(pdf_path)) self.assertFalse(os.path.exists(pdf_path2)) call_command("generate_pdf", f"id={tuto.pk}") self.assertTrue(os.path.exists(pdf_path)) self.assertFalse( os.path.exists(pdf_path2)) # only the first PDF is generated # 3. re-generate a given PDF with a wrong id os.remove(pdf_path) self.assertFalse(os.path.exists(pdf_path)) self.assertFalse(os.path.exists(pdf_path2)) call_command("generate_pdf", "id=-1") # There is no content with pk=-1 self.assertFalse(os.path.exists(pdf_path)) self.assertFalse(os.path.exists(pdf_path2)) # so no PDF is generated !
def test_no_bad_slug_renaming_on_rename(self): """ this test embodies the #5320 issue (first case simple workflow): - create an opinion with title "title" - create an article with the same title => it gets "-1" slug - rename opinion - make any update to article - try to access article """ opinion = PublishedContentFactory(type="OPINION", title="title", author_list=[self.author.user], licence=self.licence) article = PublishedContentFactory(type="ARTICLE", title="title", author_list=[self.author.user], licence=self.licence) # login with author self.client.force_login(self.author.user) result = self.client.post( reverse("content:edit", args=[opinion.pk, opinion.slug]), { "title": "new title", "description": "subtitle", "introduction": "introduction", "conclusion": "conclusion", "type": "OPINION", "licence": self.licence.pk, "subcategory": self.subcategory.pk, "last_hash": opinion.load_version().compute_hash(), "image": (settings.BASE_DIR / "fixtures" / "logo.png").open("rb"), }, follow=False, ) self.assertEqual(result.status_code, 302) updated_opinion = PublishableContent.objects.get(pk=opinion.pk) self.assertEqual("new-title", updated_opinion.slug) result = self.client.get(article.get_absolute_url()) self.assertEqual(200, result.status_code) result = self.client.post( reverse("content:edit", args=[article.pk, article.slug]), { "title": "title", "description": "subtitle", "introduction": "introduction", "conclusion": "conclusion", "type": "ARTICLE", "licence": self.licence.pk, "subcategory": self.subcategory.pk, "last_hash": article.load_version().compute_hash(), "image": (settings.BASE_DIR / "fixtures" / "logo.png").open("rb"), }, follow=True, ) self.assertEqual(200, result.status_code) result = self.client.get(article.get_absolute_url()) self.assertEqual(200, result.status_code)
def test_ensure_gallery(self): content = PublishedContentFactory() content.authors.add(ProfileFactory().user) content.authors.add(ProfileFactory().user) content.save() content.ensure_author_gallery() self.assertEqual( UserGallery.objects.filter(gallery__pk=content.gallery.pk).count(), content.authors.count()) content.authors.add(ProfileFactory().user) content.save() content.ensure_author_gallery() self.assertEqual( UserGallery.objects.filter(gallery__pk=content.gallery.pk).count(), content.authors.count())
def test_public_lists(self): tutorial = PublishedContentFactory(author_list=[self.user_author]) tutorial_unpublished = PublishableContentFactory( author_list=[self.user_author]) article = PublishedContentFactory(author_list=[self.user_author], type="ARTICLE") article_unpublished = PublishableContentFactory( author_list=[self.user_author], type="ARTICLE") self.client.logout() resp = self.client.get(reverse("publication:list") + "?type=tutorial") self.assertContains(resp, tutorial.title) self.assertNotContains(resp, tutorial_unpublished.title) resp = self.client.get(reverse("publication:list") + "?type=article") self.assertContains(resp, article.title) self.assertNotContains(resp, article_unpublished.title) resp = self.client.get( reverse("tutorial:find-tutorial", args=[self.user_author.username]) + "?filter=public") self.assertContains(resp, tutorial.title) self.assertNotContains(resp, tutorial_unpublished.title) resp = self.client.get( reverse("tutorial:find-tutorial", args=[self.user_author.username]) + "?filter=redaction") self.assertEqual(resp.status_code, 403) resp = self.client.get( reverse("article:find-article", args=[self.user_author.username]) + "?filter=public") self.assertContains(resp, article.title) self.assertNotContains(resp, article_unpublished.title) resp = self.client.get( reverse("article:find-article", args=[self.user_author.username]) + "?filter=redaction") self.assertEqual(resp.status_code, 403) resp = self.client.get( reverse("article:find-article", args=[self.user_author.username]) + "?filter=chuck-norris") self.assertEqual(resp.status_code, 404)
def test_new_cowritten_content_without_doubly_notif(self): author1 = ProfileFactory() author2 = ProfileFactory() NewPublicationSubscription.objects.toggle_follow(author2.user, author1.user) content = PublishedContentFactory(author_list=[author1.user, author2.user]) signals.content_published.send(sender=content.__class__, instance=content, by_email=False) auto_user_1_sub = NewPublicationSubscription.objects.get_existing(author1.user, author1.user, False) self.assertIsNotNone(auto_user_1_sub) notifs = list(Notification.objects.get_notifications_of(author1.user)) self.assertEqual(1, len(notifs))
def test_private_lists(self): tutorial = PublishedContentFactory(author_list=[self.user_author]) tutorial_unpublished = PublishableContentFactory( author_list=[self.user_author]) article = PublishedContentFactory(author_list=[self.user_author], type="ARTICLE") article_unpublished = PublishableContentFactory( author_list=[self.user_author], type="ARTICLE") self.client.force_login(self.user_author) resp = self.client.get( reverse("tutorial:find-tutorial", args=[self.user_author.username])) self.assertContains(resp, tutorial.title) self.assertContains(resp, tutorial_unpublished.title) self.assertContains(resp, "content-illu") resp = self.client.get( reverse("article:find-article", args=[self.user_author.username])) self.assertContains(resp, article.title) self.assertContains(resp, article_unpublished.title) self.assertContains(resp, "content-illu")
def test_success_initial_content(self): author = ProfileFactory().user author2 = ProfileFactory().user tutorial = PublishedContentFactory(author_list=[author, author2]) gallery = GalleryFactory() image = ImageFactory(gallery=gallery) tutorial.image = image tutorial.save() staff = StaffProfileFactory() self.client.force_login(staff.user) response = self.client.get( "{}{}".format( reverse("featured-resource-create"), f"?content_type=published_content&content_id={tutorial.pk}" ) ) initial_dict = response.context["form"].initial self.assertEqual(initial_dict["title"], tutorial.title) self.assertEqual(initial_dict["authors"], f"{author}, {author2}") self.assertEqual(initial_dict["type"], _("Un tutoriel")) self.assertEqual(initial_dict["url"], f"http://testserver{tutorial.get_absolute_url_online()}") self.assertEqual(initial_dict["image_url"], "http://testserver{}".format(image.physical["featured"].url))
def test_adjust_char_count(self): """Test the `adjust_char_count` command""" article = PublishedContentFactory(type="ARTICLE", author_list=[self.user_author]) published = PublishedContent.objects.filter(content=article).first() published.char_count = None published.save() call_command("adjust_char_count") published = PublishedContent.objects.get(pk=published.pk) self.assertEqual(published.char_count, published.get_char_count())
def test_request_content_exports_generation(self): author = ProfileFactory() not_author = ProfileFactory() staff = StaffProfileFactory() content = PublishedContentFactory( author_list=[author.user]).public_version self.assertEqual( 0, PublicationEvent.objects.filter(published_object=content).count()) # Anonymous sender should not be able to ask for exports generation response = self.client.post( reverse("api:content:generate_export", args=[content.content.pk])) self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) self.assertEqual( 0, PublicationEvent.objects.filter(published_object=content).count()) # An authenticated author but not an author should not either self.client.force_login(not_author.user) response = self.client.post( reverse("api:content:generate_export", args=[content.content.pk])) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertEqual( 0, PublicationEvent.objects.filter(published_object=content).count()) # But if the user is staff, it should self.client.force_login(staff.user) response = self.client.post( reverse("api:content:generate_export", args=[content.content.pk])) self.assertEqual(response.status_code, status.HTTP_201_CREATED) requests_count = PublicationEvent.objects.filter( published_object=content).count() self.assertGreater(requests_count, 0) # And if the user is an author, it should too self.client.force_login(author.user) response = self.client.post( reverse("api:content:generate_export", args=[content.content.pk])) self.assertEqual(response.status_code, status.HTTP_201_CREATED) # And it should be greater than the previous one as we added new requets self.assertGreater( PublicationEvent.objects.filter(published_object=content).count(), requests_count) self.client.logout()
def test_get_public_tutos(self): # Start with 0 self.assertEqual(len(self.user1.get_public_tutos()), 0) # Create Tuto ! publictuto = PublishableContentFactory(type="TUTORIAL") publictuto.authors.add(self.user1.user) publictuto.gallery = GalleryFactory() publictuto.sha_public = "whatever" publictuto.save() # Should be 0 because publication was not used publictutos = self.user1.get_public_tutos() self.assertEqual(len(publictutos), 0) PublishedContentFactory(author_list=[self.user1.user]) self.assertEqual(len(self.user1.get_public_tutos()), 1)
def test_ping_on_tuto(self): """Error from #4904""" content = PublishedContentFactory(author_list=[self.user1]) self.client.force_login(self.user2) result = self.client.post( reverse("content:add-reaction") + f"?pk={content.pk}", { "text": f"@{self.user1.username}", "last_note": 0, }, follow=True, ) self.assertEqual(200, result.status_code) self.assertEqual(1, len(Notification.objects.filter(is_read=False).all()))
def test_intervention_filter_for_tribunes(self): author = ProfileFactory() opinion = PublishedContentFactory(type="OPINION", author_list=[author.user]) alerter = ProfileFactory() staff = StaffProfileFactory() alert = Alert() alert.scope = "CONTENT" alert.author = alerter.user alert.content = opinion alert.pubdate = datetime.datetime.now() alert.text = "Something to say." alert.save() filter_result = get_header_notifications(staff.user)["alerts"] self.assertEqual(1, filter_result["total"]) self.assertEqual(alert.text, filter_result["list"][0]["text"])
def setUp(self): # Create users self.author = ProfileFactory().user self.staff = StaffProfileFactory().user self.outsider = ProfileFactory().user # Create contents and suggestion self.content = PublishableContentFactory(author_list=[self.author]) self.suggestable_content = PublishedContentFactory() # Get information to be reused in tests self.form_url = reverse("content:add-suggestion", kwargs={"pk": self.content.pk}) self.login_url = reverse("member-login") + "?next=" + self.form_url self.content_url = reverse("content:view", kwargs={"pk": self.content.pk, "slug": self.content.slug}) self.form_data = {"options": self.suggestable_content.pk}
def test_get_public_articles(self): # Start with 0 self.assertEqual(len(self.user1.get_public_articles()), 0) # Create article ! article = PublishableContentFactory(type="ARTICLE") article.authors.add(self.user1.user) article.sha_public = "whatever" article.save() # Should be 0 articles = self.user1.get_public_articles() self.assertEqual(len(articles), 0) # Should be 1 PublishedContentFactory(author_list=[self.user1.user], type="ARTICLE") self.assertEqual(len(self.user1.get_public_articles()), 1) self.assertEqual(len(self.user1.get_public_tutos()), 0)
def test_no_persistant_notif_on_revoke(self): from zds.tutorialv2.publication_utils import unpublish_content NewPublicationSubscription.objects.get_or_create_active( self.user1, self.user2) content = PublishedContentFactory(author_list=[self.user2]) signals.content_published.send(sender=self.tuto.__class__, instance=content, by_email=False) self.assertEqual( 1, len(Notification.objects.get_notifications_of(self.user1))) unpublish_content(content) self.assertEqual( 0, len(Notification.objects.get_notifications_of(self.user1)))
def test_no_persistant_comment_notif_on_revoke(self): from zds.tutorialv2.publication_utils import unpublish_content content = PublishedContentFactory(author_list=[self.user2]) ContentReactionAnswerSubscription.objects.get_or_create_active( self.user1, content) ContentReactionFactory(related_content=content, author=self.user2, position=1) self.assertEqual( 1, len(Notification.objects.get_unread_notifications_of(self.user1))) unpublish_content(content, moderator=self.user2) self.assertEqual( 0, len(Notification.objects.get_unread_notifications_of(self.user1)))
def test_only_one_notif_on_major_update(self): NewPublicationSubscription.objects.get_or_create_active( self.user1, self.user2) content = PublishedContentFactory(author_list=[self.user2]) notify_update(content, False, True) versioned = content.load_version() content.sha_draft = versioned.repo_update(introduction="new intro", conclusion="new conclusion", title=versioned.title) content.save() publish_content(content, content.load_version(), True) notify_update(content, True, True) notifs = get_header_notifications( self.user1)["general_notifications"]["list"] self.assertEqual(1, len(notifs), str(notifs))
def test_no_alert_on_unpublish(self): """related to #4860""" published = PublishedContentFactory(type="OPINION", author_list=[self.user_author]) reaction = ContentReactionFactory(related_content=published, author=ProfileFactory().user, position=1, pubdate=datetime.datetime.now()) Alert.objects.create( scope="CONTENT", comment=reaction, text="a text", author=ProfileFactory().user, pubdate=datetime.datetime.now(), content=published, ) staff = StaffProfileFactory().user self.assertEqual(1, get_header_notifications(staff)["alerts"]["total"]) unpublish_content(published, staff) self.assertEqual(0, get_header_notifications(staff)["alerts"]["total"])
def test_char_count_after_publication(self): """Test the ``get_char_count()`` function. Special care should be taken with this function, since: - The username of the author is, by default "Firmxxx" where "xxx" depends on the tests before ; - The titles (!) also contains a number that also depends on the number of tests before ; - The date is ``datetime.now()`` and contains the months, which is never a fixed number of letters. """ author = ProfileFactory().user author.username = "******" author.save() len_date_now = len(date(datetime.now(), "d F Y")) article = PublishedContentFactory(type="ARTICLE", author_list=[author], title="Un titre") published = PublishedContent.objects.filter(content=article).first() self.assertEqual(published.get_char_count(), 160 + len_date_now) tuto = PublishableContentFactory(type="TUTORIAL", author_list=[author], title="Un titre") # add a chapter, so it becomes a middle tutorial tuto_draft = tuto.load_version() chapter1 = ContainerFactory(parent=tuto_draft, db_object=tuto, title="Un chapitre") ExtractFactory(container=chapter1, db_object=tuto, title="Un extrait") published = publish_content(tuto, tuto_draft, is_major_update=True) tuto.sha_public = tuto_draft.current_version tuto.sha_draft = tuto_draft.current_version tuto.public_version = published tuto.save() published = PublishedContent.objects.filter(content=tuto).first() self.assertEqual(published.get_char_count(), 335 + len_date_now)
def test_last_participation_is_old(self): article = PublishedContentFactory(author_list=[self.user_author], type="ARTICLE") new_user = ProfileFactory().user reac = ContentReaction(author=self.user_author, position=1, related_content=article) reac.update_content("I will find you.") reac.save() article.last_note = reac article.save() self.assertFalse(last_participation_is_old(article, new_user)) ContentRead(user=self.user_author, note=reac, content=article).save() reac = ContentReaction(author=new_user, position=2, related_content=article) reac.update_content("I will find you.") reac.save() article.last_note = reac article.save() ContentRead(user=new_user, note=reac, content=article).save() self.assertFalse(last_participation_is_old(article, new_user)) self.assertTrue(last_participation_is_old(article, self.user_author))
def test_publication_and_attributes_consistency(self): pubdate = datetime.now() - timedelta(days=1) article = PublishedContentFactory(type="ARTICLE", author_list=[self.user_author]) public_version = article.public_version public_version.publication_date = pubdate public_version.save() # everything must come from database to have good datetime comparison article = PublishableContent.objects.get(pk=article.pk) article.public_version.load_public_version() old_date = article.public_version.publication_date old_title = article.public_version.title() old_description = article.public_version.description() article.licence = LicenceFactory() article.save() self.client.force_login(self.user_author) self.client.post( reverse("content:edit", args=[article.pk, article.slug]), { "title": old_title + "bla", "description": old_description + "bla", "type": "ARTICLE", "licence": article.licence.pk, "subcategory": SubCategoryFactory().pk, "last_hash": article.sha_draft, }, ) article = PublishableContent.objects.prefetch_related( "public_version").get(pk=article.pk) article.public_version.load_public_version() self.assertEqual(old_title, article.public_version.title()) self.assertEqual(old_description, article.public_version.description()) self.assertEqual(old_date, article.public_version.publication_date) publish_content(article, article.load_version(), False) article = PublishableContent.objects.get(pk=article.pk) article.public_version.load_public_version() self.assertEqual(old_date, article.public_version.publication_date) self.assertNotEqual(old_date, article.public_version.update_date)
def test_unregister(self): """ To test that unregistering user is working. """ # test not logged user can't unregister. self.client.logout() result = self.client.post(reverse("member-unregister"), follow=False) self.assertEqual(result.status_code, 302) # test logged user can unregister. user = ProfileFactory() self.client.force_login(user.user) result = self.client.post(reverse("member-unregister"), follow=False) self.assertEqual(result.status_code, 302) self.assertEqual(User.objects.filter(username=user.user.username).count(), 0) # Attach a user at tutorials, articles, topics and private topics. After that, # unregister this user and check that he is well removed in all contents. user = ProfileFactory() user2 = ProfileFactory() alone_gallery = GalleryFactory() UserGalleryFactory(gallery=alone_gallery, user=user.user) shared_gallery = GalleryFactory() UserGalleryFactory(gallery=shared_gallery, user=user.user) UserGalleryFactory(gallery=shared_gallery, user=user2.user) # first case : a published tutorial with only one author published_tutorial_alone = PublishedContentFactory(type="TUTORIAL") published_tutorial_alone.authors.add(user.user) published_tutorial_alone.save() # second case : a published tutorial with two authors published_tutorial_2 = PublishedContentFactory(type="TUTORIAL") published_tutorial_2.authors.add(user.user) published_tutorial_2.authors.add(user2.user) published_tutorial_2.save() # third case : a private tutorial with only one author writing_tutorial_alone = PublishableContentFactory(type="TUTORIAL") writing_tutorial_alone.authors.add(user.user) writing_tutorial_alone.save() writing_tutorial_alone_galler_path = writing_tutorial_alone.gallery.get_gallery_path() # fourth case : a private tutorial with at least two authors writing_tutorial_2 = PublishableContentFactory(type="TUTORIAL") writing_tutorial_2.authors.add(user.user) writing_tutorial_2.authors.add(user2.user) writing_tutorial_2.save() self.client.force_login(self.staff) # same thing for articles published_article_alone = PublishedContentFactory(type="ARTICLE") published_article_alone.authors.add(user.user) published_article_alone.save() published_article_2 = PublishedContentFactory(type="ARTICLE") published_article_2.authors.add(user.user) published_article_2.authors.add(user2.user) published_article_2.save() writing_article_alone = PublishableContentFactory(type="ARTICLE") writing_article_alone.authors.add(user.user) writing_article_alone.save() writing_article_2 = PublishableContentFactory(type="ARTICLE") writing_article_2.authors.add(user.user) writing_article_2.authors.add(user2.user) writing_article_2.save() # beta content beta_forum = ForumFactory(category=ForumCategoryFactory()) beta_content = BetaContentFactory(author_list=[user.user], forum=beta_forum) beta_content_2 = BetaContentFactory(author_list=[user.user, user2.user], forum=beta_forum) # about posts and topics authored_topic = TopicFactory(author=user.user, forum=self.forum11, solved_by=user.user) answered_topic = TopicFactory(author=user2.user, forum=self.forum11) PostFactory(topic=answered_topic, author=user.user, position=2) edited_answer = PostFactory(topic=answered_topic, author=user.user, position=3) edited_answer.editor = user.user edited_answer.save() upvoted_answer = PostFactory(topic=answered_topic, author=user2.user, position=4) upvoted_answer.like += 1 upvoted_answer.save() CommentVote.objects.create(user=user.user, comment=upvoted_answer, positive=True) private_topic = PrivateTopicFactory(author=user.user) private_topic.participants.add(user2.user) private_topic.save() PrivatePostFactory(author=user.user, privatetopic=private_topic, position_in_topic=1) # add API key self.assertEqual(Application.objects.count(), 0) self.assertEqual(AccessToken.objects.count(), 0) api_application = Application() api_application.client_id = "foobar" api_application.user = user.user api_application.client_type = "confidential" api_application.authorization_grant_type = "password" api_application.client_secret = "42" api_application.save() token = AccessToken() token.user = user.user token.token = "r@d0m" token.application = api_application token.expires = datetime.now() token.save() self.assertEqual(Application.objects.count(), 1) self.assertEqual(AccessToken.objects.count(), 1) # add a karma note and a sanction with this user note = KarmaNote(moderator=user.user, user=user2.user, note="Good!", karma=5) note.save() ban = Ban(moderator=user.user, user=user2.user, type="Ban définitif", note="Test") ban.save() # login and unregister: self.client.force_login(user.user) result = self.client.post(reverse("member-unregister"), follow=False) self.assertEqual(result.status_code, 302) # check that the bot have taken authorship of tutorial: self.assertEqual(published_tutorial_alone.authors.count(), 1) self.assertEqual( published_tutorial_alone.authors.first().username, settings.ZDS_APP["member"]["external_account"] ) self.assertFalse(os.path.exists(writing_tutorial_alone_galler_path)) self.assertEqual(published_tutorial_2.authors.count(), 1) self.assertEqual( published_tutorial_2.authors.filter(username=settings.ZDS_APP["member"]["external_account"]).count(), 0 ) # check that published tutorials remain published and accessible self.assertIsNotNone(published_tutorial_2.public_version.get_prod_path()) self.assertTrue(os.path.exists(published_tutorial_2.public_version.get_prod_path())) self.assertIsNotNone(published_tutorial_alone.public_version.get_prod_path()) self.assertTrue(os.path.exists(published_tutorial_alone.public_version.get_prod_path())) self.assertEqual( self.client.get( reverse("tutorial:view", args=[published_tutorial_alone.pk, published_tutorial_alone.slug]), follow=False, ).status_code, 200, ) self.assertEqual( self.client.get( reverse("tutorial:view", args=[published_tutorial_2.pk, published_tutorial_2.slug]), follow=False ).status_code, 200, ) # test that published articles remain accessible self.assertTrue(os.path.exists(published_article_alone.public_version.get_prod_path())) self.assertEqual( self.client.get( reverse("article:view", args=[published_article_alone.pk, published_article_alone.slug]), follow=True ).status_code, 200, ) self.assertEqual( self.client.get( reverse("article:view", args=[published_article_2.pk, published_article_2.slug]), follow=True ).status_code, 200, ) # check that the tutorial for which the author was alone does not exists anymore self.assertEqual(PublishableContent.objects.filter(pk=writing_tutorial_alone.pk).count(), 0) self.assertFalse(os.path.exists(writing_tutorial_alone.get_repo_path())) # check that bot haven't take the authorship of the tuto with more than one author self.assertEqual(writing_tutorial_2.authors.count(), 1) self.assertEqual( writing_tutorial_2.authors.filter(username=settings.ZDS_APP["member"]["external_account"]).count(), 0 ) # authorship for the article for which user was the only author self.assertEqual(published_article_alone.authors.count(), 1) self.assertEqual( published_article_alone.authors.first().username, settings.ZDS_APP["member"]["external_account"] ) self.assertEqual(published_article_2.authors.count(), 1) self.assertEqual(PublishableContent.objects.filter(pk=writing_article_alone.pk).count(), 0) self.assertFalse(os.path.exists(writing_article_alone.get_repo_path())) # not bot if another author: self.assertEqual( published_article_2.authors.filter(username=settings.ZDS_APP["member"]["external_account"]).count(), 0 ) self.assertEqual(writing_article_2.authors.count(), 1) self.assertEqual( writing_article_2.authors.filter(username=settings.ZDS_APP["member"]["external_account"]).count(), 0 ) # topics, gallery and PMs: self.assertEqual(Topic.objects.filter(author__username=user.user.username).count(), 0) self.assertEqual(Topic.objects.filter(solved_by=user.user).count(), 0) self.assertEqual(Topic.objects.filter(solved_by=self.anonymous).count(), 1) self.assertEqual(Post.objects.filter(author__username=user.user.username).count(), 0) self.assertEqual(Post.objects.filter(editor__username=user.user.username).count(), 0) self.assertEqual(PrivatePost.objects.filter(author__username=user.user.username).count(), 0) self.assertEqual(PrivateTopic.objects.filter(author__username=user.user.username).count(), 0) self.assertIsNotNone(Topic.objects.get(pk=authored_topic.pk)) self.assertIsNotNone(PrivateTopic.objects.get(pk=private_topic.pk)) self.assertIsNotNone(Gallery.objects.get(pk=alone_gallery.pk)) self.assertEqual(alone_gallery.get_linked_users().count(), 1) self.assertEqual(shared_gallery.get_linked_users().count(), 1) self.assertEqual(UserGallery.objects.filter(user=user.user).count(), 0) self.assertEqual(CommentVote.objects.filter(user=user.user, positive=True).count(), 0) self.assertEqual(Post.objects.filter(pk=upvoted_answer.id).first().like, 0) # zep 12, published contents and beta self.assertIsNotNone(PublishedContent.objects.filter(content__pk=published_tutorial_alone.pk).first()) self.assertIsNotNone(PublishedContent.objects.filter(content__pk=published_tutorial_2.pk).first()) self.assertTrue(Topic.objects.get(pk=beta_content.beta_topic.pk).is_locked) self.assertFalse(Topic.objects.get(pk=beta_content_2.beta_topic.pk).is_locked) # check API self.assertEqual(Application.objects.count(), 0) self.assertEqual(AccessToken.objects.count(), 0) # check that the karma note and the sanction were kept self.assertTrue(KarmaNote.objects.filter(pk=note.pk).exists()) self.assertTrue(Ban.objects.filter(pk=ban.pk).exists())
def setUp(self): self.client = APIClient() caches[extensions_api_settings.DEFAULT_USE_CACHE].clear() self.content = PublishedContentFactory() self.content.save()
class ContentReactionKarmaAPITest(TutorialTestMixin, APITestCase): def setUp(self): self.client = APIClient() caches[extensions_api_settings.DEFAULT_USE_CACHE].clear() self.content = PublishedContentFactory() self.content.save() def test_failure_reaction_karma_with_client_unauthenticated(self): author = ProfileFactory() reaction = ContentReactionFactory( author=author.user, position=1, related_content=self.content, pubdate=datetime.datetime.now() ) response = self.client.put(reverse("api:content:reaction-karma", args=(reaction.pk,))) self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) def tearDown(self): if os.path.isdir(settings.ZDS_APP["content"]["repo_private_path"]): shutil.rmtree(settings.ZDS_APP["content"]["repo_private_path"]) if os.path.isdir(settings.ZDS_APP["content"]["repo_public_path"]): shutil.rmtree(settings.ZDS_APP["content"]["repo_public_path"]) if os.path.isdir(settings.MEDIA_ROOT): shutil.rmtree(settings.MEDIA_ROOT) def test_failure_reaction_karma_with_sanctioned_user(self): author = ProfileFactory() reaction = ContentReactionFactory(author=author.user, position=1, related_content=self.content) profile = ProfileFactory() profile.can_read = False profile.can_write = False profile.save() self.client.force_login(profile.user) response = self.client.put(reverse("api:content:reaction-karma", args=(reaction.pk,))) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_failure_reaction_karma_with_a_message_not_found(self): response = self.client.get(reverse("api:content:reaction-karma", args=(99999,))) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) def test_success_reaction_karma_like(self): author = ProfileFactory() reaction = ContentReactionFactory(author=author.user, position=1, related_content=self.content) profile = ProfileFactory() self.client.force_login(profile.user) response = self.client.put(reverse("api:content:reaction-karma", args=(reaction.pk,)), {"vote": "like"}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(CommentVote.objects.filter(user=profile.user, comment=reaction, positive=True).exists()) def test_success_reaction_karma_dislike(self): author = ProfileFactory() reaction = ContentReactionFactory(author=author.user, position=1, related_content=self.content) profile = ProfileFactory() self.client.force_login(profile.user) response = self.client.put(reverse("api:content:reaction-karma", args=(reaction.pk,)), {"vote": "dislike"}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(CommentVote.objects.filter(user=profile.user, comment=reaction, positive=False).exists()) def test_success_reaction_karma_neutral(self): author = ProfileFactory() reaction = ContentReactionFactory(author=author.user, position=1, related_content=self.content) profile = ProfileFactory() vote = CommentVote(user=profile.user, comment=reaction, positive=True) vote.save() self.assertTrue(CommentVote.objects.filter(pk=vote.pk).exists()) self.client.force_login(profile.user) response = self.client.put(reverse("api:content:reaction-karma", args=(reaction.pk,)), {"vote": "neutral"}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertFalse(CommentVote.objects.filter(pk=vote.pk).exists()) def test_success_reaction_karma_like_already_disliked(self): author = ProfileFactory() reaction = ContentReactionFactory(author=author.user, position=1, related_content=self.content) profile = ProfileFactory() vote = CommentVote(user=profile.user, comment=reaction, positive=False) vote.save() self.client.force_login(profile.user) response = self.client.put(reverse("api:content:reaction-karma", args=(reaction.pk,)), {"vote": "like"}) vote.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(vote.positive) def test_get_content_reaction_voters(self): author = ProfileFactory() profile = ProfileFactory() profile2 = ProfileFactory() upvoted_reaction = ContentReactionFactory(author=author.user, position=2, related_content=self.content) upvoted_reaction.like += 2 upvoted_reaction.save() CommentVote.objects.create(user=profile.user, comment=upvoted_reaction, positive=True) downvoted_reaction = ContentReactionFactory(author=author.user, position=3, related_content=self.content) downvoted_reaction.dislike += 2 downvoted_reaction.save() anon_limit = CommentVote.objects.create(user=profile.user, comment=downvoted_reaction, positive=False) CommentVote.objects.create(user=profile2.user, comment=upvoted_reaction, positive=True) CommentVote.objects.create(user=profile2.user, comment=downvoted_reaction, positive=False) equal_reaction = ContentReactionFactory(author=author.user, position=4, related_content=self.content) equal_reaction.like += 1 equal_reaction.dislike += 1 equal_reaction.save() CommentVote.objects.create(user=profile.user, comment=equal_reaction, positive=True) CommentVote.objects.create(user=profile2.user, comment=equal_reaction, positive=False) self.client.force_login(profile.user) # on first message we should see 2 likes and 0 anonymous response = self.client.get(reverse("api:content:reaction-karma", args=[upvoted_reaction.pk])) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(2, len(response.data["like"]["users"])) self.assertEqual(0, len(response.data["dislike"]["users"])) self.assertEqual(2, response.data["like"]["count"]) self.assertEqual(0, response.data["dislike"]["count"]) # on second message we should see 2 dislikes and 0 anonymous response = self.client.get(reverse("api:content:reaction-karma", args=[downvoted_reaction.pk])) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(0, len(response.data["like"]["users"])) self.assertEqual(2, len(response.data["dislike"]["users"])) self.assertEqual(0, response.data["like"]["count"]) self.assertEqual(2, response.data["dislike"]["count"]) # on third message we should see 1 like and 1 dislike and 0 anonymous response = self.client.get(reverse("api:content:reaction-karma", args=[equal_reaction.pk])) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(1, len(response.data["like"]["users"])) self.assertEqual(1, len(response.data["dislike"]["users"])) self.assertEqual(1, response.data["like"]["count"]) self.assertEqual(1, response.data["dislike"]["count"]) # Now we change the settings to keep anonymous the first [dis]like previous_limit = settings.VOTES_ID_LIMIT settings.VOTES_ID_LIMIT = anon_limit.pk # and we run the same tests # on first message we should see 1 like and 1 anonymous response = self.client.get(reverse("api:content:reaction-karma", args=[upvoted_reaction.pk])) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(1, len(response.data["like"]["users"])) self.assertEqual(0, len(response.data["dislike"]["users"])) self.assertEqual(2, response.data["like"]["count"]) self.assertEqual(0, response.data["dislike"]["count"]) # on second message we should see 1 dislikes and 1 anonymous response = self.client.get(reverse("api:content:reaction-karma", args=[downvoted_reaction.pk])) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(0, len(response.data["like"]["users"])) self.assertEqual(1, len(response.data["dislike"]["users"])) self.assertEqual(0, response.data["like"]["count"]) self.assertEqual(2, response.data["dislike"]["count"]) # on third message we should see 1 like and 1 dislike and 0 anonymous response = self.client.get(reverse("api:content:reaction-karma", args=[equal_reaction.pk])) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(1, len(response.data["like"]["users"])) self.assertEqual(1, len(response.data["dislike"]["users"])) self.assertEqual(1, response.data["like"]["count"]) self.assertEqual(1, response.data["dislike"]["count"]) settings.VOTES_ID_LIMIT = previous_limit
def test_boosts(self): """Check if boosts are doing their job""" if not self.manager.connected_to_es: return # 1. Create topics (with identical titles), posts (with identical texts), an article and a tuto text = "test" topic_1_solved_sticky = TopicFactory(forum=self.forum, author=self.user) topic_1_solved_sticky.title = text topic_1_solved_sticky.subtitle = "" topic_1_solved_sticky.solved_by = self.user topic_1_solved_sticky.is_sticky = True topic_1_solved_sticky.save() post_1 = PostFactory(topic=topic_1_solved_sticky, author=self.user, position=1) post_1.text = post_1.text_html = text post_1.save() post_2_useful = PostFactory(topic=topic_1_solved_sticky, author=self.user, position=2) post_2_useful.text = post_2_useful.text_html = text post_2_useful.is_useful = True post_2_useful.like = 5 post_2_useful.dislike = 2 # l/d ratio above 1 post_2_useful.save() topic_2_locked = TopicFactory(forum=self.forum, author=self.user, title=text) topic_2_locked.title = text topic_2_locked.subtitle = "" topic_2_locked.is_locked = True topic_2_locked.save() post_3_ld_below_1 = PostFactory(topic=topic_2_locked, author=self.user, position=1) post_3_ld_below_1.text = post_3_ld_below_1.text_html = text post_3_ld_below_1.like = 2 post_3_ld_below_1.dislike = 5 # l/d ratio below 1 post_3_ld_below_1.save() tuto = PublishableContentFactory(type="TUTORIAL") tuto_draft = tuto.load_version() tuto.title = text tuto.authors.add(self.user) tuto.save() tuto_draft.repo_update_top_container(text, tuto.slug, text, text) chapter1 = ContainerFactory(parent=tuto_draft, db_object=tuto) chapter1.repo_update(text, "Who cares ?", "Same here") ExtractFactory(container=chapter1, db_object=tuto) published_tuto = publish_content(tuto, tuto_draft, is_major_update=True) tuto.sha_public = tuto_draft.current_version tuto.sha_draft = tuto_draft.current_version tuto.public_version = published_tuto tuto.save() article = PublishedContentFactory(type="ARTICLE", title=text) published_article = PublishedContent.objects.get(content_pk=article.pk) opinion_not_picked = PublishedContentFactory(type="OPINION", title=text) published_opinion_not_picked = PublishedContent.objects.get( content_pk=opinion_not_picked.pk) opinion_picked = PublishedContentFactory(type="OPINION", title=text) opinion_picked.sha_picked = opinion_picked.sha_draft opinion_picked.date_picked = datetime.datetime.now() opinion_picked.save() published_opinion_picked = PublishedContent.objects.get( content_pk=opinion_picked.pk) for model in self.indexable: if model is FakeChapter: continue self.manager.es_bulk_indexing_of_model(model) self.manager.refresh_index() self.assertEqual( len( self.manager.setup_search(Search().query( MatchAll())).execute()), 10) # 2. Reset all boosts to 1 for doc_type in settings.ZDS_APP["search"]["boosts"]: for key in settings.ZDS_APP["search"]["boosts"][doc_type]: settings.ZDS_APP["search"]["boosts"][doc_type][key] = 1.0 # 3. Test posts 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, 3) # score are equals without boost: self.assertTrue(response[0].meta.score == response[1].meta.score == response[2].meta.score) settings.ZDS_APP["search"]["boosts"]["post"]["if_first"] = 2.0 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, 3) self.assertTrue(response[0].meta.score == response[1].meta.score > response[2].meta.score) self.assertEqual(response[2].meta.id, str( post_2_useful.pk)) # post 2 is the only one not first settings.ZDS_APP["search"]["boosts"]["post"]["if_first"] = 1.0 settings.ZDS_APP["search"]["boosts"]["post"]["if_useful"] = 2.0 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, 3) self.assertTrue(response[0].meta.score > response[1].meta.score == response[2].meta.score) self.assertEqual(response[0].meta.id, str(post_2_useful.pk)) # post 2 is useful settings.ZDS_APP["search"]["boosts"]["post"]["if_useful"] = 1.0 settings.ZDS_APP["search"]["boosts"]["post"]["ld_ratio_above_1"] = 2.0 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, 3) self.assertTrue(response[0].meta.score > response[1].meta.score == response[2].meta.score) self.assertEqual(response[0].meta.id, str( post_2_useful.pk)) # post 2 have a l/d ratio of 5/2 settings.ZDS_APP["search"]["boosts"]["post"]["ld_ratio_above_1"] = 1.0 settings.ZDS_APP["search"]["boosts"]["post"][ "ld_ratio_below_1"] = 2.0 # no one would do that in real life 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, 3) self.assertTrue(response[0].meta.score > response[1].meta.score == response[2].meta.score) self.assertEqual(response[0].meta.id, str( post_3_ld_below_1.pk)) # post 3 have a l/d ratio of 2/5 settings.ZDS_APP["search"]["boosts"]["post"]["ld_ratio_below_1"] = 1.0 # 4. Test topics result = self.client.get(reverse("search:query") + "?q=" + text + "&models=" + Topic.get_es_document_type(), follow=False) self.assertEqual(result.status_code, 200) response = result.context["object_list"].execute() self.assertEqual(response.hits.total, 2) # score are equals without boost: self.assertTrue(response[0].meta.score == response[1].meta.score) settings.ZDS_APP["search"]["boosts"]["topic"]["if_sticky"] = 2.0 result = self.client.get(reverse("search:query") + "?q=" + text + "&models=" + Topic.get_es_document_type(), follow=False) self.assertEqual(result.status_code, 200) response = result.context["object_list"].execute() self.assertEqual(response.hits.total, 2) self.assertTrue(response[0].meta.score > response[1].meta.score) self.assertEqual(response[0].meta.id, str(topic_1_solved_sticky.pk)) # topic 1 is sticky settings.ZDS_APP["search"]["boosts"]["topic"]["if_sticky"] = 1.0 settings.ZDS_APP["search"]["boosts"]["topic"]["if_solved"] = 2.0 result = self.client.get(reverse("search:query") + "?q=" + text + "&models=" + Topic.get_es_document_type(), follow=False) self.assertEqual(result.status_code, 200) response = result.context["object_list"].execute() self.assertEqual(response.hits.total, 2) self.assertTrue(response[0].meta.score > response[1].meta.score) self.assertEqual(response[0].meta.id, str(topic_1_solved_sticky.pk)) # topic 1 is solved settings.ZDS_APP["search"]["boosts"]["topic"]["if_solved"] = 1.0 settings.ZDS_APP["search"]["boosts"]["topic"][ "if_locked"] = 2.0 # no one would do that in real life result = self.client.get(reverse("search:query") + "?q=" + text + "&models=" + Topic.get_es_document_type(), follow=False) self.assertEqual(result.status_code, 200) response = result.context["object_list"].execute() self.assertEqual(response.hits.total, 2) self.assertTrue(response[0].meta.score > response[1].meta.score) self.assertEqual(response[0].meta.id, str(topic_2_locked.pk)) # topic 2 is locked settings.ZDS_APP["search"]["boosts"]["topic"][ "if_locked"] = 1.0 # no one would do that in real life # 5. Test published contents result = self.client.get(reverse("search:query") + "?q=" + text + "&models=content", follow=False) self.assertEqual(result.status_code, 200) response = result.context["object_list"].execute() self.assertEqual(response.hits.total, 5) # score are equals without boost: self.assertTrue( response[0].meta.score == response[1].meta.score == response[2]. meta.score == response[3].meta.score == response[4].meta.score) settings.ZDS_APP["search"]["boosts"]["publishedcontent"][ "if_article"] = 2.0 result = self.client.get(reverse("search:query") + "?q=" + text + "&models=content", follow=False) self.assertEqual(result.status_code, 200) response = result.context["object_list"].execute() self.assertEqual(response.hits.total, 5) self.assertTrue(response[0].meta.score > response[1].meta.score) self.assertEqual(response[0].meta.id, str(published_article.pk)) # obvious settings.ZDS_APP["search"]["boosts"]["publishedcontent"][ "if_article"] = 1.0 settings.ZDS_APP["search"]["boosts"]["publishedcontent"][ "if_tutorial"] = 2.0 result = self.client.get(reverse("search:query") + "?q=" + text + "&models=content", follow=False) self.assertEqual(result.status_code, 200) response = result.context["object_list"].execute() self.assertEqual(response.hits.total, 5) self.assertTrue(response[0].meta.score > response[1].meta.score) self.assertEqual(response[0].meta.id, str(published_tuto.pk)) # obvious settings.ZDS_APP["search"]["boosts"]["publishedcontent"][ "if_tutorial"] = 1.0 settings.ZDS_APP["search"]["boosts"]["publishedcontent"][ "if_opinion"] = 2.0 settings.ZDS_APP["search"]["boosts"]["publishedcontent"][ "if_opinion_not_picked"] = 4.0 # Note: in "real life", unpicked opinion would get a boost < 1. result = self.client.get(reverse("search:query") + "?q=" + text + "&models=content", follow=False) self.assertEqual(result.status_code, 200) response = result.context["object_list"].execute() self.assertEqual(response.hits.total, 5) self.assertTrue(response[0].meta.score > response[1].meta.score > response[2].meta.score) self.assertEqual( response[0].meta.id, str(published_opinion_not_picked.pk)) # unpicked opinion got first self.assertEqual(response[1].meta.id, str(published_opinion_picked.pk)) settings.ZDS_APP["search"]["boosts"]["publishedcontent"][ "if_opinion"] = 1.0 settings.ZDS_APP["search"]["boosts"]["publishedcontent"][ "if_opinion_not_picked"] = 1.0 settings.ZDS_APP["search"]["boosts"]["publishedcontent"][ "if_medium_or_big_tutorial"] = 2.0 result = self.client.get(reverse("search:query") + "?q=" + text + "&models=content", follow=False) self.assertEqual(result.status_code, 200) response = result.context["object_list"].execute() self.assertEqual(response.hits.total, 5) self.assertTrue(response[0].meta.score > response[1].meta.score) self.assertEqual(response[0].meta.id, str(published_tuto.pk)) # obvious settings.ZDS_APP["search"]["boosts"]["publishedcontent"][ "if_medium_or_big_tutorial"] = 1.0 # 6. Test global boosts # NOTE: score are NOT the same for all documents, no matter how hard it tries to, small differences exists for model in self.indexable: # set a huge number to overcome the small differences: settings.ZDS_APP["search"]["boosts"][ model.get_es_document_type()]["global"] = 10.0 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, 10) self.assertEqual(response[0].meta.doc_type, model.get_es_document_type()) # obvious settings.ZDS_APP["search"]["boosts"][ model.get_es_document_type()]["global"] = 1.0
def test_toggle(self): author = ProfileFactory() self.client.force_login(author.user) # create topic and toggle request category = ForumCategoryFactory(position=1) forum = ForumFactory(category=category, position_in_category=1) topic = TopicFactory(forum=forum, author=author.user) response = self.client.post( reverse("topic-edit") + f"?topic={topic.pk}", {"request_featured": 1}, HTTP_X_REQUESTED_WITH="XMLHttpRequest", ) self.assertEqual(200, response.status_code) self.assertEqual(FeaturedRequested.objects.count(), 1) r = FeaturedRequested.objects.last() self.assertEqual(r.content_object, topic) self.assertIn(author.user, r.users_voted.all()) # lock topic: cannot vote anymore topic.is_locked = True topic.save() response = self.client.post( reverse("topic-edit") + f"?topic={topic.pk}", {"request_featured": 1}, HTTP_X_REQUESTED_WITH="XMLHttpRequest", ) self.assertEqual(403, response.status_code) self.assertEqual(FeaturedRequested.objects.count(), 1) # create tutorial and toggle request tutorial = PublishedContentFactory(author_list=[author.user]) gallery = GalleryFactory() image = ImageFactory(gallery=gallery) tutorial.image = image tutorial.save() response = self.client.post( reverse("content:request-featured", kwargs={"pk": tutorial.pk}), {"request_featured": 1}, HTTP_X_REQUESTED_WITH="XMLHttpRequest", ) self.assertEqual(200, response.status_code) self.assertEqual(FeaturedRequested.objects.count(), 2) r = FeaturedRequested.objects.last() self.assertEqual(r.content_object, tutorial) self.assertIn(author.user, r.users_voted.all()) # create opinion: cannot toggle request! opinion = PublishedContentFactory(type="OPINION", author_list=[author.user]) gallery = GalleryFactory() image = ImageFactory(gallery=gallery) opinion.image = image opinion.save() response = self.client.post( reverse("content:request-featured", kwargs={"pk": opinion.pk}), {"request_featured": 1}, HTTP_X_REQUESTED_WITH="XMLHttpRequest", ) self.assertEqual(403, response.status_code) self.assertEqual(FeaturedRequested.objects.count(), 2) # set tutorial as obsolete: cannot toggle tutorial.is_obsolete = True tutorial.save() response = self.client.post( reverse("content:request-featured", kwargs={"pk": tutorial.pk}), {"request_featured": 1}, HTTP_X_REQUESTED_WITH="XMLHttpRequest", ) self.assertEqual(403, response.status_code) r = FeaturedRequested.objects.get(pk=r.pk) self.assertEqual(r.content_object, tutorial) self.assertIn(author.user, r.users_voted.all()) # reject tutorial proposition tutorial.is_obsolete = False # can vote again tutorial.save() r = FeaturedRequested.objects.get(pk=r.pk) r.rejected = True r.save() # upvote with other user other = ProfileFactory() self.client.force_login(other.user) response = self.client.post( reverse("content:request-featured", kwargs={"pk": tutorial.pk}), {"request_featured": 1}, HTTP_X_REQUESTED_WITH="XMLHttpRequest", ) self.assertEqual(200, response.status_code) r = FeaturedRequested.objects.get(pk=r.pk) self.assertIn(other.user, r.users_voted.all()) self.assertFalse(r.rejected) # not rejected anymore # reject for good, cannot vote anymore! r.rejected_for_good = True r.save() response = self.client.post( reverse("content:request-featured", kwargs={"pk": tutorial.pk}), {"request_featured": 1}, HTTP_X_REQUESTED_WITH="XMLHttpRequest", ) self.assertEqual(403, response.status_code) r = FeaturedRequested.objects.get(pk=r.pk) self.assertIn(other.user, r.users_voted.all())