-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
get_client_root_url
overridable both in both mixins (#59)
- Loading branch information
Showing
4 changed files
with
86 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from django.contrib.auth.models import User | ||
from django.test import TestCase, override_settings | ||
from django.test import RequestFactory, TestCase, override_settings | ||
from django.urls import reverse | ||
from django.utils.http import urlencode | ||
from wagtail.models import Page | ||
|
@@ -9,7 +9,7 @@ | |
from wagtail_headless_preview.settings import headless_preview_settings | ||
|
||
|
||
class TestFrontendViews(TestCase): | ||
class TestMixins(TestCase): | ||
fixtures = ["test.json"] | ||
|
||
@classmethod | ||
|
@@ -30,6 +30,13 @@ def setUpTestData(cls): | |
cls.page.title = "Simple page with draft edit" | ||
cls.page.save_revision() | ||
|
||
cls.headless_page = HeadlessPage( | ||
title="Headless page original", slug="headless-page" | ||
) | ||
cls.homepage.add_child(instance=cls.headless_page) | ||
|
||
cls.request = RequestFactory().get("/") | ||
|
||
def setUp(self): | ||
self.client.login(username=self.admin_user.username, password="password") | ||
|
||
|
@@ -77,7 +84,7 @@ def test_redirect_on_preview(self): | |
|
||
self.assertRedirects( | ||
response, | ||
self.page.get_preview_url(preview_token), | ||
self.page.get_preview_url(self.request, preview_token), | ||
fetch_redirect_response=False, | ||
) | ||
|
||
|
@@ -86,7 +93,7 @@ def test_redirect_on_preview(self): | |
) | ||
def test_get_client_root_url_with_default_trailing_slash_enforcement(self): | ||
self.assertEqual( | ||
self.page.get_client_root_url(), | ||
self.page.get_client_root_url(self.request), | ||
"https://headless.site/", | ||
) | ||
|
||
|
@@ -98,10 +105,20 @@ def test_get_client_root_url_with_default_trailing_slash_enforcement(self): | |
) | ||
def test_get_client_root_url_without_trailing_slash_enforcement(self): | ||
self.assertEqual( | ||
self.page.get_client_root_url(), | ||
self.page.get_client_root_url(self.request), | ||
"https://headless.site", | ||
) | ||
|
||
@override_settings( | ||
WAGTAIL_HEADLESS_PREVIEW={"SERVE_BASE_URL": "https://headless.site"}, | ||
TEST_OVERRIDE_CLIENT_ROOT_URL=True, | ||
) | ||
def test_get_client_root_url_override_in_implementing_page(self): | ||
self.assertEqual( | ||
self.headless_page.get_client_root_url(self.request), | ||
"https://wagtail.org", | ||
) | ||
|
||
def test_create_page_preview_race(self): | ||
self.page.create_page_preview() | ||
|
||
|
@@ -111,36 +128,22 @@ def test_create_page_preview_race(self): | |
# This shouldn't hit a unique constraint error | ||
self.page.create_page_preview() | ||
|
||
|
||
class TestHeadlessRedirectMixin(TestCase): | ||
fixtures = ["test.json"] | ||
|
||
@classmethod | ||
def setUpTestData(cls): | ||
cls.admin_user = User.objects.create_superuser( | ||
username="admin", | ||
email="[email protected]", | ||
password="password", | ||
) | ||
|
||
cls.homepage = Page.objects.get(url_path="/home/").specific | ||
cls.page = HeadlessPage(title="Simple page original", slug="simple-page") | ||
cls.homepage.add_child(instance=cls.page) | ||
|
||
def test_serve(self): | ||
def test_headless_serve_mixin_redirects_in_serve(self): | ||
client_url = headless_preview_settings.CLIENT_URLS["default"].rstrip("/") | ||
response = self.client.get(self.page.url) | ||
response = self.client.get(self.headless_page.url) | ||
self.assertRedirects( | ||
response, f"{client_url}/{self.page.slug}/", fetch_redirect_response=False | ||
response, | ||
f"{client_url}/{self.headless_page.slug}/", | ||
fetch_redirect_response=False, | ||
) | ||
|
||
@override_settings( | ||
WAGTAIL_HEADLESS_PREVIEW={"SERVE_BASE_URL": "https://headless.site"} | ||
) | ||
def test_serve_with_headless_serve_base_url(self): | ||
response = self.client.get(self.page.url) | ||
def test_headless_serve_mixin_serve_with_headless_serve_base_url(self): | ||
response = self.client.get(self.headless_page.url) | ||
self.assertRedirects( | ||
response, | ||
f"https://headless.site/{self.page.slug}/", | ||
f"https://headless.site/{self.headless_page.slug}/", | ||
fetch_redirect_response=False, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,22 @@ | ||
from typing import TYPE_CHECKING | ||
|
||
from django.conf import settings | ||
from wagtail.models import Page | ||
|
||
from wagtail_headless_preview.models import HeadlessMixin, HeadlessPreviewMixin | ||
|
||
|
||
if TYPE_CHECKING: | ||
from django.http import HttpRequest | ||
|
||
|
||
class SimplePage(HeadlessPreviewMixin, Page): | ||
pass | ||
|
||
|
||
class HeadlessPage(HeadlessMixin, Page): | ||
pass | ||
def get_client_root_url(self, request: "HttpRequest") -> str: | ||
if getattr(settings, "TEST_OVERRIDE_CLIENT_ROOT_URL", False): | ||
return "https://wagtail.org" | ||
|
||
return super().get_client_root_url(request) |