Example #1
0
    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())
Example #2
0
 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_accessible_ui_for_author(self):
     opinion = PublishedContentFactory(author_list=[self.user_author], type='OPINION')
     subcategory = SubCategoryFactory()
     opinion.subcategory.add(subcategory)
     opinion.save()
     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')
     self.assertNotContains(resp, '{}?subcategory='.format(reverse('publication:list')))
     self.assertContains(resp, '{}?category='.format(reverse('opinion:list')))
 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.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')
     self.assertNotContains(resp, '{}?subcategory='.format(reverse('publication:list')))
     self.assertContains(resp, '{}?category='.format(reverse('opinion:list')))
Example #5
0
    def test_generate_pdf(self):
        """ensure the behavior of the `python manage.py generate_pdf` commmand"""

        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.have_pdf())
        self.assertTrue(published2.have_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', 'id={}'.format(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 !
Example #6
0
    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)
Example #7
0
 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())
Example #8
0
    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.assertEqual(
            self.client.login(username=self.author.user.username,
                              password='******'), True)
        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)
Example #9
0
    def common_test_mark_as_potential_spam_content(self, content_type):
        content_author = ProfileFactory()
        comment_author = ProfileFactory()
        staff = StaffProfileFactory()

        content: PublishableContent = PublishedContentFactory(
            author_list=[content_author.user], type=content_type)

        self.login(comment_author)
        self.client.post(
            reverse("content:add-reaction") + f"?pk={content.pk}",
            {
                "text": "Pi is not so humble",
                "last_note": "0"
            },
            follow=True,
        )
        content.refresh_from_db()
        reaction = content.last_note

        self.common_test_mark_as_potential_spam(
            url_comments_list=content.get_absolute_url_online(),
            url_comment_edit=reverse("content:update-reaction") +
            f"?message={reaction.pk}&pk={content.pk}",
            comment=reaction,
            author=comment_author,
            staff=staff,
        )
Example #10
0
 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.login(
         username=self.user_author.username,
         password='******')
     resp = self.client.get(reverse('content:find-tutorial', args=[self.user_author.pk]))
     self.assertContains(resp, tutorial.title)
     self.assertContains(resp, tutorial_unpublished.title)
     self.assertContains(resp, 'content-illu')
     resp = self.client.get(reverse('content:find-article', args=[self.user_author.pk]))
     self.assertContains(resp, article.title)
     self.assertContains(resp, article_unpublished.title)
     self.assertContains(resp, 'content-illu')
Example #11
0
    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)
Example #12
0
 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)))
Example #13
0
    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.assertTrue(
            self.client.login(username=not_author.user.username,
                              password="******"))
        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.assertTrue(
            self.client.login(username=staff.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.assertGreater(requests_count, 0)

        # And if the user is an author, it should too
        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)

        # 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()
Example #14
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])

        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)))
Example #15
0
    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)
Example #16
0
 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))
Example #17
0
    def test_top_tags_content(self):
        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 = topbar_publication_categories('TUTORIAL').get('tags')
        top_tags_article = topbar_publication_categories('ARTICLE').get('tags')

        self.assertEqual(list(top_tags_tuto), list(tags_tuto))
        self.assertEqual(list(top_tags_article), list(tags_article))
Example #18
0
 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")
Example #19
0
 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()))
Example #20
0
 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))
Example #21
0
 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")))
Example #22
0
    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())
Example #23
0
 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={}'.format(
             tutorial.pk)))
     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{}'.format(tutorial.get_absolute_url_online()))
     self.assertEqual(
         initial_dict['image_url'],
         'http://testserver{}'.format(image.physical['featured'].url))
Example #24
0
    def test_adjust_nb_letters(self):
        """Test the `adjust_nb_letters` command"""

        article = PublishedContentFactory(type='ARTICLE',
                                          author_list=[self.user_author])
        published = PublishedContent.objects.filter(content=article).first()
        published.nb_letter = None
        published.save()

        call_command('adjust_nb_letters')

        published = PublishedContent.objects.get(pk=published.pk)
        self.assertEqual(published.nb_letter, published.get_nb_letters())
Example #25
0
 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={}'.format(tutorial.pk)))
     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{}'.format(tutorial.get_absolute_url_online()))
     self.assertEqual(initial_dict['image_url'], image.physical.url)
Example #26
0
 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 = get_header_notifications(self.user1)['general_notifications']['list']
     self.assertEqual(1, len(notifs), str(notifs))
Example #27
0
 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)
Example #28
0
 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)
Example #29
0
 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)
Example #30
0
 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)
Example #31
0
 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()))
Example #32
0
 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'])
Example #33
0
 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"])
Example #34
0
    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))
Example #35
0
 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'])
Example #36
0
    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))
Example #37
0
    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
Example #38
0
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.assertTrue(self.client.login(username=profile.user.username, password='******'))
        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.assertTrue(self.client.login(username=profile.user.username, password='******'))
        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.assertTrue(self.client.login(username=profile.user.username, password='******'))
        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.assertTrue(self.client.login(username=profile.user.username, password='******'))
        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.assertTrue(self.client.login(username=profile.user.username, password='******'))
        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.assertTrue(self.client.login(username=profile.user.username, password='******'))

        # 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
        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'])
Example #39
0
 def setUp(self):
     self.client = APIClient()
     caches[extensions_api_settings.DEFAULT_USE_CACHE].clear()
     self.content = PublishedContentFactory()
     self.content.save()