Basic tests for BDS registration views

This commit is contained in:
Martin Pépin 2020-07-10 22:33:24 +02:00 committed by Ludovic Stephan
parent c1e48579f1
commit 5c1e2e9cda
3 changed files with 59 additions and 1 deletions

View file

@ -2,4 +2,4 @@ from django.contrib.auth.mixins import PermissionRequiredMixin
class StaffRequiredMixin(PermissionRequiredMixin):
permission_required = "bds:is_team"
permission_required = "bds.is_team"

0
bds/tests/__init__.py Normal file
View file

58
bds/tests/test_views.py Normal file
View file

@ -0,0 +1,58 @@
from unittest import mock
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission
from django.test import Client, TestCase
from django.urls import reverse
User = get_user_model()
def give_bds_buro_permissions(user: User) -> None:
perm = Permission.objects.get(content_type__app_label="bds", codename="is_team")
user.user_permissions.add(perm)
class TestRegistrationView(TestCase):
@mock.patch("gestioncof.signals.messages")
def test_get_autocomplete(self, mock_messages):
user = User.objects.create_user(username="toto")
url = reverse("bds:autocomplete") + "?q=foo"
client = Client()
# Anonymous GET
resp = client.get(url)
redirect_url = "/login?next={}".format(url)
self.assertRedirects(resp, redirect_url)
# Logged-in but unprivileged GET
client.force_login(user)
resp = client.get(url)
self.assertEquals(resp.status_code, 403)
# Burô user GET
give_bds_buro_permissions(user)
resp = client.get(url)
self.assertEquals(resp.status_code, 200)
@mock.patch("gestioncof.signals.messages")
def test_get(self, mock_messages):
user = User.objects.create_user(username="toto")
url = reverse("bds:user.update", args=(user.id,))
print(url)
client = Client()
# Anonymous GET
resp = client.get(url)
redirect_url = "/login?next={}".format(url)
self.assertRedirects(resp, redirect_url)
# Logged-in but unprivileged GET
client.force_login(user)
resp = client.get(url)
self.assertEquals(resp.status_code, 403)
# Burô user GET
give_bds_buro_permissions(user)
resp = client.get(url)
self.assertEquals(resp.status_code, 200)