-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test case to cover render-ability, edit-ability and preview-abili…
…ty for all page types where Factory._meta.autotest is True
- Loading branch information
Showing
1 changed file
with
66 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import os | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.http import HttpRequest | ||
from django.test import TestCase | ||
from wagtail.models import Page | ||
from wagtail.admin.views.pages import EditView | ||
|
||
import wagtail_factories | ||
|
||
|
||
class PageFeaturesTestCase(TestCase): | ||
def setUpClass(cls): | ||
super().setUpClass() | ||
cls.request = HttpRequest() | ||
cls.request.method = "GET" | ||
cls.request.user = get_user_model().objects.create_superuser("superuser") | ||
|
||
def assertCanRenderPageWithoutErrors(self, page: Page, route_path: str = "/"): | ||
request = self.request | ||
request.path = os.path.join(page.get_url(request), route_path) | ||
try: | ||
view, args, kwargs = page.resolve_subpage(route_path) | ||
except AttributeError: | ||
page.serve(request) | ||
else: | ||
page.serve(request, view, args, kwargs) | ||
|
||
def assertCanEditPageWithoutErrors(self, page: Page): | ||
request = self.request | ||
request.path = f"/admin/pages/{page.id}/edit/" | ||
EditView.as_view()(request, page.id) | ||
|
||
def assertCanPreviewPageWithoutErrors(self, page: Page, preview_mode: str = ""): | ||
request = self.request | ||
request.path = f"/admin/pages/{page.id}/edit/preview/?mode={preview_mode}" | ||
preview_request = page.make_preview_request(request, preview_mode) | ||
page.serve_preview(preview_request, preview_mode) | ||
|
||
|
||
class PageAutoTestCase(PageFeaturesTestCase): | ||
def setUpClass(cls): | ||
super().setUpClass() | ||
cls.factories = [ | ||
f for f in wagtail_factories.get_page_factories() if f._meta.autotest | ||
] | ||
|
||
def test_can_render_pages_without_errors(self): | ||
for factory in self.factories: | ||
page = factory() | ||
for path in page.get_route_paths(page): | ||
with self.subTest(f"Page type: {type(page)}, Route: {path}"): | ||
self.assertCanRenderPageWithoutErrors(page, path) | ||
|
||
def test_can_edit_pages_without_errors(self): | ||
for factory in self.factories: | ||
page = factory() | ||
with self.subTest(f"Page type: {type(page)}"): | ||
self.assertCanEditPageWithoutErrors(page) | ||
|
||
def test_can_preview_pages_without_errors(self): | ||
for factory in self.factories: | ||
page = factory() | ||
for mode, label in page.preview_modes: | ||
with self.subTest(f"Page type: {type(page)}, Mode: {label}"): | ||
self.assertCanPreviewPageWithoutErrors(page, mode) |