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 test_success_reaction_karma_like(self): author = ProfileFactory() reaction = ContentReactionFactory(author=author.user, position=1, related_content=self.content) profile = ProfileFactory() self.client.force_login(profile.user) response = self.client.put(reverse("api:content:reaction-karma", args=(reaction.pk,)), {"vote": "like"}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(CommentVote.objects.filter(user=profile.user, comment=reaction, positive=True).exists())
def test_notification_read(self): """ When the notification is a reaction, it is marked as read when the corresponding content is displayed to the user. """ ContentReactionFactory(related_content=self.tuto, author=self.user1, position=1) last_note = ContentReactionFactory(related_content=self.tuto, author=self.user2, position=2) self.tuto.last_note = last_note self.tuto.save() notification = Notification.objects.get(subscription__user=self.user1) self.assertFalse(notification.is_read) result = self.client.get(reverse("tutorial:view", args=[self.tuto.pk, self.tuto.slug]), follow=False) self.assertEqual(result.status_code, 200) notification = Notification.objects.get(subscription__user=self.user1) self.assertTrue(notification.is_read)
def test_failure_reaction_karma_with_sanctioned_user(self): author = ProfileFactory() reaction = ContentReactionFactory(author=author.user, position=1, related_content=self.content) profile = ProfileFactory() profile.can_read = False profile.can_write = False profile.save() self.client.force_login(profile.user) response = self.client.put(reverse("api:content:reaction-karma", args=(reaction.pk,))) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def load_comment_content(cli, size, fake, *_, **__): """ Load content's comments """ nb_avg_posts = size * 20 cli.stdout.write(f"Nombres de messages à poster en moyenne : {nb_avg_posts}") tps1 = time.time() contents = list(PublishableContent.objects.filter(sha_public__isnull=False)) nb_contents = len(contents) profiles = list(Profile.objects.all()) nb_users = len(profiles) for i in range(0, nb_contents): nb_posts = randint(0, nb_avg_posts * 2) post = None for j in range(0, nb_posts): post = ContentReactionFactory( related_content=contents[i], author=profiles[j % nb_users].user, position=j + 1 ) post.text = fake.paragraph(nb_sentences=5, variable_nb_sentences=True) post.text_html = emarkdown(post.text) post.save() sys.stdout.write(f"Contenu {i + 1}/{nb_contents} \tCommentaire {j + 1}/{nb_posts} \r") sys.stdout.flush() contents[i].last_note = post contents[i].save() tps2 = time.time() cli.stdout.write(f"\nFait en {tps2 - tps1:.3f} sec")
def test_success_reaction_karma_like_already_disliked(self): author = ProfileFactory() reaction = ContentReactionFactory(author=author.user, position=1, related_content=self.content) profile = ProfileFactory() vote = CommentVote(user=profile.user, comment=reaction, positive=False) vote.save() self.client.force_login(profile.user) response = self.client.put(reverse("api:content:reaction-karma", args=(reaction.pk,)), {"vote": "like"}) vote.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(vote.positive)
def test_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_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_get_content_reaction_voters(self): author = ProfileFactory() profile = ProfileFactory() profile2 = ProfileFactory() upvoted_reaction = ContentReactionFactory(author=author.user, position=2, related_content=self.content) upvoted_reaction.like += 2 upvoted_reaction.save() CommentVote.objects.create(user=profile.user, comment=upvoted_reaction, positive=True) downvoted_reaction = ContentReactionFactory(author=author.user, position=3, related_content=self.content) downvoted_reaction.dislike += 2 downvoted_reaction.save() anon_limit = CommentVote.objects.create(user=profile.user, comment=downvoted_reaction, positive=False) CommentVote.objects.create(user=profile2.user, comment=upvoted_reaction, positive=True) CommentVote.objects.create(user=profile2.user, comment=downvoted_reaction, positive=False) equal_reaction = ContentReactionFactory(author=author.user, position=4, related_content=self.content) equal_reaction.like += 1 equal_reaction.dislike += 1 equal_reaction.save() CommentVote.objects.create(user=profile.user, comment=equal_reaction, positive=True) CommentVote.objects.create(user=profile2.user, comment=equal_reaction, positive=False) self.client.force_login(profile.user) # on first message we should see 2 likes and 0 anonymous response = self.client.get(reverse("api:content:reaction-karma", args=[upvoted_reaction.pk])) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(2, len(response.data["like"]["users"])) self.assertEqual(0, len(response.data["dislike"]["users"])) self.assertEqual(2, response.data["like"]["count"]) self.assertEqual(0, response.data["dislike"]["count"]) # on second message we should see 2 dislikes and 0 anonymous response = self.client.get(reverse("api:content:reaction-karma", args=[downvoted_reaction.pk])) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(0, len(response.data["like"]["users"])) self.assertEqual(2, len(response.data["dislike"]["users"])) self.assertEqual(0, response.data["like"]["count"]) self.assertEqual(2, response.data["dislike"]["count"]) # on third message we should see 1 like and 1 dislike and 0 anonymous response = self.client.get(reverse("api:content:reaction-karma", args=[equal_reaction.pk])) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(1, len(response.data["like"]["users"])) self.assertEqual(1, len(response.data["dislike"]["users"])) self.assertEqual(1, response.data["like"]["count"]) self.assertEqual(1, response.data["dislike"]["count"]) # Now we change the settings to keep anonymous the first [dis]like previous_limit = settings.VOTES_ID_LIMIT settings.VOTES_ID_LIMIT = anon_limit.pk # and we run the same tests # on first message we should see 1 like and 1 anonymous response = self.client.get(reverse("api:content:reaction-karma", args=[upvoted_reaction.pk])) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(1, len(response.data["like"]["users"])) self.assertEqual(0, len(response.data["dislike"]["users"])) self.assertEqual(2, response.data["like"]["count"]) self.assertEqual(0, response.data["dislike"]["count"]) # on second message we should see 1 dislikes and 1 anonymous response = self.client.get(reverse("api:content:reaction-karma", args=[downvoted_reaction.pk])) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(0, len(response.data["like"]["users"])) self.assertEqual(1, len(response.data["dislike"]["users"])) self.assertEqual(0, response.data["like"]["count"]) self.assertEqual(2, response.data["dislike"]["count"]) # on third message we should see 1 like and 1 dislike and 0 anonymous response = self.client.get(reverse("api:content:reaction-karma", args=[equal_reaction.pk])) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(1, len(response.data["like"]["users"])) self.assertEqual(1, len(response.data["dislike"]["users"])) self.assertEqual(1, response.data["like"]["count"]) self.assertEqual(1, response.data["dislike"]["count"]) settings.VOTES_ID_LIMIT = previous_limit