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]) notif_signals.new_content.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_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(force_slug_update=False) publish_content(content, content.load_version(), True) notify_update(content, True, True) notifs = interventions_topics(self.user1) self.assertEqual(1, len(notifs), str(notifs))
def test_top_tags_content(self): """Unit testing top_categories_content method """ tags_tuto = ['a', 'b', 'c'] tags_article = ['a', 'd', 'e'] content = PublishedContentFactory(type='TUTORIAL', author_list=[ProfileFactory().user]) content.add_tags(tags_tuto) content.save() tags_tuto = content.tags.all() content = PublishedContentFactory(type='ARTICLE', author_list=[ProfileFactory().user]) content.add_tags(tags_article) content.save() tags_article = content.tags.all() top_tags_tuto = top_categories_content('TUTORIAL').get('tags') top_tags_article = top_categories_content('ARTICLE').get('tags') self.assertEqual(list(top_tags_tuto), list(tags_tuto)) self.assertEqual(list(top_tags_article), list(tags_article))
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_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_tutos()), 1)
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('tutorial:list')) self.assertContains(resp, tutorial.title) self.assertNotContains(resp, tutorial_unpublished.title) resp = self.client.get(reverse('article:list')) self.assertContains(resp, article.title) self.assertNotContains(resp, article_unpublished.title) resp = self.client.get( reverse('content:find-tutorial', args=[self.user_author.pk]) + '?filter=public') self.assertContains(resp, tutorial.title) self.assertNotContains(resp, tutorial_unpublished.title) resp = self.client.get( reverse('content:find-tutorial', args=[self.user_author.pk]) + '?filter=redaction') self.assertEqual(resp.status_code, 403) resp = self.client.get( reverse('content:find-article', args=[self.user_author.pk]) + '?filter=public') self.assertContains(resp, article.title) self.assertNotContains(resp, article_unpublished.title) resp = self.client.get( reverse('content:find-article', args=[self.user_author.pk]) + '?filter=redaction') self.assertEqual(resp.status_code, 403) resp = self.client.get( reverse('content:find-article', args=[self.user_author.pk]) + '?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.new_content.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_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_ping_on_tuto(self): """Error from #4904""" content = PublishedContentFactory(author_list=[self.user1]) self.assertTrue( self.client.login(username=self.user2.username, password='******')) result = self.client.post( reverse('content:add-reaction') + '?pk={}'.format(content.pk), { 'text': '@{}'.format(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_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_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 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_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_accessible_ui_for_author(self): opinion = PublishedContentFactory(author_list=[self.user_author], type='OPINION') self.assertEqual( self.client.login(username=self.user_author.username, password='******'), True) 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')
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 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_last_participation_is_old(self): article = PublishedContentFactory(author_list=[self.user_author], type="ARTICLE") newUser = ProfileFactory().user reac = ContentReaction(author=self.user_author, position=1, related_content=article) reac.update_content("I will find you. And I Will Kill you.") reac.save() article.last_note = reac article.save() self.assertFalse(last_participation_is_old(article, newUser)) ContentRead(user=self.user_author, note=reac, content=article).save() reac = ContentReaction(author=newUser, position=2, related_content=article) reac.update_content("I will find you. And I Will Kill you.") reac.save() article.last_note = reac article.save() ContentRead(user=newUser, note=reac, content=article).save() self.assertFalse(last_participation_is_old(article, newUser)) self.assertTrue(last_participation_is_old(article, self.user_author))
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_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_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_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.assertEqual( self.client.login(username=self.user_author.username, password="******"), True) 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_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.assertEqual( self.client.login(username=self.user_author.username, password='******'), True) 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_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() login_check = self.client.login(username=staff.user.username, password='******') self.assertTrue(login_check) response = self.client.get('{}{}'.format( reverse('featured-resource-create'), '?content_type=published_content&content_id=1')) initial_dict = response.context['form'].initial self.assertEqual(initial_dict['title'], tutorial.title) self.assertEqual(initial_dict['authors'], '{}, {}'.format(author, author2)) self.assertEqual(initial_dict['type'], _('Un tutoriel')) self.assertEqual(initial_dict['url'], 'http://testserver/tutoriels/1/mon-contenu-no0/') self.assertEqual(initial_dict['image_url'], image.physical.url)
def test_toggle(self): author = ProfileFactory() login_check = self.client.login(username=author.user.username, password='******') self.assertTrue(login_check) # create topic and toggle request category = CategoryFactory(position=1) forum = ForumFactory(category=category, position_in_category=1) topic = TopicFactory(forum=forum, author=author.user) response = self.client.post(reverse('topic-edit') + '?topic={}'.format(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') + '?topic={}'.format(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() login_check = self.client.login(username=other.user.username, password='******') self.assertTrue(login_check) 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())
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 register. user = ProfileFactory() login_check = self.client.login(username=user.user.username, password='******') self.assertEqual(login_check, True) 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.login(username=self.staff.username, password="******") # 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=CategoryFactory()) 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) 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) # login and unregister: login_check = self.client.login(username=user.user.username, password='******') self.assertEqual(login_check, True) 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( 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.assertEquals(alone_gallery.get_linked_users().count(), 1) self.assertEquals(shared_gallery.get_linked_users().count(), 1) self.assertEquals( UserGallery.objects.filter(user=user.user).count(), 0) self.assertEquals( CommentVote.objects.filter(user=user.user, positive=True).count(), 0) self.assertEquals( 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)
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.assertTrue( self.client.login(username=author.user.username, password="******")) 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.assertTrue( self.client.login(username=author.user.username, password="******")) 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_filters(self): # create topic and content and toggle request author = ProfileFactory().user category = CategoryFactory(position=1) forum = ForumFactory(category=category, position_in_category=1) topic = TopicFactory(forum=forum, author=author) FeaturedRequested.objects.toogle_request(topic, author) tutorial = PublishedContentFactory(author_list=[author]) gallery = GalleryFactory() image = ImageFactory(gallery=gallery) tutorial.image = image tutorial.save() FeaturedRequested.objects.toogle_request(tutorial, author) # without filter staff = StaffProfileFactory() login_check = self.client.login(username=staff.user.username, password='******') self.assertTrue(login_check) response = self.client.get(reverse('featured-resource-requests')) self.assertEqual(200, response.status_code) self.assertEqual(len(response.context['featured_request_list']), 2) self.assertTrue( any(r.content_object == topic for r in response.context['featured_request_list'])) self.assertTrue( any(r.content_object == tutorial for r in response.context['featured_request_list'])) # filter topic response = self.client.get( reverse('featured-resource-requests') + '?type=topic') self.assertEqual(200, response.status_code) self.assertEqual(len(response.context['featured_request_list']), 1) self.assertTrue( any(r.content_object == topic for r in response.context['featured_request_list'])) self.assertFalse( any(r.content_object == tutorial for r in response.context['featured_request_list'])) # filter tuto response = self.client.get( reverse('featured-resource-requests') + '?type=content') self.assertEqual(200, response.status_code) self.assertEqual(len(response.context['featured_request_list']), 1) self.assertFalse( any(r.content_object == topic for r in response.context['featured_request_list'])) self.assertTrue( any(r.content_object == tutorial for r in response.context['featured_request_list'])) # reject topic content_type = ContentType.objects.get_for_model(topic) q = FeaturedRequested.objects.get(object_id=topic.pk, content_type__pk=content_type.pk) q.rejected = True q.save() response = self.client.get( reverse('featured-resource-requests') + '?type=topic') self.assertEqual(200, response.status_code) self.assertEqual(len(response.context['featured_request_list']), 0) # filter ignored response = self.client.get( reverse('featured-resource-requests') + '?type=ignored') self.assertEqual(200, response.status_code) self.assertEqual(len(response.context['featured_request_list']), 1) self.assertTrue( any(r.content_object == topic for r in response.context['featured_request_list'])) # put back vote count to 0 for tutorial FeaturedRequested.objects.toogle_request(tutorial, author) response = self.client.get( reverse('featured-resource-requests') + '?type=content') self.assertEqual(200, response.status_code) self.assertEqual(len(response.context['featured_request_list']), 0) # does not appear with no votes # upvote topic other = ProfileFactory().user FeaturedRequested.objects.toogle_request(topic, other) response = self.client.get( reverse('featured-resource-requests') + '?type=topic') self.assertEqual(200, response.status_code) self.assertEqual(len(response.context['featured_request_list']), 1) # it is back!
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.is_solved = True 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 setUp(self): self.client = APIClient() caches[extensions_api_settings.DEFAULT_USE_CACHE].clear() self.content = PublishedContentFactory() self.content.save()