def test_simplevideo_serializer(): """ Test for SimpleVideoSerializer """ video = factories.VideoFactory() video_files = [factories.VideoFileFactory(video=video)] video_thumbnails = [factories.VideoThumbnailFactory(video=video)] expected = { "key": video.hexkey, "created_at": DateTimeField().to_representation(video.created_at), "title": video.title, "description": video.description, "videofile_set": serializers.VideoFileSerializer(video_files, many=True).data, "videosubtitle_set": [], "is_public": video.is_public, "is_private": video.is_private, "view_lists": [], "collection_view_lists": [], "videothumbnail_set": serializers.VideoThumbnailSerializer(video_thumbnails, many=True).data, "status": video.status, "collection_key": video.collection.hexkey, "cloudfront_url": "", } assert serializers.SimpleVideoSerializer(video).data == expected
def test_video_serializer_validate_title(mocker): """ Test that VideoSerializer raises if title is blank """ mocker.patch("ui.serializers.get_moira_client") video = factories.VideoFactory() video.title = "" serialized_data = serializers.VideoSerializer(video).data with pytest.raises(ValidationError) as exc: serializers.VideoSerializer(data=serialized_data).is_valid( raise_exception=True) assert exc.value.detail == {"title": ["This field may not be blank."]}
def test_video_serializer(youtube, public): """ Test for VideoSerializer """ video = factories.VideoFactory() factories.VideoFileFactory(video=video) factories.VideoThumbnailFactory(video=video) video.is_public = public if youtube and public: factories.YouTubeVideoFactory(video=video) expected = get_expected_result(video) expected["youtube_id"] = video.youtube_id if youtube and public else None assert serializers.VideoSerializer(video).data == expected
def test_get_video_analytics(mocker): """Test that video analytics data is returned""" mock_get_ga_client = mocker.patch("ui.utils.get_google_analytics_client") mock_generate_ga_query = mocker.patch( "ui.utils.generate_google_analytics_query") mock_parse_ga_response = mocker.patch( "ui.utils.parse_google_analytics_response") video = factories.VideoFactory() result = get_video_analytics(video) expected_ga_client = mock_get_ga_client.return_value expected_batchGet_call = expected_ga_client.reports.return_value.batchGet expected_ga_query = mock_generate_ga_query.return_value assert expected_batchGet_call.called_once_with(body=expected_ga_query) assert mock_parse_ga_response.called_once_with( expected_batchGet_call.return_value) assert result is mock_parse_ga_response.return_value
def test_collection_list_serializer(): """ Test for CollectionListSerializer """ collection = factories.CollectionFactory() _ = [factories.VideoFactory(collection=collection) for _ in range(3)] expected = { "key": collection.hexkey, "created_at": DateTimeField().to_representation(collection.created_at), "title": collection.title, "description": collection.description, "view_lists": [], "admin_lists": [], "video_count": collection.videos.count(), "edx_course_id": collection.edx_course_id, } assert serializers.CollectionListSerializer(collection).data == expected
def test_generate_google_analytics_query_success(settings, multiangle): """Test that expected query is generated.""" video = factories.VideoFactory(multiangle=multiangle) ga_view_id = "some_view_id" settings.GA_VIEW_ID = ga_view_id ga_dimension_camera = "some_camera_dimension" settings.GA_DIMENSION_CAMERA = ga_dimension_camera actual_query = generate_google_analytics_query(video) expected_dimensions = [{"name": "ga:eventAction"}] if multiangle: expected_dimensions.append({"name": "ga:" + ga_dimension_camera}) expected_query = { "reportRequests": [ { "viewId": ga_view_id, "dateRanges": [{ "startDate": "2005-01-01", "endDate": "9999-01-01", }], "metrics": [{ "expression": "ga:totalEvents" }], "dimensions": expected_dimensions, "dimensionFilterClauses": [ { "filters": [ { "dimensionName": "ga:eventLabel", "operator": "EXACT", "expressions": [video.hexkey.capitalize()], }, ], }, ], }, ], } assert actual_query == expected_query
def test_collection_serializer(): """ Test for CollectionSerializer """ collection = factories.CollectionFactory() videos = [factories.VideoFactory(collection=collection) for _ in range(3)] expected = { "key": collection.hexkey, "created_at": DateTimeField().to_representation(collection.created_at), "title": collection.title, "description": collection.description, "videos": serializers.SimpleVideoSerializer(videos, many=True).data, "video_count": len(videos), "view_lists": [], "admin_lists": [], "is_logged_in_only": False, "is_admin": False, "edx_course_id": collection.edx_course_id, } expected["videos"].sort(key=lambda x: x["key"]) serialized_data = serializers.CollectionSerializer(collection).data serialized_data["videos"].sort(key=lambda x: x["key"]) assert serialized_data == expected
def test_video_serializer_with_sharing_url(mocker, has_permission, allow_share_openedx, hls): """ Test for VideoSerializer for sharing cloudfront url """ mocked_admin_permission = mocker.patch( "ui.permissions.has_admin_permission", return_value=has_permission) mocked_request = mocker.MagicMock() video = factories.VideoFactory() factories.VideoFileFactory(video=video, hls=hls) factories.VideoThumbnailFactory(video=video) video.collection.allow_share_openedx = allow_share_openedx video.is_public = True expected = get_expected_result(video) expected["cloudfront_url"] = (video.videofile_set.filter( encoding=EncodingNames.HLS).first().cloudfront_url if allow_share_openedx and hls and has_permission else "") assert (serializers.VideoSerializer(video, context={ "request": mocked_request }).data == expected) mocked_admin_permission.assert_called_with(video.collection, mocked_request)
def video(): """ Returns an instance of Video """ return factories.VideoFactory()