|
1
|
|
|
# coding=utf-8 |
|
2
|
|
|
""" |
|
3
|
|
|
""" |
|
4
|
|
|
__author__ = 'Alisue <[email protected]>' |
|
5
|
|
|
from django.test import TestCase |
|
6
|
|
|
from django.http import Http404 |
|
7
|
|
|
from django.template import TemplateDoesNotExist |
|
8
|
|
|
from roughpages.tests.compat import MagicMock, override_settings |
|
9
|
|
|
from roughpages.tests.compat import patch, DEFAULT |
|
10
|
|
|
from roughpages.views import render_roughpage, roughpage |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
@override_settings( |
|
14
|
|
|
APPEND_SLASH=True, |
|
15
|
|
|
ROUGHPAGES_BACKEND='roughpages.backends.PlainTemplateFilenameBackend', |
|
16
|
|
|
ROUGHPAGES_TEMPLATE_DIR='roughpages', |
|
17
|
|
|
ROUGHPAGES_TEMPLATE_FILE_EXT='.html', |
|
18
|
|
|
ROUGHPAGES_RAISE_TEMPLATE_DOES_NOT_EXISTS=False, |
|
19
|
|
|
) |
|
20
|
|
|
class RoughpagesViewsTestCase(TestCase): |
|
21
|
|
|
def setUp(self): |
|
22
|
|
|
self.request = MagicMock() |
|
23
|
|
|
self.request.session = {} |
|
24
|
|
|
self.request.user.is_authenticated.return_value = False |
|
25
|
|
|
|
|
26
|
|
|
@patch.multiple('roughpages.views', |
|
27
|
|
|
RequestContext=DEFAULT, |
|
28
|
|
|
HttpResponse=DEFAULT) |
|
29
|
|
|
def test_render_roughpage(self, RequestContext, HttpResponse): |
|
30
|
|
|
t = MagicMock() |
|
31
|
|
|
r = render_roughpage(self.request, t) |
|
32
|
|
|
import django |
|
33
|
|
|
if django.VERSION >= (1, 8): |
|
34
|
|
|
# Newer than 1.8, template.render should be passed dict and Request instead of RequestContext as arguments |
|
35
|
|
|
t.render_assert_called_with({}, self.request) |
|
36
|
|
|
HttpResponse.assert_called_with(t.render({}, self.request)) |
|
37
|
|
|
self.assertEqual(r, HttpResponse()) |
|
38
|
|
|
else: |
|
39
|
|
|
# RequestContext should be initialized with request |
|
40
|
|
|
RequestContext.assert_called_with(self.request) |
|
41
|
|
|
# t.render should be called with the context |
|
42
|
|
|
t.render.assert_called_with(RequestContext()) |
|
43
|
|
|
# HttpResponse should be initialized with the output of |
|
44
|
|
|
# t.render(c) |
|
45
|
|
|
HttpResponse.assert_called_with(t.render(RequestContext())) |
|
46
|
|
|
# return should be response |
|
47
|
|
|
self.assertEqual(r, HttpResponse()) |
|
48
|
|
|
|
|
49
|
|
|
@patch.multiple('roughpages.views', |
|
50
|
|
|
loader=DEFAULT, |
|
51
|
|
|
render_roughpage=DEFAULT) |
|
52
|
|
|
def test_roughpage(self, loader, render_roughpage): |
|
53
|
|
|
url = '/foo/bar/hoge/' |
|
54
|
|
|
r = roughpage(self.request, url) |
|
55
|
|
|
# loader.select_template should be called with follow |
|
56
|
|
|
# because backend is PlainTemplateFilenameBackend |
|
57
|
|
|
template_filenames = ['roughpages/foo/bar/hoge.html'] |
|
58
|
|
|
loader.select_template.assert_called_with(template_filenames) |
|
59
|
|
|
# render_roughpage should be called with |
|
60
|
|
|
t = loader.select_template() |
|
61
|
|
|
render_roughpage.assert_called_with(self.request, t) |
|
62
|
|
|
# should return the return of render_roughpage |
|
63
|
|
|
self.assertEqual(r, render_roughpage()) |
|
64
|
|
|
|
|
65
|
|
|
@patch.multiple('roughpages.views', |
|
66
|
|
|
loader=DEFAULT, |
|
67
|
|
|
render_roughpage=DEFAULT) |
|
68
|
|
|
def test_roughpage_replace_dots(self, loader, render_roughpage): |
|
69
|
|
|
url = '/foo.foo/bar.bar/hoge.hoge/' |
|
70
|
|
|
r = roughpage(self.request, url) |
|
71
|
|
|
# loader.select_template should be called with follow |
|
72
|
|
|
# because backend is PlainTemplateFilenameBackend |
|
73
|
|
|
template_filenames = ['roughpages/foo.foo/bar.bar/hoge_hoge.html'] |
|
74
|
|
|
loader.select_template.assert_called_with(template_filenames) |
|
75
|
|
|
# render_roughpage should be called with |
|
76
|
|
|
t = loader.select_template() |
|
77
|
|
|
render_roughpage.assert_called_with(self.request, t) |
|
78
|
|
|
# should return the return of render_roughpage |
|
79
|
|
|
self.assertEqual(r, render_roughpage()) |
|
80
|
|
|
|
|
81
|
|
|
@patch.multiple('roughpages.views', |
|
82
|
|
|
redirect=DEFAULT) |
|
83
|
|
|
def test_roughpage_redirect(self, redirect): |
|
84
|
|
|
url = '/foo/bar/hoge' |
|
85
|
|
|
roughpage(self.request, url) |
|
86
|
|
|
# redirect should be called |
|
87
|
|
|
redirect.assert_called_with(url + '/', permanent=True) |
|
88
|
|
|
|
|
89
|
|
|
@patch.multiple('roughpages.views', |
|
90
|
|
|
loader=DEFAULT, |
|
91
|
|
|
render_roughpage=DEFAULT) |
|
92
|
|
|
def test_roughpage_index(self, loader, render_roughpage): |
|
93
|
|
|
url = '/' |
|
94
|
|
|
r = roughpage(self.request, url) |
|
95
|
|
|
# loader.select_template should be called with follow |
|
96
|
|
|
# because backend is PlainTemplateFilenameBackend |
|
97
|
|
|
template_filenames = ['roughpages/index.html'] |
|
98
|
|
|
loader.select_template.assert_called_with(template_filenames) |
|
99
|
|
|
# render_roughpage should be called with |
|
100
|
|
|
t = loader.select_template() |
|
101
|
|
|
render_roughpage.assert_called_with(self.request, t) |
|
102
|
|
|
# should return the return of render_roughpage |
|
103
|
|
|
self.assertEqual(r, render_roughpage()) |
|
104
|
|
|
|
|
105
|
|
|
@patch.multiple('roughpages.views', |
|
106
|
|
|
get_backend=DEFAULT, |
|
107
|
|
|
loader=DEFAULT, |
|
108
|
|
|
render_roughpage=DEFAULT) |
|
109
|
|
|
def test_roughpage_index_auth(self, get_backend, loader, render_roughpage): |
|
110
|
|
|
from roughpages.backends import AuthTemplateFilenameBackend |
|
111
|
|
|
get_backend.return_value = AuthTemplateFilenameBackend() |
|
112
|
|
|
url = '/' |
|
113
|
|
|
r = roughpage(self.request, url) |
|
114
|
|
|
# loader.select_template should be called with follow |
|
115
|
|
|
# because backend is PlainTemplateFilenameBackend |
|
116
|
|
|
template_filenames = [ |
|
117
|
|
|
'roughpages/index.anonymous.html', |
|
118
|
|
|
'roughpages/index.html', |
|
119
|
|
|
] |
|
120
|
|
|
loader.select_template.assert_called_with(template_filenames) |
|
121
|
|
|
# render_roughpage should be called with |
|
122
|
|
|
t = loader.select_template() |
|
123
|
|
|
render_roughpage.assert_called_with(self.request, t) |
|
124
|
|
|
# should return the return of render_roughpage |
|
125
|
|
|
self.assertEqual(r, render_roughpage()) |
|
126
|
|
|
|
|
127
|
|
|
@patch.multiple('roughpages.views', |
|
128
|
|
|
loader=DEFAULT, |
|
129
|
|
|
render_roughpage=DEFAULT) |
|
130
|
|
|
def test_roughpage_no_template(self, loader, render_roughpage): |
|
131
|
|
|
loader.select_template.side_effect = TemplateDoesNotExist('foobar') |
|
132
|
|
|
url = '/foo/bar/hoge/' |
|
133
|
|
|
self.assertRaises(Http404, |
|
134
|
|
|
roughpage, |
|
135
|
|
|
self.request, url) |
|
136
|
|
|
# loader.select_template should be called with follow |
|
137
|
|
|
# because backend is PlainTemplateFilenameBackend |
|
138
|
|
|
template_filenames = ['roughpages/foo/bar/hoge.html'] |
|
139
|
|
|
loader.select_template.assert_called_with(template_filenames) |
|
140
|
|
|
|
|
141
|
|
|
@override_settings( |
|
142
|
|
|
ROUGHPAGES_RAISE_TEMPLATE_DOES_NOT_EXISTS=True) |
|
143
|
|
|
@patch.multiple('roughpages.views', |
|
144
|
|
|
loader=DEFAULT, |
|
145
|
|
|
render_roughpage=DEFAULT) |
|
146
|
|
|
def test_roughpage_no_template_raise(self, loader, render_roughpage): |
|
147
|
|
|
loader.select_template.side_effect = TemplateDoesNotExist('foobar') |
|
148
|
|
|
url = '/foo/bar/hoge/' |
|
149
|
|
|
self.assertRaises(TemplateDoesNotExist, |
|
150
|
|
|
roughpage, |
|
151
|
|
|
self.request, url) |
|
152
|
|
|
# loader.select_template should be called with follow |
|
153
|
|
|
# because backend is PlainTemplateFilenameBackend |
|
154
|
|
|
template_filenames = ['roughpages/foo/bar/hoge.html'] |
|
155
|
|
|
loader.select_template.assert_called_with(template_filenames) |
|
156
|
|
|
|