mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-09 19:40:19 +00:00
Add a page to manage the groups that can create permissions
This commit is contained in:
43
subscription/tests/test_permissions.py
Normal file
43
subscription/tests/test_permissions.py
Normal file
@ -0,0 +1,43 @@
|
||||
from django.contrib.auth.models import Permission
|
||||
from django.test import TestCase
|
||||
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
|
||||
from core.models import User
|
||||
|
||||
|
||||
class TestSubscriptionPermission(TestCase):
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
cls.user: User = subscriber_user.make()
|
||||
cls.admin = baker.make(User, is_superuser=True)
|
||||
cls.club = baker.make(Club)
|
||||
baker.make(Membership, user=cls.user, club=cls.club, role=7)
|
||||
|
||||
def test_give_permission(self):
|
||||
self.client.force_login(self.admin)
|
||||
response = self.client.post(
|
||||
reverse("subscription:perms"), {"groups": [self.club.board_group_id]}
|
||||
)
|
||||
assertRedirects(response, reverse("subscription:perms"))
|
||||
assert self.user.has_perm("subscription.add_subscription")
|
||||
|
||||
def test_remove_permission(self):
|
||||
self.client.force_login(self.admin)
|
||||
response = self.client.post(reverse("subscription:perms"), {"groups": []})
|
||||
assertRedirects(response, reverse("subscription:perms"))
|
||||
assert not self.user.has_perm("subscription.add_subscription")
|
||||
|
||||
def test_subscription_page_access(self):
|
||||
self.client.force_login(self.user)
|
||||
response = self.client.get(reverse("subscription:subscription"))
|
||||
assert response.status_code == 403
|
||||
|
||||
self.club.board_group.permissions.add(
|
||||
Permission.objects.get(codename="add_subscription")
|
||||
)
|
||||
response = self.client.get(reverse("subscription:subscription"))
|
||||
assert response.status_code == 200
|
@ -20,6 +20,7 @@ from subscription.views import (
|
||||
CreateSubscriptionNewUserFragment,
|
||||
NewSubscription,
|
||||
SubscriptionCreatedFragment,
|
||||
SubscriptionPermissionView,
|
||||
SubscriptionsStatsView,
|
||||
)
|
||||
|
||||
@ -41,5 +42,10 @@ urlpatterns = [
|
||||
SubscriptionCreatedFragment.as_view(),
|
||||
name="creation-success",
|
||||
),
|
||||
path(
|
||||
"perms/",
|
||||
SubscriptionPermissionView.as_view(),
|
||||
name="perms",
|
||||
),
|
||||
path("stats/", SubscriptionsStatsView.as_view(), name="stats"),
|
||||
]
|
||||
|
@ -21,6 +21,7 @@ from django.utils.timezone import localdate
|
||||
from django.views.generic import CreateView, DetailView, TemplateView
|
||||
from django.views.generic.edit import FormView
|
||||
|
||||
from core.views.group import PermissionGroupsUpdateView
|
||||
from counter.apps import PAYMENT_METHOD
|
||||
from subscription.forms import (
|
||||
SelectionDateForm,
|
||||
@ -77,6 +78,12 @@ class SubscriptionCreatedFragment(PermissionRequiredMixin, DetailView):
|
||||
context_object_name = "subscription"
|
||||
|
||||
|
||||
class SubscriptionPermissionView(PermissionGroupsUpdateView):
|
||||
"""Manage the groups that have access to the subscription creation page."""
|
||||
|
||||
permission = "subscription.add_subscription"
|
||||
|
||||
|
||||
class SubscriptionsStatsView(FormView):
|
||||
template_name = "subscription/stats.jinja"
|
||||
form_class = SelectionDateForm
|
||||
|
Reference in New Issue
Block a user