Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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_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_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 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_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)
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'])