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
|
|
|
#
|
|
|
|
#
|
2023-05-02 10:36:59 +00:00
|
|
|
from datetime import timedelta
|
2017-04-24 15:51:12 +00:00
|
|
|
|
2019-05-09 16:06:11 +00:00
|
|
|
from django.conf import settings
|
2023-05-02 10:36:59 +00:00
|
|
|
from django.core.cache import cache
|
2016-01-29 14:20:00 +00:00
|
|
|
from django.test import TestCase
|
2019-10-06 11:28:56 +00:00
|
|
|
from django.urls import reverse
|
2024-06-26 17:10:24 +00:00
|
|
|
from django.utils import timezone
|
2024-10-02 22:21:16 +00:00
|
|
|
from django.utils.timezone import localdate, localtime, now
|
2024-06-24 11:07:36 +00:00
|
|
|
from django.utils.translation import gettext as _
|
2016-01-29 14:20:00 +00:00
|
|
|
|
2019-05-09 16:06:11 +00:00
|
|
|
from club.forms import MailingForm
|
2024-06-24 11:07:36 +00:00
|
|
|
from club.models import Club, Mailing, Membership
|
2024-09-04 14:21:42 +00:00
|
|
|
from core.baker_recipes import subscriber_user
|
2024-06-24 11:07:36 +00:00
|
|
|
from core.models import AnonymousUser, User
|
2023-05-02 10:36:59 +00:00
|
|
|
from sith.settings import SITH_BAR_MANAGER, SITH_MAIN_CLUB_ID
|
2016-03-22 08:01:24 +00:00
|
|
|
|
2017-06-12 06:54:48 +00:00
|
|
|
|
2024-07-23 22:39:26 +00:00
|
|
|
class TestClub(TestCase):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Set up data for test cases related to clubs and membership.
|
|
|
|
|
2023-05-02 10:36:59 +00:00
|
|
|
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")
|
2024-06-26 09:39:07 +00:00
|
|
|
# by default, Skia is in the AE, which creates side effect
|
|
|
|
cls.skia.memberships.all().delete()
|
2023-05-02 10:36:59 +00:00
|
|
|
cls.richard = User.objects.get(username="rbatsbak")
|
|
|
|
cls.comptable = User.objects.get(username="comptable")
|
|
|
|
cls.sli = User.objects.get(username="sli")
|
2024-06-26 17:10:24 +00:00
|
|
|
cls.root = User.objects.get(username="root")
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
# 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.filter(pk=SITH_MAIN_CLUB_ID)[0]
|
2024-06-26 09:39:07 +00:00
|
|
|
cls.club = Club.objects.create(
|
2023-05-02 10:36:59 +00:00
|
|
|
name="Fake Club",
|
|
|
|
unix_name="fake-club",
|
|
|
|
address="5 rue de la République, 90000 Belfort",
|
|
|
|
)
|
2024-06-26 10:28:00 +00:00
|
|
|
cls.members_url = reverse("club:club_members", kwargs={"club_id": cls.club.id})
|
2023-05-02 10:36:59 +00:00
|
|
|
a_month_ago = now() - timedelta(days=30)
|
|
|
|
yesterday = now() - timedelta(days=1)
|
|
|
|
Membership.objects.create(
|
2024-06-26 09:39:07 +00:00
|
|
|
club=cls.club, user=cls.skia, start_date=a_month_ago, role=3
|
2023-05-02 10:36:59 +00:00
|
|
|
)
|
2024-06-26 09:39:07 +00:00
|
|
|
Membership.objects.create(club=cls.club, user=cls.richard, role=1)
|
2023-05-02 10:36:59 +00:00
|
|
|
Membership.objects.create(
|
2024-06-26 09:39:07 +00:00
|
|
|
club=cls.club, user=cls.comptable, start_date=a_month_ago, role=10
|
2023-05-02 10:36:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# sli was a member but isn't anymore
|
|
|
|
Membership.objects.create(
|
2024-06-26 09:39:07 +00:00
|
|
|
club=cls.club,
|
|
|
|
user=cls.sli,
|
2023-05-02 10:36:59 +00:00
|
|
|
start_date=a_month_ago,
|
|
|
|
end_date=yesterday,
|
|
|
|
role=2,
|
|
|
|
)
|
2024-06-26 09:39:07 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2023-05-02 10:36:59 +00:00
|
|
|
cache.clear()
|
|
|
|
|
|
|
|
|
2024-07-23 22:39:26 +00:00
|
|
|
class TestMembershipQuerySet(TestClub):
|
2023-05-02 10:36:59 +00:00
|
|
|
def test_ongoing(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that the ongoing queryset method returns the memberships that
|
2023-05-02 10:36:59 +00:00
|
|
|
are not ended.
|
|
|
|
"""
|
2024-06-26 17:10:24 +00:00
|
|
|
current_members = list(self.club.members.ongoing().order_by("id"))
|
2023-05-02 10:36:59 +00:00
|
|
|
expected = [
|
|
|
|
self.skia.memberships.get(club=self.club),
|
|
|
|
self.comptable.memberships.get(club=self.club),
|
|
|
|
self.richard.memberships.get(club=self.club),
|
|
|
|
]
|
2024-06-26 17:10:24 +00:00
|
|
|
expected.sort(key=lambda i: i.id)
|
|
|
|
assert current_members == expected
|
2023-05-02 10:36:59 +00:00
|
|
|
|
2024-09-04 14:21:42 +00:00
|
|
|
def test_ongoing_with_membership_ending_today(self):
|
|
|
|
"""Test that a membership ending the present day is considered as ended."""
|
2024-10-02 22:21:16 +00:00
|
|
|
today = localdate()
|
2024-09-04 14:21:42 +00:00
|
|
|
self.richard.memberships.filter(club=self.club).update(end_date=today)
|
|
|
|
current_members = list(self.club.members.ongoing().order_by("id"))
|
|
|
|
expected = [
|
|
|
|
self.skia.memberships.get(club=self.club),
|
|
|
|
self.comptable.memberships.get(club=self.club),
|
|
|
|
]
|
|
|
|
expected.sort(key=lambda i: i.id)
|
|
|
|
assert current_members == expected
|
|
|
|
|
2023-05-02 10:36:59 +00:00
|
|
|
def test_board(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that the board queryset method returns the memberships
|
|
|
|
of user in the club board.
|
2023-05-02 10:36:59 +00:00
|
|
|
"""
|
2024-06-26 17:10:24 +00:00
|
|
|
board_members = list(self.club.members.board().order_by("id"))
|
2023-05-02 10:36:59 +00:00
|
|
|
expected = [
|
|
|
|
self.skia.memberships.get(club=self.club),
|
|
|
|
self.comptable.memberships.get(club=self.club),
|
|
|
|
# sli is no more member, but he was in the board
|
|
|
|
self.sli.memberships.get(club=self.club),
|
|
|
|
]
|
2024-06-26 17:10:24 +00:00
|
|
|
expected.sort(key=lambda i: i.id)
|
|
|
|
assert board_members == expected
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
def test_ongoing_board(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that combining ongoing and board returns users
|
|
|
|
who are currently board members of the club.
|
2023-05-02 10:36:59 +00:00
|
|
|
"""
|
2024-06-26 17:10:24 +00:00
|
|
|
members = list(self.club.members.ongoing().board().order_by("id"))
|
2023-05-02 10:36:59 +00:00
|
|
|
expected = [
|
|
|
|
self.skia.memberships.get(club=self.club),
|
|
|
|
self.comptable.memberships.get(club=self.club),
|
|
|
|
]
|
2024-06-26 17:10:24 +00:00
|
|
|
expected.sort(key=lambda i: i.id)
|
|
|
|
assert members == expected
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
def test_update_invalidate_cache(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that the `update` queryset method properly invalidate cache."""
|
2023-05-02 10:36:59 +00:00
|
|
|
mem_skia = self.skia.memberships.get(club=self.club)
|
|
|
|
cache.set(f"membership_{mem_skia.club_id}_{mem_skia.user_id}", mem_skia)
|
|
|
|
self.skia.memberships.update(end_date=localtime(now()).date())
|
2024-06-26 17:10:24 +00:00
|
|
|
assert (
|
|
|
|
cache.get(f"membership_{mem_skia.club_id}_{mem_skia.user_id}")
|
|
|
|
== "not_member"
|
2023-05-02 10:36:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
mem_richard = self.richard.memberships.get(club=self.club)
|
|
|
|
cache.set(
|
|
|
|
f"membership_{mem_richard.club_id}_{mem_richard.user_id}", mem_richard
|
|
|
|
)
|
|
|
|
self.richard.memberships.update(role=5)
|
|
|
|
new_mem = self.richard.memberships.get(club=self.club)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert new_mem != "not_member"
|
|
|
|
assert new_mem.role == 5
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
def test_delete_invalidate_cache(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that the `delete` queryset properly invalidate cache."""
|
2023-05-02 10:36:59 +00:00
|
|
|
mem_skia = self.skia.memberships.get(club=self.club)
|
|
|
|
mem_comptable = self.comptable.memberships.get(club=self.club)
|
|
|
|
cache.set(f"membership_{mem_skia.club_id}_{mem_skia.user_id}", mem_skia)
|
|
|
|
cache.set(
|
|
|
|
f"membership_{mem_comptable.club_id}_{mem_comptable.user_id}", mem_comptable
|
|
|
|
)
|
|
|
|
|
|
|
|
# should delete the subscriptions of skia and comptable
|
|
|
|
self.club.members.ongoing().board().delete()
|
|
|
|
|
2024-06-26 10:28:00 +00:00
|
|
|
for membership in (mem_skia, mem_comptable):
|
|
|
|
cached_mem = cache.get(
|
|
|
|
f"membership_{membership.club_id}_{membership.user_id}"
|
|
|
|
)
|
|
|
|
assert cached_mem == "not_member"
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
|
2024-07-23 22:39:26 +00:00
|
|
|
class TestClubModel(TestClub):
|
2024-06-26 17:10:24 +00:00
|
|
|
def assert_membership_started_today(self, user: User, role: int):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Assert that the given membership is active and started today."""
|
2023-05-02 10:36:59 +00:00
|
|
|
membership = user.memberships.ongoing().filter(club=self.club).first()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert membership is not None
|
|
|
|
assert localtime(now()).date() == membership.start_date
|
|
|
|
assert membership.end_date is None
|
|
|
|
assert membership.role == role
|
|
|
|
assert membership.club.get_membership_for(user) == membership
|
2023-05-02 10:36:59 +00:00
|
|
|
member_group = self.club.unix_name + settings.SITH_MEMBER_SUFFIX
|
|
|
|
board_group = self.club.unix_name + settings.SITH_BOARD_SUFFIX
|
2024-06-26 17:10:24 +00:00
|
|
|
assert user.is_in_group(name=member_group)
|
|
|
|
assert user.is_in_group(name=board_group)
|
2023-05-02 10:36:59 +00:00
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
def assert_membership_ended_today(self, user: User):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Assert that the given user have a membership which ended today."""
|
2023-05-02 10:36:59 +00:00
|
|
|
today = localtime(now()).date()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert user.memberships.filter(club=self.club, end_date=today).exists()
|
|
|
|
assert self.club.get_membership_for(user) is None
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
def test_access_unauthorized(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that users who never subscribed and anonymous users
|
|
|
|
cannot see the page.
|
2023-05-02 10:36:59 +00:00
|
|
|
"""
|
|
|
|
response = self.client.post(self.members_url)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 403
|
2023-05-02 10:36:59 +00:00
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.public)
|
2023-05-02 10:36:59 +00:00
|
|
|
response = self.client.post(self.members_url)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 403
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
def test_display(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that a GET request return a page where the requested
|
2023-05-02 10:36:59 +00:00
|
|
|
information are displayed.
|
|
|
|
"""
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.skia)
|
2023-05-02 10:36:59 +00:00
|
|
|
response = self.client.get(self.members_url)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 200
|
2023-05-02 10:36:59 +00:00
|
|
|
expected_html = (
|
|
|
|
"<table><thead><tr>"
|
|
|
|
"<td>Utilisateur</td><td>Rôle</td><td>Description</td>"
|
|
|
|
"<td>Depuis</td><td>Marquer comme ancien</td>"
|
|
|
|
"</tr></thead><tbody>"
|
|
|
|
)
|
|
|
|
memberships = self.club.members.ongoing().order_by("-role")
|
|
|
|
input_id = 0
|
|
|
|
for membership in memberships.select_related("user"):
|
|
|
|
user = membership.user
|
|
|
|
expected_html += (
|
|
|
|
f"<tr><td><a href=\"{reverse('core:user_profile', args=[user.id])}\">"
|
|
|
|
f"{user.get_display_name()}</a></td>"
|
|
|
|
f"<td>{settings.SITH_CLUB_ROLES[membership.role]}</td>"
|
|
|
|
f"<td>{membership.description}</td>"
|
|
|
|
f"<td>{membership.start_date}</td><td>"
|
|
|
|
)
|
|
|
|
if membership.role <= 3: # 3 is the role of skia
|
|
|
|
expected_html += (
|
|
|
|
'<input type="checkbox" name="users_old" '
|
|
|
|
f'value="{user.id}" '
|
|
|
|
f'id="id_users_old_{input_id}">'
|
|
|
|
)
|
|
|
|
input_id += 1
|
|
|
|
expected_html += "</td></tr>"
|
|
|
|
expected_html += "</tbody></table>"
|
|
|
|
self.assertInHTML(expected_html, response.content.decode())
|
|
|
|
|
|
|
|
def test_root_add_one_club_member(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that root users can add members to clubs, one at a time."""
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.root)
|
2023-05-02 10:36:59 +00:00
|
|
|
response = self.client.post(
|
|
|
|
self.members_url,
|
|
|
|
{"users": self.subscriber.id, "role": 3},
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
2023-05-02 10:36:59 +00:00
|
|
|
self.assertRedirects(response, self.members_url)
|
|
|
|
self.subscriber.refresh_from_db()
|
2024-06-26 17:10:24 +00:00
|
|
|
self.assert_membership_started_today(self.subscriber, role=3)
|
2016-03-22 08:01:24 +00:00
|
|
|
|
2023-05-02 10:36:59 +00:00
|
|
|
def test_root_add_multiple_club_member(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that root users can add multiple members at once to clubs."""
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.root)
|
2023-05-02 10:36:59 +00:00
|
|
|
response = self.client.post(
|
|
|
|
self.members_url,
|
2019-04-24 11:48:32 +00:00
|
|
|
{
|
2023-05-02 10:36:59 +00:00
|
|
|
"users": f"|{self.subscriber.id}|{self.krophil.id}|",
|
2019-04-24 11:48:32 +00:00
|
|
|
"role": 3,
|
|
|
|
},
|
|
|
|
)
|
2023-05-02 10:36:59 +00:00
|
|
|
self.assertRedirects(response, self.members_url)
|
|
|
|
self.subscriber.refresh_from_db()
|
2024-06-26 17:10:24 +00:00
|
|
|
self.assert_membership_started_today(self.subscriber, role=3)
|
|
|
|
self.assert_membership_started_today(self.krophil, role=3)
|
2019-04-24 11:48:32 +00:00
|
|
|
|
2023-05-02 10:36:59 +00:00
|
|
|
def test_add_unauthorized_members(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that users who are not currently subscribed
|
2023-05-02 10:36:59 +00:00
|
|
|
cannot be members of clubs.
|
|
|
|
"""
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.root)
|
2018-10-04 19:29:19 +00:00
|
|
|
response = self.client.post(
|
2023-05-02 10:36:59 +00:00
|
|
|
self.members_url,
|
|
|
|
{"users": self.public.id, "role": 1},
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not self.public.memberships.filter(club=self.club).exists()
|
|
|
|
assert '<ul class="errorlist"><li>' in response.content.decode()
|
2016-03-22 08:01:24 +00:00
|
|
|
|
2018-10-04 19:29:19 +00:00
|
|
|
response = self.client.post(
|
2023-05-02 10:36:59 +00:00
|
|
|
self.members_url,
|
|
|
|
{"users": self.old_subscriber.id, "role": 1},
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not self.public.memberships.filter(club=self.club).exists()
|
|
|
|
assert self.club.get_membership_for(self.public) is None
|
|
|
|
assert '<ul class="errorlist"><li>' in response.content.decode()
|
2016-03-22 08:01:24 +00:00
|
|
|
|
2023-05-02 10:36:59 +00:00
|
|
|
def test_add_members_already_members(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that users who are already members of a club
|
|
|
|
cannot be added again to this club.
|
2023-05-02 10:36:59 +00:00
|
|
|
"""
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.root)
|
2023-05-02 10:36:59 +00:00
|
|
|
current_membership = self.skia.memberships.ongoing().get(club=self.club)
|
|
|
|
nb_memberships = self.skia.memberships.count()
|
|
|
|
self.client.post(
|
|
|
|
self.members_url,
|
|
|
|
{"users": self.skia.id, "role": current_membership.role + 1},
|
|
|
|
)
|
|
|
|
self.skia.refresh_from_db()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert nb_memberships == self.skia.memberships.count()
|
2023-05-02 10:36:59 +00:00
|
|
|
new_membership = self.skia.memberships.ongoing().get(club=self.club)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert current_membership == new_membership
|
|
|
|
assert self.club.get_membership_for(self.skia) == new_membership
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
def test_add_not_existing_users(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that not existing users cannot be added in clubs.
|
2023-05-02 10:36:59 +00:00
|
|
|
If one user in the request is invalid, no membership creation at all
|
|
|
|
can take place.
|
|
|
|
"""
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.root)
|
2023-05-02 10:36:59 +00:00
|
|
|
nb_memberships = self.club.members.count()
|
2019-04-24 11:48:32 +00:00
|
|
|
response = self.client.post(
|
2023-05-02 10:36:59 +00:00
|
|
|
self.members_url,
|
|
|
|
{"users": [9999], "role": 1},
|
2019-04-24 11:48:32 +00:00
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 200
|
|
|
|
assert '<ul class="errorlist"><li>' in response.content.decode()
|
2023-05-02 10:36:59 +00:00
|
|
|
self.club.refresh_from_db()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.club.members.count() == nb_memberships
|
2019-04-24 11:48:32 +00:00
|
|
|
response = self.client.post(
|
2023-05-02 10:36:59 +00:00
|
|
|
self.members_url,
|
2019-04-24 11:48:32 +00:00
|
|
|
{
|
2023-05-02 10:36:59 +00:00
|
|
|
"users": f"|{self.subscriber.id}|{9999}|",
|
2019-04-24 11:48:32 +00:00
|
|
|
"start_date": "12/06/2016",
|
|
|
|
"role": 3,
|
|
|
|
},
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 200
|
|
|
|
assert '<ul class="errorlist"><li>' in response.content.decode()
|
2023-05-02 10:36:59 +00:00
|
|
|
self.club.refresh_from_db()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.club.members.count() == nb_memberships
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
def test_president_add_members(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that the president of the club can add members."""
|
2023-05-02 10:36:59 +00:00
|
|
|
president = self.club.members.get(role=10).user
|
|
|
|
nb_club_membership = self.club.members.count()
|
|
|
|
nb_subscriber_memberships = self.subscriber.memberships.count()
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(president)
|
2023-05-02 10:36:59 +00:00
|
|
|
response = self.client.post(
|
|
|
|
self.members_url,
|
|
|
|
{"users": self.subscriber.id, "role": 9},
|
|
|
|
)
|
|
|
|
self.assertRedirects(response, self.members_url)
|
|
|
|
self.club.refresh_from_db()
|
|
|
|
self.subscriber.refresh_from_db()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.club.members.count() == nb_club_membership + 1
|
|
|
|
assert self.subscriber.memberships.count() == nb_subscriber_memberships + 1
|
|
|
|
self.assert_membership_started_today(self.subscriber, role=9)
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
def test_add_member_greater_role(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that a member of the club member cannot create
|
2023-05-02 10:36:59 +00:00
|
|
|
a membership with a greater role than its own.
|
|
|
|
"""
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.skia)
|
2023-05-02 10:36:59 +00:00
|
|
|
nb_memberships = self.club.members.count()
|
|
|
|
response = self.client.post(
|
|
|
|
self.members_url,
|
|
|
|
{"users": self.subscriber.id, "role": 10},
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 200
|
2023-05-02 10:36:59 +00:00
|
|
|
self.assertInHTML(
|
|
|
|
"<li>Vous n'avez pas la permission de faire cela</li>",
|
2022-02-28 13:41:01 +00:00
|
|
|
response.content.decode(),
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
2023-05-02 10:36:59 +00:00
|
|
|
self.club.refresh_from_db()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert nb_memberships == self.club.members.count()
|
|
|
|
assert not self.subscriber.memberships.filter(club=self.club).exists()
|
2016-03-22 08:01:24 +00:00
|
|
|
|
2023-05-02 10:36:59 +00:00
|
|
|
def test_add_member_without_role(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that trying to add members without specifying their role fails."""
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.root)
|
2018-10-04 19:29:19 +00:00
|
|
|
response = self.client.post(
|
2023-05-02 10:36:59 +00:00
|
|
|
self.members_url,
|
|
|
|
{"users": self.subscriber.id, "start_date": "12/06/2016"},
|
2019-04-25 13:40:49 +00:00
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert (
|
|
|
|
'<ul class="errorlist"><li>Vous devez choisir un r'
|
|
|
|
in response.content.decode()
|
2019-04-25 13:40:49 +00:00
|
|
|
)
|
|
|
|
|
2023-05-02 10:36:59 +00:00
|
|
|
def test_end_membership_self(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that a member can end its own membership."""
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.skia)
|
2019-04-25 13:40:49 +00:00
|
|
|
self.client.post(
|
2023-05-02 10:36:59 +00:00
|
|
|
self.members_url,
|
|
|
|
{"users_old": self.skia.id},
|
2019-04-25 13:40:49 +00:00
|
|
|
)
|
2023-05-02 10:36:59 +00:00
|
|
|
self.skia.refresh_from_db()
|
2024-06-26 17:10:24 +00:00
|
|
|
self.assert_membership_ended_today(self.skia)
|
2019-04-25 13:40:49 +00:00
|
|
|
|
2023-05-02 10:36:59 +00:00
|
|
|
def test_end_membership_lower_role(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that board members of the club can end memberships
|
|
|
|
of users with lower roles.
|
2023-05-02 10:36:59 +00:00
|
|
|
"""
|
|
|
|
# remainder : skia has role 3, comptable has role 10, richard has role 1
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.skia)
|
2019-04-25 13:40:49 +00:00
|
|
|
response = self.client.post(
|
2023-05-02 10:36:59 +00:00
|
|
|
self.members_url,
|
|
|
|
{"users_old": self.richard.id},
|
|
|
|
)
|
|
|
|
self.assertRedirects(response, self.members_url)
|
|
|
|
self.club.refresh_from_db()
|
2024-06-26 17:10:24 +00:00
|
|
|
self.assert_membership_ended_today(self.richard)
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
def test_end_membership_higher_role(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that board members of the club cannot end memberships
|
|
|
|
of users with higher roles.
|
2023-05-02 10:36:59 +00:00
|
|
|
"""
|
|
|
|
membership = self.comptable.memberships.filter(club=self.club).first()
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.skia)
|
2019-04-25 13:40:49 +00:00
|
|
|
self.client.post(
|
2023-05-02 10:36:59 +00:00
|
|
|
self.members_url,
|
|
|
|
{"users_old": self.comptable.id},
|
|
|
|
)
|
|
|
|
self.club.refresh_from_db()
|
|
|
|
new_membership = self.club.get_membership_for(self.comptable)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert new_membership is not None
|
|
|
|
assert new_membership == membership
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
membership = self.comptable.memberships.filter(club=self.club).first()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert membership.end_date is None
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
def test_end_membership_as_main_club_board(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that board members of the main club can end the membership
|
|
|
|
of anyone.
|
2023-05-02 10:36:59 +00:00
|
|
|
"""
|
|
|
|
# make subscriber a board member
|
2024-09-04 14:21:42 +00:00
|
|
|
subscriber = subscriber_user.make()
|
|
|
|
Membership.objects.create(club=self.ae, user=subscriber, role=3)
|
2023-05-02 10:36:59 +00:00
|
|
|
|
2024-09-04 14:21:42 +00:00
|
|
|
nb_memberships = self.club.members.ongoing().count()
|
|
|
|
self.client.force_login(subscriber)
|
2019-04-25 13:40:49 +00:00
|
|
|
response = self.client.post(
|
2023-05-02 10:36:59 +00:00
|
|
|
self.members_url,
|
|
|
|
{"users_old": self.comptable.id},
|
|
|
|
)
|
|
|
|
self.assertRedirects(response, self.members_url)
|
2024-06-26 17:10:24 +00:00
|
|
|
self.assert_membership_ended_today(self.comptable)
|
|
|
|
assert self.club.members.ongoing().count() == nb_memberships - 1
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
def test_end_membership_as_root(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that root users can end the membership of anyone."""
|
2024-09-04 14:21:42 +00:00
|
|
|
nb_memberships = self.club.members.ongoing().count()
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.root)
|
2019-04-25 13:40:49 +00:00
|
|
|
response = self.client.post(
|
2023-05-02 10:36:59 +00:00
|
|
|
self.members_url,
|
|
|
|
{"users_old": [self.comptable.id]},
|
|
|
|
)
|
|
|
|
self.assertRedirects(response, self.members_url)
|
2024-06-26 17:10:24 +00:00
|
|
|
self.assert_membership_ended_today(self.comptable)
|
|
|
|
assert self.club.members.ongoing().count() == nb_memberships - 1
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
def test_end_membership_as_foreigner(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that users who are not in this club cannot end its memberships."""
|
2023-05-02 10:36:59 +00:00
|
|
|
nb_memberships = self.club.members.count()
|
|
|
|
membership = self.richard.memberships.filter(club=self.club).first()
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.subscriber)
|
2019-04-25 13:40:49 +00:00
|
|
|
self.client.post(
|
2023-05-02 10:36:59 +00:00
|
|
|
self.members_url,
|
|
|
|
{"users_old": [self.richard.id]},
|
|
|
|
)
|
|
|
|
# nothing should have changed
|
|
|
|
new_mem = self.club.get_membership_for(self.richard)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.club.members.count() == nb_memberships
|
|
|
|
assert membership == new_mem
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
def test_delete_remove_from_meta_group(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that when a club is deleted, all its members are removed from the
|
|
|
|
associated metagroup.
|
2023-05-02 10:36:59 +00:00
|
|
|
"""
|
|
|
|
memberships = self.club.members.select_related("user")
|
|
|
|
users = [membership.user for membership in memberships]
|
|
|
|
meta_group = self.club.unix_name + settings.SITH_MEMBER_SUFFIX
|
|
|
|
|
|
|
|
self.club.delete()
|
|
|
|
for user in users:
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not user.is_in_group(name=meta_group)
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
def test_add_to_meta_group(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that when a membership begins, the user is added to the meta group."""
|
2023-05-02 10:36:59 +00:00
|
|
|
group_members = self.club.unix_name + settings.SITH_MEMBER_SUFFIX
|
|
|
|
board_members = self.club.unix_name + settings.SITH_BOARD_SUFFIX
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not self.subscriber.is_in_group(name=group_members)
|
|
|
|
assert not self.subscriber.is_in_group(name=board_members)
|
2023-05-02 10:36:59 +00:00
|
|
|
Membership.objects.create(club=self.club, user=self.subscriber, role=3)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.subscriber.is_in_group(name=group_members)
|
|
|
|
assert self.subscriber.is_in_group(name=board_members)
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
def test_remove_from_meta_group(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that when a membership ends, the user is removed from meta group."""
|
2023-05-02 10:36:59 +00:00
|
|
|
group_members = self.club.unix_name + settings.SITH_MEMBER_SUFFIX
|
|
|
|
board_members = self.club.unix_name + settings.SITH_BOARD_SUFFIX
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.comptable.is_in_group(name=group_members)
|
|
|
|
assert self.comptable.is_in_group(name=board_members)
|
2023-05-02 10:36:59 +00:00
|
|
|
self.comptable.memberships.update(end_date=localtime(now()))
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not self.comptable.is_in_group(name=group_members)
|
|
|
|
assert not self.comptable.is_in_group(name=board_members)
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
def test_club_owner(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that a club is owned only by board members of the main club."""
|
2023-05-02 10:36:59 +00:00
|
|
|
anonymous = AnonymousUser()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert not self.club.is_owned_by(anonymous)
|
|
|
|
assert not self.club.is_owned_by(self.subscriber)
|
2023-05-02 10:36:59 +00:00
|
|
|
|
|
|
|
# make sli a board member
|
|
|
|
self.sli.memberships.all().delete()
|
|
|
|
Membership(club=self.ae, user=self.sli, role=3).save()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert self.club.is_owned_by(self.sli)
|
2019-05-09 16:06:11 +00:00
|
|
|
|
|
|
|
|
2024-07-23 22:39:26 +00:00
|
|
|
class TestMailingForm(TestCase):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Perform validation tests for MailingForm."""
|
2019-05-09 16:06:11 +00:00
|
|
|
|
2023-05-02 10:36:59 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2024-06-26 17:10:24 +00:00
|
|
|
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.bdf = Club.objects.get(unix_name=SITH_BAR_MANAGER["unix_name"])
|
|
|
|
cls.mail_url = reverse("club:mailing", kwargs={"club_id": cls.bdf.id})
|
2023-05-02 10:36:59 +00:00
|
|
|
|
2019-05-09 16:06:11 +00:00
|
|
|
def setUp(self):
|
|
|
|
Membership(
|
|
|
|
user=self.rbatsbak,
|
|
|
|
club=self.bdf,
|
|
|
|
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
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.comunity)
|
|
|
|
response = self.client.post(
|
|
|
|
self.mail_url,
|
2019-05-09 16:06:11 +00:00
|
|
|
{"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "foyer"},
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
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()
|
2019-05-09 16:06:11 +00:00
|
|
|
|
|
|
|
# Test with Root
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.root)
|
2019-05-09 16:06:11 +00:00
|
|
|
self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.mail_url,
|
2019-05-09 16:06:11 +00:00
|
|
|
{"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "mde"},
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
response = self.client.get(self.mail_url)
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert "Liste de diffusion mde@utbm.fr" in response.content.decode()
|
2019-05-09 16:06:11 +00:00
|
|
|
|
|
|
|
def test_mailing_list_add_moderation(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.rbatsbak)
|
2019-05-09 16:06:11 +00:00
|
|
|
self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.mail_url,
|
2019-05-09 16:06:11 +00:00
|
|
|
{"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "mde"},
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
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
|
2019-05-09 16:06:11 +00:00
|
|
|
|
|
|
|
def test_mailing_list_forbidden(self):
|
|
|
|
# With anonymous user
|
2024-06-26 17:10:24 +00:00
|
|
|
response = self.client.get(self.mail_url)
|
2019-05-09 16:06:11 +00:00
|
|
|
self.assertContains(response, "", status_code=403)
|
|
|
|
|
|
|
|
# With user not in club
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.krophil)
|
|
|
|
response = self.client.get(self.mail_url)
|
|
|
|
assert response.status_code == 403
|
2019-05-09 18:32:48 +00:00
|
|
|
|
|
|
|
def test_add_new_subscription_fail_not_moderated(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.rbatsbak)
|
2019-05-09 18:32:48 +00:00
|
|
|
self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.mail_url,
|
2019-05-09 18:32:48 +00:00
|
|
|
{"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "mde"},
|
|
|
|
)
|
|
|
|
|
|
|
|
self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.mail_url,
|
2019-05-09 18:32:48 +00:00
|
|
|
{
|
|
|
|
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
|
|
|
"subscription_users": self.skia.id,
|
|
|
|
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
|
|
|
},
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
response = self.client.get(self.mail_url)
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert "skia@git.an" not in response.content.decode()
|
2019-05-09 18:32:48 +00:00
|
|
|
|
|
|
|
def test_add_new_subscription_success(self):
|
|
|
|
# Prepare mailing list
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.comunity)
|
2019-05-09 18:32:48 +00:00
|
|
|
self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.mail_url,
|
2019-05-09 18:32:48 +00:00
|
|
|
{"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "mde"},
|
|
|
|
)
|
|
|
|
|
|
|
|
# Add single user
|
|
|
|
self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.mail_url,
|
2019-05-09 18:32:48 +00:00
|
|
|
{
|
|
|
|
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
|
|
|
"subscription_users": self.skia.id,
|
|
|
|
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
|
|
|
},
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
response = self.client.get(self.mail_url)
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert "skia@git.an" in response.content.decode()
|
2019-05-09 18:32:48 +00:00
|
|
|
|
|
|
|
# Add multiple users
|
|
|
|
self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.mail_url,
|
2019-05-09 18:32:48 +00:00
|
|
|
{
|
|
|
|
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
|
|
|
"subscription_users": "|%s|%s|" % (self.comunity.id, self.rbatsbak.id),
|
|
|
|
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
|
|
|
},
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
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
|
2019-05-09 18:32:48 +00:00
|
|
|
|
|
|
|
# Add arbitrary email
|
|
|
|
self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.mail_url,
|
2019-05-09 18:32:48 +00:00
|
|
|
{
|
|
|
|
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
|
|
|
"subscription_email": "arbitrary@git.an",
|
|
|
|
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
|
|
|
},
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
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
|
2019-05-09 18:32:48 +00:00
|
|
|
|
|
|
|
# Add user and arbitrary email
|
|
|
|
self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.mail_url,
|
2019-05-09 18:32:48 +00:00
|
|
|
{
|
|
|
|
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
|
|
|
"subscription_email": "more.arbitrary@git.an",
|
|
|
|
"subscription_users": self.krophil.id,
|
|
|
|
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
|
|
|
},
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
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
|
2019-05-09 18:32:48 +00:00
|
|
|
|
|
|
|
def test_add_new_subscription_fail_form_errors(self):
|
|
|
|
# Prepare mailing list
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.comunity)
|
2019-05-09 18:32:48 +00:00
|
|
|
self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.mail_url,
|
2019-05-09 18:32:48 +00:00
|
|
|
{"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "mde"},
|
|
|
|
)
|
|
|
|
|
|
|
|
# Neither email or email is specified
|
|
|
|
response = self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.mail_url,
|
2019-05-09 18:32:48 +00:00
|
|
|
{
|
|
|
|
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
|
|
|
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
|
|
|
},
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code
|
|
|
|
self.assertInHTML(
|
|
|
|
_("You must specify at least an user or an email address"),
|
|
|
|
response.content.decode(),
|
2019-05-09 18:32:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# No mailing specified
|
|
|
|
response = self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.mail_url,
|
2019-05-09 18:32:48 +00:00
|
|
|
{
|
|
|
|
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
|
|
|
"subscription_users": self.krophil.id,
|
|
|
|
},
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 200
|
|
|
|
assert _("This field is required") in response.content.decode()
|
2019-05-09 18:32:48 +00:00
|
|
|
|
|
|
|
# One of the selected users doesn't exist
|
|
|
|
response = self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.mail_url,
|
2019-05-09 18:32:48 +00:00
|
|
|
{
|
|
|
|
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
|
|
|
"subscription_users": "|789|",
|
|
|
|
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
|
|
|
},
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 200
|
|
|
|
self.assertInHTML(
|
|
|
|
_("One of the selected users doesn't exist"), response.content.decode()
|
2019-05-09 18:32:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# An user has no email adress
|
|
|
|
self.krophil.email = ""
|
|
|
|
self.krophil.save()
|
|
|
|
|
|
|
|
response = self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.mail_url,
|
2019-05-09 18:32:48 +00:00
|
|
|
{
|
|
|
|
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
|
|
|
"subscription_users": self.krophil.id,
|
|
|
|
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
|
|
|
},
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 200
|
|
|
|
self.assertInHTML(
|
|
|
|
_("One of the selected users doesn't have an email address"),
|
|
|
|
response.content.decode(),
|
2019-05-09 18:32:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
self.krophil.email = "krophil@git.an"
|
|
|
|
self.krophil.save()
|
|
|
|
|
|
|
|
# An user is added twice
|
|
|
|
|
|
|
|
self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.mail_url,
|
2019-05-09 18:32:48 +00:00
|
|
|
{
|
|
|
|
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
|
|
|
"subscription_users": self.krophil.id,
|
|
|
|
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
response = self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.mail_url,
|
2019-05-09 18:32:48 +00:00
|
|
|
{
|
|
|
|
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
|
|
|
"subscription_users": self.krophil.id,
|
|
|
|
"subscription_mailing": Mailing.objects.get(email="mde").id,
|
|
|
|
},
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 200
|
|
|
|
self.assertInHTML(
|
|
|
|
_("This email is already suscribed in this mailing"),
|
|
|
|
response.content.decode(),
|
2019-05-09 18:32:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_remove_subscription_success(self):
|
|
|
|
# Prepare mailing list
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.comunity)
|
2019-05-09 18:32:48 +00:00
|
|
|
self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.mail_url,
|
2019-05-09 18:32:48 +00:00
|
|
|
{"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "mde"},
|
|
|
|
)
|
|
|
|
mde = Mailing.objects.get(email="mde")
|
2019-10-16 19:21:06 +00:00
|
|
|
self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.mail_url,
|
2019-05-09 18:32:48 +00:00
|
|
|
{
|
|
|
|
"action": MailingForm.ACTION_NEW_SUBSCRIPTION,
|
|
|
|
"subscription_users": "|%s|%s|%s|"
|
|
|
|
% (self.comunity.id, self.rbatsbak.id, self.krophil.id),
|
|
|
|
"subscription_mailing": mde.id,
|
|
|
|
},
|
|
|
|
)
|
2019-10-16 19:21:06 +00:00
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
response = self.client.get(self.mail_url)
|
|
|
|
assert response.status_code == 200
|
|
|
|
content = response.content.decode()
|
2019-10-16 19:21:06 +00:00
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
assert "comunity@git.an" in content
|
|
|
|
assert "richard@git.an" in content
|
|
|
|
assert "krophil@git.an" in content
|
2019-05-09 18:32:48 +00:00
|
|
|
|
|
|
|
# Delete one user
|
|
|
|
self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.mail_url,
|
2019-05-09 18:32:48 +00:00
|
|
|
{
|
|
|
|
"action": MailingForm.ACTION_REMOVE_SUBSCRIPTION,
|
|
|
|
"removal_%d" % mde.id: mde.subscriptions.get(user=self.krophil).id,
|
|
|
|
},
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
response = self.client.get(self.mail_url)
|
|
|
|
assert response.status_code == 200
|
|
|
|
content = response.content.decode()
|
2019-05-09 18:32:48 +00:00
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
assert "comunity@git.an" in content
|
|
|
|
assert "richard@git.an" in content
|
|
|
|
assert "krophil@git.an" not in content
|
2019-05-09 18:32:48 +00:00
|
|
|
|
|
|
|
# Delete multiple users
|
|
|
|
self.client.post(
|
2024-06-26 17:10:24 +00:00
|
|
|
self.mail_url,
|
2019-05-09 18:32:48 +00:00
|
|
|
{
|
|
|
|
"action": MailingForm.ACTION_REMOVE_SUBSCRIPTION,
|
2024-06-24 09:56:38 +00:00
|
|
|
"removal_%d" % mde.id: [
|
2019-05-09 18:32:48 +00:00
|
|
|
user.id
|
|
|
|
for user in mde.subscriptions.filter(
|
|
|
|
user__in=[self.rbatsbak, self.comunity]
|
|
|
|
).all()
|
|
|
|
],
|
|
|
|
},
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
response = self.client.get(self.mail_url)
|
|
|
|
assert response.status_code == 200
|
|
|
|
content = response.content.decode()
|
2019-05-09 18:32:48 +00:00
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
assert "comunity@git.an" not in content
|
|
|
|
assert "richard@git.an" not in content
|
|
|
|
assert "krophil@git.an" not in content
|
2019-05-21 08:26:29 +00:00
|
|
|
|
|
|
|
|
2024-07-23 22:39:26 +00:00
|
|
|
class TestClubSellingView(TestCase):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Perform basics tests to ensure that the page is available."""
|
2019-05-21 08:26:29 +00:00
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.ae = Club.objects.get(unix_name="ae")
|
|
|
|
cls.skia = User.objects.get(username="skia")
|
2019-05-21 08:26:29 +00:00
|
|
|
|
|
|
|
def test_page_not_internal_error(self):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""Test that the page does not return and internal error."""
|
2024-06-26 17:10:24 +00:00
|
|
|
self.client.force_login(self.skia)
|
2019-05-21 08:26:29 +00:00
|
|
|
response = self.client.get(
|
|
|
|
reverse("club:club_sellings", kwargs={"club_id": self.ae.id})
|
|
|
|
)
|
2024-06-26 17:10:24 +00:00
|
|
|
assert response.status_code == 200
|