def test_activity_pub_track_serializer_to_ap(factories): track = factories["music.Track"]() expected = { "@context": serializers.AP_CONTEXT, "published": track.creation_date.isoformat(), "type": "Track", "musicbrainzId": track.mbid, "id": track.fid, "name": track.title, "position": track.position, "artists": [ serializers.ArtistSerializer(track.artist, context={ "include_ap_context": False }).data ], "album": serializers.AlbumSerializer(track.album, context={ "include_ap_context": False }).data, } serializer = serializers.TrackSerializer(track) assert serializer.data == expected
def test_outbox_update_track(factories): track = factories["music.Track"]() activity = list(routes.outbox_update_track({"track": track}))[0] expected = serializers.ActivitySerializer({ "type": "Update", "object": serializers.TrackSerializer(track).data }).data expected["to"] = [contexts.AS.Public, {"type": "instances_with_followers"}] assert dict(activity["payload"]) == dict(expected) assert activity["actor"] == actors.get_service_actor()
def test_inbox_update_track(factories, mocker): update_library_entity = mocker.patch( "funkwhale_api.music.tasks.update_library_entity") activity = factories["federation.Activity"]() obj = factories["music.Track"](attributed=True, attachment_cover=None) actor = obj.attributed_to data = serializers.TrackSerializer(obj).data data["name"] = "New title" payload = {"type": "Update", "actor": actor, "object": data} routes.inbox_update_track(payload, context={ "actor": actor, "raise_exception": True, "activity": activity }) update_library_entity.assert_called_once_with(obj, {"title": "New title"})
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
def test_federation_audio_track_to_metadata(now): published = now released = now.date() payload = { "type": "Track", "id": "http://hello.track", "musicbrainzId": str(uuid.uuid4()), "name": "Black in back", "position": 5, "published": published.isoformat(), "album": { "published": published.isoformat(), "type": "Album", "id": "http://hello.album", "name": "Purple album", "musicbrainzId": str(uuid.uuid4()), "released": released.isoformat(), "artists": [{ "type": "Artist", "published": published.isoformat(), "id": "http://hello.artist", "name": "John Smith", "musicbrainzId": str(uuid.uuid4()), }], }, "artists": [{ "published": published.isoformat(), "type": "Artist", "id": "http://hello.trackartist", "name": "Bob Smith", "musicbrainzId": str(uuid.uuid4()), }], } serializer = federation_serializers.TrackSerializer(data=payload) serializer.is_valid(raise_exception=True) expected = { "artist": payload["artists"][0]["name"], "album": payload["album"]["name"], "album_artist": payload["album"]["artists"][0]["name"], "title": payload["name"], "date": released, "track_number": payload["position"], # musicbrainz "musicbrainz_albumid": payload["album"]["musicbrainzId"], "musicbrainz_recordingid": payload["musicbrainzId"], "musicbrainz_artistid": payload["artists"][0]["musicbrainzId"], "musicbrainz_albumartistid": payload["album"]["artists"][0]["musicbrainzId"], # federation "fid": payload["id"], "album_fid": payload["album"]["id"], "artist_fid": payload["artists"][0]["id"], "album_artist_fid": payload["album"]["artists"][0]["id"], "fdate": serializer.validated_data["published"], "artist_fdate": serializer.validated_data["artists"][0]["published"], "album_artist_fdate": serializer.validated_data["album"]["artists"][0]["published"], "album_fdate": serializer.validated_data["album"]["published"], } result = tasks.federation_audio_track_to_metadata( serializer.validated_data) assert result == expected # ensure we never forget to test a mandatory field for k in metadata.ALL_FIELDS: assert k in result
def test_activity_pub_track_serializer_from_ap(factories, r_mock): activity = factories["federation.Activity"]() published = timezone.now() released = timezone.now().date() data = { "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["album"]["cover"]["href"], body=io.BytesIO(b"coucou")) serializer = serializers.TrackSerializer(data=data, context={"activity": activity}) assert serializer.is_valid(raise_exception=True) track = serializer.save() album = track.album artist = track.artist album_artist = track.album.artist assert track.from_activity == activity assert track.fid == data["id"] assert track.title == data["name"] assert track.position == data["position"] assert track.creation_date == published assert str(track.mbid) == data["musicbrainzId"] assert album.from_activity == activity assert album.cover.read() == b"coucou" assert album.cover.path.endswith(".png") assert album.title == data["album"]["name"] assert album.fid == data["album"]["id"] assert str(album.mbid) == data["album"]["musicbrainzId"] assert album.creation_date == published assert album.release_date == released assert artist.from_activity == activity assert artist.name == data["artists"][0]["name"] assert artist.fid == data["artists"][0]["id"] assert str(artist.mbid) == data["artists"][0]["musicbrainzId"] assert artist.creation_date == published assert album_artist.from_activity == activity assert album_artist.name == data["album"]["artists"][0]["name"] assert album_artist.fid == data["album"]["artists"][0]["id"] assert str( album_artist.mbid) == data["album"]["artists"][0]["musicbrainzId"] assert album_artist.creation_date == published