Skip to content

Commit

Permalink
Return 404 if variant page is accessed directly (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
tm-kn authored and jberghoef committed Sep 25, 2018
1 parent 7d679d7 commit d15f6c3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/wagtail_personalisation/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from django.conf.urls import include, url
from django.db import transaction
from django.http import Http404
from django.shortcuts import redirect, render
from django.template.defaultfilters import pluralize
from django.urls import reverse
Expand Down Expand Up @@ -104,9 +105,13 @@ def serve_variant(page, request, serve_args, serve_kwargs):
adapter = get_segment_adapter(request)
user_segments = adapter.get_segments()

if user_segments:
metadata = page.personalisation_metadata
metadata = page.personalisation_metadata

# If page is not canonical, don't serve it.
if not metadata.is_canonical:
raise Http404

if user_segments:
# TODO: This is never more then one page? (fix query count)
metadata = metadata.metadata_for_segments(user_segments)
if metadata:
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_wagtail_hooks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import pytest

from django.http import Http404

from wagtail.core.models import Page

from tests.factories.segment import SegmentFactory
Expand All @@ -16,6 +19,15 @@ def test_serve_variant_no_variant(site, rf):
assert result is None


@pytest.mark.django_db
def test_variant_accessed_directly_returns_404(segmented_page, rf):
request = rf.get('/')
args = tuple()
kwargs = {}
with pytest.raises(Http404):
wagtail_hooks.serve_variant(segmented_page, request, args, kwargs)


@pytest.mark.django_db
def test_serve_variant_with_variant_no_segment(site, rf, segmented_page):
request = rf.get('/')
Expand Down

0 comments on commit d15f6c3

Please sign in to comment.