Make bds tests resilient to LOGIN_URL changes

This commit is contained in:
Martin Pépin 2020-07-10 23:00:17 +02:00 committed by Ludovic Stephan
parent 5c1e2e9cda
commit ac06211841

View file

@ -1,9 +1,10 @@
from unittest import mock
from django.conf import settings
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
from django.urls import reverse, reverse_lazy
User = get_user_model()
@ -13,6 +14,14 @@ def give_bds_buro_permissions(user: User) -> None:
user.user_permissions.add(perm)
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)
class TestRegistrationView(TestCase):
@mock.patch("gestioncof.signals.messages")
def test_get_autocomplete(self, mock_messages):
@ -22,8 +31,7 @@ class TestRegistrationView(TestCase):
# Anonymous GET
resp = client.get(url)
redirect_url = "/login?next={}".format(url)
self.assertRedirects(resp, redirect_url)
self.assertRedirects(resp, login_url(next=url))
# Logged-in but unprivileged GET
client.force_login(user)
@ -44,8 +52,7 @@ class TestRegistrationView(TestCase):
# Anonymous GET
resp = client.get(url)
redirect_url = "/login?next={}".format(url)
self.assertRedirects(resp, redirect_url)
self.assertRedirects(resp, login_url(next=url))
# Logged-in but unprivileged GET
client.force_login(user)