Ejemplo n.º 1
0
def test_collection_serializer_validate_title(mocker):
    """
    Test that we can't save a blank title
    """
    mocker.patch("ui.serializers.get_moira_client")
    collection = factories.CollectionFactory(title="")
    serialized_data = serializers.CollectionSerializer(collection).data
    with pytest.raises(ValidationError) as exc:
        serializers.CollectionSerializer(data=serialized_data).is_valid(
            raise_exception=True)
    assert exc.value.detail == {"title": ["This field may not be blank."]}
Ejemplo n.º 2
0
def test_collection_serializer_validation_fake_view_lists(mocker):
    """
    Test for CollectionSerializer's viewable moira lists validation for fake lists
    """
    mock_client = mocker.patch("ui.serializers.get_moira_client")
    mock_client().list_exists.return_value = False
    collection = factories.CollectionFactory(
        view_lists=[factories.MoiraListFactory()])
    serialized_data = serializers.CollectionSerializer(collection).data
    with pytest.raises(ValidationError) as exc:
        serializers.CollectionSerializer(data=serialized_data).is_valid(
            raise_exception=True)
    assert exc.match("Moira list does not exist: {}".format(
        collection.view_lists.first().name))
Ejemplo n.º 3
0
def test_collection_serializer_admin_flag(mocker, has_permission):
    """
    Test that the is_admin flag returns an expected value based on a user's admin permission
    """
    mocked_admin_permission = mocker.patch(
        "ui.permissions.has_admin_permission", return_value=has_permission)
    mocked_request = mocker.MagicMock()
    collection = factories.CollectionFactory()
    serialized_data = serializers.CollectionSerializer(
        collection, context=dict(request=mocked_request)).data
    mocked_admin_permission.assert_called_with(collection, mocked_request)
    assert serialized_data["is_admin"] is has_permission
Ejemplo n.º 4
0
def test_collection_serializer_private_video(mocker, is_admin, is_superuser):
    """
    Test that a private video is not included in the serializer unless the user is super/admin
    """
    has_permission = is_superuser or is_admin
    mocker.patch("ui.permissions.has_admin_permission",
                 return_value=has_permission)
    mocked_request = mocker.MagicMock()
    mocked_request.user = UserFactory.create(is_superuser=is_superuser)

    mocker.patch("ui.serializers.has_common_lists", return_value=is_admin)

    collection = factories.CollectionFactory(
        admin_lists=[MoiraListFactory.create()])
    VideoFactory.create(collection=collection)
    VideoFactory.create(is_private=True, collection=collection)

    serialized_data = serializers.CollectionSerializer(
        collection, context=dict(request=mocked_request)).data

    assert len(serialized_data["videos"]) == (2 if has_permission else 1)
Ejemplo n.º 5
0
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