This repository has been archived by the owner on Jun 29, 2020. It is now read-only.
/
test_crud.py
369 lines (334 loc) · 16.8 KB
/
test_crud.py
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
import datetime
from django.conf import settings
from django.core.exceptions import ValidationError
from django.test import TestCase
from django.contrib.auth.models import AnonymousUser
from ..factories import EntryFactory
from ..factories import CategoryFactory
from ...models import Entry
from kawaz.core.personas.tests.factories import PersonaFactory
class EntryDetailViewTestCase(TestCase):
def setUp(self):
self.user = PersonaFactory()
self.user.set_password('password')
self.user.save()
def test_anonymous_user_can_view_public_entry(self):
'''Tests anonymous user can view public entry'''
entry = EntryFactory()
r = self.client.get(entry.get_absolute_url())
self.assertTemplateUsed(r, 'blogs/entry_detail.html')
self.assertEqual(r.context_data['object'], entry)
def test_authorized_user_can_view_public_entry(self):
'''Tests authorized user can view public entry'''
entry = EntryFactory()
self.assertTrue(self.client.login(username=self.user, password='password'))
r = self.client.get(entry.get_absolute_url())
self.assertTemplateUsed(r, 'blogs/entry_detail.html')
self.assertEqual(r.context_data['object'], entry)
def test_anonymous_user_can_not_view_protected_entry(self):
'''Tests anonymous user can not view protected entry'''
entry = EntryFactory(pub_state='protected')
r = self.client.get(entry.get_absolute_url())
self.assertRedirects(r, '{0}?next={1}'.format(settings.LOGIN_URL, entry.get_absolute_url()))
def test_authorized_user_can_view_protected_entry(self):
'''Tests authorized user can view public entry'''
entry = EntryFactory(pub_state='protected')
self.assertTrue(self.client.login(username=self.user, password='password'))
r = self.client.get(entry.get_absolute_url())
self.assertTemplateUsed(r, 'blogs/entry_detail.html')
self.assertEqual(r.context_data['object'], entry)
def test_anonymous_user_can_not_view_draft_entry(self):
'''Tests anonymous user can not view draft entry'''
entry = EntryFactory(pub_state='draft')
r = self.client.get(entry.get_absolute_url())
self.assertRedirects(r, '{0}?next={1}'.format(settings.LOGIN_URL, entry.get_absolute_url()))
def test_others_can_not_view_draft_entry(self):
'''
Tests others can not view draft entry
User will redirect to '/entries/1/update/'
'''
entry = EntryFactory(pub_state='draft')
self.assertTrue(self.client.login(username=self.user, password='password'))
r = self.client.get(entry.get_absolute_url())
self.assertRedirects(r, settings.LOGIN_URL + '?next=/blogs/{}/1/update/'.format(entry.author.username))
def test_organizer_can_view_draft_entry(self):
'''Tests organizer can view draft entry on update view'''
entry = EntryFactory(pub_state='draft', author=self.user)
self.assertTrue(self.client.login(username=self.user, password='password'))
r = self.client.get(entry.get_absolute_url())
self.assertTemplateUsed(r, 'blogs/entry_form.html')
self.assertEqual(r.context_data['object'], entry)
class EntryCreateViewTestCase(TestCase):
def setUp(self):
self.user = PersonaFactory()
def test_anonymous_user_can_not_create_view(self):
'''Tests anonymous user can not view EntryCreateView'''
r = self.client.get('/blogs/{}/create/'.format(self.user.username))
self.assertRedirects(r, settings.LOGIN_URL + '?next=/blogs/{}/create/'.format(self.user.username))
def test_authorized_user_can_view_entry_create_view(self):
'''Tests authorized user can view EntryCreateView'''
self.assertTrue(self.client.login(username=self.user, password='password'))
r = self.client.get('/blogs/{}/create/'.format(self.user.username))
self.assertTemplateUsed(r, 'blogs/entry_form.html')
self.assertFalse('object' in r.context_data)
def test_anonymous_user_can_not_create_via_create_view(self):
'''Tests anonymous user can not create entry via EntryCreateView'''
r = self.client.post('/blogs/{}/create/'.format(self.user.username), {
'pub_state' : 'public',
'title' : '日記です',
'body' : '天気が良かったです',
})
self.assertRedirects(r, settings.LOGIN_URL + '?next=/blogs/{}/create/'.format(self.user.username))
def test_authorized_user_can_create_via_create_view(self):
'''Tests authorized user can create entry via EntryCreateView'''
self.assertTrue(self.client.login(username=self.user, password='password'))
r = self.client.post('/blogs/{}/create/'.format(self.user.username), {
'pub_state' : 'public',
'title' : '日記です',
'body' : '天気が良かったです',
})
today = datetime.date.today()
self.assertRedirects(r, '/blogs/{0}/{1}/{2}/{3}/1/'.format(self.user.username, today.year, today.month, today.day))
self.assertEqual(Entry.objects.count(), 1)
e = Entry.objects.get(pk=1)
self.assertEqual(e.title, '日記です')
def test_user_cannot_modify_author_id(self):
'''
Tests authorized user cannot modify author id.
In entry creation form, `author` is exist as hidden field.
So user can modify `author` to invalid values.
This test checks that `author` will be set by `request.user`
'''
other = PersonaFactory()
self.assertTrue(self.client.login(username=self.user, password='password'))
r = self.client.post('/blogs/{0}/create/'.format(self.user.username), {
'pub_state' : 'public',
'title' : '日記です',
'body' : '天気が良かったです',
'author' : other.pk # crackers attempt to masquerade
})
today = datetime.date.today()
self.assertRedirects(r, '/blogs/{0}/{1}/{2}/{3}/1/'.format(self.user.username, today.year, today.month, today.day))
self.assertEqual(Entry.objects.count(), 1)
e = Entry.objects.get(pk=1)
self.assertEqual(e.author, self.user)
self.assertNotEqual(e.author, other)
def test_user_can_create_entry_with_category(self):
"""
カテゴリを指定してブログ記事を新しく投稿できる
"""
category = CategoryFactory(author=self.user)
self.assertTrue(self.client.login(username=self.user, password='password'))
r = self.client.post('/blogs/{0}/create/'.format(self.user.username), {
'pub_state': 'public',
'title': '日記です',
'body': '天気が良かったです',
'category': category.pk
})
today = datetime.date.today()
self.assertRedirects(r, '/blogs/{0}/{1}/{2}/{3}/1/'.format(self.user.username, today.year, today.month, today.day))
self.assertEqual(Entry.objects.count(), 1)
e = Entry.objects.get(pk=1)
self.assertEqual(e.author, self.user)
self.assertEqual(e.category, category)
def test_user_can_create_entry_with_others_category(self):
"""
他人のカテゴリを指定してブログ記事を新しく投稿しようとすると
ValidationErrorを送出する
"""
category = CategoryFactory()
self.assertTrue(self.client.login(username=self.user, password='password'))
r = self.client.post('/blogs/{0}/create/'.format(self.user.username), {
'pub_state': 'public',
'title': '日記です',
'body': '天気が良かったです',
'category': category.pk
})
self.assertEqual(r.status_code, 200)
self.assertEqual(Entry.objects.count(), 0)
class EntryUpdateViewTestCase(TestCase):
def setUp(self):
self.user = PersonaFactory(username='author_kawaztan')
self.user.set_password('password')
self.other = PersonaFactory(username='black_kawaztan')
self.user.save()
self.other.save()
self.entry = EntryFactory(title='かわずたんだよ☆', author=self.user)
def test_anonymous_user_can_not_view_entry_update_view(self):
'''Tests anonymous user can not view EntryUpdateView'''
r = self.client.get('/blogs/author_kawaztan/1/update/')
self.assertRedirects(r, settings.LOGIN_URL + '?next=/blogs/author_kawaztan/1/update/')
def test_authorized_user_can_view_entry_update_view(self):
'''
Tests authorized user can view EntryUpdateView
'''
self.assertTrue(self.client.login(username=self.user, password='password'))
r = self.client.get('/blogs/author_kawaztan/1/update/')
self.assertTemplateUsed(r, 'blogs/entry_form.html')
self.assertTrue('object' in r.context_data)
self.assertEqual(r.context_data['object'], self.entry)
def test_anonymous_user_can_not_update_via_update_view(self):
'''
Tests anonymous user can not update entry via EntryUpdateView
It will redirect to LOGIN_URL
'''
r = self.client.post('/blogs/author_kawaztan/1/update/', {
'pub_state' : 'public',
'title' : 'クラッカーだよー',
'body' : 'うえーい',
})
self.assertRedirects(r, settings.LOGIN_URL + '?next=/blogs/author_kawaztan/1/update/')
self.assertEqual(self.entry.title, 'かわずたんだよ☆')
def test_other_user_cannot_update_via_update_view(self):
'''
Tests other user cannot update entry via EntryUpdateView
It will redirect to LOGIN_URL
'''
self.assertTrue(self.client.login(username=self.other, password='password'))
r = self.client.post('/blogs/author_kawaztan/1/update/', {
'pub_state' : 'public',
'title' : 'いたずら日記です',
'body' : '黒かわずたんだよーん',
})
self.assertRedirects(r, settings.LOGIN_URL + '?next=/blogs/author_kawaztan/1/update/')
self.assertEqual(self.entry.title, 'かわずたんだよ☆')
def test_author_can_update_via_update_view(self):
'''Tests author user can update entry via EntryUpdateView'''
self.assertTrue(self.client.login(username=self.user, password='password'))
r = self.client.post('/blogs/author_kawaztan/1/update/', {
'pub_state' : 'public',
'title' : 'やっぱり書き換えます!',
'body' : 'うえーい',
})
self.assertRedirects(r, '/blogs/author_kawaztan/{0}/{1}/{2}/1/'.format(self.entry.publish_at.year, self.entry.publish_at.month, self.entry.publish_at.day))
self.assertEqual(Entry.objects.count(), 1)
e = Entry.objects.get(pk=1)
self.assertEqual(e.title, 'やっぱり書き換えます!')
def test_user_cannot_modify_author_id(self):
'''
Tests authorized user cannot modify author id.
In entry update form, `author` is exist as hidden field.
So user can modify `author` to invalid values.
This test checks that `author` will be set by `request.user`
'''
other = PersonaFactory()
self.assertTrue(self.client.login(username=self.user, password='password'))
r = self.client.post('/blogs/author_kawaztan/1/update/', {
'pub_state' : 'public',
'title' : 'ID書き換えます!',
'body' : 'うえーい',
'author' : other.pk # crackers attempt to masquerade
})
self.assertRedirects(r, '/blogs/author_kawaztan/{0}/{1}/{2}/1/'.format(self.entry.publish_at.year, self.entry.publish_at.month, self.entry.publish_at.day))
self.assertEqual(Entry.objects.count(), 1)
e = Entry.objects.get(pk=1)
self.assertEqual(e.author, self.user)
self.assertNotEqual(e.author, other)
self.assertEqual(e.title, 'ID書き換えます!')
class EntryListViewTestCase(TestCase):
def setUp(self):
self.entries = (
EntryFactory(),
EntryFactory(pub_state='protected'),
EntryFactory(pub_state='draft'),
)
self.user = PersonaFactory()
self.user.set_password('password')
self.user.save()
self.wille = PersonaFactory(role='wille')
self.wille.set_password('password')
self.wille.save()
def test_anonymous_can_view_only_public_entries(self):
'''
Tests anonymous user can view public Entry only.
The protected entries are not displayed.
'''
user = AnonymousUser()
r = self.client.get('/blogs/')
self.assertTemplateUsed('blogs/entry_list.html')
self.assertTrue('object_list' in r.context_data)
list = r.context_data['object_list']
self.assertEqual(list.count(), 1, 'object_list has one entry')
self.assertEqual(list[0], self.entries[0])
def test_wille_can_view_only_public_entries(self):
'''
Tests wille user can view public Entry only.
The protected entries are not displayed.
'''
self.assertTrue(self.client.login(username=self.wille, password='password'))
r = self.client.get('/blogs/')
self.assertTemplateUsed('blogs/entry_list.html')
self.assertTrue('object_list' in r.context_data)
list = r.context_data['object_list']
self.assertEqual(list.count(), 1, 'object_list has one entry')
self.assertEqual(list[0], self.entries[0])
def test_authenticated_can_view_all_publish_entries(self):
'''
Tests authenticated user can view all published entries.
'''
self.assertTrue(self.client.login(username=self.user, password='password'))
r = self.client.get('/blogs/')
self.assertTemplateUsed('blogs/entry_list.html')
self.assertTrue('object_list' in r.context_data)
list = r.context_data['object_list']
self.assertEqual(list.count(), 2, 'object_list has two entries')
self.assertEqual(list[0], self.entries[1], 'protected')
self.assertEqual(list[1], self.entries[0], 'public')
class EntryAuthorListViewTestCase(TestCase):
def setUp(self):
self.user = PersonaFactory()
self.user.set_password('password')
self.user.save()
self.wille = PersonaFactory(role='wille')
self.wille.set_password('password')
self.wille.save()
self.entries = (
EntryFactory(),
EntryFactory(author=self.user),
EntryFactory(pub_state='protected'),
EntryFactory(pub_state='protected', author=self.user),
EntryFactory(pub_state='draft'),
)
def test_anonymous_can_view_only_public_entries_of_the_author(self):
'''
Tests anonymous user can view public Entry written by specific author only.
The protected entries are not displayed.
'''
user = AnonymousUser()
r = self.client.get('/blogs/{}/'.format(self.user.username))
self.assertTemplateUsed('blogs/entry_list.html')
self.assertTrue('object_list' in r.context_data)
list = r.context_data['object_list']
self.assertEqual(list.count(), 1, 'object_list has one entry')
self.assertEqual(list[0], self.entries[1])
def test_wille_can_view_only_public_entries_of_the_author(self):
'''
Tests wille user can view public Entry written by specific author only.
The protected entries are not displayed.
'''
self.assertTrue(self.client.login(username=self.wille, password='password'))
r = self.client.get('/blogs/{}/'.format(self.user.username))
self.assertTemplateUsed('blogs/entry_list.html')
self.assertTrue('object_list' in r.context_data)
list = r.context_data['object_list']
self.assertEqual(list.count(), 1, 'object_list has one entry')
self.assertEqual(list[0], self.entries[1])
def test_authenticated_can_view_all_publish_entries_of_the_author(self):
'''
Tests authenticated user can view all published entries written by specific author.
'''
self.assertTrue(self.client.login(username=self.user, password='password'))
r = self.client.get('/blogs/{}/'.format(self.user.username))
self.assertTemplateUsed('blogs/entry_list.html')
self.assertTrue('object_list' in r.context_data)
list = r.context_data['object_list']
self.assertEqual(list.count(), 2, 'object_list has two entries')
self.assertEqual(list[0], self.entries[3], 'protected')
self.assertEqual(list[1], self.entries[1], 'public')
class EntryPreviewTestCase(TestCase):
def test_preview(self):
"""
ユーザーがEntryのPreviewを見れる
"""
r = self.client.get('/blogs/preview/')
self.assertTemplateUsed(r, 'blogs/components/entry_detail.html')