2023-04-06 11:08:42 +00:00
|
|
|
# -*- coding:utf-8 -*-
|
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
|
2023-04-06 11:08:42 +00:00
|
|
|
# All contributors are listed in the CONTRIBUTORS file.
|
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
|
|
|
#
|
2023-04-06 11:08:42 +00:00
|
|
|
# You can find the whole source code at https://github.com/ae-utbm/sith3
|
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)
|
|
|
|
# SEE : https://raw.githubusercontent.com/ae-utbm/sith3/master/LICENSE
|
|
|
|
# OR WITHIN THE LOCAL FILE "LICENSE"
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
2023-04-06 11:08:42 +00:00
|
|
|
# PREVIOUSLY LICENSED UNDER THE MIT LICENSE,
|
|
|
|
# SEE : https://raw.githubusercontent.com/ae-utbm/sith3/master/LICENSE.old
|
|
|
|
# OR WITHIN THE LOCAL FILE "LICENSE.old"
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
|
|
|
|
2017-08-24 14:29:40 +00:00
|
|
|
import os
|
2023-09-07 21:11:58 +00:00
|
|
|
from datetime import date, timedelta
|
2017-08-24 14:29:40 +00:00
|
|
|
|
2023-09-07 21:11:58 +00:00
|
|
|
import freezegun
|
2024-06-26 17:10:24 +00:00
|
|
|
import pytest
|
2023-05-02 10:36:59 +00:00
|
|
|
from django.core.cache import cache
|
2024-06-26 17:10:24 +00:00
|
|
|
from django.test import TestCase
|
2019-10-06 11:28:56 +00:00
|
|
|
from django.urls import reverse
|
2023-05-02 10:36:59 +00:00
|
|
|
from django.utils.timezone import now
|
2024-06-26 17:10:24 +00:00
|
|
|
from pytest_django.asserts import assertRedirects
|
2015-11-19 10:23:08 +00:00
|
|
|
|
2023-05-02 10:36:59 +00:00
|
|
|
from club.models import Membership
|
2017-08-24 14:29:40 +00:00
|
|
|
from core.markdown import markdown
|
2023-09-07 21:11:58 +00:00
|
|
|
from core.models import AnonymousUser, Group, Page, User
|
|
|
|
from core.utils import get_semester_code, get_start_of_semester
|
2023-05-02 10:36:59 +00:00
|
|
|
from sith import settings
|
2015-11-19 10:23:08 +00:00
|
|
|
|
2015-12-03 17:41:22 +00:00
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
@pytest.mark.django_db
|
|
|
|
class TestUserRegistration:
|
|
|
|
@pytest.fixture()
|
|
|
|
def valid_payload(self):
|
|
|
|
return {
|
|
|
|
"first_name": "this user does not exist (yet)",
|
|
|
|
"last_name": "this user does not exist (yet)",
|
|
|
|
"email": "i-dont-exist-yet@git.an",
|
|
|
|
"password1": "plop",
|
|
|
|
"password2": "plop",
|
|
|
|
"captcha_0": "dummy-value",
|
|
|
|
"captcha_1": "PASSED",
|
|
|
|
}
|
|
|
|
|
|
|
|
def test_register_user_form_ok(self, client, valid_payload):
|
|
|
|
"""Should register a user correctly."""
|
|
|
|
response = client.post(reverse("core:register"), valid_payload)
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert "TEST_REGISTER_USER_FORM_OK" in str(response.content)
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"payload_edit",
|
|
|
|
[
|
|
|
|
{"password2": "not the same as password1"},
|
|
|
|
{"email": "not-an-email"},
|
|
|
|
{"first_name": ""},
|
|
|
|
{"last_name": ""},
|
|
|
|
{"captcha_1": "WRONG_CAPTCHA"},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_register_user_form_fail(self, client, valid_payload, payload_edit):
|
|
|
|
"""Should not register a user correctly."""
|
|
|
|
payload = valid_payload | payload_edit
|
|
|
|
response = client.post(reverse("core:register"), payload)
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert "TEST_REGISTER_USER_FORM_FAIL" in str(response.content)
|
|
|
|
|
|
|
|
def test_register_user_form_fail_already_exists(self, client, valid_payload):
|
|
|
|
"""Should not register a user correctly if it already exists."""
|
|
|
|
# create the user, then try to create it again
|
|
|
|
client.post(reverse("core:register"), valid_payload)
|
|
|
|
response = client.post(reverse("core:register"), valid_payload)
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert "TEST_REGISTER_USER_FORM_FAIL" in str(response.content)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
class TestUserLogin:
|
|
|
|
@pytest.fixture()
|
|
|
|
def user(self) -> User:
|
|
|
|
return User.objects.first()
|
|
|
|
|
|
|
|
def test_login_fail(self, client, user):
|
2015-11-19 10:23:08 +00:00
|
|
|
"""
|
2024-06-26 17:10:24 +00:00
|
|
|
Should not login a user correctly
|
2018-07-06 09:35:02 +00:00
|
|
|
"""
|
2015-11-19 10:23:08 +00:00
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
response = client.post(
|
|
|
|
reverse("core:login"),
|
|
|
|
{"username": user.username, "password": "wrong-password"},
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 200
|
|
|
|
assert (
|
|
|
|
'<p class="alert alert-red">Votre nom d\'utilisateur '
|
|
|
|
"et votre mot de passe ne correspondent pas. Merci de réessayer.</p>"
|
|
|
|
) in str(response.content.decode())
|
2015-11-18 08:44:06 +00:00
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
def test_login_success(self, client, user):
|
2015-11-19 13:44:48 +00:00
|
|
|
"""
|
|
|
|
Should login a user correctly
|
|
|
|
"""
|
2024-06-26 17:10:24 +00:00
|
|
|
response = client.post(
|
|
|
|
reverse("core:login"), {"username": user.username, "password": "plop"}
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assertRedirects(response, reverse("core:index"))
|
2018-10-04 19:29:19 +00:00
|
|
|
|
2015-12-02 16:14:47 +00:00
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
def test_full_markdown_syntax():
|
|
|
|
root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
with open(os.path.join(root_path) + "/doc/SYNTAX.md", "r") as md_file:
|
|
|
|
md = md_file.read()
|
|
|
|
with open(os.path.join(root_path) + "/doc/SYNTAX.html", "r") as html_file:
|
|
|
|
html = html_file.read()
|
|
|
|
result = markdown(md)
|
|
|
|
assert result == html
|
2017-06-12 07:42:03 +00:00
|
|
|
|
2018-10-04 19:29:19 +00:00
|
|
|
|
2015-12-03 17:41:22 +00:00
|
|
|
class PageHandlingTest(TestCase):
|
2024-06-26 17:10:24 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.root = User.objects.get(username="root")
|
|
|
|
cls.root_group = Group.objects.get(name="Root")
|
|
|
|
|
2015-12-02 16:14:47 +00:00
|
|
|
def setUp(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.root)
|
2015-11-24 09:53:16 +00:00
|
|
|
|
|
|
|
def test_create_page_ok(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
"""Should create a page correctly."""
|
2023-01-09 21:07:03 +00:00
|
|
|
|
|
|
|
response = self.client.post(
|
|
|
|
reverse("core:page_new"),
|
|
|
|
{"parent": "", "name": "guy", "owner_group": self.root_group.id},
|
|
|
|
)
|
|
|
|
self.assertRedirects(
|
|
|
|
response, reverse("core:page", kwargs={"page_name": "guy"})
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert Page.objects.filter(name="guy").exists()
|
2023-01-09 21:07:03 +00:00
|
|
|
|
2018-10-04 19:29:19 +00:00
|
|
|
response = self.client.get(reverse("core:page", kwargs={"page_name": "guy"}))
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 200
|
2023-01-09 21:07:03 +00:00
|
|
|
html = response.content.decode()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert '<a href="/page/guy/hist/">' in html
|
|
|
|
assert '<a href="/page/guy/edit/">' in html
|
|
|
|
assert '<a href="/page/guy/prop/">' in html
|
2015-11-24 09:53:16 +00:00
|
|
|
|
|
|
|
def test_create_child_page_ok(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
"""Should create a page correctly."""
|
2023-05-02 09:00:23 +00:00
|
|
|
# remove all other pages to make sure there is no side effect
|
|
|
|
Page.objects.all().delete()
|
2018-10-04 19:29:19 +00:00
|
|
|
self.client.post(
|
2023-05-02 09:00:23 +00:00
|
|
|
reverse("core:page_new"),
|
|
|
|
{"parent": "", "name": "guy", "owner_group": str(self.root_group.id)},
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
2023-05-02 09:00:23 +00:00
|
|
|
page = Page.objects.first()
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.post(
|
2018-10-04 19:29:19 +00:00
|
|
|
reverse("core:page_new"),
|
2023-05-02 09:00:23 +00:00
|
|
|
{
|
|
|
|
"parent": str(page.id),
|
|
|
|
"name": "bibou",
|
|
|
|
"owner_group": str(self.root_group.id),
|
|
|
|
},
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
|
|
|
response = self.client.get(
|
|
|
|
reverse("core:page", kwargs={"page_name": "guy/bibou"})
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 200
|
|
|
|
assert '<a href="/page/guy/bibou/">' in str(response.content)
|
2015-11-24 09:53:16 +00:00
|
|
|
|
|
|
|
def test_access_child_page_ok(self):
|
|
|
|
"""
|
|
|
|
Should display a page correctly
|
|
|
|
"""
|
2023-01-09 21:07:03 +00:00
|
|
|
parent = Page(name="guy", owner_group=self.root_group)
|
2016-12-14 17:05:19 +00:00
|
|
|
parent.save(force_lock=True)
|
2023-01-09 21:07:03 +00:00
|
|
|
page = Page(name="bibou", owner_group=self.root_group, parent=parent)
|
2016-12-14 17:05:19 +00:00
|
|
|
page.save(force_lock=True)
|
2018-10-04 19:29:19 +00:00
|
|
|
response = self.client.get(
|
|
|
|
reverse("core:page", kwargs={"page_name": "guy/bibou"})
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 200
|
2023-01-09 21:07:03 +00:00
|
|
|
html = response.content.decode()
|
|
|
|
self.assertIn('<a href="/page/guy/bibou/edit/">', html)
|
2015-11-24 09:53:16 +00:00
|
|
|
|
|
|
|
def test_access_page_not_found(self):
|
|
|
|
"""
|
|
|
|
Should not display a page correctly
|
|
|
|
"""
|
2018-10-04 19:29:19 +00:00
|
|
|
response = self.client.get(reverse("core:page", kwargs={"page_name": "swagg"}))
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 200
|
2023-01-09 21:07:03 +00:00
|
|
|
html = response.content.decode()
|
|
|
|
self.assertIn('<a href="/page/create/?page=swagg">', html)
|
2016-01-08 15:14:54 +00:00
|
|
|
|
|
|
|
def test_create_page_markdown_safe(self):
|
|
|
|
"""
|
|
|
|
Should format the markdown and escape html correctly
|
|
|
|
"""
|
2018-10-04 19:29:19 +00:00
|
|
|
self.client.post(
|
|
|
|
reverse("core:page_new"), {"parent": "", "name": "guy", "owner_group": "1"}
|
|
|
|
)
|
|
|
|
self.client.post(
|
|
|
|
reverse("core:page_edit", kwargs={"page_name": "guy"}),
|
|
|
|
{
|
|
|
|
"title": "Bibou",
|
|
|
|
"content": """Guy *bibou*
|
2016-01-08 15:14:54 +00:00
|
|
|
|
|
|
|
http://git.an
|
|
|
|
|
|
|
|
# Swag
|
|
|
|
|
|
|
|
<guy>Bibou</guy>
|
|
|
|
|
|
|
|
<script>alert('Guy');</script>
|
2018-10-04 19:29:19 +00:00
|
|
|
""",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
response = self.client.get(reverse("core:page", kwargs={"page_name": "guy"}))
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 200
|
|
|
|
assert (
|
2018-10-04 19:29:19 +00:00
|
|
|
'<p>Guy <em>bibou</em></p>\\n<p><a href="http://git.an">http://git.an</a></p>\\n'
|
|
|
|
+ "<h1>Swag</h1>\\n<guy>Bibou</guy>"
|
|
|
|
+ "<script>alert(\\'Guy\\');</script>"
|
|
|
|
in str(response.content)
|
|
|
|
)
|
|
|
|
|
2016-01-08 15:14:54 +00:00
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
class UserToolsTest:
|
|
|
|
def test_anonymous_user_unauthorized(self, client):
|
|
|
|
"""An anonymous user shouldn't have access to the tools page"""
|
|
|
|
response = client.get(reverse("core:user_tools"))
|
|
|
|
assert response.status_code == 403
|
2019-07-15 10:36:05 +00:00
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
@pytest.mark.parametrize("username", ["guy", "root", "skia", "comunity"])
|
|
|
|
def test_page_is_working(self, client, username):
|
|
|
|
"""All existing users should be able to see the test page"""
|
2019-08-29 15:23:27 +00:00
|
|
|
# Test for simple user
|
2024-06-26 17:10:24 +00:00
|
|
|
client.force_login(User.objects.get(username=username))
|
|
|
|
response = client.get(reverse("core:user_tools"))
|
|
|
|
assert response.status_code == 200
|
2019-08-29 15:23:27 +00:00
|
|
|
|
2019-07-15 10:36:05 +00:00
|
|
|
|
2017-06-12 07:42:03 +00:00
|
|
|
# TODO: many tests on the pages:
|
2015-11-24 13:01:10 +00:00
|
|
|
# - renaming a page
|
|
|
|
# - changing a page's parent --> check that page's children's full_name
|
|
|
|
# - changing the different groups of the page
|
2016-12-09 14:48:09 +00:00
|
|
|
|
2017-06-12 07:42:03 +00:00
|
|
|
|
2016-12-09 14:48:09 +00:00
|
|
|
class FileHandlingTest(TestCase):
|
2023-05-02 09:00:23 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.subscriber = User.objects.get(username="subscriber")
|
|
|
|
|
2016-12-09 14:48:09 +00:00
|
|
|
def setUp(self):
|
2023-05-02 09:00:23 +00:00
|
|
|
self.client.login(username="subscriber", password="plop")
|
2016-12-09 14:48:09 +00:00
|
|
|
|
|
|
|
def test_create_folder_home(self):
|
2018-10-04 19:29:19 +00:00
|
|
|
response = self.client.post(
|
|
|
|
reverse("core:file_detail", kwargs={"file_id": self.subscriber.home.id}),
|
|
|
|
{"folder_name": "GUY_folder_test"},
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 302
|
2018-10-04 19:29:19 +00:00
|
|
|
response = self.client.get(
|
|
|
|
reverse("core:file_detail", kwargs={"file_id": self.subscriber.home.id})
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 200
|
|
|
|
assert "GUY_folder_test</a>" in str(response.content)
|
2016-12-09 14:48:09 +00:00
|
|
|
|
|
|
|
def test_upload_file_home(self):
|
|
|
|
with open("/bin/ls", "rb") as f:
|
2018-10-04 19:29:19 +00:00
|
|
|
response = self.client.post(
|
|
|
|
reverse(
|
|
|
|
"core:file_detail", kwargs={"file_id": self.subscriber.home.id}
|
|
|
|
),
|
|
|
|
{"file_field": f},
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 302
|
2018-10-04 19:29:19 +00:00
|
|
|
response = self.client.get(
|
|
|
|
reverse("core:file_detail", kwargs={"file_id": self.subscriber.home.id})
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 200
|
|
|
|
assert "ls</a>" in str(response.content)
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserIsInGroupTest(TestCase):
|
|
|
|
"""
|
|
|
|
Test that the User.is_in_group() and AnonymousUser.is_in_group()
|
|
|
|
work as intended
|
|
|
|
"""
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
from club.models import Club
|
|
|
|
|
|
|
|
cls.root_group = Group.objects.get(name="Root")
|
|
|
|
cls.public = Group.objects.get(name="Public")
|
2024-06-26 17:10:24 +00:00
|
|
|
cls.skia = User.objects.get(username="skia")
|
|
|
|
cls.toto = User.objects.create(
|
|
|
|
username="toto", first_name="a", last_name="b", email="a.b@toto.fr"
|
|
|
|
)
|
2023-05-02 10:36:59 +00:00
|
|
|
cls.subscribers = Group.objects.get(name="Subscribers")
|
|
|
|
cls.old_subscribers = Group.objects.get(name="Old subscribers")
|
|
|
|
cls.accounting_admin = Group.objects.get(name="Accounting admin")
|
|
|
|
cls.com_admin = Group.objects.get(name="Communication admin")
|
|
|
|
cls.counter_admin = Group.objects.get(name="Counter admin")
|
|
|
|
cls.banned_alcohol = Group.objects.get(name="Banned from buying alcohol")
|
|
|
|
cls.banned_counters = Group.objects.get(name="Banned from counters")
|
|
|
|
cls.banned_subscription = Group.objects.get(name="Banned to subscribe")
|
|
|
|
cls.sas_admin = Group.objects.get(name="SAS admin")
|
|
|
|
cls.club = Club.objects.create(
|
|
|
|
name="Fake Club",
|
|
|
|
unix_name="fake-club",
|
|
|
|
address="Fake address",
|
|
|
|
)
|
|
|
|
cls.main_club = Club.objects.get(id=1)
|
|
|
|
|
|
|
|
def assert_in_public_group(self, user):
|
2024-06-26 17:10:24 +00:00
|
|
|
assert user.is_in_group(pk=self.public.id)
|
|
|
|
assert user.is_in_group(name=self.public.name)
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
def assert_in_club_metagroups(self, user, club):
|
|
|
|
meta_groups_board = club.unix_name + settings.SITH_BOARD_SUFFIX
|
|
|
|
meta_groups_members = club.unix_name + settings.SITH_MEMBER_SUFFIX
|
2024-06-26 17:10:24 +00:00
|
|
|
assert user.is_in_group(name=meta_groups_board) is False
|
|
|
|
assert user.is_in_group(name=meta_groups_members) is False
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
def assert_only_in_public_group(self, user):
|
|
|
|
self.assert_in_public_group(user)
|
|
|
|
for group in (
|
|
|
|
self.root_group,
|
|
|
|
self.banned_counters,
|
|
|
|
self.accounting_admin,
|
|
|
|
self.sas_admin,
|
|
|
|
self.subscribers,
|
|
|
|
self.old_subscribers,
|
|
|
|
):
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not user.is_in_group(pk=group.pk)
|
|
|
|
assert not user.is_in_group(name=group.name)
|
2023-05-02 10:36:59 +00:00
|
|
|
meta_groups_board = self.club.unix_name + settings.SITH_BOARD_SUFFIX
|
|
|
|
meta_groups_members = self.club.unix_name + settings.SITH_MEMBER_SUFFIX
|
2024-06-26 17:10:24 +00:00
|
|
|
assert user.is_in_group(name=meta_groups_board) is False
|
|
|
|
assert user.is_in_group(name=meta_groups_members) is False
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
def test_anonymous_user(self):
|
|
|
|
"""
|
|
|
|
Test that anonymous users are only in the public group
|
|
|
|
"""
|
|
|
|
user = AnonymousUser()
|
|
|
|
self.assert_only_in_public_group(user)
|
|
|
|
|
|
|
|
def test_not_subscribed_user(self):
|
|
|
|
"""
|
|
|
|
Test that users who never subscribed are only in the public group
|
|
|
|
"""
|
|
|
|
self.assert_only_in_public_group(self.toto)
|
|
|
|
|
|
|
|
def test_wrong_parameter_fail(self):
|
|
|
|
"""
|
|
|
|
Test that when neither the pk nor the name argument is given,
|
|
|
|
the function raises a ValueError
|
|
|
|
"""
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
self.toto.is_in_group()
|
|
|
|
|
|
|
|
def test_number_queries(self):
|
|
|
|
"""
|
|
|
|
Test that the number of db queries is stable
|
|
|
|
and that less queries are made when making a new call
|
|
|
|
"""
|
|
|
|
# make sure Skia is in at least one group
|
|
|
|
self.skia.groups.add(Group.objects.first().pk)
|
|
|
|
skia_groups = self.skia.groups.all()
|
|
|
|
|
|
|
|
group_in = skia_groups.first()
|
|
|
|
cache.clear()
|
|
|
|
# Test when the user is in the group
|
|
|
|
with self.assertNumQueries(2):
|
|
|
|
self.skia.is_in_group(pk=group_in.id)
|
|
|
|
with self.assertNumQueries(0):
|
|
|
|
self.skia.is_in_group(pk=group_in.id)
|
|
|
|
|
|
|
|
ids = skia_groups.values_list("pk", flat=True)
|
|
|
|
group_not_in = Group.objects.exclude(pk__in=ids).first()
|
|
|
|
cache.clear()
|
|
|
|
# Test when the user is not in the group
|
|
|
|
with self.assertNumQueries(2):
|
|
|
|
self.skia.is_in_group(pk=group_not_in.id)
|
|
|
|
with self.assertNumQueries(0):
|
|
|
|
self.skia.is_in_group(pk=group_not_in.id)
|
|
|
|
|
|
|
|
def test_cache_properly_cleared_membership(self):
|
|
|
|
"""
|
|
|
|
Test that when the membership of a user end,
|
|
|
|
the cache is properly invalidated
|
|
|
|
"""
|
|
|
|
membership = Membership.objects.create(
|
|
|
|
club=self.club, user=self.toto, end_date=None
|
|
|
|
)
|
|
|
|
meta_groups_members = self.club.unix_name + settings.SITH_MEMBER_SUFFIX
|
|
|
|
cache.clear()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.toto.is_in_group(name=meta_groups_members) is True
|
|
|
|
assert membership == cache.get(f"membership_{self.club.id}_{self.toto.id}")
|
2023-05-02 10:36:59 +00:00
|
|
|
membership.end_date = now() - timedelta(minutes=5)
|
|
|
|
membership.save()
|
|
|
|
cached_membership = cache.get(f"membership_{self.club.id}_{self.toto.id}")
|
2024-06-26 17:10:24 +00:00
|
|
|
assert cached_membership == "not_member"
|
|
|
|
assert self.toto.is_in_group(name=meta_groups_members) is False
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
def test_cache_properly_cleared_group(self):
|
|
|
|
"""
|
|
|
|
Test that when a user is removed from a group,
|
|
|
|
the is_in_group_method return False when calling it again
|
|
|
|
"""
|
2023-05-12 11:27:51 +00:00
|
|
|
# testing with pk
|
2023-05-02 10:36:59 +00:00
|
|
|
self.toto.groups.add(self.com_admin.pk)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.toto.is_in_group(pk=self.com_admin.pk) is True
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
self.toto.groups.remove(self.com_admin.pk)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.toto.is_in_group(pk=self.com_admin.pk) is False
|
2023-05-02 10:36:59 +00:00
|
|
|
|
2023-05-12 11:27:51 +00:00
|
|
|
# testing with name
|
|
|
|
self.toto.groups.add(self.sas_admin.pk)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.toto.is_in_group(name="SAS admin") is True
|
2023-05-12 11:27:51 +00:00
|
|
|
|
|
|
|
self.toto.groups.remove(self.sas_admin.pk)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.toto.is_in_group(name="SAS admin") is False
|
2023-05-12 11:27:51 +00:00
|
|
|
|
2023-05-02 10:36:59 +00:00
|
|
|
def test_not_existing_group(self):
|
|
|
|
"""
|
|
|
|
Test that searching for a not existing group
|
|
|
|
returns False
|
|
|
|
"""
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.skia.is_in_group(name="This doesn't exist") is False
|
2023-09-07 21:11:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DateUtilsTest(TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.autumn_month = settings.SITH_SEMESTER_START_AUTUMN[0]
|
|
|
|
cls.autumn_day = settings.SITH_SEMESTER_START_AUTUMN[1]
|
|
|
|
cls.spring_month = settings.SITH_SEMESTER_START_SPRING[0]
|
|
|
|
cls.spring_day = settings.SITH_SEMESTER_START_SPRING[1]
|
|
|
|
|
|
|
|
cls.autumn_semester_january = date(2025, 1, 4)
|
|
|
|
cls.autumn_semester_september = date(2024, 9, 4)
|
|
|
|
cls.autumn_first_day = date(2024, cls.autumn_month, cls.autumn_day)
|
|
|
|
|
|
|
|
cls.spring_semester_march = date(2023, 3, 4)
|
|
|
|
cls.spring_first_day = date(2023, cls.spring_month, cls.spring_day)
|
|
|
|
|
|
|
|
def test_get_semester(self):
|
|
|
|
"""
|
|
|
|
Test that the get_semester function returns the correct semester string
|
|
|
|
"""
|
2024-06-26 17:10:24 +00:00
|
|
|
assert get_semester_code(self.autumn_semester_january) == "A24"
|
|
|
|
assert get_semester_code(self.autumn_semester_september) == "A24"
|
|
|
|
assert get_semester_code(self.autumn_first_day) == "A24"
|
2023-09-07 21:11:58 +00:00
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
assert get_semester_code(self.spring_semester_march) == "P23"
|
|
|
|
assert get_semester_code(self.spring_first_day) == "P23"
|
2023-09-07 21:11:58 +00:00
|
|
|
|
|
|
|
def test_get_start_of_semester_fixed_date(self):
|
|
|
|
"""
|
|
|
|
Test that the get_start_of_semester correctly the starting date of the semester.
|
|
|
|
"""
|
|
|
|
automn_2024 = date(2024, self.autumn_month, self.autumn_day)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert get_start_of_semester(self.autumn_semester_january) == automn_2024
|
|
|
|
assert get_start_of_semester(self.autumn_semester_september) == automn_2024
|
|
|
|
assert get_start_of_semester(self.autumn_first_day) == automn_2024
|
2023-09-07 21:11:58 +00:00
|
|
|
|
|
|
|
spring_2023 = date(2023, self.spring_month, self.spring_day)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert get_start_of_semester(self.spring_semester_march) == spring_2023
|
|
|
|
assert get_start_of_semester(self.spring_first_day) == spring_2023
|
2023-09-07 21:11:58 +00:00
|
|
|
|
|
|
|
def test_get_start_of_semester_today(self):
|
|
|
|
"""
|
|
|
|
Test that the get_start_of_semester returns the start of the current semester
|
|
|
|
when no date is given
|
|
|
|
"""
|
|
|
|
with freezegun.freeze_time(self.autumn_semester_september):
|
2024-06-26 17:10:24 +00:00
|
|
|
assert get_start_of_semester() == self.autumn_first_day
|
2023-09-07 21:11:58 +00:00
|
|
|
|
|
|
|
with freezegun.freeze_time(self.spring_semester_march):
|
2024-06-26 17:10:24 +00:00
|
|
|
assert get_start_of_semester() == self.spring_first_day
|
2023-09-07 21:11:58 +00:00
|
|
|
|
|
|
|
def test_get_start_of_semester_changing_date(self):
|
|
|
|
"""
|
|
|
|
Test that the get_start_of_semester correctly gives the starting date of the semester,
|
|
|
|
even when the semester changes while the server isn't restarted.
|
|
|
|
"""
|
|
|
|
spring_2023 = date(2023, self.spring_month, self.spring_day)
|
|
|
|
autumn_2023 = date(2023, self.autumn_month, self.autumn_day)
|
|
|
|
mid_spring = spring_2023 + timedelta(days=45)
|
|
|
|
mid_autumn = autumn_2023 + timedelta(days=45)
|
|
|
|
|
|
|
|
with freezegun.freeze_time(mid_spring) as frozen_time:
|
2024-06-26 17:10:24 +00:00
|
|
|
assert get_start_of_semester() == spring_2023
|
2023-09-07 21:11:58 +00:00
|
|
|
|
|
|
|
# forward time to the middle of the next semester
|
|
|
|
frozen_time.move_to(mid_autumn)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert get_start_of_semester() == autumn_2023
|