|
| 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, "<h1>Test Title</h1><br><p>Test paragraph</p>") |
| 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