0c323494c9ad261398d7b4edc38ae5d6223328dc
[django-ssify.git] / tests / tests / test_basic.py
1 # -*- coding: utf-8 -*-
2 # This file is part of django-ssify, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See README.md for more information.
4 #
5 from __future__ import unicode_literals
6
7 import re
8 import warnings
9 from django.test import TestCase
10 from django.test.utils import override_settings
11 from ssify.exceptions import UndeclaredSsiVarsError, UnusedSsiVarsWarning
12 from tests.tests_utils import split_ssi
13
14
15 class BasicTestCase(TestCase):
16     def test_zero(self):
17         self.assertEqual(
18             self.client.get('/number_zero').content.strip(),
19             b"<!--#set var='ve023a08d2c2075118e25b5f4339438dc' value='0'-->\n"
20             b"<!--#echo var='ve023a08d2c2075118e25b5f4339438dc' "
21             b"encoding='none'-->"
22         )
23
24     def test_basic_include(self):
25         self.assertEqual(
26             self.client.get('/basic_include').content.strip(),
27             b"<!--#include file='/language/pl'-->"
28         )
29
30     def test_single_quote(self):
31         self.assertEqual(
32             self.client.get('/quote/3').content.strip(),
33             b"""Explicit is better than implicit.
34 Line 3 of <!--#echo var='va50d914691ecf9b421c680d93ba1263e' encoding='none'-->
35 <!--#if expr='${vddc386e120ab274a980ab67384391a1a}'-->Odd number of characters.
36 <!--#else-->Even number of characters.
37 <!--#endif-->"""
38         )
39
40     def test_undeclared_vars(self):
41         self.assertRaises(UndeclaredSsiVarsError,
42                           self.client.get,
43                           '/quote_undeclared/3')
44
45     def test_overdeclared_vars(self):
46         with warnings.catch_warnings(record=True) as w:
47             response = self.client.get('/quote_overdeclared/3')
48             self.assertIs(w[-1].category, UnusedSsiVarsWarning)
49
50     def test_random_quote(self):
51         self.assertEqual(
52             sorted(split_ssi(self.client.get('/').content)),
53             sorted([b"<!--#set var='va50d914691ecf9b421c680d93ba1263e' value='22'-->",
54                  b"<!--#set var='v3e7f638af74c9f420b6d2c5fe4dda51d' value='4'-->",
55                  b"<!--#set var='vafe010f2e683908fee32c48d01bb2650' value=''-->",
56                  b"<!--#include file='/random_quote'-->"])
57         )
58
59         # Do it again, this time from cache.
60         self.assertEqual(
61             sorted(split_ssi(self.client.get('/').content)),
62             sorted([b"<!--#set var='va50d914691ecf9b421c680d93ba1263e' value='22'-->",
63                  b"<!--#set var='v3e7f638af74c9f420b6d2c5fe4dda51d' value='4'-->",
64                  b"<!--#set var='vafe010f2e683908fee32c48d01bb2650' value=''-->",
65                  b"<!--#include file='/random_quote'-->"])
66         )
67         self.assertEqual(
68             self.client.get('/random_quote').content.strip(),
69             b"<!--#include "
70             b"file='/quote/${v3e7f638af74c9f420b6d2c5fe4dda51d}'-->"
71         )
72
73     @override_settings(SSIFY_DEBUG=True)
74     def test_debug_render_random_quote(self):
75         """Renders the complete view using the DebugSsiMiddleware."""
76         response = self.client.get('/')
77         if hasattr(response, 'render') and callable(response.render):
78             response.render()
79         self.assertEqual(
80             response.content.strip(),
81             b"""Simple is better than complex.
82 Line 4 of 22
83 Even number of characters."""
84         )