def test_upload_owner_serializer(factories, to_api_date):
    upload = factories["music.Upload"](
        import_status="success",
        import_details={"hello": "world"},
        import_metadata={"import": "metadata"},
        import_reference="ref",
        metadata={"test": "metadata"},
        source="upload://test",
    )

    expected = {
        "uuid": str(upload.uuid),
        "filename": upload.filename,
        "track": serializers.TrackSerializer(upload.track).data,
        "duration": upload.duration,
        "mimetype": upload.mimetype,
        "bitrate": upload.bitrate,
        "size": upload.size,
        "library": serializers.LibraryForOwnerSerializer(upload.library).data,
        "creation_date": upload.creation_date.isoformat().split("+")[0] + "Z",
        "metadata": {"test": "metadata"},
        "import_metadata": {"import": "metadata"},
        "import_date": None,
        "import_status": "success",
        "import_details": {"hello": "world"},
        "source": "upload://test",
        "import_reference": "ref",
    }
    serializer = serializers.UploadForOwnerSerializer(upload)
    assert serializer.data == expected
def test_user_cannot_bind_file_to_a_not_owned_library(factories):
    user = factories["users.User"]()
    library = factories["music.Library"]()

    s = serializers.UploadForOwnerSerializer(
        data={"library": library.uuid, "source": "upload://test"},
        context={"user": user},
    )
    assert s.is_valid() is False
    assert "library" in s.errors
Exemple #3
0
def test_upload_with_not_owned_library_fails(factories, uploaded_audio_file):
    library = factories["music.Library"]()
    user = factories["users.User"]()
    data = {
        "library": library.uuid,
        "audio_file": uploaded_audio_file,
    }
    serializer = serializers.UploadForOwnerSerializer(
        data=data,
        context={"user": user},
    )
    assert serializer.is_valid() is False
    assert "library" in serializer.errors
Exemple #4
0
def test_upload_with_not_owned_channel_fails(factories, uploaded_audio_file):
    channel = factories["audio.Channel"]()
    user = factories["users.User"]()
    data = {
        "channel": channel.uuid,
        "audio_file": uploaded_audio_file,
    }
    serializer = serializers.UploadForOwnerSerializer(
        data=data,
        context={"user": user},
    )
    assert serializer.is_valid() is False
    assert "channel" in serializer.errors
def test_user_can_create_file_in_own_library(factories, uploaded_audio_file):
    user = factories["users.User"]()
    library = factories["music.Library"](actor__user=user)
    s = serializers.UploadForOwnerSerializer(
        data={
            "library": library.uuid,
            "source": "upload://test",
            "audio_file": uploaded_audio_file,
        },
        context={"user": user},
    )
    assert s.is_valid(raise_exception=True) is True
    upload = s.save()

    assert upload.library == library
Exemple #6
0
def test_upload_requires_library_or_channel(factories, uploaded_audio_file):
    user = factories["users.User"]()
    data = {
        "audio_file": uploaded_audio_file,
    }
    serializer = serializers.UploadForOwnerSerializer(
        data=data,
        context={"user": user},
    )

    with pytest.raises(
            serializers.serializers.ValidationError,
            match=r"You need to specify a channel or a library",
    ):
        serializer.is_valid(raise_exception=True)
Exemple #7
0
def test_upload_with_channel(factories, uploaded_audio_file):
    channel = factories["audio.Channel"](attributed_to__local=True)
    user = channel.attributed_to.user
    data = {
        "channel": channel.uuid,
        "audio_file": uploaded_audio_file,
        "import_status": "draft",
    }
    serializer = serializers.UploadForOwnerSerializer(
        data=data,
        context={"user": user},
    )
    assert serializer.is_valid(raise_exception=True) is True
    upload = serializer.save()

    assert upload.library == channel.library
Exemple #8
0
def test_upload_with_channel_validates_import_metadata(factories,
                                                       uploaded_audio_file):
    channel = factories["audio.Channel"](attributed_to__local=True)
    user = channel.attributed_to.user
    data = {
        "channel": channel.uuid,
        "audio_file": uploaded_audio_file,
        "import_metadata": {
            "title": None
        },
    }
    serializer = serializers.UploadForOwnerSerializer(
        data=data,
        context={"user": user},
    )
    with pytest.raises(serializers.serializers.ValidationError):
        assert serializer.is_valid(raise_exception=True)
Exemple #9
0
def test_create_file_checks_for_user_quota(factories, preferences,
                                           uploaded_audio_file, mocker):
    mocker.patch(
        "funkwhale_api.users.models.User.get_quota_status",
        return_value={"remaining": 0},
    )
    user = factories["users.User"]()
    library = factories["music.Library"](actor__user=user)
    s = serializers.UploadForOwnerSerializer(
        data={
            "library": library.uuid,
            "source": "upload://test",
            "audio_file": uploaded_audio_file,
        },
        context={"user": user},
    )
    assert s.is_valid() is False
    assert s.errors["non_field_errors"] == ["upload_quota_reached"]
Exemple #10
0
def test_upload_with_channel_keeps_import_metadata(factories,
                                                   uploaded_audio_file):
    channel = factories["audio.Channel"](attributed_to__local=True)
    user = channel.attributed_to.user
    data = {
        "channel": channel.uuid,
        "audio_file": uploaded_audio_file,
        "import_metadata": {
            "title": "hello"
        },
    }
    serializer = serializers.UploadForOwnerSerializer(
        data=data,
        context={"user": user},
    )
    assert serializer.is_valid(raise_exception=True) is True
    upload = serializer.save()

    assert upload.import_metadata == data["import_metadata"]
Exemple #11
0
def test_upload_requires_library_or_channel_but_not_both(
        factories, uploaded_audio_file):
    channel = factories["audio.Channel"](attributed_to__local=True)
    library = channel.library
    user = channel.attributed_to.user
    data = {
        "audio_file": uploaded_audio_file,
        "library": library.uuid,
        "channel": channel.uuid,
    }
    serializer = serializers.UploadForOwnerSerializer(
        data=data,
        context={"user": user},
    )
    with pytest.raises(
            serializers.serializers.ValidationError,
            match=r"You may specify a channel or a library, not both",
    ):
        serializer.is_valid(raise_exception=True)
def test_upload_import_status_updated_broadcast(factories, mocker):
    group_send = mocker.patch("funkwhale_api.common.channels.group_send")
    user = factories["users.User"]()
    upload = factories["music.Upload"](import_status="finished",
                                       library__actor__user=user)
    signals.upload_import_status_updated.send(sender=None,
                                              upload=upload,
                                              old_status="pending",
                                              new_status="finished")
    group_send.assert_called_once_with(
        "user.{}.imports".format(user.pk),
        {
            "type": "event.send",
            "text": "",
            "data": {
                "type": "import.status_updated",
                "old_status": "pending",
                "new_status": "finished",
                "upload": serializers.UploadForOwnerSerializer(upload).data,
            },
        },
    )