Improve tests

This commit is contained in:
Antoine Bartuccio 2024-07-10 17:09:12 +02:00 committed by Bartuccio Antoine
parent 6240eff160
commit 7e98e184a0

View File

@ -24,7 +24,7 @@ from forum.models import Forum, ForumMessage, ForumTopic
@pytest.mark.django_db @pytest.mark.django_db
class TestTopicCreation: class TestTopicCreation:
def test_topic_creation(self, client: Client): def test_topic_creation_success(self, client: Client):
user: User = User.objects.get(username="root") user: User = User.objects.get(username="root")
forum = Forum.objects.get(name="AE") forum = Forum.objects.get(name="AE")
client.force_login(user) client.force_login(user)
@ -32,15 +32,29 @@ class TestTopicCreation:
"title": "Hello IT.", "title": "Hello IT.",
"message": "Have you tried turning it off and on again ?", "message": "Have you tried turning it off and on again ?",
} }
assert not ForumTopic.objects.filter(_title=payload["title"]).first() assert not ForumTopic.objects.filter(_title=payload["title"]).exists()
response = client.post(reverse("forum:new_topic", args=str(forum.id)), payload) response = client.post(reverse("forum:new_topic", args=str(forum.id)), payload)
assertRedirects( assertRedirects(
response, response,
expected_url=reverse( expected_url=reverse(
"forum:view_message", args=str(ForumMessage.objects.all().count()) "forum:view_message",
args=str(ForumMessage.objects.order_by("date").last().id),
), # Get the last created message id ), # Get the last created message id
target_status_code=302, target_status_code=302,
) )
topic = ForumTopic.objects.filter(_title=payload["title"]).first() topic = ForumTopic.objects.filter(_title=payload["title"]).first()
assert topic assert topic
assert topic.last_message.message == payload["message"] assert topic.last_message.message == payload["message"]
def test_topic_creation_failure(self, client: Client):
user: User = User.objects.get(username="krophil")
forum = Forum.objects.get(name="AE")
client.force_login(user)
payload = {
"title": "You shall",
"message": "Not pass !",
}
assert not ForumTopic.objects.filter(_title=payload["title"]).exists()
response = client.post(reverse("forum:new_topic", args=str(forum.id)), payload)
assert response.status_code == 403
assert not ForumTopic.objects.filter(_title=payload["title"]).exists()