2019-05-16 14:51:30 +00:00
|
|
|
#
|
|
|
|
# Copyright 2019
|
|
|
|
# - Sli <antoine@bartuccio.fr>
|
|
|
|
#
|
|
|
|
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
|
|
|
|
# http://ae.utbm.fr.
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify it under
|
|
|
|
# the terms of the GNU General Public License a published by the Free Software
|
|
|
|
# Foundation; either version 3 of the License, or (at your option) any later
|
|
|
|
# version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along with
|
|
|
|
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
|
|
|
|
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2024-07-18 18:23:30 +00:00
|
|
|
import pytest
|
2019-07-04 13:32:00 +00:00
|
|
|
from django.conf import settings
|
2024-07-18 18:23:30 +00:00
|
|
|
from django.test import Client, TestCase
|
2019-10-06 11:28:56 +00:00
|
|
|
from django.urls import reverse
|
2022-08-03 22:26:43 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2019-06-16 10:19:04 +00:00
|
|
|
|
2024-06-24 11:07:36 +00:00
|
|
|
from core.models import Notification, User
|
2019-06-20 12:22:06 +00:00
|
|
|
from pedagogy.models import UV, UVComment, UVCommentReport
|
2019-06-16 10:19:04 +00:00
|
|
|
|
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
def create_uv_template(user_id, code="IFC1", exclude_list=None):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Factory to help UV creation/update in post requests."""
|
2024-06-26 17:10:24 +00:00
|
|
|
if exclude_list is None:
|
|
|
|
exclude_list = []
|
2019-06-16 11:10:27 +00:00
|
|
|
uv = {
|
|
|
|
"code": code,
|
|
|
|
"author": user_id,
|
|
|
|
"credit_type": "TM",
|
|
|
|
"semester": "SPRING",
|
|
|
|
"language": "FR",
|
2019-06-18 08:56:05 +00:00
|
|
|
"department": "TC",
|
2019-06-16 11:10:27 +00:00
|
|
|
"credits": 3,
|
|
|
|
"hours_CM": 10,
|
|
|
|
"hours_TD": 28,
|
|
|
|
"hours_TP": 0,
|
|
|
|
"hours_THE": 37,
|
|
|
|
"hours_TE": 0,
|
|
|
|
"manager": "Gilles BERTRAND",
|
|
|
|
"title": "Algorithmique et programmation : niveau I, initiés - partie I",
|
|
|
|
"objectives": """* Introduction à l'algorithmique et à la programmation pour initiés.
|
2019-06-16 10:19:04 +00:00
|
|
|
* Pratiques et développement en langage C.""",
|
2019-06-16 11:10:27 +00:00
|
|
|
"program": """* Découverte des outils élémentaires utilisés pour écrire, compiler et exécuter un programme écrit en langage C
|
2019-06-16 10:19:04 +00:00
|
|
|
* Règles de programmation : normes en cours, règles de présentation du code, commentaires
|
|
|
|
* Initiation à l'algorithmique et découverte des bases du langage C :
|
|
|
|
* les conditions
|
|
|
|
* les boucles
|
|
|
|
* les types de données
|
|
|
|
* les tableaux à une dimension
|
|
|
|
* manipulations des chaînes de caractères
|
|
|
|
* les fonctions et procédures""",
|
2019-06-16 11:10:27 +00:00
|
|
|
"skills": "* D'écrire un algorithme et de l'implémenter en C",
|
|
|
|
"key_concepts": """* Algorithme
|
2019-06-16 10:19:04 +00:00
|
|
|
* Variables scalaires et vectorielles
|
|
|
|
* Structures alternatives, répétitives
|
|
|
|
* Fonctions, procédures
|
|
|
|
* Chaînes de caractères""",
|
2019-06-16 11:10:27 +00:00
|
|
|
}
|
|
|
|
for excluded in exclude_list:
|
|
|
|
uv.pop(excluded)
|
|
|
|
return uv
|
|
|
|
|
|
|
|
|
2019-06-16 16:26:30 +00:00
|
|
|
# UV class tests
|
|
|
|
|
|
|
|
|
2024-07-23 22:39:26 +00:00
|
|
|
class TestUVCreation(TestCase):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test uv creation."""
|
2019-06-16 11:10:27 +00:00
|
|
|
|
2023-05-02 09:00:23 +00:00
|
|
|
@classmethod
|
2024-06-26 17:10:24 +00:00
|
|
|
def setUpTestData(cls):
|
2023-05-02 09:00:23 +00:00
|
|
|
cls.bibou = User.objects.get(username="root")
|
|
|
|
cls.tutu = User.objects.get(username="tutu")
|
|
|
|
cls.sli = User.objects.get(username="sli")
|
|
|
|
cls.guy = User.objects.get(username="guy")
|
2024-06-26 17:10:24 +00:00
|
|
|
cls.create_uv_url = reverse("pedagogy:uv_create")
|
2019-06-16 10:19:04 +00:00
|
|
|
|
|
|
|
def test_create_uv_admin_success(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.bibou)
|
2019-06-16 10:19:04 +00:00
|
|
|
response = self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.create_uv_url, create_uv_template(self.bibou.id)
|
2019-06-16 10:19:04 +00:00
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 302
|
|
|
|
assert UV.objects.filter(code="IFC1").exists()
|
2019-06-16 10:19:04 +00:00
|
|
|
|
|
|
|
def test_create_uv_pedagogy_admin_success(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.tutu)
|
2019-06-16 10:19:04 +00:00
|
|
|
response = self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.create_uv_url, create_uv_template(self.tutu.id)
|
2019-06-16 10:19:04 +00:00
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 302
|
|
|
|
assert UV.objects.filter(code="IFC1").exists()
|
2019-06-16 10:19:04 +00:00
|
|
|
|
|
|
|
def test_create_uv_unauthorized_fail(self):
|
|
|
|
# Test with anonymous user
|
2024-06-26 17:10:24 +00:00
|
|
|
response = self.client.post(self.create_uv_url, create_uv_template(0))
|
|
|
|
assert response.status_code == 403
|
2019-06-16 10:19:04 +00:00
|
|
|
|
|
|
|
# Test with subscribed user
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.sli)
|
|
|
|
response = self.client.post(self.create_uv_url, create_uv_template(self.sli.id))
|
|
|
|
assert response.status_code == 403
|
2019-06-16 10:19:04 +00:00
|
|
|
|
|
|
|
# Test with non subscribed user
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.guy)
|
|
|
|
response = self.client.post(self.create_uv_url, create_uv_template(self.guy.id))
|
|
|
|
assert response.status_code == 403
|
2019-06-16 10:19:04 +00:00
|
|
|
|
|
|
|
# Check that the UV has never been created
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not UV.objects.filter(code="IFC1").exists()
|
2019-06-16 10:19:04 +00:00
|
|
|
|
|
|
|
def test_create_uv_bad_request_fail(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.tutu)
|
2019-06-16 10:19:04 +00:00
|
|
|
|
|
|
|
# Test with wrong user id (if someone cheats on the hidden input)
|
|
|
|
response = self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.create_uv_url, create_uv_template(self.bibou.id)
|
2019-06-16 10:19:04 +00:00
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 200
|
2019-06-16 10:19:04 +00:00
|
|
|
|
|
|
|
# Remove a required field
|
|
|
|
response = self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.create_uv_url,
|
2019-06-16 11:10:27 +00:00
|
|
|
create_uv_template(self.tutu.id, exclude_list=["title"]),
|
2019-06-16 10:19:04 +00:00
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 200
|
2019-06-16 10:19:04 +00:00
|
|
|
|
|
|
|
# Check that the UV hase never been created
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not UV.objects.filter(code="IFC1").exists()
|
2019-06-16 10:44:55 +00:00
|
|
|
|
|
|
|
|
2024-07-18 18:23:30 +00:00
|
|
|
@pytest.mark.django_db
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("username", "expected_code"),
|
|
|
|
[
|
|
|
|
("root", 200),
|
|
|
|
("tutu", 200),
|
|
|
|
("sli", 200),
|
|
|
|
("old_subscriber", 200),
|
|
|
|
("public", 403),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_guide_permissions(client: Client, username: str, expected_code: int):
|
|
|
|
client.force_login(User.objects.get(username=username))
|
|
|
|
res = client.get(reverse("pedagogy:guide"))
|
|
|
|
assert res.status_code == expected_code
|
2024-06-26 17:10:24 +00:00
|
|
|
|
2019-06-16 10:44:55 +00:00
|
|
|
|
2024-07-18 18:23:30 +00:00
|
|
|
@pytest.mark.django_db
|
|
|
|
def test_guide_anonymous_permission_denied(client: Client):
|
|
|
|
res = client.get(reverse("pedagogy:guide"))
|
|
|
|
assert res.status_code == 302
|
2019-06-16 11:10:27 +00:00
|
|
|
|
|
|
|
|
2024-07-23 22:39:26 +00:00
|
|
|
class TestUVDelete(TestCase):
|
2024-06-26 17:10:24 +00:00
|
|
|
"""Test UV deletion rights."""
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.bibou = User.objects.get(username="root")
|
|
|
|
cls.tutu = User.objects.get(username="tutu")
|
|
|
|
cls.sli = User.objects.get(username="sli")
|
|
|
|
cls.guy = User.objects.get(username="guy")
|
|
|
|
cls.uv = UV.objects.get(code="PA00")
|
|
|
|
cls.delete_uv_url = reverse("pedagogy:uv_delete", kwargs={"uv_id": cls.uv.id})
|
2019-06-16 11:10:27 +00:00
|
|
|
|
2019-06-16 16:26:30 +00:00
|
|
|
def test_uv_delete_root_success(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.bibou)
|
|
|
|
self.client.post(self.delete_uv_url)
|
|
|
|
assert not UV.objects.filter(pk=self.uv.pk).exists()
|
2019-06-16 11:10:27 +00:00
|
|
|
|
2019-06-16 16:26:30 +00:00
|
|
|
def test_uv_delete_pedagogy_admin_success(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.tutu)
|
|
|
|
self.client.post(self.delete_uv_url)
|
|
|
|
assert not UV.objects.filter(pk=self.uv.pk).exists()
|
2019-06-16 11:10:27 +00:00
|
|
|
|
2019-06-16 16:26:30 +00:00
|
|
|
def test_uv_delete_pedagogy_unauthorized_fail(self):
|
2019-06-16 11:10:27 +00:00
|
|
|
# Anonymous user
|
2024-06-26 17:10:24 +00:00
|
|
|
response = self.client.post(self.delete_uv_url)
|
|
|
|
assert response.status_code == 403
|
|
|
|
assert UV.objects.filter(pk=self.uv.pk).exists()
|
2019-06-16 11:10:27 +00:00
|
|
|
|
|
|
|
# Not subscribed user
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.guy)
|
|
|
|
response = self.client.post(self.delete_uv_url)
|
|
|
|
assert response.status_code == 403
|
|
|
|
assert UV.objects.filter(pk=self.uv.pk).exists()
|
2019-06-16 11:10:27 +00:00
|
|
|
|
|
|
|
# Simply subscribed user
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.sli)
|
|
|
|
response = self.client.post(self.delete_uv_url)
|
|
|
|
assert response.status_code == 403
|
|
|
|
assert UV.objects.filter(pk=self.uv.pk).exists()
|
2019-06-16 11:10:27 +00:00
|
|
|
|
|
|
|
|
2024-07-23 22:39:26 +00:00
|
|
|
class TestUVUpdate(TestCase):
|
2024-06-26 17:10:24 +00:00
|
|
|
"""Test UV update rights."""
|
2019-06-16 11:10:27 +00:00
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.bibou = User.objects.get(username="root")
|
|
|
|
cls.tutu = User.objects.get(username="tutu")
|
|
|
|
cls.sli = User.objects.get(username="sli")
|
|
|
|
cls.guy = User.objects.get(username="guy")
|
|
|
|
cls.uv = UV.objects.get(code="PA00")
|
|
|
|
cls.update_uv_url = reverse("pedagogy:uv_update", kwargs={"uv_id": cls.uv.id})
|
2019-06-16 11:10:27 +00:00
|
|
|
|
2019-06-16 16:26:30 +00:00
|
|
|
def test_uv_update_root_success(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.bibou)
|
2019-06-16 11:10:27 +00:00
|
|
|
self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.update_uv_url, create_uv_template(self.bibou.id, code="PA00")
|
2019-06-16 11:10:27 +00:00
|
|
|
)
|
2019-08-27 20:46:41 +00:00
|
|
|
self.uv.refresh_from_db()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.uv.credit_type == "TM"
|
2019-06-16 11:10:27 +00:00
|
|
|
|
2019-06-16 16:26:30 +00:00
|
|
|
def test_uv_update_pedagogy_admin_success(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.tutu)
|
2019-06-16 11:10:27 +00:00
|
|
|
self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.update_uv_url, create_uv_template(self.bibou.id, code="PA00")
|
2019-08-27 20:46:41 +00:00
|
|
|
)
|
|
|
|
self.uv.refresh_from_db()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.uv.credit_type == "TM"
|
2019-08-27 20:46:41 +00:00
|
|
|
|
|
|
|
def test_uv_update_original_author_does_not_change(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.tutu)
|
2019-08-27 20:46:41 +00:00
|
|
|
response = self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.update_uv_url,
|
2019-06-16 16:26:30 +00:00
|
|
|
create_uv_template(self.tutu.id, code="PA00"),
|
2019-06-16 11:10:27 +00:00
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 200
|
2019-08-27 20:46:41 +00:00
|
|
|
self.uv.refresh_from_db()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.uv.author == self.bibou
|
2019-06-16 11:10:27 +00:00
|
|
|
|
2019-06-16 16:26:30 +00:00
|
|
|
def test_uv_update_pedagogy_unauthorized_fail(self):
|
2019-06-16 11:10:27 +00:00
|
|
|
# Anonymous user
|
2019-06-16 16:26:30 +00:00
|
|
|
response = self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.update_uv_url, create_uv_template(self.bibou.id, code="PA00")
|
2019-06-16 11:10:27 +00:00
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 403
|
2019-06-16 11:10:27 +00:00
|
|
|
|
|
|
|
# Not subscribed user
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.guy)
|
2019-06-16 16:26:30 +00:00
|
|
|
response = self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.update_uv_url, create_uv_template(self.bibou.id, code="PA00")
|
2019-06-16 11:10:27 +00:00
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 403
|
2019-06-16 11:10:27 +00:00
|
|
|
|
|
|
|
# Simply subscribed user
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.sli)
|
2019-06-16 16:26:30 +00:00
|
|
|
response = self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.update_uv_url, create_uv_template(self.bibou.id, code="PA00")
|
2019-06-16 11:10:27 +00:00
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 403
|
2019-06-16 11:10:27 +00:00
|
|
|
|
|
|
|
# Check that the UV has not changed
|
2019-08-27 20:46:41 +00:00
|
|
|
self.uv.refresh_from_db()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.uv.credit_type == "OM"
|
2019-06-16 16:26:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
# UVComment class tests
|
|
|
|
|
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
def create_uv_comment_template(user_id, uv_code="PA00", exclude_list=None):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Factory to help UVComment creation/update in post requests."""
|
2024-06-26 17:10:24 +00:00
|
|
|
if exclude_list is None:
|
|
|
|
exclude_list = []
|
2019-06-16 16:26:30 +00:00
|
|
|
comment = {
|
|
|
|
"author": user_id,
|
|
|
|
"uv": UV.objects.get(code=uv_code).id,
|
2019-06-17 16:12:21 +00:00
|
|
|
"grade_global": 4,
|
|
|
|
"grade_utility": 4,
|
|
|
|
"grade_interest": 4,
|
2019-06-16 16:26:30 +00:00
|
|
|
"grade_teaching": -1,
|
2019-06-17 16:12:21 +00:00
|
|
|
"grade_work_load": 2,
|
2019-06-16 16:26:30 +00:00
|
|
|
"comment": "Superbe UV qui fait vivre la vie associative de l'école",
|
|
|
|
}
|
|
|
|
for excluded in exclude_list:
|
|
|
|
comment.pop(excluded)
|
|
|
|
return comment
|
|
|
|
|
|
|
|
|
2024-07-23 22:39:26 +00:00
|
|
|
class TestUVCommentCreationAndDisplay(TestCase):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test UVComment creation and its display.
|
|
|
|
|
|
|
|
Display and creation are the same view.
|
2019-06-16 16:26:30 +00:00
|
|
|
"""
|
|
|
|
|
2023-05-02 09:00:23 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.bibou = User.objects.get(username="root")
|
|
|
|
cls.tutu = User.objects.get(username="tutu")
|
|
|
|
cls.sli = User.objects.get(username="sli")
|
|
|
|
cls.guy = User.objects.get(username="guy")
|
|
|
|
cls.uv = UV.objects.get(code="PA00")
|
2024-06-26 17:10:24 +00:00
|
|
|
cls.uv_url = reverse("pedagogy:uv_detail", kwargs={"uv_id": cls.uv.id})
|
2019-06-16 16:26:30 +00:00
|
|
|
|
|
|
|
def test_create_uv_comment_admin_success(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.bibou)
|
2019-06-16 16:26:30 +00:00
|
|
|
response = self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.uv_url, create_uv_comment_template(self.bibou.id)
|
2019-06-16 16:26:30 +00:00
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
self.assertRedirects(response, self.uv_url)
|
|
|
|
response = self.client.get(self.uv_url)
|
2019-06-16 16:26:30 +00:00
|
|
|
self.assertContains(response, text="Superbe UV")
|
|
|
|
|
|
|
|
def test_create_uv_comment_pedagogy_admin_success(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.tutu)
|
2019-06-16 16:26:30 +00:00
|
|
|
response = self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.uv_url, create_uv_comment_template(self.tutu.id)
|
2019-06-16 16:26:30 +00:00
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
self.assertRedirects(response, self.uv_url)
|
|
|
|
response = self.client.get(self.uv_url)
|
2019-06-16 16:26:30 +00:00
|
|
|
self.assertContains(response, text="Superbe UV")
|
|
|
|
|
|
|
|
def test_create_uv_comment_subscriber_success(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.sli)
|
2019-06-16 16:26:30 +00:00
|
|
|
response = self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.uv_url, create_uv_comment_template(self.sli.id)
|
2019-06-16 16:26:30 +00:00
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
self.assertRedirects(response, self.uv_url)
|
|
|
|
response = self.client.get(self.uv_url)
|
2019-06-16 16:26:30 +00:00
|
|
|
self.assertContains(response, text="Superbe UV")
|
|
|
|
|
|
|
|
def test_create_uv_comment_unauthorized_fail(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
nb_comments = self.uv.comments.count()
|
2019-06-16 16:26:30 +00:00
|
|
|
# Test with anonymous user
|
2024-06-26 17:10:24 +00:00
|
|
|
response = self.client.post(self.uv_url, create_uv_comment_template(0))
|
|
|
|
assert response.status_code == 403
|
2019-06-16 16:26:30 +00:00
|
|
|
|
|
|
|
# Test with non subscribed user
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.guy)
|
2019-06-16 16:26:30 +00:00
|
|
|
response = self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.uv_url, create_uv_comment_template(self.guy.id)
|
2019-06-16 16:26:30 +00:00
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 403
|
2019-06-16 16:26:30 +00:00
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
# Check that no comment has been created
|
|
|
|
assert self.uv.comments.count() == nb_comments
|
2019-06-16 16:26:30 +00:00
|
|
|
|
|
|
|
def test_create_uv_comment_bad_form_fail(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
nb_comments = self.uv.comments.count()
|
|
|
|
self.client.force_login(self.bibou)
|
2019-06-16 16:26:30 +00:00
|
|
|
response = self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.uv_url,
|
2019-06-16 16:26:30 +00:00
|
|
|
create_uv_comment_template(self.bibou.id, exclude_list=["grade_global"]),
|
|
|
|
)
|
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 200
|
|
|
|
assert self.uv.comments.count() == nb_comments
|
2019-06-16 16:26:30 +00:00
|
|
|
|
2019-09-04 18:49:17 +00:00
|
|
|
def test_create_uv_comment_twice_fail(self):
|
|
|
|
# Checks that the has_user_already_commented method works proprely
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not self.uv.has_user_already_commented(self.bibou)
|
2019-09-04 18:49:17 +00:00
|
|
|
|
|
|
|
# Create a first comment
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.bibou)
|
|
|
|
self.client.post(self.uv_url, create_uv_comment_template(self.bibou.id))
|
2019-09-04 18:49:17 +00:00
|
|
|
|
|
|
|
# Checks that the has_user_already_commented method works proprely
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.uv.has_user_already_commented(self.bibou)
|
2019-09-04 18:49:17 +00:00
|
|
|
|
|
|
|
# Create the second comment
|
|
|
|
comment = create_uv_comment_template(self.bibou.id)
|
|
|
|
comment["comment"] = "Twice"
|
2024-06-26 17:10:24 +00:00
|
|
|
response = self.client.post(self.uv_url, comment)
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert UVComment.objects.filter(comment__contains="Superbe UV").exists()
|
|
|
|
assert not UVComment.objects.filter(comment__contains="Twice").exists()
|
2019-09-04 18:49:17 +00:00
|
|
|
self.assertContains(
|
|
|
|
response,
|
|
|
|
_(
|
|
|
|
"You already posted a comment on this UV. If you want to comment again, please modify or delete your previous comment."
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Ensure that there is no crash when no uv or no author is given
|
|
|
|
self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.uv_url, create_uv_comment_template(self.bibou.id, exclude_list=["uv"])
|
2019-09-04 18:49:17 +00:00
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 200
|
2019-09-04 18:49:17 +00:00
|
|
|
self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.uv_url,
|
2019-09-04 18:49:17 +00:00
|
|
|
create_uv_comment_template(self.bibou.id, exclude_list=["author"]),
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 200
|
2019-09-04 18:49:17 +00:00
|
|
|
|
2019-06-16 16:26:30 +00:00
|
|
|
|
2024-07-23 22:39:26 +00:00
|
|
|
class TestUVCommentDelete(TestCase):
|
2024-06-26 17:10:24 +00:00
|
|
|
"""Test UVComment deletion rights."""
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.bibou = User.objects.get(username="root")
|
|
|
|
cls.tutu = User.objects.get(username="tutu")
|
|
|
|
cls.sli = User.objects.get(username="sli")
|
|
|
|
cls.guy = User.objects.get(username="guy")
|
|
|
|
cls.krophil = User.objects.get(username="krophil")
|
2019-06-16 16:26:30 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
comment_kwargs = create_uv_comment_template(
|
|
|
|
User.objects.get(username="krophil").id
|
|
|
|
)
|
|
|
|
comment_kwargs["author"] = User.objects.get(id=comment_kwargs["author"])
|
|
|
|
comment_kwargs["uv"] = UV.objects.get(id=comment_kwargs["uv"])
|
|
|
|
self.comment = UVComment(**comment_kwargs)
|
|
|
|
self.comment.save()
|
|
|
|
|
|
|
|
def test_uv_comment_delete_root_success(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.bibou)
|
2019-06-16 16:26:30 +00:00
|
|
|
self.client.post(
|
|
|
|
reverse("pedagogy:comment_delete", kwargs={"comment_id": self.comment.id})
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not UVComment.objects.filter(id=self.comment.id).exists()
|
2019-06-16 16:26:30 +00:00
|
|
|
|
|
|
|
def test_uv_comment_delete_pedagogy_admin_success(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.tutu)
|
2019-06-16 16:26:30 +00:00
|
|
|
self.client.post(
|
|
|
|
reverse("pedagogy:comment_delete", kwargs={"comment_id": self.comment.id})
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not UVComment.objects.filter(id=self.comment.id).exists()
|
2019-06-16 16:26:30 +00:00
|
|
|
|
|
|
|
def test_uv_comment_delete_author_success(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.krophil)
|
2019-06-16 16:26:30 +00:00
|
|
|
self.client.post(
|
|
|
|
reverse("pedagogy:comment_delete", kwargs={"comment_id": self.comment.id})
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not UVComment.objects.filter(id=self.comment.id).exists()
|
2019-06-16 16:26:30 +00:00
|
|
|
|
|
|
|
def test_uv_comment_delete_unauthorized_fail(self):
|
|
|
|
# Anonymous user
|
|
|
|
response = self.client.post(
|
|
|
|
reverse("pedagogy:comment_delete", kwargs={"comment_id": self.comment.id})
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 403
|
2019-06-16 16:26:30 +00:00
|
|
|
|
|
|
|
# Unsbscribed user
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.guy)
|
2019-06-16 16:26:30 +00:00
|
|
|
response = self.client.post(
|
|
|
|
reverse("pedagogy:comment_delete", kwargs={"comment_id": self.comment.id})
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 403
|
2019-06-16 16:26:30 +00:00
|
|
|
|
|
|
|
# Subscribed user (not author of the comment)
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.sli)
|
2019-06-16 16:26:30 +00:00
|
|
|
response = self.client.post(
|
|
|
|
reverse("pedagogy:comment_delete", kwargs={"comment_id": self.comment.id})
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 403
|
2019-06-16 16:26:30 +00:00
|
|
|
|
|
|
|
# Check that the comment still exists
|
2024-06-26 17:10:24 +00:00
|
|
|
assert UVComment.objects.filter(id=self.comment.id).exists()
|
2019-06-16 18:05:53 +00:00
|
|
|
|
|
|
|
|
2024-07-23 22:39:26 +00:00
|
|
|
class TestUVCommentUpdate(TestCase):
|
2024-06-26 17:10:24 +00:00
|
|
|
"""Test UVComment update rights."""
|
2019-06-16 18:05:53 +00:00
|
|
|
|
2023-05-02 09:00:23 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2024-06-26 17:10:24 +00:00
|
|
|
cls.bibou = User.objects.get(username="root")
|
|
|
|
cls.tutu = User.objects.get(username="tutu")
|
|
|
|
cls.sli = User.objects.get(username="sli")
|
|
|
|
cls.guy = User.objects.get(username="guy")
|
2023-05-02 09:00:23 +00:00
|
|
|
cls.krophil = User.objects.get(username="krophil")
|
2019-06-16 18:05:53 +00:00
|
|
|
|
2023-05-02 09:00:23 +00:00
|
|
|
def setUp(self):
|
2019-06-16 18:05:53 +00:00
|
|
|
# Prepare a comment
|
|
|
|
comment_kwargs = create_uv_comment_template(self.krophil.id)
|
|
|
|
comment_kwargs["author"] = self.krophil
|
|
|
|
comment_kwargs["uv"] = UV.objects.get(id=comment_kwargs["uv"])
|
|
|
|
self.comment = UVComment(**comment_kwargs)
|
|
|
|
self.comment.save()
|
|
|
|
|
|
|
|
# Prepare edit of this comment for post requests
|
|
|
|
self.comment_edit = create_uv_comment_template(self.krophil.id)
|
|
|
|
self.comment_edit["comment"] = "Edited"
|
|
|
|
|
|
|
|
def test_uv_comment_update_root_success(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.bibou)
|
2019-06-16 18:05:53 +00:00
|
|
|
response = self.client.post(
|
|
|
|
reverse("pedagogy:comment_update", kwargs={"comment_id": self.comment.id}),
|
|
|
|
self.comment_edit,
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 302
|
2019-06-16 18:05:53 +00:00
|
|
|
self.comment.refresh_from_db()
|
2022-08-03 19:48:37 +00:00
|
|
|
self.assertEqual(self.comment.comment, self.comment_edit["comment"])
|
2019-06-16 18:05:53 +00:00
|
|
|
|
|
|
|
def test_uv_comment_update_pedagogy_admin_success(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.tutu)
|
2019-06-16 18:05:53 +00:00
|
|
|
response = self.client.post(
|
|
|
|
reverse("pedagogy:comment_update", kwargs={"comment_id": self.comment.id}),
|
|
|
|
self.comment_edit,
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 302
|
2019-06-16 18:05:53 +00:00
|
|
|
self.comment.refresh_from_db()
|
2022-08-03 19:48:37 +00:00
|
|
|
self.assertEqual(self.comment.comment, self.comment_edit["comment"])
|
2019-06-16 18:05:53 +00:00
|
|
|
|
|
|
|
def test_uv_comment_update_author_success(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.krophil)
|
2019-06-16 18:05:53 +00:00
|
|
|
response = self.client.post(
|
|
|
|
reverse("pedagogy:comment_update", kwargs={"comment_id": self.comment.id}),
|
|
|
|
self.comment_edit,
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 302
|
2019-06-16 18:05:53 +00:00
|
|
|
self.comment.refresh_from_db()
|
2022-08-03 19:48:37 +00:00
|
|
|
self.assertEqual(self.comment.comment, self.comment_edit["comment"])
|
2019-06-16 18:05:53 +00:00
|
|
|
|
|
|
|
def test_uv_comment_update_unauthorized_fail(self):
|
|
|
|
# Anonymous user
|
|
|
|
response = self.client.post(
|
|
|
|
reverse("pedagogy:comment_update", kwargs={"comment_id": self.comment.id}),
|
|
|
|
self.comment_edit,
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 403
|
2019-06-16 18:05:53 +00:00
|
|
|
|
|
|
|
# Unsbscribed user
|
|
|
|
response = self.client.post(
|
|
|
|
reverse("pedagogy:comment_update", kwargs={"comment_id": self.comment.id}),
|
|
|
|
self.comment_edit,
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 403
|
2019-06-16 18:05:53 +00:00
|
|
|
|
|
|
|
# Subscribed user (not author of the comment)
|
|
|
|
response = self.client.post(
|
|
|
|
reverse("pedagogy:comment_update", kwargs={"comment_id": self.comment.id}),
|
|
|
|
self.comment_edit,
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 403
|
2019-06-16 18:05:53 +00:00
|
|
|
|
|
|
|
# Check that the comment hasn't change
|
|
|
|
self.comment.refresh_from_db()
|
2022-08-03 19:48:37 +00:00
|
|
|
self.assertNotEqual(self.comment.comment, self.comment_edit["comment"])
|
2019-06-16 18:05:53 +00:00
|
|
|
|
|
|
|
def test_uv_comment_update_original_author_does_not_change(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.bibou)
|
2019-06-16 18:05:53 +00:00
|
|
|
self.comment_edit["author"] = User.objects.get(username="root").id
|
|
|
|
|
|
|
|
response = self.client.post(
|
|
|
|
reverse("pedagogy:comment_update", kwargs={"comment_id": self.comment.id}),
|
|
|
|
self.comment_edit,
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 200
|
2022-08-03 19:48:37 +00:00
|
|
|
self.assertEqual(self.comment.author, self.krophil)
|
2019-06-19 00:34:51 +00:00
|
|
|
|
|
|
|
|
2024-07-23 22:39:26 +00:00
|
|
|
class TestUVModerationForm(TestCase):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Assert access rights and if the form works well."""
|
2019-06-20 12:22:06 +00:00
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.bibou = User.objects.get(username="root")
|
|
|
|
cls.tutu = User.objects.get(username="tutu")
|
|
|
|
cls.sli = User.objects.get(username="sli")
|
|
|
|
cls.guy = User.objects.get(username="guy")
|
|
|
|
cls.krophil = User.objects.get(username="krophil")
|
2019-06-20 12:22:06 +00:00
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
def setUp(self):
|
2019-06-20 12:22:06 +00:00
|
|
|
# Prepare a comment
|
|
|
|
comment_kwargs = create_uv_comment_template(self.krophil.id)
|
|
|
|
comment_kwargs["author"] = self.krophil
|
|
|
|
comment_kwargs["uv"] = UV.objects.get(id=comment_kwargs["uv"])
|
|
|
|
self.comment_1 = UVComment(**comment_kwargs)
|
|
|
|
self.comment_1.save()
|
|
|
|
|
|
|
|
# Prepare another comment
|
|
|
|
comment_kwargs = create_uv_comment_template(self.krophil.id)
|
|
|
|
comment_kwargs["author"] = self.krophil
|
|
|
|
comment_kwargs["uv"] = UV.objects.get(id=comment_kwargs["uv"])
|
|
|
|
self.comment_2 = UVComment(**comment_kwargs)
|
|
|
|
self.comment_2.save()
|
|
|
|
|
|
|
|
# Prepare a comment report for comment 1
|
|
|
|
self.report_1 = UVCommentReport(
|
|
|
|
comment=self.comment_1, reporter=self.krophil, reason="C'est moche"
|
|
|
|
)
|
|
|
|
self.report_1.save()
|
|
|
|
self.report_1_bis = UVCommentReport(
|
|
|
|
comment=self.comment_1, reporter=self.krophil, reason="C'est moche 2"
|
|
|
|
)
|
|
|
|
self.report_1_bis.save()
|
|
|
|
|
|
|
|
# Prepare a comment report for comment 2
|
|
|
|
self.report_2 = UVCommentReport(
|
|
|
|
comment=self.comment_2, reporter=self.krophil, reason="C'est moche"
|
|
|
|
)
|
|
|
|
self.report_2.save()
|
|
|
|
|
|
|
|
def test_access_authorized_success(self):
|
|
|
|
# Test with root
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.bibou)
|
2019-06-20 12:22:06 +00:00
|
|
|
response = self.client.get(reverse("pedagogy:moderation"))
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 200
|
2019-06-20 12:22:06 +00:00
|
|
|
|
|
|
|
# Test with pedagogy admin
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.tutu)
|
2019-06-20 12:22:06 +00:00
|
|
|
response = self.client.get(reverse("pedagogy:moderation"))
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 200
|
2019-06-20 12:22:06 +00:00
|
|
|
|
|
|
|
def test_access_unauthorized_fail(self):
|
|
|
|
# Test with anonymous user
|
|
|
|
response = self.client.get(reverse("pedagogy:moderation"))
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 403
|
2019-06-20 12:22:06 +00:00
|
|
|
|
|
|
|
# Test with unsubscribed user
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.guy)
|
2019-06-20 12:22:06 +00:00
|
|
|
response = self.client.get(reverse("pedagogy:moderation"))
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 403
|
2019-06-20 12:22:06 +00:00
|
|
|
|
|
|
|
# Test with subscribed user
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.sli)
|
2019-06-20 12:22:06 +00:00
|
|
|
response = self.client.get(reverse("pedagogy:moderation"))
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 403
|
2019-06-20 12:22:06 +00:00
|
|
|
|
|
|
|
def test_do_nothing(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.bibou)
|
2019-06-20 12:22:06 +00:00
|
|
|
response = self.client.post(reverse("pedagogy:moderation"))
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 302
|
2019-06-20 12:22:06 +00:00
|
|
|
|
|
|
|
# Test that nothing has changed
|
2024-06-26 17:10:24 +00:00
|
|
|
assert UVCommentReport.objects.filter(id=self.report_1.id).exists()
|
|
|
|
assert UVComment.objects.filter(id=self.comment_1.id).exists()
|
|
|
|
assert UVCommentReport.objects.filter(id=self.report_1_bis.id).exists()
|
|
|
|
assert UVCommentReport.objects.filter(id=self.report_2.id).exists()
|
|
|
|
assert UVComment.objects.filter(id=self.comment_2.id).exists()
|
2019-06-20 12:22:06 +00:00
|
|
|
|
|
|
|
def test_delete_comment(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.bibou)
|
2019-06-20 12:22:06 +00:00
|
|
|
response = self.client.post(
|
|
|
|
reverse("pedagogy:moderation"), {"accepted_reports": [self.report_1.id]}
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 302
|
2019-06-20 12:22:06 +00:00
|
|
|
|
|
|
|
# Test that the comment and it's associated report has been deleted
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not UVCommentReport.objects.filter(id=self.report_1.id).exists()
|
|
|
|
assert not UVComment.objects.filter(id=self.comment_1.id).exists()
|
2019-06-20 12:22:06 +00:00
|
|
|
# Test that the bis report has been deleted
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not UVCommentReport.objects.filter(id=self.report_1_bis.id).exists()
|
2019-06-20 12:22:06 +00:00
|
|
|
|
|
|
|
# Test that the other comment and report still exists
|
2024-06-26 17:10:24 +00:00
|
|
|
assert UVCommentReport.objects.filter(id=self.report_2.id).exists()
|
|
|
|
assert UVComment.objects.filter(id=self.comment_2.id).exists()
|
2019-06-20 12:22:06 +00:00
|
|
|
|
|
|
|
def test_delete_comment_bulk(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.bibou)
|
2019-06-20 12:22:06 +00:00
|
|
|
response = self.client.post(
|
|
|
|
reverse("pedagogy:moderation"),
|
|
|
|
{"accepted_reports": [self.report_1.id, self.report_2.id]},
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 302
|
2019-06-20 12:22:06 +00:00
|
|
|
|
|
|
|
# Test that comments and their associated reports has been deleted
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not UVCommentReport.objects.filter(id=self.report_1.id).exists()
|
|
|
|
assert not UVComment.objects.filter(id=self.comment_1.id).exists()
|
|
|
|
assert not UVCommentReport.objects.filter(id=self.report_2.id).exists()
|
|
|
|
assert not UVComment.objects.filter(id=self.comment_2.id).exists()
|
2019-06-20 12:22:06 +00:00
|
|
|
# Test that the bis report has been deleted
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not UVCommentReport.objects.filter(id=self.report_1_bis.id).exists()
|
2019-06-20 12:22:06 +00:00
|
|
|
|
|
|
|
def test_delete_comment_with_bis(self):
|
|
|
|
# Test case if two reports targets the same comment and are both deleted
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.bibou)
|
2019-06-20 12:22:06 +00:00
|
|
|
response = self.client.post(
|
|
|
|
reverse("pedagogy:moderation"),
|
|
|
|
{"accepted_reports": [self.report_1.id, self.report_1_bis.id]},
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 302
|
2019-06-20 12:22:06 +00:00
|
|
|
|
|
|
|
# Test that the comment and it's associated report has been deleted
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not UVCommentReport.objects.filter(id=self.report_1.id).exists()
|
|
|
|
assert not UVComment.objects.filter(id=self.comment_1.id).exists()
|
2019-06-20 12:22:06 +00:00
|
|
|
# Test that the bis report has been deleted
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not UVCommentReport.objects.filter(id=self.report_1_bis.id).exists()
|
2019-06-20 12:22:06 +00:00
|
|
|
|
|
|
|
def test_delete_report(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.bibou)
|
2019-06-20 12:22:06 +00:00
|
|
|
response = self.client.post(
|
|
|
|
reverse("pedagogy:moderation"), {"denied_reports": [self.report_1.id]}
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 302
|
2019-06-20 12:22:06 +00:00
|
|
|
|
|
|
|
# Test that the report has been deleted and that the comment still exists
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not UVCommentReport.objects.filter(id=self.report_1.id).exists()
|
|
|
|
assert UVComment.objects.filter(id=self.comment_1.id).exists()
|
2019-06-20 12:22:06 +00:00
|
|
|
# Test that the bis report is still there
|
2024-06-26 17:10:24 +00:00
|
|
|
assert UVCommentReport.objects.filter(id=self.report_1_bis.id).exists()
|
2019-06-20 12:22:06 +00:00
|
|
|
|
|
|
|
# Test that the other comment and report still exists
|
2024-06-26 17:10:24 +00:00
|
|
|
assert UVCommentReport.objects.filter(id=self.report_2.id).exists()
|
|
|
|
assert UVComment.objects.filter(id=self.comment_2.id).exists()
|
2019-06-20 12:22:06 +00:00
|
|
|
|
|
|
|
def test_delete_report_bulk(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.bibou)
|
2019-06-20 12:22:06 +00:00
|
|
|
response = self.client.post(
|
|
|
|
reverse("pedagogy:moderation"),
|
|
|
|
{
|
|
|
|
"denied_reports": [
|
|
|
|
self.report_1.id,
|
|
|
|
self.report_1_bis.id,
|
|
|
|
self.report_2.id,
|
|
|
|
]
|
|
|
|
},
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 302
|
2019-06-20 12:22:06 +00:00
|
|
|
|
|
|
|
# Test that every reports has been deleted
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not UVCommentReport.objects.filter(id=self.report_1.id).exists()
|
|
|
|
assert not UVCommentReport.objects.filter(id=self.report_1_bis.id).exists()
|
|
|
|
assert not UVCommentReport.objects.filter(id=self.report_2.id).exists()
|
2019-06-20 12:22:06 +00:00
|
|
|
# Test that comments still exists
|
2024-06-26 17:10:24 +00:00
|
|
|
assert UVComment.objects.filter(id=self.comment_1.id).exists()
|
|
|
|
assert UVComment.objects.filter(id=self.comment_2.id).exists()
|
2019-06-20 12:22:06 +00:00
|
|
|
|
|
|
|
def test_delete_mixed(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.bibou)
|
2019-06-20 12:22:06 +00:00
|
|
|
response = self.client.post(
|
|
|
|
reverse("pedagogy:moderation"),
|
|
|
|
{
|
|
|
|
"accepted_reports": [self.report_2.id],
|
|
|
|
"denied_reports": [self.report_1.id],
|
|
|
|
},
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 302
|
2019-06-20 12:22:06 +00:00
|
|
|
|
|
|
|
# Test that report 2 and his comment has been deleted
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not UVCommentReport.objects.filter(id=self.report_2.id).exists()
|
|
|
|
assert not UVComment.objects.filter(id=self.comment_2.id).exists()
|
2019-06-20 12:22:06 +00:00
|
|
|
|
|
|
|
# Test that report 1 has been deleted and it's comment still exists
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not UVCommentReport.objects.filter(id=self.report_1.id).exists()
|
|
|
|
assert UVComment.objects.filter(id=self.comment_1.id).exists()
|
2019-06-20 12:22:06 +00:00
|
|
|
|
|
|
|
# Test that report 1 bis is still there
|
2024-06-26 17:10:24 +00:00
|
|
|
assert UVCommentReport.objects.filter(id=self.report_1_bis.id).exists()
|
2019-06-20 12:22:06 +00:00
|
|
|
|
|
|
|
def test_delete_mixed_with_bis(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.bibou)
|
2019-06-20 12:22:06 +00:00
|
|
|
response = self.client.post(
|
|
|
|
reverse("pedagogy:moderation"),
|
|
|
|
{
|
|
|
|
"accepted_reports": [self.report_1.id],
|
|
|
|
"denied_reports": [self.report_1_bis.id],
|
|
|
|
},
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 302
|
2019-06-20 12:22:06 +00:00
|
|
|
|
|
|
|
# Test that report 1 and 1 bis has been deleted
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not UVCommentReport.objects.filter(
|
|
|
|
id__in=[self.report_1.id, self.report_1_bis.id]
|
|
|
|
).exists()
|
2019-06-20 12:22:06 +00:00
|
|
|
|
|
|
|
# Test that comment 1 has been deleted
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not UVComment.objects.filter(id=self.comment_1.id).exists()
|
2019-06-20 12:22:06 +00:00
|
|
|
|
|
|
|
# Test that report and comment 2 still exists
|
2024-06-26 17:10:24 +00:00
|
|
|
assert UVCommentReport.objects.filter(id=self.report_2.id).exists()
|
|
|
|
assert UVComment.objects.filter(id=self.comment_2.id).exists()
|
2019-06-20 12:57:58 +00:00
|
|
|
|
|
|
|
|
2024-07-23 22:39:26 +00:00
|
|
|
class TestUVCommentReportCreate(TestCase):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test report creation view.
|
|
|
|
|
|
|
|
Assert access rights and if you can create with it.
|
2019-06-20 12:57:58 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.krophil = User.objects.get(username="krophil")
|
2019-07-04 13:32:00 +00:00
|
|
|
self.tutu = User.objects.get(username="tutu")
|
2019-06-20 12:57:58 +00:00
|
|
|
|
|
|
|
# Prepare a comment
|
|
|
|
comment_kwargs = create_uv_comment_template(self.krophil.id)
|
|
|
|
comment_kwargs["author"] = self.krophil
|
|
|
|
comment_kwargs["uv"] = UV.objects.get(id=comment_kwargs["uv"])
|
|
|
|
self.comment = UVComment(**comment_kwargs)
|
|
|
|
self.comment.save()
|
|
|
|
|
2024-06-27 12:57:40 +00:00
|
|
|
def create_report_test(self, username: str, *, success: bool):
|
2019-06-20 12:57:58 +00:00
|
|
|
self.client.login(username=username, password="plop")
|
|
|
|
response = self.client.post(
|
|
|
|
reverse("pedagogy:comment_report", kwargs={"comment_id": self.comment.id}),
|
|
|
|
{
|
|
|
|
"comment": self.comment.id,
|
|
|
|
"reporter": User.objects.get(username=username).id,
|
|
|
|
"reason": "C'est moche",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if success:
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 302
|
2019-06-20 12:57:58 +00:00
|
|
|
else:
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 403
|
2022-08-03 19:48:37 +00:00
|
|
|
self.assertEqual(UVCommentReport.objects.all().exists(), success)
|
2019-06-20 12:57:58 +00:00
|
|
|
|
|
|
|
def test_create_report_root_success(self):
|
2024-06-27 12:57:40 +00:00
|
|
|
self.create_report_test("root", success=True)
|
2019-06-20 12:57:58 +00:00
|
|
|
|
|
|
|
def test_create_report_pedagogy_admin_success(self):
|
2024-06-27 12:57:40 +00:00
|
|
|
self.create_report_test("tutu", success=True)
|
2019-06-20 12:57:58 +00:00
|
|
|
|
|
|
|
def test_create_report_subscriber_success(self):
|
2024-06-27 12:57:40 +00:00
|
|
|
self.create_report_test("sli", success=True)
|
2019-06-20 12:57:58 +00:00
|
|
|
|
|
|
|
def test_create_report_unsubscribed_fail(self):
|
2024-06-27 12:57:40 +00:00
|
|
|
self.create_report_test("guy", success=False)
|
2019-06-20 12:57:58 +00:00
|
|
|
|
|
|
|
def test_create_report_anonymous_fail(self):
|
|
|
|
response = self.client.post(
|
|
|
|
reverse("pedagogy:comment_report", kwargs={"comment_id": self.comment.id}),
|
|
|
|
{"comment": self.comment.id, "reporter": 0, "reason": "C'est moche"},
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 403
|
|
|
|
assert not UVCommentReport.objects.all().exists()
|
2019-07-04 13:32:00 +00:00
|
|
|
|
|
|
|
def test_notifications(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not self.tutu.notifications.filter(type="PEDAGOGY_MODERATION").exists()
|
2019-07-04 13:32:00 +00:00
|
|
|
# Create a comment report
|
2024-06-27 12:57:40 +00:00
|
|
|
self.create_report_test("tutu", success=True)
|
2019-07-04 13:32:00 +00:00
|
|
|
|
|
|
|
# Check that a notification has been created for pedagogy admins
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.tutu.notifications.filter(type="PEDAGOGY_MODERATION").exists()
|
2019-07-04 13:32:00 +00:00
|
|
|
|
|
|
|
# Check that only pedagogy admins recieves this notification
|
|
|
|
for notif in Notification.objects.filter(type="PEDAGOGY_MODERATION").all():
|
2024-06-26 17:10:24 +00:00
|
|
|
assert notif.user.is_in_group(pk=settings.SITH_GROUP_PEDAGOGY_ADMIN_ID)
|
2019-07-04 13:32:00 +00:00
|
|
|
|
|
|
|
# Check that notifications are not duplicated if not viewed
|
2024-06-27 12:57:40 +00:00
|
|
|
self.create_report_test("tutu", success=True)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.tutu.notifications.filter(type="PEDAGOGY_MODERATION").count() == 1
|
2019-07-04 13:32:00 +00:00
|
|
|
|
|
|
|
# Check that a new notification is created when the old one has been viewed
|
|
|
|
notif = self.tutu.notifications.filter(type="PEDAGOGY_MODERATION").first()
|
|
|
|
notif.viewed = True
|
|
|
|
notif.save()
|
|
|
|
|
2024-06-27 12:57:40 +00:00
|
|
|
self.create_report_test("tutu", success=True)
|
2019-07-04 13:32:00 +00:00
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.tutu.notifications.filter(type="PEDAGOGY_MODERATION").count() == 2
|