Skip to content

Commit b6aec9f

Browse files
Merge pull request #152 from django-commons/feature/114-support-swapping-out-the-landing-page
Support swapping out the landing page
2 parents a7539d1 + 763b1b8 commit b6aec9f

File tree

4 files changed

+44
-17
lines changed

4 files changed

+44
-17
lines changed

cookie_consent/conf.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from django.conf import settings # NOQA
22

33
from appconf import AppConf
4+
from django.urls import reverse_lazy
45

56
__all__ = ["settings"]
67

@@ -24,3 +25,5 @@ class CookieConsentConf(AppConf):
2425
CACHE_BACKEND = "default"
2526

2627
LOG_ENABLED = True
28+
29+
SUCCESS_URL = reverse_lazy("cookie_consent_cookie_group_list")

cookie_consent/views.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django.utils.http import url_has_allowed_host_and_scheme
77
from django.views.generic import ListView, View
88

9+
from .conf import settings
910
from .models import CookieGroup
1011
from .util import (
1112
accept_cookies,
@@ -48,7 +49,7 @@ def get_success_url(self):
4849
require_https=self.request.is_secure(),
4950
):
5051
raise SuspiciousOperation("Unsafe open redirect suspected.")
51-
return redirect_to or reverse("cookie_consent_cookie_group_list")
52+
return redirect_to or settings.COOKIE_CONSENT_SUCCESS_URL
5253

5354
def process(self, request, response, varname): # pragma: no cover
5455
raise NotImplementedError()

docs/settings.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,8 @@ for more details about the meaning.
6666
Boolean value represents if user actions when they accepting and declining cookies will be logged. Turning it off might be useful for preventing your database from getting filled up with log items.
6767

6868
Default: ``True``
69+
70+
``COOKIE_CONSENT_SUCCESS_URL``
71+
The success URL to redirect the user too after a successful accept/decline action. If
72+
a ``?next`` parameter is present in the request, then it takes priority over this
73+
setting. Defaults to the URL of the built-in cookie list view.

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)