Skip to content

fix: Added test coverage to admin preview view #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Changelog

Unreleased
==========

* fix: Added test coverage to admin preview view

4.0.0.dev4 (2022-02-03)
=======================
Expand Down
40 changes: 40 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from cms.test_utils.testcases import CMSTestCase
from cms.utils.urlutils import admin_reverse

from .utils.factories import SnippetWithVersionFactory


class PreviewViewTestCase(CMSTestCase):
def setUp(self):
self.snippet = SnippetWithVersionFactory(html="<h1>Test Title</h1><br><p>Test paragraph</p>")
self.user = self.get_superuser()

def test_preview_renders_html(self):
"""
Check that our snippet HTML is rendered, unescaped, on the page
"""
preview_url = admin_reverse(
"djangocms_snippet_snippet_preview",
kwargs={"snippet_id": self.snippet.id},
)
with self.login_user_context(self.user):
response = self.client.get(preview_url)

self.assertEqual(self.snippet.html, "<h1>Test Title</h1><br><p>Test paragraph</p>")
self.assertEqual(response.status_code, 200)
# Removing html escaping, means the content is rendered including the tags on the page, but also means that
# the response will contain character entity references.
self.assertContains(response, "&lt;h1&gt;Test Title&lt;/h1&gt;&lt;br&gt;&lt;p&gt;Test paragraph&lt;/p&gt;")

def test_preview_raises_302_no_snippet(self):
"""
With no Snippet to preview, a 302 will be raised and the user will be redirected to the admin
"""
preview_url = admin_reverse(
"djangocms_snippet_snippet_preview",
kwargs={"snippet_id": 999}, # Non existent PK!
)
with self.login_user_context(self.user):
response = self.client.get(preview_url)

self.assertEqual(response.status_code, 302)