Skip to content

Commit

Permalink
wip test
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Jul 3, 2023
1 parent 234c744 commit e139ae9
Showing 1 changed file with 54 additions and 50 deletions.
104 changes: 54 additions & 50 deletions lacommunaute/forum_conversation/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,6 @@ def test_machina_route_forbidden(self):
self.assertEqual(response.status_code, 403)


# @patch("machina.apps.forum.views.ForumView.perform_permissions_check", return_value=True)
# @patch("machina.apps.forum_permission.handler.PermissionHandler.can_edit_post", return_value=True)
class PostUpdateViewTest(TestCase):
@classmethod
def setUpTestData(cls):
Expand Down Expand Up @@ -526,25 +524,26 @@ def test_has_liked(self):
self.assertContains(response, '<i class="ri-heart-3-fill" aria-hidden="true"></i><span class="ml-1">1</span>')

def test_queryset(self):
answered_topic = TopicFactory(with_post=True, forum=self.forum)
PostFactory(topic=answered_topic)
certified_topic = TopicFactory(with_post=True, with_certified_post=True, forum=self.forum)

nonreadable_forum = ForumFactory(kind=ForumKind.PRIVATE_FORUM)
nonreadable_topic = TopicFactory(with_post=True, forum=nonreadable_forum)

self.client.force_login(self.user)
TopicFactory(with_post=True, forum=ForumFactory(kind=ForumKind.PRIVATE_FORUM, with_public_perms=True))
TopicFactory(with_post=True, forum=ForumFactory(kind=ForumKind.NEWS, with_public_perms=True))

response = self.client.get(self.url)

self.assertEqual(response.status_code, 200)
self.assertEqual(response.context_data["total"], 3)
self.assertNotContains(response, nonreadable_topic.subject)
self.assertEqual(response.context_data["total"], 1)

for topic in Topic.objects.filter(forum=self.forum):
for topic in Topic.objects.exclude(id=self.topic.id):
with self.subTest(topic):
self.assertNotContains(response, topic.subject)

for topic in Topic.objects.filter(id=self.topic.id):
with self.subTest(topic):
self.assertContains(response, topic.subject)

def test_queryset_for_unanswered_topic(self):
TopicFactory(with_post=True, forum=ForumFactory(kind=ForumKind.PRIVATE_FORUM, with_public_perms=True))
TopicFactory(with_post=True, forum=ForumFactory(kind=ForumKind.NEWS, with_public_perms=True))

response = self.client.get(self.url + "?filter=NEW")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context_data["total"], 1)
Expand All @@ -554,52 +553,26 @@ def test_queryset(self):
with self.subTest(topic):
self.assertNotContains(response, topic.subject)

def test_queryset_for_certified_topic(self):
certified_topic = TopicFactory(
with_post=True, with_certified_post=True, forum=ForumFactory(with_public_perms=True)
)
TopicFactory(
with_post=True,
with_certified_post=True,
forum=ForumFactory(kind=ForumKind.PRIVATE_FORUM, with_public_perms=True),
)

response = self.client.get(self.url + "?filter=CERTIFIED")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context_data["total"], 1)
self.assertContains(response, certified_topic.subject)
self.assertContains(response, certified_topic.certified_post.post.content.raw[:100])

for topic in Topic.objects.exclude(id=certified_topic.id):
with self.subTest(topic):
self.assertNotContains(response, topic.subject)

def test_unanswerd_topics_visibility(self):
url = self.url + "?filter=NEW"
self.client.force_login(self.user)

response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertContains(response, self.topic.subject)

self.forum.kind = ForumKind.PRIVATE_FORUM
self.forum.save()

response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertNotContains(response, self.topic.subject)

def test_certified_topics_list_content(self):
certified_private_topic = TopicFactory(
with_certified_post=True, forum=ForumFactory(kind=ForumKind.PRIVATE_FORUM)
)
certified_public_topic = TopicFactory(with_certified_post=True, forum=self.forum)
topic = TopicFactory(with_post=True)
self.client.force_login(self.user)

response = self.client.get(self.url + "?filter=CERTIFIED")
self.assertEqual(response.status_code, 200)

self.assertContains(response, certified_public_topic.first_post.subject)
self.assertContains(response, str(certified_public_topic.first_post.content)[:100])
self.assertContains(response, str(certified_public_topic.certified_post.post.content)[:100])

self.assertNotContains(response, certified_private_topic.first_post.subject)
self.assertNotContains(response, str(certified_private_topic.first_post.content)[:100])
self.assertNotContains(response, str(certified_private_topic.certified_post.post.content)[:100])

self.assertNotContains(response, topic.first_post.subject)
self.assertNotContains(response, str(topic.first_post.content)[:100])

def test_pagination(self):
self.client.force_login(self.user)
TopicFactory.create_batch(9, with_post=True, forum=self.forum)
Expand Down Expand Up @@ -641,3 +614,34 @@ def test_template_name(self):

response = self.client.get(self.url, **{"HTTP_HX_REQUEST": "true"})
self.assertTemplateUsed(response, "forum_conversation/topic_list.html")


class NewsFeedTopicListViewTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.url = reverse("forum_conversation_extension:newsfeed")

def test_template_name(self):
response = self.client.get(self.url)
self.assertTemplateUsed(response, "forum_conversation/topics_newsfeed.html")

response = self.client.get(self.url, **{"HTTP_HX_REQUEST": "true"})
self.assertTemplateUsed(response, "forum_conversation/topic_list_newsfeed.html")

def test_queryset(self):
news_topic = TopicFactory(with_post=True, forum=ForumFactory(kind=ForumKind.NEWS, with_public_perms=True))
TopicFactory(with_post=True, forum=ForumFactory(kind=ForumKind.PRIVATE_FORUM, with_public_perms=True))
TopicFactory(with_post=True, forum=ForumFactory(kind=ForumKind.PUBLIC_FORUM, with_public_perms=True))

response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertContains(response, news_topic.subject)
for topic in Topic.objects.exclude(id=news_topic.id):
with self.subTest(topic):
self.assertNotContains(response, topic.subject)

def test_context_data(self):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context_data["forum"], None)
self.assertEqual(response.context_data["loadmoretopic_url"], reverse("forum_conversation_extension:newsfeed"))

0 comments on commit e139ae9

Please sign in to comment.