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 12, 2024
1 parent a23d75f commit 984e3ff
Showing 1 changed file with 50 additions and 47 deletions.
97 changes: 50 additions & 47 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 @@ -24,7 +22,7 @@
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 +285,37 @@ 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):
post = AnonymousPostFactory(topic=TopicFactory(forum=self.forum))
session = self.client.session
session["_anonymous_forum_key"] = post.anonymous_key
session.save()

response = self.client.post(
reverse(
"forum_conversation:topic_update",
kwargs={
"forum_slug": self.forum.slug,
"forum_pk": self.forum.pk,
"slug": post.topic.slug,
"pk": post.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": post.topic.slug,
"pk": post.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,44 +348,6 @@ 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))
session = self.client.session
Expand Down Expand Up @@ -450,23 +441,35 @@ 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": post.content.raw, "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)

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

response = self.client.post(
url,
Expand Down

0 comments on commit 984e3ff

Please sign in to comment.