2020-07-10 22:33:24 +02:00
|
|
|
from unittest import mock
|
|
|
|
|
2020-07-10 23:00:17 +02:00
|
|
|
from django.conf import settings
|
2020-07-10 22:33:24 +02:00
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from django.contrib.auth.models import Permission
|
|
|
|
from django.test import Client, TestCase
|
2020-07-10 23:00:17 +02:00
|
|
|
from django.urls import reverse, reverse_lazy
|
2020-07-10 22:33:24 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2020-07-10 23:00:17 +02:00
|
|
|
def login_url(next=None):
|
|
|
|
login_url = reverse_lazy(settings.LOGIN_URL)
|
|
|
|
if next is None:
|
|
|
|
return login_url
|
|
|
|
else:
|
|
|
|
return "{}?next={}".format(login_url, next)
|
|
|
|
|
|
|
|
|
2021-02-07 16:30:02 +01:00
|
|
|
class TestHomeView(TestCase):
|
|
|
|
@mock.patch("gestioncof.signals.messages")
|
|
|
|
def test_get(self, mock_messages):
|
|
|
|
user = User.objects.create_user(username="random_user")
|
|
|
|
give_bds_buro_permissions(user)
|
2021-06-15 16:52:56 +02:00
|
|
|
self.client.force_login(
|
|
|
|
user, backend="django.contrib.auth.backends.ModelBackend"
|
|
|
|
)
|
2021-02-07 16:30:02 +01:00
|
|
|
resp = self.client.get(reverse("bds:home"))
|
2022-06-30 11:19:04 +02:00
|
|
|
self.assertEqual(resp.status_code, 200)
|
2021-02-07 16:30:02 +01:00
|
|
|
|
|
|
|
|
2020-07-10 22:33:24 +02:00
|
|
|
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)
|
2020-07-10 23:00:17 +02:00
|
|
|
self.assertRedirects(resp, login_url(next=url))
|
2020-07-10 22:33:24 +02:00
|
|
|
|
|
|
|
# Logged-in but unprivileged GET
|
2021-06-15 16:52:56 +02:00
|
|
|
client.force_login(user, backend="django.contrib.auth.backends.ModelBackend")
|
2020-07-10 22:33:24 +02:00
|
|
|
resp = client.get(url)
|
2022-06-30 11:19:04 +02:00
|
|
|
self.assertEqual(resp.status_code, 403)
|
2020-07-10 22:33:24 +02:00
|
|
|
|
|
|
|
# Burô user GET
|
|
|
|
give_bds_buro_permissions(user)
|
|
|
|
resp = client.get(url)
|
2022-06-30 11:19:04 +02:00
|
|
|
self.assertEqual(resp.status_code, 200)
|
2020-07-10 22:33:24 +02:00
|
|
|
|
|
|
|
@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,))
|
|
|
|
client = Client()
|
|
|
|
|
|
|
|
# Anonymous GET
|
|
|
|
resp = client.get(url)
|
2020-07-10 23:00:17 +02:00
|
|
|
self.assertRedirects(resp, login_url(next=url))
|
2020-07-10 22:33:24 +02:00
|
|
|
|
|
|
|
# Logged-in but unprivileged GET
|
2021-06-15 16:52:56 +02:00
|
|
|
client.force_login(user, backend="django.contrib.auth.backends.ModelBackend")
|
2020-07-10 22:33:24 +02:00
|
|
|
resp = client.get(url)
|
2022-06-30 11:19:04 +02:00
|
|
|
self.assertEqual(resp.status_code, 403)
|
2020-07-10 22:33:24 +02:00
|
|
|
|
|
|
|
# Burô user GET
|
|
|
|
give_bds_buro_permissions(user)
|
|
|
|
resp = client.get(url)
|
2022-06-30 11:19:04 +02:00
|
|
|
self.assertEqual(resp.status_code, 200)
|