mirror of
https://github.com/ae-utbm/sith.git
synced 2025-04-16 02:50:22 +00:00
commit
b541e7c1fc
0
club/tests/__init__.py
Normal file
0
club/tests/__init__.py
Normal file
71
club/tests/base.py
Normal file
71
club/tests/base.py
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
from django.core.cache import cache
|
||||||
|
from django.test import TestCase
|
||||||
|
from django.urls import reverse
|
||||||
|
from django.utils.timezone import now
|
||||||
|
from model_bakery import baker
|
||||||
|
|
||||||
|
from club.models import Club, Membership
|
||||||
|
from core.models import User
|
||||||
|
|
||||||
|
|
||||||
|
class TestClub(TestCase):
|
||||||
|
"""Set up data for test cases related to clubs and membership.
|
||||||
|
|
||||||
|
The generated dataset is the one created by the populate command,
|
||||||
|
plus the following modifications :
|
||||||
|
|
||||||
|
- `self.club` is a dummy club recreated for each test
|
||||||
|
- `self.club` has two board members : skia (role 3) and comptable (role 10)
|
||||||
|
- `self.club` has one regular member : richard
|
||||||
|
- `self.club` has one former member : sli (who had role 2)
|
||||||
|
- None of the `self.club` members are in the AE club.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpTestData(cls):
|
||||||
|
# subscribed users - initial members
|
||||||
|
cls.skia = User.objects.get(username="skia")
|
||||||
|
# by default, Skia is in the AE, which creates side effect
|
||||||
|
cls.skia.memberships.all().delete()
|
||||||
|
cls.richard = User.objects.get(username="rbatsbak")
|
||||||
|
cls.comptable = User.objects.get(username="comptable")
|
||||||
|
cls.sli = User.objects.get(username="sli")
|
||||||
|
cls.root = User.objects.get(username="root")
|
||||||
|
|
||||||
|
# subscribed users - not initial members
|
||||||
|
cls.krophil = User.objects.get(username="krophil")
|
||||||
|
cls.subscriber = User.objects.get(username="subscriber")
|
||||||
|
|
||||||
|
# old subscriber
|
||||||
|
cls.old_subscriber = User.objects.get(username="old_subscriber")
|
||||||
|
|
||||||
|
# not subscribed
|
||||||
|
cls.public = User.objects.get(username="public")
|
||||||
|
|
||||||
|
cls.ae = Club.objects.get(pk=settings.SITH_MAIN_CLUB_ID)
|
||||||
|
cls.club = baker.make(Club)
|
||||||
|
cls.members_url = reverse("club:club_members", kwargs={"club_id": cls.club.id})
|
||||||
|
a_month_ago = now() - timedelta(days=30)
|
||||||
|
yesterday = now() - timedelta(days=1)
|
||||||
|
Membership.objects.create(
|
||||||
|
club=cls.club, user=cls.skia, start_date=a_month_ago, role=3
|
||||||
|
)
|
||||||
|
Membership.objects.create(club=cls.club, user=cls.richard, role=1)
|
||||||
|
Membership.objects.create(
|
||||||
|
club=cls.club, user=cls.comptable, start_date=a_month_ago, role=10
|
||||||
|
)
|
||||||
|
|
||||||
|
# sli was a member but isn't anymore
|
||||||
|
Membership.objects.create(
|
||||||
|
club=cls.club,
|
||||||
|
user=cls.sli,
|
||||||
|
start_date=a_month_ago,
|
||||||
|
end_date=yesterday,
|
||||||
|
role=2,
|
||||||
|
)
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
cache.clear()
|
38
club/tests/test_edit.py
Normal file
38
club/tests/test_edit.py
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
import pytest
|
||||||
|
from django.test import Client
|
||||||
|
from django.urls import reverse
|
||||||
|
from model_bakery import baker
|
||||||
|
from pytest_django.asserts import assertRedirects
|
||||||
|
|
||||||
|
from club.models import Club, Membership
|
||||||
|
from core.baker_recipes import subscriber_user
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_club_board_member_cannot_edit_club_properties(client: Client):
|
||||||
|
user = subscriber_user.make()
|
||||||
|
club = baker.make(Club, name="old name", is_active=True, address="old address")
|
||||||
|
baker.make(Membership, club=club, user=user, role=7)
|
||||||
|
client.force_login(user)
|
||||||
|
res = client.post(
|
||||||
|
reverse("club:club_edit", kwargs={"club_id": club.id}),
|
||||||
|
{"name": "new name", "is_active": False, "address": "new address"},
|
||||||
|
)
|
||||||
|
# The request should success,
|
||||||
|
# but admin-only fields shouldn't be taken into account
|
||||||
|
assertRedirects(res, club.get_absolute_url())
|
||||||
|
club.refresh_from_db()
|
||||||
|
assert club.name == "old name"
|
||||||
|
assert club.is_active
|
||||||
|
assert club.address == "new address"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_edit_club_page_doesnt_crash(client: Client):
|
||||||
|
"""crash test for club:club_edit"""
|
||||||
|
club = baker.make(Club)
|
||||||
|
user = subscriber_user.make()
|
||||||
|
baker.make(Membership, club=club, user=user, role=3)
|
||||||
|
client.force_login(user)
|
||||||
|
res = client.get(reverse("club:club_edit", kwargs={"club_id": club.id}))
|
||||||
|
assert res.status_code == 200
|
327
club/tests/test_mailing.py
Normal file
327
club/tests/test_mailing.py
Normal file
@ -0,0 +1,327 @@
|
|||||||
|
from django.conf import settings
|
||||||
|
from django.test import TestCase
|
||||||
|
from django.urls import reverse
|
||||||
|
from django.utils import timezone
|
||||||
|
from django.utils.translation import gettext as _
|
||||||
|
|
||||||
|
from club.forms import MailingForm
|
||||||
|
from club.models import Club, Mailing, Membership
|
||||||
|
from core.models import User
|
||||||
|
|
||||||
|
|
||||||
|
class TestMailingForm(TestCase):
|
||||||
|
"""Perform validation tests for MailingForm."""
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpTestData(cls):
|
||||||
|
cls.skia = User.objects.get(username="skia")
|
||||||
|
cls.rbatsbak = User.objects.get(username="rbatsbak")
|
||||||
|
cls.krophil = User.objects.get(username="krophil")
|
||||||
|
cls.comunity = User.objects.get(username="comunity")
|
||||||
|
cls.root = User.objects.get(username="root")
|
||||||
|
cls.club = Club.objects.get(id=settings.SITH_PDF_CLUB_ID)
|
||||||
|
cls.mail_url = reverse("club:mailing", kwargs={"club_id": cls.club.id})
|
||||||
|
Membership(
|
||||||
|
user=cls.rbatsbak,
|
||||||
|
club=cls.club,
|
||||||
|
start_date=timezone.now(),
|
||||||
|
role=settings.SITH_CLUB_ROLES_ID["Board member"],
|
||||||
|
).save()
|
||||||
|
|
||||||
|
def test_mailing_list_add_no_moderation(self):
|
||||||
|
# Test with Communication admin
|
||||||
|
self.client.force_login(self.comunity)
|
||||||
|
response = self.client.post(
|
||||||
|
self.mail_url,
|
||||||
|
{"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "foyer"},
|
||||||
|
)
|
||||||
|
self.assertRedirects(response, self.mail_url)
|
||||||
|
response = self.client.get(self.mail_url)
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert "Liste de diffusion foyer@utbm.fr" in response.content.decode()
|
||||||
|
|
||||||
|
# Test with Root
|
||||||
|
self.client.force_login(self.root)
|
||||||
|
self.client.post(
|
||||||
|
self.mail_url,
|
||||||
|
{"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "mde"},
|
||||||
|
)
|
||||||
|
response = self.client.get(self.mail_url)
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert "Liste de diffusion mde@utbm.fr" in response.content.decode()
|
||||||
|
|
||||||
|
def test_mailing_list_add_moderation(self):
|
||||||
|
self.client.force_login(self.rbatsbak)
|
||||||
|
self.client.post(
|
||||||
|
self.mail_url,
|
||||||
|
{"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "mde"},
|
||||||
|
)
|
||||||
|
response = self.client.get(self.mail_url)
|
||||||
|
assert response.status_code == 200
|
||||||
|
content = response.content.decode()
|
||||||
|
assert "Liste de diffusion mde@utbm.fr" not in content
|
||||||
|
assert "<p>Listes de diffusions en attente de modération</p>" in content
|
||||||
|
assert "<li>mde@utbm.fr" in content
|
||||||
|
|
||||||
|
def test_mailing_list_forbidden(self):
|
||||||
|
# With anonymous user
|
||||||
|
response = self.client.get(self.mail_url)
|
||||||
|
self.assertContains(response, "", status_code=403)
|
||||||
|
|
||||||
|
# With user not in club
|
||||||
|
self.client.force_login(self.krophil)
|
||||||
|
response = self.client.get(self.mail_url)
|
||||||
|
assert response.status_code == 403
|
||||||
|
|
||||||
|
def test_add_new_subscription_fail_not_moderated(self):
|
||||||
|
self.client.force_login(self.rbatsbak)
|
||||||
|
self.client.post(
|
||||||
|
self.mail_url,
|
||||||
|
{"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "mde"},
|
||||||
|
)
|
||||||
|
|
||||||
|
self.client.post(
|
||||||
|
self.mail_url,
|
||||||
|
{
|
||||||
|
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
||||||
|
"subscription_users": self.skia.id,
|
||||||
|
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
response = self.client.get(self.mail_url)
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert "skia@git.an" not in response.content.decode()
|
||||||
|
|
||||||
|
def test_add_new_subscription_success(self):
|
||||||
|
# Prepare mailing list
|
||||||
|
self.client.force_login(self.comunity)
|
||||||
|
self.client.post(
|
||||||
|
self.mail_url,
|
||||||
|
{"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "mde"},
|
||||||
|
)
|
||||||
|
|
||||||
|
# Add single user
|
||||||
|
self.client.post(
|
||||||
|
self.mail_url,
|
||||||
|
{
|
||||||
|
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
||||||
|
"subscription_users": self.skia.id,
|
||||||
|
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
response = self.client.get(self.mail_url)
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert "skia@git.an" in response.content.decode()
|
||||||
|
|
||||||
|
# Add multiple users
|
||||||
|
self.client.post(
|
||||||
|
self.mail_url,
|
||||||
|
{
|
||||||
|
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
||||||
|
"subscription_users": (self.comunity.id, self.rbatsbak.id),
|
||||||
|
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
response = self.client.get(self.mail_url)
|
||||||
|
assert response.status_code == 200
|
||||||
|
content = response.content.decode()
|
||||||
|
assert "richard@git.an" in content
|
||||||
|
assert "comunity@git.an" in content
|
||||||
|
assert "skia@git.an" in content
|
||||||
|
|
||||||
|
# Add arbitrary email
|
||||||
|
self.client.post(
|
||||||
|
self.mail_url,
|
||||||
|
{
|
||||||
|
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
||||||
|
"subscription_email": "arbitrary@git.an",
|
||||||
|
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
response = self.client.get(self.mail_url)
|
||||||
|
assert response.status_code == 200
|
||||||
|
content = response.content.decode()
|
||||||
|
assert "richard@git.an" in content
|
||||||
|
assert "comunity@git.an" in content
|
||||||
|
assert "skia@git.an" in content
|
||||||
|
assert "arbitrary@git.an" in content
|
||||||
|
|
||||||
|
# Add user and arbitrary email
|
||||||
|
self.client.post(
|
||||||
|
self.mail_url,
|
||||||
|
{
|
||||||
|
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
||||||
|
"subscription_email": "more.arbitrary@git.an",
|
||||||
|
"subscription_users": self.krophil.id,
|
||||||
|
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
response = self.client.get(self.mail_url)
|
||||||
|
assert response.status_code == 200
|
||||||
|
content = response.content.decode()
|
||||||
|
assert "richard@git.an" in content
|
||||||
|
assert "comunity@git.an" in content
|
||||||
|
assert "skia@git.an" in content
|
||||||
|
assert "arbitrary@git.an" in content
|
||||||
|
assert "more.arbitrary@git.an" in content
|
||||||
|
assert "krophil@git.an" in content
|
||||||
|
|
||||||
|
def test_add_new_subscription_fail_form_errors(self):
|
||||||
|
# Prepare mailing list
|
||||||
|
self.client.force_login(self.comunity)
|
||||||
|
self.client.post(
|
||||||
|
self.mail_url,
|
||||||
|
{"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "mde"},
|
||||||
|
)
|
||||||
|
|
||||||
|
# Neither email or email is specified
|
||||||
|
response = self.client.post(
|
||||||
|
self.mail_url,
|
||||||
|
{
|
||||||
|
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
||||||
|
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
assert response.status_code
|
||||||
|
self.assertInHTML(
|
||||||
|
_("You must specify at least an user or an email address"),
|
||||||
|
response.content.decode(),
|
||||||
|
)
|
||||||
|
|
||||||
|
# No mailing specified
|
||||||
|
response = self.client.post(
|
||||||
|
self.mail_url,
|
||||||
|
{
|
||||||
|
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
||||||
|
"subscription_users": self.krophil.id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert _("This field is required") in response.content.decode()
|
||||||
|
|
||||||
|
# One of the selected users doesn't exist
|
||||||
|
response = self.client.post(
|
||||||
|
self.mail_url,
|
||||||
|
{
|
||||||
|
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
||||||
|
"subscription_users": [789],
|
||||||
|
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
assert response.status_code == 200
|
||||||
|
self.assertInHTML(
|
||||||
|
_("You must specify at least an user or an email address"),
|
||||||
|
response.content.decode(),
|
||||||
|
)
|
||||||
|
|
||||||
|
# An user has no email address
|
||||||
|
self.krophil.email = ""
|
||||||
|
self.krophil.save()
|
||||||
|
|
||||||
|
response = self.client.post(
|
||||||
|
self.mail_url,
|
||||||
|
{
|
||||||
|
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
||||||
|
"subscription_users": self.krophil.id,
|
||||||
|
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
assert response.status_code == 200
|
||||||
|
self.assertInHTML(
|
||||||
|
_("One of the selected users doesn't have an email address"),
|
||||||
|
response.content.decode(),
|
||||||
|
)
|
||||||
|
|
||||||
|
self.krophil.email = "krophil@git.an"
|
||||||
|
self.krophil.save()
|
||||||
|
|
||||||
|
# An user is added twice
|
||||||
|
|
||||||
|
self.client.post(
|
||||||
|
self.mail_url,
|
||||||
|
{
|
||||||
|
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
||||||
|
"subscription_users": self.krophil.id,
|
||||||
|
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
response = self.client.post(
|
||||||
|
self.mail_url,
|
||||||
|
{
|
||||||
|
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
||||||
|
"subscription_users": self.krophil.id,
|
||||||
|
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
assert response.status_code == 200
|
||||||
|
self.assertInHTML(
|
||||||
|
_("This email is already suscribed in this mailing"),
|
||||||
|
response.content.decode(),
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_remove_subscription_success(self):
|
||||||
|
# Prepare mailing list
|
||||||
|
self.client.force_login(self.comunity)
|
||||||
|
self.client.post(
|
||||||
|
self.mail_url,
|
||||||
|
{"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "mde"},
|
||||||
|
)
|
||||||
|
mde = Mailing.objects.get(email="mde")
|
||||||
|
self.client.post(
|
||||||
|
self.mail_url,
|
||||||
|
{
|
||||||
|
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
||||||
|
"subscription_users": (
|
||||||
|
self.comunity.id,
|
||||||
|
self.rbatsbak.id,
|
||||||
|
self.krophil.id,
|
||||||
|
),
|
||||||
|
"subscription_mailing": mde.id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
response = self.client.get(self.mail_url)
|
||||||
|
assert response.status_code == 200
|
||||||
|
content = response.content.decode()
|
||||||
|
|
||||||
|
assert "comunity@git.an" in content
|
||||||
|
assert "richard@git.an" in content
|
||||||
|
assert "krophil@git.an" in content
|
||||||
|
|
||||||
|
# Delete one user
|
||||||
|
self.client.post(
|
||||||
|
self.mail_url,
|
||||||
|
{
|
||||||
|
"action": MailingForm.ACTION_REMOVE_SUBSCRIPTION,
|
||||||
|
"removal_%d" % mde.id: mde.subscriptions.get(user=self.krophil).id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
response = self.client.get(self.mail_url)
|
||||||
|
assert response.status_code == 200
|
||||||
|
content = response.content.decode()
|
||||||
|
|
||||||
|
assert "comunity@git.an" in content
|
||||||
|
assert "richard@git.an" in content
|
||||||
|
assert "krophil@git.an" not in content
|
||||||
|
|
||||||
|
# Delete multiple users
|
||||||
|
self.client.post(
|
||||||
|
self.mail_url,
|
||||||
|
{
|
||||||
|
"action": MailingForm.ACTION_REMOVE_SUBSCRIPTION,
|
||||||
|
"removal_%d" % mde.id: [
|
||||||
|
user.id
|
||||||
|
for user in mde.subscriptions.filter(
|
||||||
|
user__in=[self.rbatsbak, self.comunity]
|
||||||
|
).all()
|
||||||
|
],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
response = self.client.get(self.mail_url)
|
||||||
|
assert response.status_code == 200
|
||||||
|
content = response.content.decode()
|
||||||
|
|
||||||
|
assert "comunity@git.an" not in content
|
||||||
|
assert "richard@git.an" not in content
|
||||||
|
assert "krophil@git.an" not in content
|
@ -1,96 +1,15 @@
|
|||||||
#
|
|
||||||
# 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)
|
|
||||||
# SEE : https://raw.githubusercontent.com/ae-utbm/sith/master/LICENSE
|
|
||||||
# OR WITHIN THE LOCAL FILE "LICENSE"
|
|
||||||
#
|
|
||||||
#
|
|
||||||
from datetime import timedelta
|
|
||||||
|
|
||||||
import pytest
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from django.test import Client, TestCase
|
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.utils import timezone
|
|
||||||
from django.utils.timezone import localdate, localtime, now
|
from django.utils.timezone import localdate, localtime, now
|
||||||
from django.utils.translation import gettext as _
|
|
||||||
from model_bakery import baker
|
from model_bakery import baker
|
||||||
from pytest_django.asserts import assertRedirects
|
|
||||||
|
|
||||||
from club.forms import MailingForm
|
from club.models import Membership
|
||||||
from club.models import Club, Mailing, Membership
|
from club.tests.base import TestClub
|
||||||
from core.baker_recipes import subscriber_user
|
from core.baker_recipes import subscriber_user
|
||||||
from core.models import AnonymousUser, User
|
from core.models import AnonymousUser, User
|
||||||
|
|
||||||
|
|
||||||
class TestClub(TestCase):
|
|
||||||
"""Set up data for test cases related to clubs and membership.
|
|
||||||
|
|
||||||
The generated dataset is the one created by the populate command,
|
|
||||||
plus the following modifications :
|
|
||||||
|
|
||||||
- `self.club` is a dummy club recreated for each test
|
|
||||||
- `self.club` has two board members : skia (role 3) and comptable (role 10)
|
|
||||||
- `self.club` has one regular member : richard
|
|
||||||
- `self.club` has one former member : sli (who had role 2)
|
|
||||||
- None of the `self.club` members are in the AE club.
|
|
||||||
"""
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def setUpTestData(cls):
|
|
||||||
# subscribed users - initial members
|
|
||||||
cls.skia = User.objects.get(username="skia")
|
|
||||||
# by default, Skia is in the AE, which creates side effect
|
|
||||||
cls.skia.memberships.all().delete()
|
|
||||||
cls.richard = User.objects.get(username="rbatsbak")
|
|
||||||
cls.comptable = User.objects.get(username="comptable")
|
|
||||||
cls.sli = User.objects.get(username="sli")
|
|
||||||
cls.root = User.objects.get(username="root")
|
|
||||||
|
|
||||||
# subscribed users - not initial members
|
|
||||||
cls.krophil = User.objects.get(username="krophil")
|
|
||||||
cls.subscriber = User.objects.get(username="subscriber")
|
|
||||||
|
|
||||||
# old subscriber
|
|
||||||
cls.old_subscriber = User.objects.get(username="old_subscriber")
|
|
||||||
|
|
||||||
# not subscribed
|
|
||||||
cls.public = User.objects.get(username="public")
|
|
||||||
|
|
||||||
cls.ae = Club.objects.get(pk=settings.SITH_MAIN_CLUB_ID)
|
|
||||||
cls.club = baker.make(Club)
|
|
||||||
cls.members_url = reverse("club:club_members", kwargs={"club_id": cls.club.id})
|
|
||||||
a_month_ago = now() - timedelta(days=30)
|
|
||||||
yesterday = now() - timedelta(days=1)
|
|
||||||
Membership.objects.create(
|
|
||||||
club=cls.club, user=cls.skia, start_date=a_month_ago, role=3
|
|
||||||
)
|
|
||||||
Membership.objects.create(club=cls.club, user=cls.richard, role=1)
|
|
||||||
Membership.objects.create(
|
|
||||||
club=cls.club, user=cls.comptable, start_date=a_month_ago, role=10
|
|
||||||
)
|
|
||||||
|
|
||||||
# sli was a member but isn't anymore
|
|
||||||
Membership.objects.create(
|
|
||||||
club=cls.club,
|
|
||||||
user=cls.sli,
|
|
||||||
start_date=a_month_ago,
|
|
||||||
end_date=yesterday,
|
|
||||||
role=2,
|
|
||||||
)
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
cache.clear()
|
|
||||||
|
|
||||||
|
|
||||||
class TestMembershipQuerySet(TestClub):
|
class TestMembershipQuerySet(TestClub):
|
||||||
def test_ongoing(self):
|
def test_ongoing(self):
|
||||||
"""Test that the ongoing queryset method returns the memberships that
|
"""Test that the ongoing queryset method returns the memberships that
|
||||||
@ -215,7 +134,7 @@ class TestMembershipQuerySet(TestClub):
|
|||||||
assert set(user.groups.all()).isdisjoint(club_groups)
|
assert set(user.groups.all()).isdisjoint(club_groups)
|
||||||
|
|
||||||
|
|
||||||
class TestClubModel(TestClub):
|
class TestMembership(TestClub):
|
||||||
def assert_membership_started_today(self, user: User, role: int):
|
def assert_membership_started_today(self, user: User, role: int):
|
||||||
"""Assert that the given membership is active and started today."""
|
"""Assert that the given membership is active and started today."""
|
||||||
membership = user.memberships.ongoing().filter(club=self.club).first()
|
membership = user.memberships.ongoing().filter(club=self.club).first()
|
||||||
@ -564,368 +483,3 @@ class TestClubModel(TestClub):
|
|||||||
new_board = set(self.club.board_group.users.values_list("id", flat=True))
|
new_board = set(self.club.board_group.users.values_list("id", flat=True))
|
||||||
assert new_members == initial_members
|
assert new_members == initial_members
|
||||||
assert new_board == initial_board
|
assert new_board == initial_board
|
||||||
|
|
||||||
|
|
||||||
class TestMailingForm(TestCase):
|
|
||||||
"""Perform validation tests for MailingForm."""
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def setUpTestData(cls):
|
|
||||||
cls.skia = User.objects.get(username="skia")
|
|
||||||
cls.rbatsbak = User.objects.get(username="rbatsbak")
|
|
||||||
cls.krophil = User.objects.get(username="krophil")
|
|
||||||
cls.comunity = User.objects.get(username="comunity")
|
|
||||||
cls.root = User.objects.get(username="root")
|
|
||||||
cls.club = Club.objects.get(id=settings.SITH_PDF_CLUB_ID)
|
|
||||||
cls.mail_url = reverse("club:mailing", kwargs={"club_id": cls.club.id})
|
|
||||||
Membership(
|
|
||||||
user=cls.rbatsbak,
|
|
||||||
club=cls.club,
|
|
||||||
start_date=timezone.now(),
|
|
||||||
role=settings.SITH_CLUB_ROLES_ID["Board member"],
|
|
||||||
).save()
|
|
||||||
|
|
||||||
def test_mailing_list_add_no_moderation(self):
|
|
||||||
# Test with Communication admin
|
|
||||||
self.client.force_login(self.comunity)
|
|
||||||
response = self.client.post(
|
|
||||||
self.mail_url,
|
|
||||||
{"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "foyer"},
|
|
||||||
)
|
|
||||||
self.assertRedirects(response, self.mail_url)
|
|
||||||
response = self.client.get(self.mail_url)
|
|
||||||
assert response.status_code == 200
|
|
||||||
assert "Liste de diffusion foyer@utbm.fr" in response.content.decode()
|
|
||||||
|
|
||||||
# Test with Root
|
|
||||||
self.client.force_login(self.root)
|
|
||||||
self.client.post(
|
|
||||||
self.mail_url,
|
|
||||||
{"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "mde"},
|
|
||||||
)
|
|
||||||
response = self.client.get(self.mail_url)
|
|
||||||
assert response.status_code == 200
|
|
||||||
assert "Liste de diffusion mde@utbm.fr" in response.content.decode()
|
|
||||||
|
|
||||||
def test_mailing_list_add_moderation(self):
|
|
||||||
self.client.force_login(self.rbatsbak)
|
|
||||||
self.client.post(
|
|
||||||
self.mail_url,
|
|
||||||
{"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "mde"},
|
|
||||||
)
|
|
||||||
response = self.client.get(self.mail_url)
|
|
||||||
assert response.status_code == 200
|
|
||||||
content = response.content.decode()
|
|
||||||
assert "Liste de diffusion mde@utbm.fr" not in content
|
|
||||||
assert "<p>Listes de diffusions en attente de modération</p>" in content
|
|
||||||
assert "<li>mde@utbm.fr" in content
|
|
||||||
|
|
||||||
def test_mailing_list_forbidden(self):
|
|
||||||
# With anonymous user
|
|
||||||
response = self.client.get(self.mail_url)
|
|
||||||
self.assertContains(response, "", status_code=403)
|
|
||||||
|
|
||||||
# With user not in club
|
|
||||||
self.client.force_login(self.krophil)
|
|
||||||
response = self.client.get(self.mail_url)
|
|
||||||
assert response.status_code == 403
|
|
||||||
|
|
||||||
def test_add_new_subscription_fail_not_moderated(self):
|
|
||||||
self.client.force_login(self.rbatsbak)
|
|
||||||
self.client.post(
|
|
||||||
self.mail_url,
|
|
||||||
{"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "mde"},
|
|
||||||
)
|
|
||||||
|
|
||||||
self.client.post(
|
|
||||||
self.mail_url,
|
|
||||||
{
|
|
||||||
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
|
||||||
"subscription_users": self.skia.id,
|
|
||||||
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
response = self.client.get(self.mail_url)
|
|
||||||
assert response.status_code == 200
|
|
||||||
assert "skia@git.an" not in response.content.decode()
|
|
||||||
|
|
||||||
def test_add_new_subscription_success(self):
|
|
||||||
# Prepare mailing list
|
|
||||||
self.client.force_login(self.comunity)
|
|
||||||
self.client.post(
|
|
||||||
self.mail_url,
|
|
||||||
{"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "mde"},
|
|
||||||
)
|
|
||||||
|
|
||||||
# Add single user
|
|
||||||
self.client.post(
|
|
||||||
self.mail_url,
|
|
||||||
{
|
|
||||||
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
|
||||||
"subscription_users": self.skia.id,
|
|
||||||
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
response = self.client.get(self.mail_url)
|
|
||||||
assert response.status_code == 200
|
|
||||||
assert "skia@git.an" in response.content.decode()
|
|
||||||
|
|
||||||
# Add multiple users
|
|
||||||
self.client.post(
|
|
||||||
self.mail_url,
|
|
||||||
{
|
|
||||||
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
|
||||||
"subscription_users": (self.comunity.id, self.rbatsbak.id),
|
|
||||||
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
response = self.client.get(self.mail_url)
|
|
||||||
assert response.status_code == 200
|
|
||||||
content = response.content.decode()
|
|
||||||
assert "richard@git.an" in content
|
|
||||||
assert "comunity@git.an" in content
|
|
||||||
assert "skia@git.an" in content
|
|
||||||
|
|
||||||
# Add arbitrary email
|
|
||||||
self.client.post(
|
|
||||||
self.mail_url,
|
|
||||||
{
|
|
||||||
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
|
||||||
"subscription_email": "arbitrary@git.an",
|
|
||||||
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
response = self.client.get(self.mail_url)
|
|
||||||
assert response.status_code == 200
|
|
||||||
content = response.content.decode()
|
|
||||||
assert "richard@git.an" in content
|
|
||||||
assert "comunity@git.an" in content
|
|
||||||
assert "skia@git.an" in content
|
|
||||||
assert "arbitrary@git.an" in content
|
|
||||||
|
|
||||||
# Add user and arbitrary email
|
|
||||||
self.client.post(
|
|
||||||
self.mail_url,
|
|
||||||
{
|
|
||||||
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
|
||||||
"subscription_email": "more.arbitrary@git.an",
|
|
||||||
"subscription_users": self.krophil.id,
|
|
||||||
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
response = self.client.get(self.mail_url)
|
|
||||||
assert response.status_code == 200
|
|
||||||
content = response.content.decode()
|
|
||||||
assert "richard@git.an" in content
|
|
||||||
assert "comunity@git.an" in content
|
|
||||||
assert "skia@git.an" in content
|
|
||||||
assert "arbitrary@git.an" in content
|
|
||||||
assert "more.arbitrary@git.an" in content
|
|
||||||
assert "krophil@git.an" in content
|
|
||||||
|
|
||||||
def test_add_new_subscription_fail_form_errors(self):
|
|
||||||
# Prepare mailing list
|
|
||||||
self.client.force_login(self.comunity)
|
|
||||||
self.client.post(
|
|
||||||
self.mail_url,
|
|
||||||
{"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "mde"},
|
|
||||||
)
|
|
||||||
|
|
||||||
# Neither email or email is specified
|
|
||||||
response = self.client.post(
|
|
||||||
self.mail_url,
|
|
||||||
{
|
|
||||||
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
|
||||||
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
assert response.status_code
|
|
||||||
self.assertInHTML(
|
|
||||||
_("You must specify at least an user or an email address"),
|
|
||||||
response.content.decode(),
|
|
||||||
)
|
|
||||||
|
|
||||||
# No mailing specified
|
|
||||||
response = self.client.post(
|
|
||||||
self.mail_url,
|
|
||||||
{
|
|
||||||
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
|
||||||
"subscription_users": self.krophil.id,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
assert response.status_code == 200
|
|
||||||
assert _("This field is required") in response.content.decode()
|
|
||||||
|
|
||||||
# One of the selected users doesn't exist
|
|
||||||
response = self.client.post(
|
|
||||||
self.mail_url,
|
|
||||||
{
|
|
||||||
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
|
||||||
"subscription_users": [789],
|
|
||||||
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
assert response.status_code == 200
|
|
||||||
self.assertInHTML(
|
|
||||||
_("You must specify at least an user or an email address"),
|
|
||||||
response.content.decode(),
|
|
||||||
)
|
|
||||||
|
|
||||||
# An user has no email address
|
|
||||||
self.krophil.email = ""
|
|
||||||
self.krophil.save()
|
|
||||||
|
|
||||||
response = self.client.post(
|
|
||||||
self.mail_url,
|
|
||||||
{
|
|
||||||
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
|
||||||
"subscription_users": self.krophil.id,
|
|
||||||
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
assert response.status_code == 200
|
|
||||||
self.assertInHTML(
|
|
||||||
_("One of the selected users doesn't have an email address"),
|
|
||||||
response.content.decode(),
|
|
||||||
)
|
|
||||||
|
|
||||||
self.krophil.email = "krophil@git.an"
|
|
||||||
self.krophil.save()
|
|
||||||
|
|
||||||
# An user is added twice
|
|
||||||
|
|
||||||
self.client.post(
|
|
||||||
self.mail_url,
|
|
||||||
{
|
|
||||||
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
|
||||||
"subscription_users": self.krophil.id,
|
|
||||||
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
response = self.client.post(
|
|
||||||
self.mail_url,
|
|
||||||
{
|
|
||||||
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
|
||||||
"subscription_users": self.krophil.id,
|
|
||||||
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
assert response.status_code == 200
|
|
||||||
self.assertInHTML(
|
|
||||||
_("This email is already suscribed in this mailing"),
|
|
||||||
response.content.decode(),
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_remove_subscription_success(self):
|
|
||||||
# Prepare mailing list
|
|
||||||
self.client.force_login(self.comunity)
|
|
||||||
self.client.post(
|
|
||||||
self.mail_url,
|
|
||||||
{"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "mde"},
|
|
||||||
)
|
|
||||||
mde = Mailing.objects.get(email="mde")
|
|
||||||
self.client.post(
|
|
||||||
self.mail_url,
|
|
||||||
{
|
|
||||||
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
|
||||||
"subscription_users": (
|
|
||||||
self.comunity.id,
|
|
||||||
self.rbatsbak.id,
|
|
||||||
self.krophil.id,
|
|
||||||
),
|
|
||||||
"subscription_mailing": mde.id,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
response = self.client.get(self.mail_url)
|
|
||||||
assert response.status_code == 200
|
|
||||||
content = response.content.decode()
|
|
||||||
|
|
||||||
assert "comunity@git.an" in content
|
|
||||||
assert "richard@git.an" in content
|
|
||||||
assert "krophil@git.an" in content
|
|
||||||
|
|
||||||
# Delete one user
|
|
||||||
self.client.post(
|
|
||||||
self.mail_url,
|
|
||||||
{
|
|
||||||
"action": MailingForm.ACTION_REMOVE_SUBSCRIPTION,
|
|
||||||
"removal_%d" % mde.id: mde.subscriptions.get(user=self.krophil).id,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
response = self.client.get(self.mail_url)
|
|
||||||
assert response.status_code == 200
|
|
||||||
content = response.content.decode()
|
|
||||||
|
|
||||||
assert "comunity@git.an" in content
|
|
||||||
assert "richard@git.an" in content
|
|
||||||
assert "krophil@git.an" not in content
|
|
||||||
|
|
||||||
# Delete multiple users
|
|
||||||
self.client.post(
|
|
||||||
self.mail_url,
|
|
||||||
{
|
|
||||||
"action": MailingForm.ACTION_REMOVE_SUBSCRIPTION,
|
|
||||||
"removal_%d" % mde.id: [
|
|
||||||
user.id
|
|
||||||
for user in mde.subscriptions.filter(
|
|
||||||
user__in=[self.rbatsbak, self.comunity]
|
|
||||||
).all()
|
|
||||||
],
|
|
||||||
},
|
|
||||||
)
|
|
||||||
response = self.client.get(self.mail_url)
|
|
||||||
assert response.status_code == 200
|
|
||||||
content = response.content.decode()
|
|
||||||
|
|
||||||
assert "comunity@git.an" not in content
|
|
||||||
assert "richard@git.an" not in content
|
|
||||||
assert "krophil@git.an" not in content
|
|
||||||
|
|
||||||
|
|
||||||
class TestClubSellingView(TestCase):
|
|
||||||
"""Perform basics tests to ensure that the page is available."""
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def setUpTestData(cls):
|
|
||||||
cls.club = baker.make(Club)
|
|
||||||
cls.admin = baker.make(User, is_superuser=True)
|
|
||||||
|
|
||||||
def test_page_not_internal_error(self):
|
|
||||||
"""Test that the page does not return and internal error."""
|
|
||||||
self.client.force_login(self.admin)
|
|
||||||
response = self.client.get(
|
|
||||||
reverse("club:club_sellings", kwargs={"club_id": self.club.id})
|
|
||||||
)
|
|
||||||
assert response.status_code == 200
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
|
||||||
def test_club_board_member_cannot_edit_club_properties(client: Client):
|
|
||||||
user = subscriber_user.make()
|
|
||||||
club = baker.make(Club, name="old name", is_active=True, address="old address")
|
|
||||||
baker.make(Membership, club=club, user=user, role=7)
|
|
||||||
client.force_login(user)
|
|
||||||
res = client.post(
|
|
||||||
reverse("club:club_edit", kwargs={"club_id": club.id}),
|
|
||||||
{"name": "new name", "is_active": False, "address": "new address"},
|
|
||||||
)
|
|
||||||
# The request should success,
|
|
||||||
# but admin-only fields shouldn't be taken into account
|
|
||||||
assertRedirects(res, club.get_absolute_url())
|
|
||||||
club.refresh_from_db()
|
|
||||||
assert club.name == "old name"
|
|
||||||
assert club.is_active
|
|
||||||
assert club.address == "new address"
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
|
||||||
def test_edit_club_page_doesnt_crash(client: Client):
|
|
||||||
"""crash test for club:club_edit"""
|
|
||||||
club = baker.make(Club)
|
|
||||||
user = subscriber_user.make()
|
|
||||||
baker.make(Membership, club=club, user=user, role=3)
|
|
||||||
client.force_login(user)
|
|
||||||
res = client.get(reverse("club:club_edit", kwargs={"club_id": club.id}))
|
|
||||||
assert res.status_code == 200
|
|
39
club/tests/test_page.py
Normal file
39
club/tests/test_page.py
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import pytest
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
from django.test import Client
|
||||||
|
from django.urls import reverse
|
||||||
|
from model_bakery import baker
|
||||||
|
from pytest_django.asserts import assertHTMLEqual
|
||||||
|
|
||||||
|
from club.models import Club
|
||||||
|
from core.markdown import markdown
|
||||||
|
from core.models import PageRev, User
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_page_display_on_club_main_page(client: Client):
|
||||||
|
"""Test the club Page is properly displayed on the club main view"""
|
||||||
|
club = baker.make(Club)
|
||||||
|
content = "# foo\nLorem ipsum dolor sit amet"
|
||||||
|
baker.make(PageRev, page=club.page, revision=1, content=content)
|
||||||
|
client.force_login(baker.make(User))
|
||||||
|
res = client.get(reverse("club:club_view", kwargs={"club_id": club.id}))
|
||||||
|
|
||||||
|
assert res.status_code == 200
|
||||||
|
soup = BeautifulSoup(res.content.decode(), "lxml")
|
||||||
|
detail_html = soup.find(id="club_detail").find(class_="markdown")
|
||||||
|
assertHTMLEqual(detail_html.decode_contents(), markdown(content))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_club_main_page_without_content(client: Client):
|
||||||
|
"""Test the club view works, even if the club page is empty"""
|
||||||
|
club = baker.make(Club)
|
||||||
|
club.page.revisions.all().delete()
|
||||||
|
client.force_login(baker.make(User))
|
||||||
|
res = client.get(reverse("club:club_view", kwargs={"club_id": club.id}))
|
||||||
|
|
||||||
|
assert res.status_code == 200
|
||||||
|
soup = BeautifulSoup(res.content.decode(), "lxml")
|
||||||
|
detail_html = soup.find(id="club_detail")
|
||||||
|
assert detail_html.find_all("markdown") == []
|
16
club/tests/test_sales.py
Normal file
16
club/tests/test_sales.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import pytest
|
||||||
|
from django.test import Client
|
||||||
|
from django.urls import reverse
|
||||||
|
from model_bakery import baker
|
||||||
|
|
||||||
|
from club.models import Club
|
||||||
|
from core.models import User
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_sales_page_doesnt_crash(client: Client):
|
||||||
|
club = baker.make(Club)
|
||||||
|
admin = baker.make(User, is_superuser=True)
|
||||||
|
client.force_login(admin)
|
||||||
|
response = client.get(reverse("club:club_sellings", kwargs={"club_id": club.id}))
|
||||||
|
assert response.status_code == 200
|
@ -185,6 +185,7 @@ class ClubView(ClubTabsMixin, DetailView):
|
|||||||
kwargs["page_revision"] = (
|
kwargs["page_revision"] = (
|
||||||
PageRev.objects.filter(page_id=self.object.page_id)
|
PageRev.objects.filter(page_id=self.object.page_id)
|
||||||
.order_by("-date")
|
.order_by("-date")
|
||||||
|
.values_list("content", flat=True)
|
||||||
.first()
|
.first()
|
||||||
)
|
)
|
||||||
return kwargs
|
return kwargs
|
||||||
|
Loading…
x
Reference in New Issue
Block a user