Skip to content

Commit 46cc3bc

Browse files
✅ [#114] Add test for desired redirect-override behaviour
Using a django setting to control the redirect target is the least invasive.
1 parent a7539d1 commit 46cc3bc

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

tests/test_views.py

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
from django.test import TestCase
1+
from django.test import Client, TestCase
22
from django.test.utils import override_settings
33
from django.urls import reverse
44

5+
import pytest
6+
from pytest_django.asserts import assertRedirects
7+
58
from cookie_consent.models import (
69
ACTION_ACCEPTED,
710
ACTION_DECLINED,
@@ -11,21 +14,36 @@
1114
)
1215

1316

14-
class CookieGroupBaseProcessViewTests(TestCase):
15-
def test_get_success_url(self):
16-
"""
17-
If user adds a 'next' as URL parameter it should,
18-
redirect to the value of 'next'
19-
"""
20-
expected_url = reverse("test_page")
21-
url = "{}?next={}".format(reverse("cookie_consent_accept_all"), expected_url)
22-
response = self.client.post(url, follow=True)
23-
self.assertRedirects(response, expected_url)
24-
25-
def test_no_open_redirects(self):
26-
url = "{}?next=https://evil.com".format(reverse("cookie_consent_accept_all"))
27-
response = self.client.post(url, follow=True)
28-
self.assertEqual(response.status_code, 400) # result of SupiciousOperation
17+
@pytest.mark.django_db
18+
def test_processing_get_success_url(client: Client):
19+
"""
20+
If user adds a 'next' as URL parameter it should,
21+
redirect to the value of 'next'
22+
"""
23+
expected_url = reverse("test_page")
24+
url = "{}?next={}".format(reverse("cookie_consent_accept_all"), expected_url)
25+
26+
response = client.post(url, follow=True)
27+
28+
assertRedirects(response, expected_url)
29+
30+
31+
@pytest.mark.django_db
32+
def test_processing_no_open_redirects(client: Client):
33+
url = "{}?next=https://evil.com".format(reverse("cookie_consent_accept_all"))
34+
35+
response = client.post(url, follow=True)
36+
37+
assert response.status_code == 400 # result of SupiciousOperation
38+
39+
40+
@pytest.mark.django_db
41+
def test_alternative_redirect_fallback(client: Client, settings):
42+
settings.COOKIE_CONSENT_SUCCESS_URL = "/alternative"
43+
44+
response = client.post(reverse("cookie_consent_accept_all"), follow=False)
45+
46+
assertRedirects(response, "/alternative", fetch_redirect_response=False)
2947

3048

3149
class IntegrationTest(TestCase):

0 commit comments

Comments
 (0)