-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathtest_admin_views.py
700 lines (567 loc) · 23.9 KB
/
test_admin_views.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
import json
from unittest.mock import patch
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group, Permission
from django.core.files.uploadedfile import SimpleUploadedFile
from django.template.defaultfilters import filesizeformat
from django.test import TestCase, override_settings
from django.urls import reverse
from wagtail.models import Collection, GroupCollectionPermission
from wagtail.test.utils import WagtailTestUtils
from tests.utils import create_test_video_file
from wagtailvideos.models import Video
class TestVideoIndexView(WagtailTestUtils, TestCase):
def setUp(self):
self.login()
def get(self, params={}):
return self.client.get(reverse("wagtailvideos:index"), params)
def test_simple(self):
response = self.get()
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "wagtailvideos/videos/index.html")
self.assertContains(response, "Add a video")
def test_search(self):
response = self.get({"q": "Hello"})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context["query_string"], "Hello")
def test_pagination(self):
# page numbers in range should be accepted
response = self.get({"p": 1})
self.assertEqual(response.status_code, 200)
# page numbers out of range should return 404
response = self.get({"p": 9999})
self.assertEqual(response.status_code, 404)
class TestVideoAddView(TestCase, WagtailTestUtils):
def setUp(self):
self.login()
def get(self, params={}):
return self.client.get(reverse("wagtailvideos:add"), params)
def post(self, post_data={}):
return self.client.post(reverse("wagtailvideos:add"), post_data)
def test_get(self):
response = self.get()
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "wagtailvideos/videos/add.html")
# as standard, only the root collection exists and so no 'Collection' option
# is displayed on the form
self.assertNotContains(
response,
'<label class="w-field__label" for="id_collection" id="id_collection-label">',
)
# Ensure the form supports file uploads
self.assertContains(response, 'enctype="multipart/form-data"')
def test_get_with_collections(self):
root_collection = Collection.get_first_root_node()
collection_name = "Takeflight manifesto"
root_collection.add_child(name=collection_name)
response = self.get()
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "wagtailvideos/videos/add.html")
self.assertContains(
response,
'<label class="w-field__label" for="id_collection" id="id_collection-label">',
)
self.assertContains(response, collection_name)
def test_add(self):
video_file = create_test_video_file()
title = "Test Video"
response = self.post(
{
"title": title,
"file": SimpleUploadedFile("small.mp4", video_file.read(), "video/mp4"),
}
)
# Should redirect back to index
self.assertRedirects(response, reverse("wagtailvideos:index"))
# Check that the video was created
videos = Video.objects.filter(title=title)
self.assertEqual(videos.count(), 1)
# Test that extra fields were populated from post_save signal
video = videos.first()
self.assertTrue(video.thumbnail)
self.assertTrue(video.duration)
self.assertTrue(video.file_size)
self.assertTrue(video.width)
self.assertTrue(video.height)
# Test that it was placed in the root collection
root_collection = Collection.get_first_root_node()
self.assertEqual(video.collection, root_collection)
@patch("wagtailvideos.transcoders.ffmpeg.ffmpeg.installed")
def test_add_no_ffmpeg(self, ffmpeg_installed):
ffmpeg_installed.return_value = False
video_file = create_test_video_file()
title = "no_ffmpeg"
response = self.post(
{
"title": title,
"file": SimpleUploadedFile("small.mp4", video_file.read(), "video/mp4"),
}
)
# Should redirect back to index
self.assertRedirects(response, reverse("wagtailvideos:index"))
# Check video exists but has no thumb or duration
videos = Video.objects.filter(title=title)
self.assertEqual(videos.count(), 1)
video = videos.first()
self.assertFalse(video.thumbnail)
self.assertFalse(video.duration)
self.assertFalse(video.width)
self.assertFalse(video.height)
def test_add_no_file_selected(self):
response = self.post(
{
"title": "nothing here",
}
)
# Shouldn't redirect anywhere
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "wagtailvideos/videos/add.html")
# The form should have an error
self.assertFormError(
response.context["form"], "file", "This field is required."
)
@override_settings(WAGTAILVIDEOS_MAX_UPLOAD_SIZE=1)
def test_add_too_large_file(self):
video_file = create_test_video_file()
response = self.post(
{
"title": "Test video",
"file": SimpleUploadedFile("small.mp4", video_file.read(), "video/mp4"),
}
)
# Shouldn't redirect anywhere
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "wagtailvideos/videos/add.html")
# The form should have an error
self.assertFormError(
response.context["form"],
"file",
"This file is too big ({file_size}). Maximum filesize {max_file_size}.".format(
file_size=filesizeformat(video_file.size),
max_file_size=filesizeformat(1),
),
)
def test_add_too_long_filename(self):
video_file = create_test_video_file()
name = "a_very_long_filename_" + ("x" * 100) + ".mp4"
response = self.post(
{
"title": "Test video",
"file": SimpleUploadedFile(name, video_file.read(), "video/mp4"),
}
)
# Should be valid
self.assertEqual(response.status_code, 302)
video = Video.objects.get()
self.assertEqual(len(video.file.name), Video._meta.get_field("file").max_length)
def test_add_with_collections(self):
root_collection = Collection.get_first_root_node()
evil_plans_collection = root_collection.add_child(name="Evil plans")
response = self.post(
{
"title": "Test video",
"file": SimpleUploadedFile(
"small.mp4", create_test_video_file().read(), "video/mp4"
),
"collection": evil_plans_collection.id,
}
)
# Should redirect back to index
self.assertRedirects(response, reverse("wagtailvideos:index"))
# Check that the video was created
videos = Video.objects.filter(title="Test video")
self.assertEqual(videos.count(), 1)
# Test that it was placed in the Evil Plans collection
video = videos.first()
self.assertEqual(video.collection, evil_plans_collection)
class TestVideoEditView(TestCase, WagtailTestUtils):
def setUp(self):
self.login()
self.video = Video.objects.create(
title="Test video",
file=create_test_video_file(),
)
def get(self, params={}):
return self.client.get(
reverse("wagtailvideos:edit", args=(self.video.id,)), params
)
def post(self, post_data={}):
return self.client.post(
reverse("wagtailvideos:edit", args=(self.video.id,)), post_data
)
def test_simple(self):
response = self.get()
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "wagtailvideos/videos/edit.html")
# Ensure the form supports file uploads
self.assertContains(response, 'enctype="multipart/form-data"')
def test_usage_count(self):
response = self.get()
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "wagtailvideos/videos/edit.html")
self.assertContains(response, "Used 0 times")
expected_url = "/admin/videos/usage/%d/" % self.video.id
self.assertContains(response, expected_url)
def test_edit(self):
self.post(
{
"title": "Edited",
}
)
# Check that the video was edited
video = Video.objects.get(id=self.video.id)
self.assertEqual(video.title, "Edited")
self.assertEqual(self.video.file, video.file)
def test_edit_with_new_video_file(self):
# Change the file size of the video
self.video.file_size = 100000
self.video.save()
new_file = create_test_video_file()
self.post(
{
"title": "Edited",
"file": SimpleUploadedFile("new.mp4", new_file.read(), "video/mp4"),
}
)
# Check that the video file size changed (assume it changed to the correct value)
video = Video.objects.get(id=self.video.id)
self.assertNotEqual(video.file_size, 100000)
def test_with_missing_video_file(self):
self.video.file.delete(False)
response = self.get()
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "wagtailvideos/videos/edit.html")
self.assertContains(response, "The source video file could not be found")
class TestVideoDeleteView(TestCase, WagtailTestUtils):
def setUp(self):
self.login()
# Create an video to edit
self.video = Video.objects.create(
title="Test video",
file=create_test_video_file(),
)
def get(self, params={}):
return self.client.get(
reverse("wagtailvideos:delete", args=(self.video.id,)), params
)
def post(self, post_data={}):
return self.client.post(
reverse("wagtailvideos:delete", args=(self.video.id,)), post_data
)
def test_simple(self):
response = self.get()
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "wagtailvideos/videos/confirm_delete.html")
def test_delete(self):
# FIXME HACK Not sure why the test fails when no data is posted
response = self.post({"data": "data"})
# Should redirect back to index
self.assertRedirects(response, reverse("wagtailvideos:index"))
# Check that the video was deleted
videos = Video.objects.filter(title="Test video")
self.assertEqual(videos.count(), 0)
class TestVideoChooserView(TestCase, WagtailTestUtils):
def setUp(self):
self.login()
def get(self, params={}):
return self.client.get(reverse("wagtailvideos_chooser:choose"), params)
def test_simple(self):
response = self.get()
self.assertEqual(response.status_code, 200)
response_json = json.loads(response.content.decode())
self.assertEqual(response_json["step"], "choose")
def test_search(self):
response = self.get({"q": "Hello"})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context["search_query"], "Hello")
def test_pagination(self):
# page numbers in range should be accepted
response = self.get({"p": 1})
self.assertEqual(response.status_code, 200)
# page numbers out of range should return 404
response = self.get({"p": 9999})
self.assertEqual(response.status_code, 404)
def test_filter_by_tag(self):
for i in range(0, 10):
video = Video.objects.create(
title="Test video %d is even better than the last one" % i,
file=create_test_video_file(),
)
if i % 2 == 0:
video.tags.add("even")
response = self.get({"tag": "even"})
self.assertEqual(response.status_code, 200)
# Results should include videos tagged 'even'
self.assertContains(response, "Test video 2 is even better")
# Results should not include videos that just have 'even' in the title
self.assertNotContains(response, "Test video 3 is even better")
class TestVideoChooserChosenView(TestCase, WagtailTestUtils):
def setUp(self):
self.login()
# Create an video to edit
self.video = Video.objects.create(
title="Test video",
file=create_test_video_file(),
)
def get(self, params={}):
return self.client.get(
reverse("wagtailvideos_chooser:chosen", args=(self.video.id,)), params
)
def test_simple(self):
response = self.get()
self.assertEqual(response.status_code, 200)
response_json = json.loads(response.content.decode())
self.assertEqual(response_json["step"], "chosen")
class TestVideoChooserUploadView(TestCase, WagtailTestUtils):
def setUp(self):
self.login()
def get(self, params={}):
return self.client.get(reverse("wagtailvideos_chooser:create"), params)
def test_simple(self):
response = self.get()
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(
response, "wagtailadmin/generic/chooser/creation_form.html"
)
response_json = json.loads(response.content.decode())
self.assertEqual(response_json["step"], "reshow_creation_form")
def test_upload(self):
response = self.client.post(
reverse("wagtailvideos_chooser:create"),
{
"title": "Test video",
"file": SimpleUploadedFile(
"small.mp4", create_test_video_file().read(), "video/mp4"
),
},
)
# Check response
self.assertEqual(response.status_code, 200)
# Check that the video was created
videos = Video.objects.filter(title="Test video")
self.assertEqual(videos.count(), 1)
def test_upload_no_file_selected(self):
response = self.client.post(
reverse("wagtailvideos_chooser:create"),
{
"title": "Test video",
},
)
# Shouldn't redirect anywhere
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(
response, "wagtailadmin/generic/chooser/creation_form.html"
)
# The form should have an error
self.assertFormError(
response.context["form"], "file", "This field is required."
)
class TestVideoChooserUploadViewWithLimitedPermissions(TestCase, WagtailTestUtils):
def setUp(self):
add_video_permission = Permission.objects.get(
content_type__app_label="wagtailvideos", codename="add_video"
)
admin_permission = Permission.objects.get(
content_type__app_label="wagtailadmin", codename="access_admin"
)
root_collection = Collection.get_first_root_node()
self.evil_plans_collection = root_collection.add_child(name="Evil plans")
conspirators_group = Group.objects.create(name="Evil conspirators")
conspirators_group.permissions.add(admin_permission)
GroupCollectionPermission.objects.create(
group=conspirators_group,
collection=self.evil_plans_collection,
permission=add_video_permission,
)
user = get_user_model().objects.create_user(
username="moriarty", email="[email protected]", password="password"
)
user.groups.add(conspirators_group)
self.client.login(username="moriarty", password="password")
def test_get(self):
response = self.client.get(reverse("wagtailvideos_chooser:create"))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(
response, "wagtailadmin/generic/chooser/creation_form.html"
)
# user only has access to one collection, so no 'Collection' option
# is displayed on the form
self.assertNotContains(
response,
'<label class="w-field__label" for="id_collection" id="id_collection-label">',
)
def test_get_chooser(self):
response = self.client.get(reverse("wagtailvideos_chooser:choose"))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "wagtailvideos/chooser/chooser.html")
# user only has access to one collection, so no 'Collection' option
# is displayed on the form
self.assertNotContains(
response,
'<label class="w-field__label" for="id_collection" id="id_collection-label">',
)
class TestMultipleVideoUploader(TestCase, WagtailTestUtils):
"""
This tests the multiple video upload views located in wagtailvideos/views/multiple.py
"""
def setUp(self):
self.login()
# Create an video for running tests on
self.video = Video.objects.create(
title="Test video",
file=create_test_video_file(),
)
def test_add(self):
"""
This tests that the add view responds correctly on a GET request
"""
# Send request
response = self.client.get(reverse("wagtailvideos:add_multiple"))
# Check response
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "wagtailvideos/multiple/add.html")
def test_add_post(self):
"""
This tests that a POST request to the add view saves the video and returns an edit form
"""
response = self.client.post(
reverse("wagtailvideos:add_multiple"),
{
"files[]": SimpleUploadedFile(
"small.mp4", create_test_video_file().read(), "video/mp4"
),
},
HTTP_X_REQUESTED_WITH="XMLHttpRequest",
)
# Check response
self.assertEqual(response.status_code, 200)
self.assertEqual(response["Content-Type"], "application/json")
self.assertTemplateUsed(response, "wagtailvideos/multiple/edit_form.html")
# Check video
self.assertIn("video", response.context)
self.assertEqual(response.context["video"].title, "small.mp4")
self.assertTrue(response.context["video"].file_size)
# Check form
self.assertIn("form", response.context)
self.assertEqual(response.context["form"].initial["title"], "small.mp4")
# Check JSON
response_json = json.loads(response.content.decode())
self.assertIn("video_id", response_json)
self.assertIn("form", response_json)
self.assertIn("success", response_json)
self.assertEqual(response_json["video_id"], response.context["video"].id)
self.assertTrue(response_json["success"])
def test_add_post_badfile(self):
"""
This tests that the add view checks for a file when a user POSTs to it
"""
response = self.client.post(
reverse("wagtailvideos:add_multiple"),
{
"files[]": SimpleUploadedFile("small.mp4", b"This is not an video!"),
},
HTTP_X_REQUESTED_WITH="XMLHttpRequest",
)
# Check response
self.assertEqual(response.status_code, 200)
self.assertEqual(response["Content-Type"], "application/json")
# Check JSON
response_json = json.loads(response.content.decode())
self.assertNotIn("video_id", response_json)
self.assertNotIn("form", response_json)
self.assertIn("success", response_json)
self.assertIn("error_message", response_json)
self.assertFalse(response_json["success"])
self.assertIn("Not a valid video.", response_json["error_message"])
def test_edit_get(self):
"""
This tests that a GET request to the edit view returns a 405 "METHOD NOT ALLOWED" response
"""
# Send request
response = self.client.get(
reverse("wagtailvideos:edit_multiple", args=(self.video.id,))
)
# Check response
self.assertEqual(response.status_code, 405)
def test_edit_post(self):
"""
This tests that a POST request to the edit view edits the video
"""
# Send request
response = self.client.post(
reverse("wagtailvideos:edit_multiple", args=(self.video.id,)),
{
("video-%d-title" % self.video.id): "New title!",
("video-%d-tags" % self.video.id): "",
},
)
# Check response
self.assertEqual(response.status_code, 200)
self.assertEqual(response["Content-Type"], "application/json")
# Check JSON
response_json = json.loads(response.content.decode())
self.assertIn("video_id", response_json)
self.assertNotIn("form", response_json)
self.assertIn("success", response_json)
self.assertEqual(response_json["video_id"], self.video.id)
self.assertTrue(response_json["success"])
self.assertEqual(Video.objects.get(id=self.video.id).title, "New title!")
def test_edit_post_validation_error(self):
"""
This tests that a POST request to the edit page returns a json document with "success=False"
and a form with the validation error indicated
"""
# Send request
response = self.client.post(
reverse("wagtailvideos:edit_multiple", args=(self.video.id,)),
{
("video-%d-title" % self.video.id): "", # Required
("video-%d-tags" % self.video.id): "",
},
HTTP_X_REQUESTED_WITH="XMLHttpRequest",
)
# Check response
self.assertEqual(response.status_code, 200)
self.assertEqual(response["Content-Type"], "application/json")
self.assertTemplateUsed(response, "wagtailadmin/generic/multiple_upload/edit_form.html")
# Check that a form error was raised
self.assertFormError(
response.context["form"], "title", "This field is required."
)
# Check JSON
response_json = json.loads(response.content.decode())
self.assertIn("video_id", response_json)
self.assertIn("form", response_json)
self.assertIn("success", response_json)
self.assertEqual(response_json["video_id"], self.video.id)
self.assertFalse(response_json["success"])
def test_delete_get(self):
"""
This tests that a GET request to the delete view returns a 405 "METHOD NOT ALLOWED" response
"""
# Send request
response = self.client.get(
reverse("wagtailvideos:delete_multiple", args=(self.video.id,))
)
# Check response
self.assertEqual(response.status_code, 405)
def test_delete_post(self):
"""
This tests that a POST request to the delete view deletes the video
"""
# Send request
response = self.client.post(
reverse("wagtailvideos:delete_multiple", args=(self.video.id,)),
HTTP_X_REQUESTED_WITH="XMLHttpRequest",
)
# Check response
self.assertEqual(response.status_code, 200)
self.assertEqual(response["Content-Type"], "application/json")
# Make sure the video is deleted
self.assertFalse(Video.objects.filter(id=self.video.id).exists())
# Check JSON
response_json = json.loads(response.content.decode())
self.assertIn("video_id", response_json)
self.assertIn("success", response_json)
self.assertEqual(response_json["video_id"], self.video.id)
self.assertTrue(response_json["success"])