2017-04-24 15:51:12 +00:00
|
|
|
#
|
2023-04-04 16:39:45 +00:00
|
|
|
# Copyright 2023 © AE UTBM
|
|
|
|
# ae@utbm.fr / ae.info@utbm.fr
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
2023-04-04 16:39:45 +00:00
|
|
|
# This file is part of the website of the UTBM Student Association (AE UTBM),
|
|
|
|
# https://ae.utbm.fr.
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
2024-09-22 23:37:25 +00:00
|
|
|
# You can find the source code of the website at https://github.com/ae-utbm/sith
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
2023-04-04 16:39:45 +00:00
|
|
|
# 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
|
2023-04-04 16:39:45 +00:00
|
|
|
# OR WITHIN THE LOCAL FILE "LICENSE"
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
|
|
|
#
|
2024-06-26 17:10:24 +00:00
|
|
|
import pytest
|
2024-06-24 11:07:36 +00:00
|
|
|
from django.conf import settings
|
2023-05-02 10:36:59 +00:00
|
|
|
from django.core.files.uploadedfile import SimpleUploadedFile
|
2016-12-21 01:38:21 +00:00
|
|
|
from django.test import TestCase
|
2019-10-06 11:28:56 +00:00
|
|
|
from django.urls import reverse
|
2019-08-16 14:59:13 +00:00
|
|
|
from django.utils import html
|
2023-05-02 10:36:59 +00:00
|
|
|
from django.utils.timezone import localtime, now
|
2022-08-03 22:26:43 +00:00
|
|
|
from django.utils.translation import gettext as _
|
2019-08-16 14:59:13 +00:00
|
|
|
|
2023-05-02 10:36:59 +00:00
|
|
|
from club.models import Club, Membership
|
2024-06-24 11:07:36 +00:00
|
|
|
from com.models import News, Poster, Sith, Weekmail, WeekmailArticle
|
|
|
|
from core.models import AnonymousUser, RealGroup, User
|
2017-06-12 06:59:03 +00:00
|
|
|
|
2016-12-21 01:38:21 +00:00
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
@pytest.fixture()
|
|
|
|
def user_community():
|
|
|
|
return User.objects.get(username="comunity")
|
2019-08-29 15:29:38 +00:00
|
|
|
|
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
@pytest.mark.django_db
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"url",
|
|
|
|
[
|
|
|
|
reverse("com:alert_edit"),
|
|
|
|
reverse("com:info_edit"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_com_page_is_working(client, url, user_community):
|
|
|
|
client.force_login(user_community)
|
|
|
|
response = client.get(url)
|
|
|
|
assert response.status_code == 200
|
2019-08-29 15:29:38 +00:00
|
|
|
|
|
|
|
|
2024-07-23 22:39:26 +00:00
|
|
|
class TestCom(TestCase):
|
2023-05-02 09:00:23 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2024-06-26 17:10:24 +00:00
|
|
|
cls.skia = User.objects.get(username="skia")
|
2023-05-02 09:00:23 +00:00
|
|
|
cls.com_group = RealGroup.objects.filter(
|
2018-10-04 19:29:19 +00:00
|
|
|
id=settings.SITH_GROUP_COM_ADMIN_ID
|
|
|
|
).first()
|
2023-05-02 09:00:23 +00:00
|
|
|
cls.skia.groups.set([cls.com_group])
|
|
|
|
|
|
|
|
def setUp(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.skia)
|
2016-12-21 01:38:21 +00:00
|
|
|
|
|
|
|
def test_alert_msg(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.post(
|
2018-10-04 19:29:19 +00:00
|
|
|
reverse("com:alert_edit"),
|
|
|
|
{
|
|
|
|
"alert_msg": """
|
2016-12-21 01:38:21 +00:00
|
|
|
### ALERTE!
|
|
|
|
|
|
|
|
**Caaaataaaapuuuulte!!!!**
|
2018-10-04 19:29:19 +00:00
|
|
|
"""
|
|
|
|
},
|
|
|
|
)
|
2016-12-21 01:38:21 +00:00
|
|
|
r = self.client.get(reverse("core:index"))
|
2024-07-23 22:16:31 +00:00
|
|
|
assert r.status_code == 200
|
|
|
|
self.assertInHTML(
|
|
|
|
"""<div id="alert_box"><div class="markdown"><h3>ALERTE!</h3>
|
|
|
|
<p><strong>Caaaataaaapuuuulte!!!!</strong></p>""",
|
|
|
|
r.content.decode(),
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
2016-12-21 01:38:21 +00:00
|
|
|
|
|
|
|
def test_info_msg(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.post(
|
2018-10-04 19:29:19 +00:00
|
|
|
reverse("com:info_edit"),
|
|
|
|
{
|
|
|
|
"info_msg": """
|
2016-12-21 01:38:21 +00:00
|
|
|
### INFO: **Caaaataaaapuuuulte!!!!**
|
2018-10-04 19:29:19 +00:00
|
|
|
"""
|
|
|
|
},
|
|
|
|
)
|
2016-12-21 01:38:21 +00:00
|
|
|
r = self.client.get(reverse("core:index"))
|
2024-07-23 22:16:31 +00:00
|
|
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
self.assertInHTML(
|
|
|
|
"""<div id="info_box"><div class="markdown">
|
|
|
|
<h3>INFO: <strong>Caaaataaaapuuuulte!!!!</strong></h3>""",
|
|
|
|
r.content.decode(),
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
2019-08-16 14:59:13 +00:00
|
|
|
|
|
|
|
def test_birthday_non_subscribed_user(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(User.objects.get(username="guy"))
|
2019-08-16 14:59:13 +00:00
|
|
|
response = self.client.get(reverse("core:index"))
|
|
|
|
self.assertContains(
|
|
|
|
response,
|
|
|
|
text=html.escape(
|
|
|
|
_("You need an up to date subscription to access this content")
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_birthday_subscibed_user(self):
|
|
|
|
response = self.client.get(reverse("core:index"))
|
|
|
|
|
|
|
|
self.assertNotContains(
|
|
|
|
response,
|
|
|
|
text=html.escape(
|
|
|
|
_("You need an up to date subscription to access this content")
|
|
|
|
),
|
|
|
|
)
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
|
2024-07-23 22:39:26 +00:00
|
|
|
class TestSith(TestCase):
|
2023-05-02 10:36:59 +00:00
|
|
|
def test_sith_owner(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that the sith instance is owned by com admins and nobody else."""
|
2023-05-02 10:36:59 +00:00
|
|
|
sith: Sith = Sith.objects.first()
|
|
|
|
|
|
|
|
com_admin = User.objects.get(username="comunity")
|
2024-06-26 17:10:24 +00:00
|
|
|
assert sith.is_owned_by(com_admin)
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
anonymous = AnonymousUser()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not sith.is_owned_by(anonymous)
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
sli = User.objects.get(username="sli")
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not sith.is_owned_by(sli)
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
|
2024-07-23 22:39:26 +00:00
|
|
|
class TestNews(TestCase):
|
2023-05-02 10:36:59 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.com_admin = User.objects.get(username="comunity")
|
|
|
|
new = News.objects.create(
|
|
|
|
title="dummy new",
|
|
|
|
summary="This is a dummy new",
|
|
|
|
content="Look at that beautiful dummy new",
|
|
|
|
author=User.objects.get(username="subscriber"),
|
|
|
|
club=Club.objects.first(),
|
|
|
|
)
|
|
|
|
cls.new = new
|
|
|
|
cls.author = new.author
|
|
|
|
cls.sli = User.objects.get(username="sli")
|
|
|
|
cls.anonymous = AnonymousUser()
|
|
|
|
|
|
|
|
def test_news_owner(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that news are owned by com admins
|
|
|
|
or by their author but nobody else.
|
2023-05-02 10:36:59 +00:00
|
|
|
"""
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.new.is_owned_by(self.com_admin)
|
|
|
|
assert self.new.is_owned_by(self.author)
|
|
|
|
assert not self.new.is_owned_by(self.anonymous)
|
|
|
|
assert not self.new.is_owned_by(self.sli)
|
2023-05-02 10:36:59 +00:00
|
|
|
|
2023-09-07 21:53:42 +00:00
|
|
|
def test_news_viewer(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that moderated news can be viewed by anyone
|
|
|
|
and not moderated news only by com admins.
|
2023-09-07 21:53:42 +00:00
|
|
|
"""
|
|
|
|
# by default a news isn't moderated
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.new.can_be_viewed_by(self.com_admin)
|
|
|
|
assert not self.new.can_be_viewed_by(self.sli)
|
|
|
|
assert not self.new.can_be_viewed_by(self.anonymous)
|
|
|
|
assert not self.new.can_be_viewed_by(self.author)
|
2023-09-07 21:53:42 +00:00
|
|
|
|
|
|
|
self.new.is_moderated = True
|
|
|
|
self.new.save()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.new.can_be_viewed_by(self.com_admin)
|
|
|
|
assert self.new.can_be_viewed_by(self.sli)
|
|
|
|
assert self.new.can_be_viewed_by(self.anonymous)
|
|
|
|
assert self.new.can_be_viewed_by(self.author)
|
2023-09-07 21:53:42 +00:00
|
|
|
|
|
|
|
def test_news_editor(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that only com admins can edit news."""
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.new.can_be_edited_by(self.com_admin)
|
|
|
|
assert not self.new.can_be_edited_by(self.sli)
|
|
|
|
assert not self.new.can_be_edited_by(self.anonymous)
|
|
|
|
assert not self.new.can_be_edited_by(self.author)
|
2023-09-07 21:53:42 +00:00
|
|
|
|
2023-05-02 10:36:59 +00:00
|
|
|
|
2024-07-23 22:39:26 +00:00
|
|
|
class TestWeekmailArticle(TestCase):
|
2023-05-02 10:36:59 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.com_admin = User.objects.get(username="comunity")
|
|
|
|
author = User.objects.get(username="subscriber")
|
|
|
|
cls.article = WeekmailArticle.objects.create(
|
|
|
|
weekmail=Weekmail.objects.create(),
|
|
|
|
author=author,
|
|
|
|
title="title",
|
|
|
|
content="Some content",
|
|
|
|
club=Club.objects.first(),
|
|
|
|
)
|
|
|
|
cls.author = author
|
|
|
|
cls.sli = User.objects.get(username="sli")
|
|
|
|
cls.anonymous = AnonymousUser()
|
|
|
|
|
|
|
|
def test_weekmail_owner(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that weekmails are owned only by com admins."""
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.article.is_owned_by(self.com_admin)
|
|
|
|
assert not self.article.is_owned_by(self.author)
|
|
|
|
assert not self.article.is_owned_by(self.anonymous)
|
|
|
|
assert not self.article.is_owned_by(self.sli)
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
|
2024-07-23 22:39:26 +00:00
|
|
|
class TestPoster(TestCase):
|
2023-05-02 10:36:59 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.com_admin = User.objects.get(username="comunity")
|
|
|
|
cls.poster = Poster.objects.create(
|
|
|
|
name="dummy",
|
|
|
|
file=SimpleUploadedFile("dummy.jpg", b"azertyuiop"),
|
|
|
|
club=Club.objects.first(),
|
|
|
|
date_begin=localtime(now()),
|
|
|
|
)
|
|
|
|
cls.sli = User.objects.get(username="sli")
|
|
|
|
cls.sli.memberships.all().delete()
|
|
|
|
Membership(user=cls.sli, club=Club.objects.first(), role=5).save()
|
|
|
|
cls.susbcriber = User.objects.get(username="subscriber")
|
|
|
|
cls.anonymous = AnonymousUser()
|
|
|
|
|
|
|
|
def test_poster_owner(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that poster are owned by com admins and board members in clubs."""
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.poster.is_owned_by(self.com_admin)
|
|
|
|
assert not self.poster.is_owned_by(self.anonymous)
|
2023-05-02 10:36:59 +00:00
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not self.poster.is_owned_by(self.susbcriber)
|
|
|
|
assert self.poster.is_owned_by(self.sli)
|