Skip to content

Commit

Permalink
(forum_conversation) remove patch usage on anonymous posts update, se…
Browse files Browse the repository at this point in the history
…eding anonymous_key to get explicitly representative tests
  • Loading branch information
vincentporte committed Feb 13, 2024
1 parent dba9b54 commit a820486
Showing 1 changed file with 64 additions and 59 deletions.
123 changes: 64 additions & 59 deletions lacommunaute/forum_conversation/tests/tests_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from unittest.mock import patch

import pytest
from django.conf import settings
from django.contrib.messages.api import get_messages
Expand All @@ -19,12 +17,13 @@
from lacommunaute.forum_conversation.enums import Filters
from lacommunaute.forum_conversation.factories import (
AnonymousPostFactory,
AnonymousTopicFactory,
CertifiedPostFactory,
PostFactory,
TopicFactory,
)
from lacommunaute.forum_conversation.forms import PostForm
from lacommunaute.forum_conversation.models import Post, Topic
from lacommunaute.forum_conversation.models import Topic
from lacommunaute.forum_conversation.views import PostDeleteView, TopicCreateView
from lacommunaute.forum_upvote.factories import UpVoteFactory
from lacommunaute.notification.factories import BouncedDomainNameFactory, BouncedEmailFactory
Expand Down Expand Up @@ -287,6 +286,38 @@ def test_selected_tags_are_checked(self):
not_checked_box = f'class="form-check-input" type="checkbox" name="tags" value="{tag.id}" >'
self.assertContains(response, not_checked_box)

def test_update_by_anonymous_user(self):
topic = AnonymousTopicFactory(with_post=True, forum=self.forum)
session = self.client.session
session["_anonymous_forum_key"] = topic.first_post.anonymous_key
session.save()
updated_subject = faker.word()

response = self.client.post(
reverse(
"forum_conversation:topic_update",
kwargs={
"forum_slug": self.forum.slug,
"forum_pk": self.forum.pk,
"slug": topic.slug,
"pk": topic.pk,
},
),
{"subject": updated_subject, "content": faker.paragraph(nb_sentences=5), "username": "[email protected]"},
)
self.assertRedirects(
response,
reverse(
"forum_conversation:topic",
kwargs={
"forum_slug": self.forum.slug,
"forum_pk": self.forum.pk,
"slug": updated_subject,
"pk": topic.pk,
},
),
)

def test_topic_update_with_nonfr_content(self, *args):
self.client.force_login(self.poster)
post_data = {"subject": "s", "content": "популярные лучшие песни слушать онлайн"}
Expand Down Expand Up @@ -319,48 +350,10 @@ def test_topic_update_with_html_content(self, *args):
self.assertFalse(self.topic.first_post.approved)
self.assertEqual("HTML tags detected", self.topic.first_post.update_reason)

def test_update_by_anonymous_user(self):
response = self.client.post(
reverse(
"forum_conversation:topic_create",
kwargs={
"forum_slug": self.forum.slug,
"forum_pk": self.forum.pk,
},
),
{"subject": "subject", "content": "La communauté", "username": "[email protected]"},
)
post = Post.objects.select_related("topic").get(username="[email protected]")
topic = post.topic
response = self.client.post(
reverse(
"forum_conversation:topic_update",
kwargs={
"forum_slug": self.forum.slug,
"forum_pk": self.forum.pk,
"slug": topic.slug,
"pk": topic.pk,
},
),
{"subject": "subject", "content": "La communauté", "username": "[email protected]"},
)
self.assertRedirects(
response,
reverse(
"forum_conversation:topic",
kwargs={
"forum_slug": self.forum.slug,
"forum_pk": self.forum.pk,
"slug": topic.slug,
"pk": topic.pk,
},
),
)

def test_topic_update_with_bounced_domain_name(self, *args):
post = AnonymousPostFactory(topic=TopicFactory(forum=self.forum))
topic = AnonymousTopicFactory(with_post=True, forum=self.forum)
session = self.client.session
session["_anonymous_forum_key"] = post.anonymous_key
session["_anonymous_forum_key"] = topic.first_post.anonymous_key
session.save()
BouncedDomainNameFactory(domain="blackhat.com")

Expand All @@ -370,17 +363,17 @@ def test_topic_update_with_bounced_domain_name(self, *args):
kwargs={
"forum_slug": self.forum.slug,
"forum_pk": self.forum.pk,
"slug": post.topic.slug,
"pk": post.topic.pk,
"slug": topic.slug,
"pk": topic.pk,
},
),
{"subject": "subject", "content": "La communauté", "username": "[email protected]"},
)

self.assertEqual(response.status_code, 302)
post.refresh_from_db()
self.assertFalse(post.approved)
self.assertEqual("Bounced Domain detected", post.update_reason)
topic.refresh_from_db()
self.assertFalse(topic.first_post.approved)
self.assertEqual("Bounced Domain detected", topic.first_post.update_reason)


class PostCreateViewTest(TestCase):
Expand Down Expand Up @@ -450,33 +443,45 @@ def test_update_post_as_authenticated_user(self, *args):
self.assertIsNone(self.post.username)
self.assertTrue(self.post.approved)

@patch("machina.apps.forum_conversation.views.PostUpdateView.perform_permissions_check", return_value=True)
def test_update_post_as_anonymous_user(self, *args):
self.post_data["username"] = faker.email()
url = reverse("forum_conversation:post_update", kwargs=self.kwargs)
post = AnonymousPostFactory(topic=self.topic)
session = self.client.session
session["_anonymous_forum_key"] = post.anonymous_key
session.save()
url = reverse(
"forum_conversation:post_update",
kwargs={
"forum_slug": self.forum.slug,
"forum_pk": self.forum.pk,
"topic_slug": self.topic.slug,
"topic_pk": self.topic.pk,
"pk": post.pk,
},
)

post_data = {"content": faker.paragraph(nb_sentences=5), "username": post.username}

response = self.client.post(
url,
self.post_data,
post_data,
follow=True,
)

self.assertEqual(response.status_code, 200)
self.post.refresh_from_db()
self.assertEqual(self.post.username, self.post_data["username"])
self.assertTrue(self.post.approved)
post.refresh_from_db()
self.assertTrue(post.approved)

BouncedEmailFactory(email=self.post_data["username"])
BouncedEmailFactory(email=post.username)

response = self.client.post(
url,
self.post_data,
post_data,
follow=True,
)

self.assertEqual(response.status_code, 200)
self.post.refresh_from_db()
self.assertFalse(self.post.approved)
post.refresh_from_db()
self.assertFalse(post.approved)

def test_update_post_with_nonfr_content(self, *args):
self.client.force_login(self.poster)
Expand Down

0 comments on commit a820486

Please sign in to comment.