Sith/forum/tests.py

78 lines
3.0 KiB
Python
Raw Normal View History

#
# Copyright 2023 © AE UTBM
# ae@utbm.fr / ae.info@utbm.fr
#
# This file is part of the website of the UTBM Student Association (AE UTBM),
# https://ae.utbm.fr.
#
# You can find the source code of the website at https://github.com/ae-utbm/sith
#
# LICENSED UNDER THE GNU GENERAL PUBLIC LICENSE VERSION 3 (GPLv3)
2024-09-23 08:25:27 +00:00
# SEE : https://raw.githubusercontent.com/ae-utbm/sith/master/LICENSE
# OR WITHIN THE LOCAL FILE "LICENSE"
#
#
2024-07-09 14:55:07 +00:00
import pytest
2024-07-22 09:40:11 +00:00
from django.conf import settings
2024-07-09 14:55:07 +00:00
from django.test import Client
from django.urls import reverse
2024-07-10 08:17:08 +00:00
from pytest_django.asserts import assertRedirects
2024-07-09 14:55:07 +00:00
from core.models import User
2024-07-10 08:17:08 +00:00
from forum.models import Forum, ForumMessage, ForumTopic
2024-07-09 14:55:07 +00:00
@pytest.mark.django_db
class TestTopicCreation:
2024-07-22 09:40:11 +00:00
def test_topic_creation_ok(self, client: Client):
2024-07-09 14:55:07 +00:00
user: User = User.objects.get(username="root")
forum = Forum.objects.get(name="AE")
client.force_login(user)
payload = {
"title": "Hello IT.",
"message": "Have you tried turning it off and on again ?",
2024-07-22 09:40:11 +00:00
settings.HONEYPOT_FIELD_NAME_FORUM: settings.HONEYPOT_VALUE,
2024-07-09 14:55:07 +00:00
}
2024-07-10 15:09:12 +00:00
assert not ForumTopic.objects.filter(_title=payload["title"]).exists()
2024-07-09 14:55:07 +00:00
response = client.post(reverse("forum:new_topic", args=str(forum.id)), payload)
2024-07-10 08:17:08 +00:00
assertRedirects(
response,
expected_url=reverse(
2024-07-10 15:09:12 +00:00
"forum:view_message",
args=str(ForumMessage.objects.order_by("date").last().id),
2024-07-10 08:17:08 +00:00
), # Get the last created message id
target_status_code=302,
)
2024-07-09 14:55:07 +00:00
topic = ForumTopic.objects.filter(_title=payload["title"]).first()
assert topic
assert topic.last_message.message == payload["message"]
2024-07-10 15:09:12 +00:00
2024-07-22 09:40:11 +00:00
def test_topic_creation_honeypot_fail(self, client: Client):
user: User = User.objects.get(username="root")
forum = Forum.objects.get(name="AE")
client.force_login(user)
payload = {
"title": "You shall",
"message": "Not pass !",
settings.HONEYPOT_FIELD_NAME_FORUM: settings.HONEYPOT_VALUE + "random",
}
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 == 200
assert not ForumTopic.objects.filter(_title=payload["title"]).exists()
def test_topic_creation_fail(self, client: Client):
2024-07-10 15:09:12 +00:00
user: User = User.objects.get(username="krophil")
forum = Forum.objects.get(name="AE")
client.force_login(user)
payload = {
"title": "You shall",
"message": "Not pass !",
2024-07-22 09:40:11 +00:00
settings.HONEYPOT_FIELD_NAME_FORUM: settings.HONEYPOT_VALUE,
2024-07-10 15:09:12 +00:00
}
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()