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) # Test that it was placed in the root collection root_collection = Collection.get_first_root_node() self.assertEqual(video.collection, root_collection)
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 setUp(self): self.login() self.video = Video.objects.create( title="Test video", file=create_test_video_file(), )
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_upload(self): response = self.client.post(reverse('wagtailvideos:chooser_upload'), { '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_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_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_pagination_after_upload_form_error(self): for i in range(0, 20): Video.objects.create( title="Test video %d" % i, file=create_test_video_file(), ) response = self.client.post(reverse('wagtailvideos:chooser_upload'), { 'title': "Test video", }) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'wagtailvideos/chooser/chooser.html') # The re-rendered video chooser listing should be paginated self.assertContains(response, "Page 1 of ") self.assertEqual(12, len(response.context['videos']))
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")
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() response = self.post({ 'title': "Edited", 'file': SimpleUploadedFile('new.mp4', new_file.read(), "video/mp4"), }) # Should redirect back to index self.assertRedirects(response, reverse('wagtailvideos:index')) # 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_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)
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, '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_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)
def setUp(self): self.video = Video.objects.create( title="Test Video", file=create_test_video_file() )
def setUp(self): super().setUp() self.root_page = Page.objects.get(pk=1) self.video = Video.objects.create(title="Test Video", file=create_test_video_file())