From ac062118410e5cb37ba8ccdfa4499d5d83656e23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Fri, 10 Jul 2020 23:00:17 +0200 Subject: [PATCH] Make bds tests resilient to LOGIN_URL changes --- bds/tests/test_views.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/bds/tests/test_views.py b/bds/tests/test_views.py index 0ad42018..8e30adf4 100644 --- a/bds/tests/test_views.py +++ b/bds/tests/test_views.py @@ -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)