def test_upload_subtitle_to_s3(mocker, video, file_object, replace, s3error): """ Test that a VideoSubtitle object is returned after a .vtt upload to S3 """ mocker.patch("cloudsync.api.boto3") mock_s3delete = mocker.patch( "ui.models.VideoS3.delete_from_s3", side_effect=(None if not s3error else ClientError( error_response={ "Job": { "Id": "1498220566931-qtmtcu", "Status": "Error" }, "Error": { "Code": 200, "Message": "FAIL" }, }, operation_name="DeleteObject", )), ) if replace: VideoSubtitleFactory.create(video=video) subtitle_data = { "video": video.hexkey, "language": "en", "filename": file_object.name, } subtitle = upload_subtitle_to_s3(subtitle_data, file_object.data) assert mock_s3delete.call_count == (1 if replace else 0) assert subtitle.filename == file_object.name assert subtitle.video == video assert subtitle.language == "en"
def test_remove_youtube_caption(mocker, public_video): """ Test that the upload_youtube_caption task calls YouTubeApi.upload_caption with correct arguments, and only for language captions that actually exist on Youtube """ mock_delete = mocker.patch("cloudsync.tasks.YouTubeApi.delete_caption") mocker.patch( "cloudsync.tasks.YouTubeApi.list_captions", return_value={ "fr": "foo", "en": "bar" }, ) YouTubeVideoFactory(video=public_video) VideoSubtitleFactory(video=public_video, language="en") VideoSubtitleFactory(video=public_video, language="fr") remove_youtube_caption(public_video.id, "fr") remove_youtube_caption(public_video.id, "zh") mock_delete.assert_called_once_with("foo")
def test_upload_youtube_caption(mocker, public_video): """ Test that the upload_youtube_caption task calls YouTubeApi.upload_caption with correct arguments """ mocker.patch("cloudsync.tasks.YouTubeApi.upload_video") mock_uploader = mocker.patch("cloudsync.tasks.YouTubeApi.upload_caption") subtitle = VideoSubtitleFactory(video=public_video) yt_video = YouTubeVideoFactory(video=public_video) upload_youtube_caption(subtitle.id) mock_uploader.assert_called_once_with(subtitle, yt_video.id)
def test_upload_caption_calls_insert(mocker): """ Test that the upload_caption task calls insert_caption for a YouTube video if no caption for that language exists """ subtitle = VideoSubtitleFactory() caption_id = "foo" caption_response = {"id": caption_id} mocker.patch("cloudsync.youtube.YouTubeApi.list_captions", return_value={}) youtube_mocker = mocker.patch("cloudsync.youtube.build") youtube_mocker( ).captions.return_value.insert.return_value.next_chunk.return_value = ( None, caption_response, ) response = YouTubeApi().upload_caption(subtitle, caption_id) assert response == caption_response
def test_update_youtube_statuses_failed(mocker): """ Test that the correct number of YouTubeVideo objects have their statuses updated to FAILED and no captions are uploaded. """ mock_uploader = mocker.patch("cloudsync.tasks.YouTubeApi.upload_caption") mocker.patch("cloudsync.tasks.YouTubeApi.video_status", return_value=YouTubeStatus.FAILED) processing_videos = YouTubeVideoFactory.create_batch( 2, status=YouTubeStatus.UPLOADED) for yt_video in processing_videos: VideoSubtitleFactory(video=yt_video.video) update_youtube_statuses() assert mock_uploader.call_count == 0 assert YouTubeVideo.objects.filter( status=YouTubeStatus.FAILED).count() == 2
def test_youtube_video_permissions_signal(mocker): """ Tests that a video's public permissions are removed if it's subtitle is deleted """ mock_delete_video = mocker.patch("ui.signals.remove_youtube_video.delay") mock_delete_caption = mocker.patch("ui.signals.remove_youtube_caption.delay") mocker.patch("ui.models.VideoSubtitle.delete_from_s3") video = VideoFactory(is_public=True) YouTubeVideoFactory(video=video) VideoSubtitleFactory(video=video) VideoSubtitleFactory(video=video, language="fr") video.videosubtitle_set.get(language="fr").delete() # video's public status should not be changed as long as 1 subtitle still exists assert video.is_public is True assert mock_delete_caption.call_count == 1 video.videosubtitle_set.first().delete() # If no subtitles exists, video should be made non-public and deleted from youtube assert mock_delete_video.call_count == 1 assert not video.is_public caption = VideoSubtitleFactory(video=video) mock_video_save = mocker.patch("ui.models.Video.save") caption.delete() # If video is not public, no change to it should be saved after a caption is deleted. assert mock_video_save.call_count == 0
def videosubtitle(): """Fixture to create a video subtitle""" return VideoSubtitleFactory()
def test_video_subtitle_language(): """ Tests that the correct language name for a code is returned""" assert VideoSubtitleFactory(language="en").language_name == "English"