Ejemplo n.º 1
0
def test_collection_for_for_owner():
    """
    Tests the for_owner Collection method
    """
    owner = UserFactory()
    collections = [CollectionFactory(owner=owner) for _ in range(5)]
    extra_collection = CollectionFactory()
    qset = Collection.for_owner(owner)
    assert qset.count() == 5
    assert list(qset) == sorted(collections,
                                key=lambda x: x.created_at,
                                reverse=True)
    assert extra_collection not in qset
Ejemplo n.º 2
0
def alt_moira_data():
    """
    Fixture for tests requiring a moira list, collection, and user
    """
    alt_moira_list = MoiraListFactory()
    alt_collection = CollectionFactory()
    alt_user = factories.UserFactory()
    return SimpleNamespace(moira_list=alt_moira_list,
                           collection=alt_collection,
                           user=alt_user)
Ejemplo n.º 3
0
def test_edx_video_file_signal(mocker):
    """When a VideoFile is created with the right properties, a task to add the video to edX should be called"""
    patched_edx_task = mocker.patch("ui.signals.ovs_tasks.post_hls_to_edx.delay")

    collections = CollectionFactory.create_batch(
        3, edx_course_id=factory.Iterator(["courseid", None, "courseid"])
    )
    video_files = VideoFileFactory.create_batch(
        3,
        encoding=factory.Iterator(
            [EncodingNames.HLS, EncodingNames.HLS, "other-encoding"]
        ),
        video__collection=factory.Iterator(collections),
    )
    patched_edx_task.assert_called_once_with(video_files[0].id)
Ejemplo n.º 4
0
def test_video_ordering():
    """
    Tests that videos are sorted by reverse creation date or forward custom order
    """
    collection = CollectionFactory.create()
    VideoFactory.create_batch(10, collection=collection)
    # Should be sorted by reverse creation date
    videos = collection.videos.all()
    for (idx, video) in enumerate(videos):
        if idx > len(videos) - 1:
            assert video.created_at >= videos[idx + 1].created_at
        videos[idx].custom_order = len(videos) - idx - 1
        videos[idx].save()
    # Should be sorted by custom_order
    resorted_videos = Collection.objects.get(id=collection.id).videos.all()
    for (idx, video) in enumerate(resorted_videos):
        assert video.custom_order == idx
Ejemplo n.º 5
0
def test_schedule_retranscodes(mocker, mock_transcode,
                               mock_successful_encode_job, mocked_celery):
    """
    Test that schedule_retranscodes triggers retranscode_video tasks for each scheduled video
    """
    retranscode_video_mock = mocker.patch("cloudsync.tasks.retranscode_video",
                                          autospec=True)
    collection = CollectionFactory.create(schedule_retranscode=True)
    scheduled_videos = VideoFactory.create_batch(5, schedule_retranscode=True)
    VideoFactory.create_batch(3, schedule_retranscode=False)
    with pytest.raises(mocked_celery.replace_exception_class):
        schedule_retranscodes.delay()
    assert mocked_celery.group.call_count == 1
    assert retranscode_video_mock.si.call_count == len(scheduled_videos)
    for video in scheduled_videos:
        retranscode_video_mock.si.assert_any_call(video.id)
    assert Collection.objects.get(
        id=collection.id).schedule_retranscode is False
Ejemplo n.º 6
0
def test_process_dropbox_data_empty_link_list(mocker):
    """
    Tests that the process_dropbox_data in case the collection does not exist
    """
    mocked_chain = mocker.patch("ui.api.chain")
    mocked_stream_to_s3 = mocker.patch("cloudsync.tasks.stream_to_s3")
    mocked_transcode_from_s3 = mocker.patch("cloudsync.tasks.transcode_from_s3")
    collection = CollectionFactory()

    assert (
        api.process_dropbox_data(
            {
                "collection": collection.hexkey,
                "files": [],
            }
        )
        == {}
    )
    assert mocked_chain.call_count == 0
    assert mocked_stream_to_s3.s.call_count == 0
    assert mocked_transcode_from_s3.si.call_count == 0
Ejemplo n.º 7
0
def test_video_sources_youtube(youtube_status, is_public, stream_source):
    """ Tests that a public video can play from cloudfront if a youtube video does not exist """
    public_video = VideoFactory.create(
        key="8494dafc-3665-4960-8e00-9790574ec93a",
        is_public=is_public,
        collection=CollectionFactory(stream_source=stream_source),
    )
    videofiles = [
        VideoFileFactory(video=public_video,
                         s3_object_key="hd.mp4",
                         encoding=EncodingNames.HD),
    ]
    if youtube_status is not None:
        YouTubeVideoFactory.create(video=public_video, status=youtube_status)
    if (youtube_status == YouTubeStatus.PROCESSED and is_public
            and stream_source == StreamSource.YOUTUBE):
        assert public_video.sources == []
    else:
        assert public_video.sources == [{
            "src": videofiles[0].cloudfront_url,
            "label": EncodingNames.HD,
            "type": "video/mp4",
        }]
Ejemplo n.º 8
0
def test_process_dropbox_data_happy_path(mocker):
    """
    Tests that the process_dropbox_data in case everything is fine
    """
    mocked_chain = mocker.patch("ui.api.chain")
    mocked_stream_to_s3 = mocker.patch("cloudsync.tasks.stream_to_s3")
    mocked_transcode_from_s3 = mocker.patch("cloudsync.tasks.transcode_from_s3")
    collection = CollectionFactory()

    input_data = {
        "collection": collection.hexkey,
        "files": [
            {"name": name, "link": "http://example.com/{}".format(name)}
            for name in (
                "foo",
                "bar",
            )
        ],
    }

    results = api.process_dropbox_data(input_data)
    assert len(results) == 2
    assert mocked_chain.call_count == 2
    for key, data in results.items():
        qset = models.Video.objects.filter(key=key)
        assert qset.exists()
        assert qset.count() == 1
        video = qset.first()
        assert video.collection == collection
        assert video.title == data["title"]
        assert video.get_s3_key() == data["s3key"]
        # checking that the functions in the chain have been called
        mocked_stream_to_s3.s.assert_any_call(video.id)
        mocked_transcode_from_s3.si.assert_any_call(video.id)
        mocked_chain.assert_any_call(
            mocked_stream_to_s3.s(video.id), mocked_transcode_from_s3.si(video.id)
        )
Ejemplo n.º 9
0
def test_upload_youtube_videos(mocker, source, max_uploads):
    """
    Test that the upload_youtube_videos task calls YouTubeApi.upload_video
    & creates a YoutubeVideo object for each public video, up to the max daily limit
    """
    settings.YT_UPLOAD_LIMIT = max_uploads
    private_videos = VideoFactory.create_batch(2,
                                               is_public=False,
                                               status=VideoStatus.COMPLETE)
    VideoFactory.create_batch(
        3,
        collection=CollectionFactory(stream_source=source),
        is_public=True,
        status=VideoStatus.COMPLETE,
    )
    mock_uploader = mocker.patch(
        "cloudsync.tasks.YouTubeApi.upload_video",
        return_value={
            "id":
            "".join([random.choice(string.ascii_lowercase) for n in range(8)]),
            "status": {
                "uploadStatus": "uploaded"
            },
        },
    )
    upload_youtube_videos()
    assert mock_uploader.call_count == (min(
        3, max_uploads) if source != StreamSource.CLOUDFRONT else 0)
    for video in Video.objects.filter(
            is_public=True).order_by("-created_at")[:settings.YT_UPLOAD_LIMIT]:
        if video.collection.stream_source != StreamSource.CLOUDFRONT:
            assert YouTubeVideo.objects.filter(video=video).first() is not None
        else:
            assert YouTubeVideo.objects.filter(video=video).first() is None
    for video in private_videos:
        assert YouTubeVideo.objects.filter(video=video).first() is None
Ejemplo n.º 10
0
def test_collection_hexkey():
    """
    Tests the collection hexkey property method
    """
    collection = CollectionFactory()
    assert collection.hexkey == collection.key.hex