-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtests.py
More file actions
385 lines (352 loc) · 13.8 KB
/
tests.py
File metadata and controls
385 lines (352 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
from contextlib import redirect_stderr
from datetime import timedelta
from io import StringIO
from django.contrib.auth.models import User
from django.test import TestCase
from django.urls import reverse
from django.utils import timezone
from .models import ContentFormat, Entry, Event
from .sitemaps import WeblogSitemap
class DateTimeMixin:
def setUp(self):
self.now = timezone.now()
self.yesterday = self.now - timedelta(days=1)
self.tomorrow = self.now + timedelta(days=1)
class EntryTestCase(DateTimeMixin, TestCase):
def test_manager_active(self):
"""
Make sure that the Entry manager's `active` method works
"""
Entry.objects.create(
pub_date=self.now, is_active=False, headline="inactive", slug="a"
)
Entry.objects.create(
pub_date=self.now, is_active=True, headline="active", slug="b"
)
self.assertQuerySetEqual(
Entry.objects.published(),
["active"],
transform=lambda entry: entry.headline,
)
def test_manager_published(self):
"""
Make sure that the Entry manager's `published` method works
"""
Entry.objects.create(
pub_date=self.yesterday, is_active=False, headline="past inactive", slug="a"
)
Entry.objects.create(
pub_date=self.yesterday, is_active=True, headline="past active", slug="b"
)
Entry.objects.create(
pub_date=self.tomorrow,
is_active=False,
headline="future inactive",
slug="c",
)
Entry.objects.create(
pub_date=self.tomorrow, is_active=True, headline="future active", slug="d"
)
self.assertQuerySetEqual(
Entry.objects.published(),
["past active"],
transform=lambda entry: entry.headline,
)
def test_docutils_safe(self):
"""
Make sure docutils' file inclusion directives are disabled by default.
"""
with redirect_stderr(StringIO()):
entry = Entry.objects.create(
pub_date=self.now,
is_active=True,
headline="active",
content_format="reST",
body=".. raw:: html\n :file: somefile\n",
slug="a",
)
self.assertIn("<p>"raw" directive disabled.</p>", entry.body_html)
self.assertIn(".. raw:: html\n :file: somefile", entry.body_html)
def test_content_format_html(self):
entry = Entry.objects.create(
pub_date=self.now,
slug="a",
body="<strong>test</strong>",
content_format=ContentFormat.HTML,
)
self.assertHTMLEqual(entry.body_html, "<strong>test</strong>")
def test_content_format_reST(self):
entry = Entry.objects.create(
pub_date=self.now,
slug="a",
body="**test**",
content_format=ContentFormat.REST,
)
self.assertHTMLEqual(entry.body_html, "<p><strong>test</strong></p>")
def test_content_format_markdown(self):
entry = Entry.objects.create(
pub_date=self.now,
slug="a",
body="**test**",
content_format=ContentFormat.MARKDOWN,
)
self.assertHTMLEqual(entry.body_html, "<p><strong>test</strong></p>")
def test_header_base_level_reST(self):
entry = Entry.objects.create(
pub_date=self.now,
slug="a",
body="test\n====",
content_format=ContentFormat.REST,
)
self.assertHTMLEqual(
entry.body_html, '<div class="section" id="s-test"><h3>test</h3></div>'
)
def test_header_base_level_markdown(self):
entry = Entry.objects.create(
pub_date=self.now,
slug="a",
body="# test",
content_format=ContentFormat.MARKDOWN,
)
self.assertHTMLEqual(entry.body_html, '<h3 id="s-test">test</h3>')
class EventTestCase(DateTimeMixin, TestCase):
def test_manager_past_future(self):
"""
Make sure that the Event manager's `past` and `future` methods works
"""
Event.objects.create(date=self.yesterday, pub_date=self.now, headline="past")
Event.objects.create(date=self.tomorrow, pub_date=self.now, headline="future")
self.assertQuerySetEqual(
Event.objects.future(), ["future"], transform=lambda event: event.headline
)
self.assertQuerySetEqual(
Event.objects.past(), ["past"], transform=lambda event: event.headline
)
def test_manager_past_future_include_today(self):
"""
Make sure that both .future() and .past() include today's events.
"""
Event.objects.create(date=self.now, pub_date=self.now, headline="today")
self.assertQuerySetEqual(
Event.objects.future(), ["today"], transform=lambda event: event.headline
)
self.assertQuerySetEqual(
Event.objects.past(), ["today"], transform=lambda event: event.headline
)
def test_past_future_ordering(self):
"""
Make sure the that .future() and .past() use the actual date for ordering
(and not the pub_date).
"""
D = timedelta(days=1)
Event.objects.create(
date=self.yesterday - D, pub_date=self.yesterday - D, headline="a"
)
Event.objects.create(date=self.yesterday, pub_date=self.yesterday, headline="b")
Event.objects.create(date=self.tomorrow, pub_date=self.tomorrow, headline="c")
Event.objects.create(
date=self.tomorrow + D, pub_date=self.tomorrow + D, headline="d"
)
self.assertQuerySetEqual(
Event.objects.future(), ["c", "d"], transform=lambda event: event.headline
)
self.assertQuerySetEqual(
Event.objects.past(), ["b", "a"], transform=lambda event: event.headline
)
class ViewsTestCase(DateTimeMixin, TestCase):
"""
TODO:
* anon users can't see unpublished entries at all (list or detail)
* logged in users (non-staff) can't see unpublished entries at all
* staff users without write permission on BlogEntry can't see unpublished
entries at all
* staff users with write permission on BlogEntry can't see unpublished
entries in the list, but can view the detail page
"""
# def test_anonymous_user_cant_see_entries(self):
# """
# A test which creates an unpublished entry and then loads the list view
# followed by detail view as an anonymous user to check that the entry cannot
# be seen.
# """
# e1 = Entry.objects.create(
# pub_date=self.yesterday, is_active=False, headline="inactive", slug="a"
# )
# e2 = Entry.objects.create(
# pub_date=self.yesterday, is_active=True, headline="active", slug="b"
# )
# response = self.client.get(reverse("weblog:index"))
# self.assertNotContains(response, "active")
# response = self.client.get(
# reverse(
# "weblog:entry",
# kwargs={
# "year": e1.pub_date.year,
# "month": e1.pub_date.month,
# "day": e1.pub_date.day,
# "slug": e1.slug,
# },
# )
# )
# self.assertEqual(response.status_code, 404)
# response = self.client.get(
# reverse(
# "weblog:entry",
# kwargs={
# "year": e2.pub_date.year,
# "month": e2.pub_date.month,
# "day": e2.pub_date.day,
# "slug": e2.slug,
# },
# )
# )
# self.assertEqual(response.status_code, 404)
#
# def test_logged_in_user_cant_see_entries(self):
# """
# A test which creates an unpublished entry and then loads the list view
# followed by detail view as a non-staff user to check that the entry cannot be
# seen.
# """
# e = Entry.objects.create(
# pub_date=self.yesterday, is_active=False, headline="inactive", slug="a"
# )
# user = User.objects.create_user("user", "user@example.com", "password")
# self.client.force_login(user)
# response = self.client.get(reverse("weblog:index"))
# self.assertNotContains(response, "active")
# response = self.client.get(
# reverse(
# "weblog:entry",
# kwargs={
# "year": e.pub_date.year,
# "month": e.pub_date.month,
# "day": e.pub_date.day,
# "slug": e.slug,
# },
# )
# )
# self.assertEqual(response.status_code, 404)
#
# def test_staff_no_write_permission_cant_see_entries(self):
# """
# A test which creates an unpublished entry and then loads the list view
# followed by detail view as a staff user without blog write permissions to
# check that the entry cannot be seen.
# """
# e1 = Entry.objects.create(
# pub_date=self.yesterday, is_active=False, headline="inactive", slug="a"
# )
# e2 = Entry.objects.create(
# pub_date=self.yesterday, is_active=True, headline="active", slug="b"
# )
# user = User.objects.create_user(
# "staff", "staff@example.com", "password", is_staff=True
# )
# self.client.force_login(user)
# response = self.client.get(reverse("weblog:index"))
#
# self.assertContains(response, "active")
# response = self.client.get(
# reverse(
# "weblog:entry",
# kwargs={
# "year": e1.pub_date.year,
# "month": e1.pub_date.month,
# "day": e1.pub_date.day,
# "slug": e1.slug,
# },
# )
# )
# self.assertEqual(response.status_code, 404)
# response = self.client.get(
# reverse(
# "weblog:entry",
# kwargs={
# "year": e2.pub_date.year,
# "month": e2.pub_date.month,
# "day": e2.pub_date.day,
# "slug": e2.slug,
# },
# )
# )
# self.assertEqual(response.status_code, 404)
def test_staff_with_write_permission_can_see_unpublished_detail_view(self):
"""
staff users with write permission on BlogEntry can't see unpublished entries
in the list, but can view the detail page
"""
e1 = Entry.objects.create(
pub_date=self.yesterday, is_active=False, headline="inactive", slug="a"
)
user = User.objects.create(username="staff", is_staff=True)
self.client.force_login(user)
self.assertEqual(Entry.objects.all().count(), 1)
response = self.client.get(reverse("weblog:index"))
self.assertEqual(response.status_code, 404)
response = self.client.get(
reverse(
"weblog:entry",
kwargs={
"year": e1.pub_date.year,
"month": e1.pub_date.month,
"day": e1.pub_date.day,
"slug": e1.slug,
},
)
)
request = response.context["request"]
self.assertTrue(request.user.is_staff)
self.assertEqual(response.status_code, 200)
def test_no_past_upcoming_events(self):
"""
Make sure there are no past event in the "upcoming events" sidebar (#399)
"""
# We need a published entry on the index page so that it doesn't return a 404
Entry.objects.create(pub_date=self.yesterday, is_active=True, slug="a")
Event.objects.create(
date=self.yesterday, pub_date=self.now, is_active=True, headline="Jezdezcon"
)
response = self.client.get(reverse("weblog:index"))
self.assertEqual(response.status_code, 200)
self.assertQuerySetEqual(response.context["events"], [])
def test_no_unpublished_future_events(self):
"""
Make sure there are no unpublished future events in the "upcoming events" sidebar
"""
# We need a published entry on the index page so that it doesn't return a 404
Entry.objects.create(pub_date=self.yesterday, is_active=True, slug="a")
Event.objects.create(
date=self.tomorrow,
pub_date=self.yesterday,
is_active=False,
headline="inactive",
)
Event.objects.create(
date=self.tomorrow,
pub_date=self.tomorrow,
is_active=True,
headline="future publish date",
)
for user in [
None,
User.objects.create(username="non-staff", is_staff=False),
User.objects.create(username="staff", is_staff=True),
User.objects.create_superuser(username="superuser"),
]:
if user:
self.client.force_login(user)
response = self.client.get(reverse("weblog:index"))
with self.subTest(user=user):
self.assertEqual(response.status_code, 200)
self.assertQuerySetEqual(response.context["events"], [])
class SitemapTests(DateTimeMixin, TestCase):
def test_sitemap(self):
entry = Entry.objects.create(
pub_date=self.yesterday, is_active=True, headline="foo", slug="foo"
)
sitemap = WeblogSitemap()
urls = sitemap.get_urls()
self.assertEqual(len(urls), 1)
url_info = urls[0]
self.assertEqual(url_info["location"], entry.get_absolute_url())