mirror of
https://code.eliotberriot.com/funkwhale/funkwhale.git
synced 2025-10-06 09:59:56 +02:00
See #170: cover on tracks and artists
This commit is contained in:
parent
db1cb30df8
commit
71b400a9b8
34 changed files with 582 additions and 254 deletions
|
@ -64,7 +64,7 @@ def test_attachment(factories, now):
|
|||
@pytest.mark.parametrize("args, expected", [([], [0]), ([True], [0]), ([False], [1])])
|
||||
def test_attachment_queryset_attached(args, expected, factories, queryset_equal_list):
|
||||
attachments = [
|
||||
factories["music.Album"]().attachment_cover,
|
||||
factories["music.Album"](artist__attachment_cover=None).attachment_cover,
|
||||
factories["common.Attachment"](),
|
||||
]
|
||||
|
||||
|
|
|
@ -63,12 +63,13 @@ def test_db_serialize_update_mutation(factories, mutations_registry, mocker):
|
|||
user = factories["users.User"](email="hello@test.email", with_actor=True)
|
||||
|
||||
class S(mutations.UpdateMutationSerializer):
|
||||
serialized_relations = {"actor": "full_username"}
|
||||
|
||||
class Meta:
|
||||
model = user.__class__
|
||||
fields = ["actor"]
|
||||
|
||||
def get_serialized_relations(self):
|
||||
return {"actor": "full_username"}
|
||||
|
||||
expected = {"actor": user.actor.full_username}
|
||||
assert S().db_serialize({"actor": user.actor}) == expected
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import io
|
||||
import pytest
|
||||
|
||||
from funkwhale_api.common import utils
|
||||
|
@ -124,3 +125,51 @@ def test_join_url(start, end, expected):
|
|||
def test_render_html(text, content_type, expected):
|
||||
result = utils.render_html(text, content_type)
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test_attach_file_url(factories):
|
||||
album = factories["music.Album"]()
|
||||
existing_attachment = album.attachment_cover
|
||||
assert existing_attachment is not None
|
||||
|
||||
data = {"mimetype": "image/jpeg", "url": "https://example.com/test.jpg"}
|
||||
new_attachment = utils.attach_file(album, "attachment_cover", data)
|
||||
|
||||
album.refresh_from_db()
|
||||
|
||||
with pytest.raises(existing_attachment.DoesNotExist):
|
||||
existing_attachment.refresh_from_db()
|
||||
|
||||
assert album.attachment_cover == new_attachment
|
||||
assert not new_attachment.file
|
||||
assert new_attachment.url == data["url"]
|
||||
assert new_attachment.mimetype == data["mimetype"]
|
||||
|
||||
|
||||
def test_attach_file_url_fetch(factories, r_mock):
|
||||
album = factories["music.Album"]()
|
||||
|
||||
data = {"mimetype": "image/jpeg", "url": "https://example.com/test.jpg"}
|
||||
r_mock.get(data["url"], body=io.BytesIO(b"content"))
|
||||
new_attachment = utils.attach_file(album, "attachment_cover", data, fetch=True)
|
||||
|
||||
album.refresh_from_db()
|
||||
|
||||
assert album.attachment_cover == new_attachment
|
||||
assert new_attachment.file.read() == b"content"
|
||||
assert new_attachment.url == data["url"]
|
||||
assert new_attachment.mimetype == data["mimetype"]
|
||||
|
||||
|
||||
def test_attach_file_content(factories, r_mock):
|
||||
album = factories["music.Album"]()
|
||||
|
||||
data = {"mimetype": "image/jpeg", "content": b"content"}
|
||||
new_attachment = utils.attach_file(album, "attachment_cover", data)
|
||||
|
||||
album.refresh_from_db()
|
||||
|
||||
assert album.attachment_cover == new_attachment
|
||||
assert new_attachment.file.read() == b"content"
|
||||
assert new_attachment.url is None
|
||||
assert new_attachment.mimetype == data["mimetype"]
|
||||
|
|
|
@ -540,7 +540,7 @@ def test_inbox_update_artist(factories, mocker):
|
|||
"funkwhale_api.music.tasks.update_library_entity"
|
||||
)
|
||||
activity = factories["federation.Activity"]()
|
||||
obj = factories["music.Artist"](attributed=True)
|
||||
obj = factories["music.Artist"](attributed=True, attachment_cover=None)
|
||||
actor = obj.attributed_to
|
||||
data = serializers.ArtistSerializer(obj).data
|
||||
data["name"] = "New name"
|
||||
|
@ -602,7 +602,7 @@ def test_inbox_update_track(factories, mocker):
|
|||
"funkwhale_api.music.tasks.update_library_entity"
|
||||
)
|
||||
activity = factories["federation.Activity"]()
|
||||
obj = factories["music.Track"](attributed=True)
|
||||
obj = factories["music.Track"](attributed=True, attachment_cover=None)
|
||||
actor = obj.attributed_to
|
||||
data = serializers.TrackSerializer(obj).data
|
||||
data["name"] = "New title"
|
||||
|
|
|
@ -575,6 +575,11 @@ def test_activity_pub_artist_serializer_to_ap(factories):
|
|||
"attributedTo": artist.attributed_to.fid,
|
||||
"mediaType": "text/html",
|
||||
"content": common_utils.render_html(content.text, content.content_type),
|
||||
"image": {
|
||||
"type": "Image",
|
||||
"mediaType": "image/jpeg",
|
||||
"href": utils.full_url(artist.attachment_cover.file.url),
|
||||
},
|
||||
"tag": [
|
||||
{"type": "Hashtag", "name": "#Punk"},
|
||||
{"type": "Hashtag", "name": "#Rock"},
|
||||
|
@ -601,6 +606,11 @@ def test_activity_pub_album_serializer_to_ap(factories):
|
|||
"mediaType": "image/jpeg",
|
||||
"href": utils.full_url(album.attachment_cover.file.url),
|
||||
},
|
||||
"image": {
|
||||
"type": "Image",
|
||||
"mediaType": "image/jpeg",
|
||||
"href": utils.full_url(album.attachment_cover.file.url),
|
||||
},
|
||||
"musicbrainzId": album.mbid,
|
||||
"published": album.creation_date.isoformat(),
|
||||
"released": album.release_date.isoformat(),
|
||||
|
@ -622,6 +632,44 @@ def test_activity_pub_album_serializer_to_ap(factories):
|
|||
assert serializer.data == expected
|
||||
|
||||
|
||||
def test_activity_pub_artist_serializer_from_ap_update(factories, faker):
|
||||
artist = factories["music.Artist"](attributed=True)
|
||||
payload = {
|
||||
"@context": jsonld.get_default_context(),
|
||||
"type": "Artist",
|
||||
"id": artist.fid,
|
||||
"name": faker.sentence(),
|
||||
"musicbrainzId": faker.uuid4(),
|
||||
"published": artist.creation_date.isoformat(),
|
||||
"attributedTo": artist.attributed_to.fid,
|
||||
"mediaType": "text/html",
|
||||
"content": common_utils.render_html(faker.sentence(), "text/html"),
|
||||
"image": {"type": "Image", "mediaType": "image/jpeg", "href": faker.url()},
|
||||
"tag": [
|
||||
{"type": "Hashtag", "name": "#Punk"},
|
||||
{"type": "Hashtag", "name": "#Rock"},
|
||||
],
|
||||
}
|
||||
|
||||
serializer = serializers.ArtistSerializer(artist, data=payload)
|
||||
assert serializer.is_valid(raise_exception=True) is True
|
||||
|
||||
serializer.save()
|
||||
|
||||
artist.refresh_from_db()
|
||||
|
||||
assert artist.name == payload["name"]
|
||||
assert str(artist.mbid) == payload["musicbrainzId"]
|
||||
assert artist.attachment_cover.url == payload["image"]["href"]
|
||||
assert artist.attachment_cover.mimetype == payload["image"]["mediaType"]
|
||||
assert artist.description.text == payload["content"]
|
||||
assert artist.description.content_type == "text/html"
|
||||
assert sorted(artist.tagged_items.values_list("tag__name", flat=True)) == [
|
||||
"Punk",
|
||||
"Rock",
|
||||
]
|
||||
|
||||
|
||||
def test_activity_pub_album_serializer_from_ap_update(factories, faker):
|
||||
album = factories["music.Album"](attributed=True)
|
||||
released = faker.date_object()
|
||||
|
@ -699,6 +747,11 @@ def test_activity_pub_track_serializer_to_ap(factories):
|
|||
{"type": "Hashtag", "name": "#Punk"},
|
||||
{"type": "Hashtag", "name": "#Rock"},
|
||||
],
|
||||
"image": {
|
||||
"type": "Image",
|
||||
"mediaType": "image/jpeg",
|
||||
"href": utils.full_url(track.attachment_cover.file.url),
|
||||
},
|
||||
}
|
||||
serializer = serializers.TrackSerializer(track)
|
||||
|
||||
|
@ -726,6 +779,11 @@ def test_activity_pub_track_serializer_from_ap(factories, r_mock, mocker):
|
|||
"disc": 1,
|
||||
"content": "Hello there",
|
||||
"attributedTo": track_attributed_to.fid,
|
||||
"image": {
|
||||
"type": "Link",
|
||||
"href": "https://cover.image/track.png",
|
||||
"mediaType": "image/png",
|
||||
},
|
||||
"album": {
|
||||
"type": "Album",
|
||||
"id": "http://hello.album",
|
||||
|
@ -753,6 +811,11 @@ def test_activity_pub_track_serializer_from_ap(factories, r_mock, mocker):
|
|||
"published": published.isoformat(),
|
||||
"attributedTo": album_artist_attributed_to.fid,
|
||||
"tag": [{"type": "Hashtag", "name": "AlbumArtistTag"}],
|
||||
"image": {
|
||||
"type": "Link",
|
||||
"href": "https://cover.image/album-artist.png",
|
||||
"mediaType": "image/png",
|
||||
},
|
||||
}
|
||||
],
|
||||
},
|
||||
|
@ -767,6 +830,11 @@ def test_activity_pub_track_serializer_from_ap(factories, r_mock, mocker):
|
|||
"attributedTo": artist_attributed_to.fid,
|
||||
"published": published.isoformat(),
|
||||
"tag": [{"type": "Hashtag", "name": "ArtistTag"}],
|
||||
"image": {
|
||||
"type": "Link",
|
||||
"href": "https://cover.image/artist.png",
|
||||
"mediaType": "image/png",
|
||||
},
|
||||
}
|
||||
],
|
||||
"tag": [
|
||||
|
@ -774,7 +842,6 @@ def test_activity_pub_track_serializer_from_ap(factories, r_mock, mocker):
|
|||
{"type": "Hashtag", "name": "World"},
|
||||
],
|
||||
}
|
||||
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)
|
||||
|
||||
|
@ -793,10 +860,12 @@ def test_activity_pub_track_serializer_from_ap(factories, r_mock, mocker):
|
|||
assert str(track.mbid) == data["musicbrainzId"]
|
||||
assert track.description.text == data["content"]
|
||||
assert track.description.content_type == "text/html"
|
||||
assert track.attachment_cover.url == data["image"]["href"]
|
||||
assert track.attachment_cover.mimetype == data["image"]["mediaType"]
|
||||
|
||||
assert album.from_activity == activity
|
||||
assert album.attachment_cover.file.read() == b"coucou"
|
||||
assert album.attachment_cover.file.path.endswith(".png")
|
||||
assert album.attachment_cover.url == data["album"]["cover"]["href"]
|
||||
assert album.attachment_cover.mimetype == data["album"]["cover"]["mediaType"]
|
||||
assert album.title == data["album"]["name"]
|
||||
assert album.fid == data["album"]["id"]
|
||||
assert str(album.mbid) == data["album"]["musicbrainzId"]
|
||||
|
@ -814,6 +883,8 @@ def test_activity_pub_track_serializer_from_ap(factories, r_mock, mocker):
|
|||
assert artist.attributed_to == artist_attributed_to
|
||||
assert artist.description.text == data["artists"][0]["content"]
|
||||
assert artist.description.content_type == data["artists"][0]["mediaType"]
|
||||
assert artist.attachment_cover.url == data["artists"][0]["image"]["href"]
|
||||
assert artist.attachment_cover.mimetype == data["artists"][0]["image"]["mediaType"]
|
||||
|
||||
assert album_artist.from_activity == activity
|
||||
assert album_artist.name == data["album"]["artists"][0]["name"]
|
||||
|
@ -826,6 +897,14 @@ def test_activity_pub_track_serializer_from_ap(factories, r_mock, mocker):
|
|||
album_artist.description.content_type
|
||||
== data["album"]["artists"][0]["mediaType"]
|
||||
)
|
||||
assert (
|
||||
album_artist.attachment_cover.url
|
||||
== data["album"]["artists"][0]["image"]["href"]
|
||||
)
|
||||
assert (
|
||||
album_artist.attachment_cover.mimetype
|
||||
== data["album"]["artists"][0]["image"]["mediaType"]
|
||||
)
|
||||
|
||||
add_tags.assert_any_call(track, *["Hello", "World"])
|
||||
add_tags.assert_any_call(album, *["AlbumTag"])
|
||||
|
@ -833,7 +912,7 @@ def test_activity_pub_track_serializer_from_ap(factories, r_mock, mocker):
|
|||
add_tags.assert_any_call(artist, *["ArtistTag"])
|
||||
|
||||
|
||||
def test_activity_pub_track_serializer_from_ap_update(factories, r_mock, mocker):
|
||||
def test_activity_pub_track_serializer_from_ap_update(factories, r_mock, mocker, faker):
|
||||
set_tags = mocker.patch("funkwhale_api.tags.models.set_tags")
|
||||
content = factories["common.Content"]()
|
||||
track_attributed_to = factories["federation.Actor"]()
|
||||
|
@ -853,6 +932,7 @@ def test_activity_pub_track_serializer_from_ap_update(factories, r_mock, mocker)
|
|||
"attributedTo": track_attributed_to.fid,
|
||||
"album": serializers.AlbumSerializer(track.album).data,
|
||||
"artists": [serializers.ArtistSerializer(track.artist).data],
|
||||
"image": {"type": "Link", "mediaType": "image/jpeg", "href": faker.url()},
|
||||
"tag": [
|
||||
{"type": "Hashtag", "name": "#Hello"},
|
||||
# Ensure we can handle tags without a leading #
|
||||
|
@ -873,6 +953,8 @@ def test_activity_pub_track_serializer_from_ap_update(factories, r_mock, mocker)
|
|||
assert track.description.content_type == "text/html"
|
||||
assert track.description.text == "hello there"
|
||||
assert str(track.mbid) == data["musicbrainzId"]
|
||||
assert track.attachment_cover.url == data["image"]["href"]
|
||||
assert track.attachment_cover.mimetype == data["image"]["mediaType"]
|
||||
|
||||
set_tags.assert_called_once_with(track, *["Hello", "World"])
|
||||
|
||||
|
|
|
@ -303,6 +303,7 @@ def test_manage_artist_serializer(factories, now, to_api_date):
|
|||
artist.attributed_to
|
||||
).data,
|
||||
"tags": [],
|
||||
"cover": common_serializers.AttachmentSerializer(artist.attachment_cover).data,
|
||||
}
|
||||
s = serializers.ManageArtistSerializer(artist)
|
||||
|
||||
|
@ -412,6 +413,7 @@ def test_manage_track_serializer(factories, now, to_api_date):
|
|||
).data,
|
||||
"uploads_count": 44,
|
||||
"tags": [],
|
||||
"cover": common_serializers.AttachmentSerializer(track.attachment_cover).data,
|
||||
}
|
||||
s = serializers.ManageTrackSerializer(track)
|
||||
|
||||
|
|
|
@ -440,7 +440,7 @@ def test_track_metadata_serializer(path, expected, mocker):
|
|||
path = os.path.join(DATA_DIR, path)
|
||||
data = metadata.Metadata(path)
|
||||
get_picture = mocker.patch.object(data, "get_picture")
|
||||
expected["cover_data"] = get_picture.return_value
|
||||
expected["album"]["cover_data"] = get_picture.return_value
|
||||
|
||||
serializer = metadata.TrackMetadataSerializer(data=data)
|
||||
assert serializer.is_valid(raise_exception=True) is True
|
||||
|
@ -566,13 +566,13 @@ def test_fake_metadata_with_serializer():
|
|||
"mbid": uuid.UUID("5b4d7d2d-36df-4b38-95e3-a964234f520f"),
|
||||
},
|
||||
],
|
||||
"cover_data": None,
|
||||
},
|
||||
"position": 1,
|
||||
"disc_number": 1,
|
||||
"mbid": uuid.UUID("bd21ac48-46d8-4e78-925f-d9cc2a294656"),
|
||||
"license": "Dummy license: http://creativecommons.org/licenses/by-sa/4.0/",
|
||||
"copyright": "Someone",
|
||||
"cover_data": None,
|
||||
}
|
||||
serializer = metadata.TrackMetadataSerializer(data=metadata.FakeMetadata(data))
|
||||
assert serializer.is_valid(raise_exception=True) is True
|
||||
|
@ -594,8 +594,8 @@ def test_serializer_album_artist_missing():
|
|||
"mbid": None,
|
||||
"release_date": None,
|
||||
"artists": [],
|
||||
"cover_data": None,
|
||||
},
|
||||
"cover_data": None,
|
||||
}
|
||||
serializer = metadata.TrackMetadataSerializer(data=metadata.FakeMetadata(data))
|
||||
assert serializer.is_valid(raise_exception=True) is True
|
||||
|
@ -622,8 +622,8 @@ def test_serializer_album_default_title_when_missing_or_empty(data):
|
|||
"mbid": None,
|
||||
"release_date": None,
|
||||
"artists": [],
|
||||
"cover_data": None,
|
||||
},
|
||||
"cover_data": None,
|
||||
}
|
||||
serializer = metadata.TrackMetadataSerializer(data=metadata.FakeMetadata(data))
|
||||
assert serializer.is_valid(raise_exception=True) is True
|
||||
|
@ -649,8 +649,8 @@ def test_serializer_empty_fields(field_name):
|
|||
"mbid": None,
|
||||
"release_date": None,
|
||||
"artists": [],
|
||||
"cover_data": None,
|
||||
},
|
||||
"cover_data": None,
|
||||
}
|
||||
serializer = metadata.TrackMetadataSerializer(data=metadata.FakeMetadata(data))
|
||||
assert serializer.is_valid(raise_exception=True) is True
|
||||
|
@ -701,8 +701,8 @@ def test_acquire_tags_from_genre(genre, expected_tags):
|
|||
"mbid": None,
|
||||
"release_date": None,
|
||||
"artists": [],
|
||||
"cover_data": None,
|
||||
},
|
||||
"cover_data": None,
|
||||
}
|
||||
if expected_tags:
|
||||
expected["tags"] = expected_tags
|
||||
|
|
|
@ -187,14 +187,6 @@ def test_track_get_file_size_in_place(factories):
|
|||
assert upload.get_file_size() == 297745
|
||||
|
||||
|
||||
def test_album_get_image_content(factories):
|
||||
album = factories["music.Album"]()
|
||||
album.get_image(data={"content": b"test", "mimetype": "image/jpeg"})
|
||||
album.refresh_from_db()
|
||||
|
||||
assert album.attachment_cover.file.read() == b"test"
|
||||
|
||||
|
||||
def test_library(factories):
|
||||
now = timezone.now()
|
||||
actor = factories["federation.Actor"]()
|
||||
|
|
|
@ -121,23 +121,3 @@ def test_can_get_or_create_track_from_api(artists, albums, tracks, mocker, db):
|
|||
track2, created = models.Track.get_or_create_from_api(mbid=data["id"])
|
||||
assert not created
|
||||
assert track == track2
|
||||
|
||||
|
||||
def test_can_download_image_file_for_album(binary_cover, mocker, factories):
|
||||
mocker.patch(
|
||||
"funkwhale_api.musicbrainz.api.images.get_front", return_value=binary_cover
|
||||
)
|
||||
# client._api.get_image_front('55ea4f82-b42b-423e-a0e5-290ccdf443ed')
|
||||
album = factories["music.Album"](mbid="55ea4f82-b42b-423e-a0e5-290ccdf443ed")
|
||||
album.get_image()
|
||||
album.save()
|
||||
|
||||
assert album.attachment_cover.file.read() == binary_cover
|
||||
|
||||
|
||||
def test_album_get_image_doesnt_crash_with_empty_data(mocker, factories):
|
||||
album = factories["music.Album"](mbid=None, attachment_cover=None)
|
||||
assert (
|
||||
album.get_image(data={"content": "", "url": "", "mimetype": "image/png"})
|
||||
is None
|
||||
)
|
||||
|
|
|
@ -179,9 +179,10 @@ def test_perm_checkers_can_approve(
|
|||
assert mutations.can_approve(obj, actor=actor) is expected
|
||||
|
||||
|
||||
def test_mutation_set_attachment_cover(factories, now, mocker):
|
||||
@pytest.mark.parametrize("factory_name", ["music.Artist", "music.Track", "music.Album"])
|
||||
def test_mutation_set_attachment_cover(factory_name, factories, now, mocker):
|
||||
new_attachment = factories["common.Attachment"](actor__local=True)
|
||||
obj = factories["music.Album"]()
|
||||
obj = factories[factory_name]()
|
||||
old_attachment = obj.attachment_cover
|
||||
mutation = factories["common.Mutation"](
|
||||
type="update", target=obj, payload={"cover": new_attachment.uuid}
|
||||
|
|
|
@ -71,6 +71,7 @@ def test_artist_with_albums_serializer(factories, to_api_date):
|
|||
"tags": [],
|
||||
"attributed_to": federation_serializers.APIActorSerializer(actor).data,
|
||||
"tracks_count": 42,
|
||||
"cover": common_serializers.AttachmentSerializer(artist.attachment_cover).data,
|
||||
}
|
||||
serializer = serializers.ArtistWithAlbumsSerializer(artist)
|
||||
assert serializer.data == expected
|
||||
|
@ -217,6 +218,7 @@ def test_track_serializer(factories, to_api_date):
|
|||
"is_local": upload.track.is_local,
|
||||
"tags": [],
|
||||
"attributed_to": federation_serializers.APIActorSerializer(actor).data,
|
||||
"cover": common_serializers.AttachmentSerializer(track.attachment_cover).data,
|
||||
}
|
||||
serializer = serializers.TrackSerializer(track)
|
||||
assert serializer.data == expected
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import datetime
|
||||
import io
|
||||
import os
|
||||
import pytest
|
||||
import uuid
|
||||
|
@ -270,7 +269,7 @@ def test_can_create_track_from_file_metadata_distinct_position(factories):
|
|||
assert new_track != track
|
||||
|
||||
|
||||
def test_can_create_track_from_file_metadata_federation(factories, mocker, r_mock):
|
||||
def test_can_create_track_from_file_metadata_federation(factories, mocker):
|
||||
metadata = {
|
||||
"artists": [
|
||||
{"name": "Artist", "fid": "https://artist.fid", "fdate": timezone.now()}
|
||||
|
@ -279,6 +278,7 @@ def test_can_create_track_from_file_metadata_federation(factories, mocker, r_moc
|
|||
"title": "Album",
|
||||
"fid": "https://album.fid",
|
||||
"fdate": timezone.now(),
|
||||
"cover_data": {"url": "https://cover/hello.png", "mimetype": "image/png"},
|
||||
"artists": [
|
||||
{
|
||||
"name": "Album artist",
|
||||
|
@ -291,9 +291,7 @@ def test_can_create_track_from_file_metadata_federation(factories, mocker, r_moc
|
|||
"position": 4,
|
||||
"fid": "https://hello",
|
||||
"fdate": timezone.now(),
|
||||
"cover_data": {"url": "https://cover/hello.png", "mimetype": "image/png"},
|
||||
}
|
||||
r_mock.get(metadata["cover_data"]["url"], body=io.BytesIO(b"coucou"))
|
||||
|
||||
track = tasks.get_track_from_import_metadata(metadata, update_cover=True)
|
||||
|
||||
|
@ -301,10 +299,11 @@ def test_can_create_track_from_file_metadata_federation(factories, mocker, r_moc
|
|||
assert track.fid == metadata["fid"]
|
||||
assert track.creation_date == metadata["fdate"]
|
||||
assert track.position == 4
|
||||
assert track.album.attachment_cover.file.read() == b"coucou"
|
||||
assert track.album.attachment_cover.file.path.endswith(".png")
|
||||
assert track.album.attachment_cover.url == metadata["cover_data"]["url"]
|
||||
assert track.album.attachment_cover.mimetype == metadata["cover_data"]["mimetype"]
|
||||
assert track.album.attachment_cover.url == metadata["album"]["cover_data"]["url"]
|
||||
assert (
|
||||
track.album.attachment_cover.mimetype
|
||||
== metadata["album"]["cover_data"]["mimetype"]
|
||||
)
|
||||
|
||||
assert track.album.fid == metadata["album"]["fid"]
|
||||
assert track.album.title == metadata["album"]["title"]
|
||||
|
@ -328,7 +327,9 @@ def test_sort_candidates(factories):
|
|||
|
||||
def test_upload_import(now, factories, temp_signal, mocker):
|
||||
outbox = mocker.patch("funkwhale_api.federation.routes.outbox.dispatch")
|
||||
update_album_cover = mocker.patch("funkwhale_api.music.tasks.update_album_cover")
|
||||
populate_album_cover = mocker.patch(
|
||||
"funkwhale_api.music.tasks.populate_album_cover"
|
||||
)
|
||||
get_picture = mocker.patch("funkwhale_api.music.metadata.Metadata.get_picture")
|
||||
get_track_from_import_metadata = mocker.spy(tasks, "get_track_from_import_metadata")
|
||||
track = factories["music.Track"](album__attachment_cover=None)
|
||||
|
@ -348,8 +349,8 @@ def test_upload_import(now, factories, temp_signal, mocker):
|
|||
assert upload.import_status == "finished"
|
||||
assert upload.import_date == now
|
||||
get_picture.assert_called_once_with("cover_front", "other")
|
||||
update_album_cover.assert_called_once_with(
|
||||
upload.track.album, cover_data=get_picture.return_value, source=upload.source
|
||||
populate_album_cover.assert_called_once_with(
|
||||
upload.track.album, source=upload.source
|
||||
)
|
||||
assert (
|
||||
get_track_from_import_metadata.call_args[-1]["attributed_to"]
|
||||
|
@ -557,46 +558,33 @@ def test_upload_import_error_metadata(factories, now, temp_signal, mocker):
|
|||
|
||||
|
||||
def test_upload_import_updates_cover_if_no_cover(factories, mocker, now):
|
||||
mocked_update = mocker.patch("funkwhale_api.music.tasks.update_album_cover")
|
||||
populate_album_cover = mocker.patch(
|
||||
"funkwhale_api.music.tasks.populate_album_cover"
|
||||
)
|
||||
album = factories["music.Album"](attachment_cover=None)
|
||||
track = factories["music.Track"](album=album)
|
||||
upload = factories["music.Upload"](
|
||||
track=None, import_metadata={"funkwhale": {"track": {"uuid": track.uuid}}}
|
||||
)
|
||||
tasks.process_upload(upload_id=upload.pk)
|
||||
mocked_update.assert_called_once_with(album, source=None, cover_data=None)
|
||||
|
||||
|
||||
def test_update_album_cover_mbid(factories, mocker):
|
||||
album = factories["music.Album"](attachment_cover=None)
|
||||
|
||||
mocked_get = mocker.patch("funkwhale_api.music.models.Album.get_image")
|
||||
tasks.update_album_cover(album=album)
|
||||
|
||||
mocked_get.assert_called_once_with()
|
||||
|
||||
|
||||
def test_update_album_cover_file_data(factories, mocker):
|
||||
album = factories["music.Album"](attachment_cover=None, mbid=None)
|
||||
|
||||
mocked_get = mocker.patch("funkwhale_api.music.models.Album.get_image")
|
||||
tasks.update_album_cover(album=album, cover_data={"hello": "world"})
|
||||
mocked_get.assert_called_once_with(data={"hello": "world"})
|
||||
populate_album_cover.assert_called_once_with(album, source=None)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("ext,mimetype", [("jpg", "image/jpeg"), ("png", "image/png")])
|
||||
def test_update_album_cover_file_cover_separate_file(ext, mimetype, factories, mocker):
|
||||
def test_populate_album_cover_file_cover_separate_file(
|
||||
ext, mimetype, factories, mocker
|
||||
):
|
||||
mocker.patch("funkwhale_api.music.tasks.IMAGE_TYPES", [(ext, mimetype)])
|
||||
image_path = os.path.join(DATA_DIR, "cover.{}".format(ext))
|
||||
with open(image_path, "rb") as f:
|
||||
image_content = f.read()
|
||||
album = factories["music.Album"](attachment_cover=None, mbid=None)
|
||||
|
||||
mocked_get = mocker.patch("funkwhale_api.music.models.Album.get_image")
|
||||
attach_file = mocker.patch("funkwhale_api.common.utils.attach_file")
|
||||
mocker.patch("funkwhale_api.music.metadata.Metadata.get_picture", return_value=None)
|
||||
tasks.update_album_cover(album=album, source="file://" + image_path)
|
||||
mocked_get.assert_called_once_with(
|
||||
data={"mimetype": mimetype, "content": image_content}
|
||||
tasks.populate_album_cover(album=album, source="file://" + image_path)
|
||||
attach_file.assert_called_once_with(
|
||||
album, "attachment_cover", {"mimetype": mimetype, "content": image_content}
|
||||
)
|
||||
|
||||
|
||||
|
@ -623,6 +611,11 @@ def test_federation_audio_track_to_metadata(now, mocker):
|
|||
"attributedTo": "http://track.attributed",
|
||||
"tag": [{"type": "Hashtag", "name": "TrackTag"}],
|
||||
"content": "hello there",
|
||||
"image": {
|
||||
"type": "Link",
|
||||
"href": "http://cover.test/track",
|
||||
"mediaType": "image/png",
|
||||
},
|
||||
"album": {
|
||||
"published": published.isoformat(),
|
||||
"type": "Album",
|
||||
|
@ -645,6 +638,11 @@ def test_federation_audio_track_to_metadata(now, mocker):
|
|||
"musicbrainzId": str(uuid.uuid4()),
|
||||
"attributedTo": "http://album-artist.attributed",
|
||||
"tag": [{"type": "Hashtag", "name": "AlbumArtistTag"}],
|
||||
"image": {
|
||||
"type": "Link",
|
||||
"href": "http://cover.test/album-artist",
|
||||
"mediaType": "image/png",
|
||||
},
|
||||
}
|
||||
],
|
||||
"cover": {
|
||||
|
@ -664,6 +662,11 @@ def test_federation_audio_track_to_metadata(now, mocker):
|
|||
"musicbrainzId": str(uuid.uuid4()),
|
||||
"attributedTo": "http://artist.attributed",
|
||||
"tag": [{"type": "Hashtag", "name": "ArtistTag"}],
|
||||
"image": {
|
||||
"type": "Link",
|
||||
"href": "http://cover.test/artist",
|
||||
"mediaType": "image/png",
|
||||
},
|
||||
}
|
||||
],
|
||||
}
|
||||
|
@ -681,6 +684,10 @@ def test_federation_audio_track_to_metadata(now, mocker):
|
|||
"attributed_to": references["http://track.attributed"],
|
||||
"tags": ["TrackTag"],
|
||||
"description": {"content_type": "text/html", "text": "hello there"},
|
||||
"cover_data": {
|
||||
"mimetype": serializer.validated_data["image"]["mediaType"],
|
||||
"url": serializer.validated_data["image"]["href"],
|
||||
},
|
||||
"album": {
|
||||
"title": payload["album"]["name"],
|
||||
"attributed_to": references["http://album.attributed"],
|
||||
|
@ -690,6 +697,10 @@ def test_federation_audio_track_to_metadata(now, mocker):
|
|||
"fdate": serializer.validated_data["album"]["published"],
|
||||
"tags": ["AlbumTag"],
|
||||
"description": {"content_type": "text/plain", "text": "album desc"},
|
||||
"cover_data": {
|
||||
"mimetype": serializer.validated_data["album"]["cover"]["mediaType"],
|
||||
"url": serializer.validated_data["album"]["cover"]["href"],
|
||||
},
|
||||
"artists": [
|
||||
{
|
||||
"name": a["name"],
|
||||
|
@ -704,6 +715,14 @@ def test_federation_audio_track_to_metadata(now, mocker):
|
|||
"text": "album artist desc",
|
||||
},
|
||||
"tags": ["AlbumArtistTag"],
|
||||
"cover_data": {
|
||||
"mimetype": serializer.validated_data["album"]["artists"][i][
|
||||
"image"
|
||||
]["mediaType"],
|
||||
"url": serializer.validated_data["album"]["artists"][i][
|
||||
"image"
|
||||
]["href"],
|
||||
},
|
||||
}
|
||||
for i, a in enumerate(payload["album"]["artists"])
|
||||
],
|
||||
|
@ -719,13 +738,15 @@ def test_federation_audio_track_to_metadata(now, mocker):
|
|||
"attributed_to": references["http://artist.attributed"],
|
||||
"tags": ["ArtistTag"],
|
||||
"description": {"content_type": "text/html", "text": "artist desc"},
|
||||
"cover_data": {
|
||||
"mimetype": serializer.validated_data["artists"][i]["image"][
|
||||
"mediaType"
|
||||
],
|
||||
"url": serializer.validated_data["artists"][i]["image"]["href"],
|
||||
},
|
||||
}
|
||||
for i, a in enumerate(payload["artists"])
|
||||
],
|
||||
"cover_data": {
|
||||
"mimetype": serializer.validated_data["album"]["cover"]["mediaType"],
|
||||
"url": serializer.validated_data["album"]["cover"]["href"],
|
||||
},
|
||||
}
|
||||
|
||||
result = tasks.federation_audio_track_to_metadata(
|
||||
|
@ -764,7 +785,7 @@ def test_scan_page_fetches_page_and_creates_tracks(now, mocker, factories, r_moc
|
|||
scan_page = mocker.patch("funkwhale_api.music.tasks.scan_library_page.delay")
|
||||
scan = factories["music.LibraryScan"](status="scanning", total_files=5)
|
||||
uploads = [
|
||||
factories["music.Upload"].build(
|
||||
factories["music.Upload"](
|
||||
fid="https://track.test/{}".format(i),
|
||||
size=42,
|
||||
bitrate=66,
|
||||
|
@ -780,7 +801,9 @@ def test_scan_page_fetches_page_and_creates_tracks(now, mocker, factories, r_moc
|
|||
"page": Paginator(uploads, 3).page(1),
|
||||
"item_serializer": federation_serializers.UploadSerializer,
|
||||
}
|
||||
uploads[0].__class__.objects.filter(pk__in=[u.pk for u in uploads]).delete()
|
||||
page = federation_serializers.CollectionPageSerializer(page_conf)
|
||||
|
||||
r_mock.get(page.data["id"], json=page.data)
|
||||
|
||||
tasks.scan_library_page(library_scan_id=scan.pk, page_url=page.data["id"])
|
||||
|
@ -1129,3 +1152,15 @@ def test_tag_artists_from_tracks(queryset_equal_queries, factories, mocker):
|
|||
add_tags_batch.assert_called_once_with(
|
||||
get_tags_from_foreign_key.return_value, model=models.Artist,
|
||||
)
|
||||
|
||||
|
||||
def test_can_download_image_file_for_album_mbid(binary_cover, mocker, factories):
|
||||
mocker.patch(
|
||||
"funkwhale_api.musicbrainz.api.images.get_front", return_value=binary_cover
|
||||
)
|
||||
# client._api.get_image_front('55ea4f82-b42b-423e-a0e5-290ccdf443ed')
|
||||
album = factories["music.Album"](mbid="55ea4f82-b42b-423e-a0e5-290ccdf443ed")
|
||||
tasks.populate_album_cover(album, replace=True)
|
||||
|
||||
assert album.attachment_cover.file.read() == binary_cover
|
||||
assert album.attachment_cover.mimetype == "image/jpeg"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue