Skip to content

Commit 59c22af

Browse files
authored
fix: Added test coverage to admin preview view (#96)
1 parent c25b365 commit 59c22af

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Changelog
44

55
Unreleased
66
==========
7-
7+
* fix: Added test coverage to admin preview view
88

99
4.0.0.dev4 (2022-02-03)
1010
=======================

tests/test_views.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from cms.test_utils.testcases import CMSTestCase
2+
from cms.utils.urlutils import admin_reverse
3+
4+
from .utils.factories import SnippetWithVersionFactory
5+
6+
7+
class PreviewViewTestCase(CMSTestCase):
8+
def setUp(self):
9+
self.snippet = SnippetWithVersionFactory(html="<h1>Test Title</h1><br><p>Test paragraph</p>")
10+
self.user = self.get_superuser()
11+
12+
def test_preview_renders_html(self):
13+
"""
14+
Check that our snippet HTML is rendered, unescaped, on the page
15+
"""
16+
preview_url = admin_reverse(
17+
"djangocms_snippet_snippet_preview",
18+
kwargs={"snippet_id": self.snippet.id},
19+
)
20+
with self.login_user_context(self.user):
21+
response = self.client.get(preview_url)
22+
23+
self.assertEqual(self.snippet.html, "<h1>Test Title</h1><br><p>Test paragraph</p>")
24+
self.assertEqual(response.status_code, 200)
25+
# Removing html escaping, means the content is rendered including the tags on the page, but also means that
26+
# the response will contain character entity references.
27+
self.assertContains(response, "&lt;h1&gt;Test Title&lt;/h1&gt;&lt;br&gt;&lt;p&gt;Test paragraph&lt;/p&gt;")
28+
29+
def test_preview_raises_302_no_snippet(self):
30+
"""
31+
With no Snippet to preview, a 302 will be raised and the user will be redirected to the admin
32+
"""
33+
preview_url = admin_reverse(
34+
"djangocms_snippet_snippet_preview",
35+
kwargs={"snippet_id": 999}, # Non existent PK!
36+
)
37+
with self.login_user_context(self.user):
38+
response = self.client.get(preview_url)
39+
40+
self.assertEqual(response.status_code, 302)

0 commit comments

Comments
 (0)