Esempio n. 1
0
def test_inbox_create_audio(factories, mocker):
    activity = factories["federation.Activity"]()
    upload = factories["music.Upload"](bitrate=42, duration=55)
    payload = {
        "type": "Create",
        "actor": upload.library.actor.fid,
        "object": serializers.UploadSerializer(upload).data,
    }
    library = upload.library
    upload.delete()
    init = mocker.spy(serializers.UploadSerializer, "__init__")
    save = mocker.spy(serializers.UploadSerializer, "save")
    assert library.uploads.count() == 0
    result = routes.inbox_create_audio(
        payload,
        context={
            "actor": library.actor,
            "raise_exception": True,
            "activity": activity
        },
    )
    assert library.uploads.count() == 1
    assert result == {
        "object": library.uploads.latest("id"),
        "target": library
    }

    assert init.call_count == 1
    args = init.call_args
    assert args[1]["data"] == payload["object"]
    assert args[1]["context"] == {"activity": activity, "actor": library.actor}
    assert save.call_count == 1
Esempio n. 2
0
def test_activity_pub_upload_serializer_validtes_library_actor(
        factories, mocker):
    library = factories["music.Library"]()
    usurpator = factories["federation.Actor"]()

    serializer = serializers.UploadSerializer(data={},
                                              context={"actor": usurpator})

    with pytest.raises(serializers.serializers.ValidationError):
        serializer.validate_library(library.fid)
Esempio n. 3
0
def test_music_upload_detail(factories, api_client, privacy_level, expected):
    upload = factories["music.Upload"](
        library__privacy_level=privacy_level,
        library__actor__local=True,
        import_status="finished",
    )
    url = reverse("federation:music:uploads-detail",
                  kwargs={"uuid": upload.uuid})
    response = api_client.get(url)

    assert response.status_code == expected
    if expected == 200:
        assert response.data == serializers.UploadSerializer(upload).data
Esempio n. 4
0
def test_outbox_create_audio(factories, mocker):
    upload = factories["music.Upload"]()
    activity = list(routes.outbox_create_audio({"upload": upload}))[0]
    serializer = serializers.ActivitySerializer({
        "type":
        "Create",
        "object":
        serializers.UploadSerializer(upload).data,
        "actor":
        upload.library.actor.fid,
    })
    expected = serializer.data
    expected["to"] = [{"type": "followers", "target": upload.library}]

    assert dict(activity["payload"]) == dict(expected)
    assert activity["actor"] == upload.library.actor
    assert activity["target"] == upload.library
    assert activity["object"] == upload
Esempio n. 5
0
def test_activity_pub_audio_serializer_to_ap(factories):
    upload = factories["music.Upload"](mimetype="audio/mp3",
                                       bitrate=42,
                                       duration=43,
                                       size=44)
    expected = {
        "@context":
        serializers.AP_CONTEXT,
        "type":
        "Audio",
        "id":
        upload.fid,
        "name":
        upload.track.full_name,
        "published":
        upload.creation_date.isoformat(),
        "updated":
        upload.modification_date.isoformat(),
        "duration":
        upload.duration,
        "bitrate":
        upload.bitrate,
        "size":
        upload.size,
        "url": {
            "href": utils.full_url(upload.listen_url),
            "type": "Link",
            "mediaType": "audio/mp3",
        },
        "library":
        upload.library.fid,
        "track":
        serializers.TrackSerializer(upload.track,
                                    context={
                                        "include_ap_context": False
                                    }).data,
    }

    serializer = serializers.UploadSerializer(upload)

    assert serializer.data == expected
Esempio n. 6
0
def test_activity_pub_upload_serializer_from_ap(factories, mocker, r_mock):
    activity = factories["federation.Activity"]()
    library = factories["music.Library"]()

    published = timezone.now()
    updated = timezone.now()
    released = timezone.now().date()
    data = {
        "@context": serializers.AP_CONTEXT,
        "type": "Audio",
        "id": "https://track.file",
        "name": "Ignored",
        "published": published.isoformat(),
        "updated": updated.isoformat(),
        "duration": 43,
        "bitrate": 42,
        "size": 66,
        "url": {
            "href": "https://audio.file",
            "type": "Link",
            "mediaType": "audio/mp3"
        },
        "library": library.fid,
        "track": {
            "type":
            "Track",
            "id":
            "http://hello.track",
            "published":
            published.isoformat(),
            "musicbrainzId":
            str(uuid.uuid4()),
            "name":
            "Black in back",
            "position":
            5,
            "album": {
                "type":
                "Album",
                "id":
                "http://hello.album",
                "name":
                "Purple album",
                "musicbrainzId":
                str(uuid.uuid4()),
                "published":
                published.isoformat(),
                "released":
                released.isoformat(),
                "cover": {
                    "type": "Link",
                    "href": "https://cover.image/test.png",
                    "mediaType": "image/png",
                },
                "artists": [{
                    "type": "Artist",
                    "id": "http://hello.artist",
                    "name": "John Smith",
                    "musicbrainzId": str(uuid.uuid4()),
                    "published": published.isoformat(),
                }],
            },
            "artists": [{
                "type": "Artist",
                "id": "http://hello.trackartist",
                "name": "Bob Smith",
                "musicbrainzId": str(uuid.uuid4()),
                "published": published.isoformat(),
            }],
        },
    }
    r_mock.get(data["track"]["album"]["cover"]["href"],
               body=io.BytesIO(b"coucou"))

    serializer = serializers.UploadSerializer(data=data,
                                              context={"activity": activity})
    assert serializer.is_valid(raise_exception=True)
    track_create = mocker.spy(serializers.TrackSerializer, "create")
    upload = serializer.save()

    assert upload.track.from_activity == activity
    assert upload.from_activity == activity
    assert track_create.call_count == 1
    assert upload.fid == data["id"]
    assert upload.track.fid == data["track"]["id"]
    assert upload.duration == data["duration"]
    assert upload.size == data["size"]
    assert upload.bitrate == data["bitrate"]
    assert upload.source == data["url"]["href"]
    assert upload.mimetype == data["url"]["mediaType"]
    assert upload.creation_date == published
    assert upload.import_status == "finished"
    assert upload.modification_date == updated