See #170: dispatch / handle delete and update on Audio

This commit is contained in:
Eliot Berriot 2020-04-08 15:59:06 +02:00
commit 8c93169d94
No known key found for this signature in database
GPG key ID: 6B501DFD73514E14
7 changed files with 255 additions and 9 deletions

View file

@ -31,7 +31,9 @@ from funkwhale_api.moderation import serializers as moderation_serializers
({"type": "Update", "object": {"type": "Artist"}}, routes.inbox_update_artist),
({"type": "Update", "object": {"type": "Album"}}, routes.inbox_update_album),
({"type": "Update", "object": {"type": "Track"}}, routes.inbox_update_track),
({"type": "Update", "object": {"type": "Audio"}}, routes.inbox_update_audio),
({"type": "Delete", "object": {"type": "Person"}}, routes.inbox_delete_actor),
({"type": "Delete", "object": {"type": "Tombstone"}}, routes.inbox_delete),
({"type": "Flag"}, routes.inbox_flag),
],
)
@ -62,6 +64,7 @@ def test_inbox_routes(route, handler):
({"type": "Delete", "object": {"type": "Album"}}, routes.outbox_delete_album),
({"type": "Undo", "object": {"type": "Follow"}}, routes.outbox_undo_follow),
({"type": "Update", "object": {"type": "Track"}}, routes.outbox_update_track),
({"type": "Update", "object": {"type": "Audio"}}, routes.outbox_update_audio),
(
{"type": "Delete", "object": {"type": "Tombstone"}},
routes.outbox_delete_actor,
@ -354,7 +357,7 @@ def test_inbox_create_audio_channel(factories, mocker):
"@context": jsonld.get_default_context(),
"type": "Create",
"actor": channel.actor.fid,
"object": serializers.ChannelCreateUploadSerializer(upload).data,
"object": serializers.ChannelUploadSerializer(upload).data,
}
upload.delete()
init = mocker.spy(serializers.ChannelCreateUploadSerializer, "__init__")
@ -368,7 +371,7 @@ def test_inbox_create_audio_channel(factories, mocker):
assert init.call_count == 1
args = init.call_args
assert args[1]["data"] == payload["object"]
assert args[1]["data"] == payload
assert args[1]["context"] == {"channel": channel}
assert save.call_count == 1
@ -765,6 +768,46 @@ def test_inbox_update_track(factories, mocker):
update_library_entity.assert_called_once_with(obj, {"title": "New title"})
def test_inbox_update_audio(factories, mocker, r_mock):
channel = factories["audio.Channel"]()
upload = factories["music.Upload"](
library=channel.library,
track__artist=channel.artist,
track__attributed_to=channel.actor,
)
upload.track.fid = upload.fid
upload.track.save()
r_mock.get(
upload.track.album.fid,
json=serializers.AlbumSerializer(upload.track.album).data,
)
data = serializers.ChannelCreateUploadSerializer(upload).data
data["object"]["name"] = "New title"
routes.inbox_update_audio(
data, context={"actor": channel.actor, "raise_exception": True}
)
upload.track.refresh_from_db()
assert upload.track.title == "New title"
def test_outbox_update_audio(factories, faker, mocker):
fake_uuid = faker.uuid4()
mocker.patch("uuid.uuid4", return_value=fake_uuid)
upload = factories["music.Upload"](channel=True)
activity = list(routes.outbox_update_audio({"upload": upload}))[0]
expected = serializers.ChannelCreateUploadSerializer(upload).data
expected["type"] = "Update"
expected["id"] += "/" + fake_uuid[:8]
expected["to"] = [contexts.AS.Public, {"type": "instances_with_followers"}]
assert dict(activity["payload"]) == dict(expected)
assert activity["actor"] == upload.library.channel.actor
def test_outbox_update_track(factories):
track = factories["music.Track"]()
activity = list(routes.outbox_update_track({"track": track}))[0]

View file

@ -1,3 +1,6 @@
import pytest
from funkwhale_api.federation import routes
from funkwhale_api.federation import serializers
@ -141,3 +144,93 @@ def test_reel2bits_channel_from_actor_ap(db, mocker):
assert channel.rss_url == payload["url"][1]["href"]
assert channel.artist.name == actor.name
assert channel.artist.attributed_to == actor
def test_reel2bits_upload_create(factories):
channel = factories["audio.Channel"]()
payload = {
"id": "https://r2b.example/outbox/cb89c969224d7c9d",
"to": ["https://www.w3.org/ns/activitystreams#Public"],
"type": "Create",
"actor": "https://r2b.example/user/anna",
"object": {
"cc": ["https://r2b.example/user/anna/followers"],
"id": "https://r2b.example/outbox/cb89c969224d7c9d/activity",
"to": ["https://www.w3.org/ns/activitystreams#Public"],
"url": {
"href": "https://r2b.example/uploads/sounds/anna/test.mp3",
"type": "Link",
"mediaType": "audio/mpeg",
},
"name": "nya",
"tag": [
{"name": "#nya", "type": "Hashtag"},
{"name": "#cat", "type": "Hashtag"},
{"name": "#paws", "type": "Hashtag"},
],
"type": "Audio",
"genre": "cat",
"image": {
"url": "https://r2b.example/uploads/artwork_sounds/anna/test.jpg",
"type": "Image",
"mediaType": "image/jpeg",
},
"content": "nya nya",
"licence": {"id": "0", "icon": "", "link": "", "name": "Not Specified"},
"mediaType": "text/plain",
"published": "2020-04-08T12:47:29Z",
"attributedTo": "https://r2b.example/user/anna",
},
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
{
"toot": "http://joinmastodon.org/ns#",
"Hashtag": "as:Hashtag",
"featured": "toot:featured",
"sensitive": "as:sensitive",
},
],
"published": "2020-04-08T12:47:29Z",
}
serializer = serializers.ChannelCreateUploadSerializer(
data=payload, context={"channel": channel}
)
assert serializer.is_valid(raise_exception=True) is True
serializer.save()
def test_reel2bits_upload_delete(factories):
actor = factories["federation.Actor"]()
channel = factories["audio.Channel"](actor=actor, attributed_to=actor)
upload = factories["music.Upload"](channel=channel, track__attributed_to=actor)
payload = {
"id": "https://r2b.example/outbox/4987acc5b25f0aac",
"to": [
"https://channels.tests.funkwhale.audio/federation/actors/demo",
"https://www.w3.org/ns/activitystreams#Public",
],
"type": "Delete",
"actor": actor.fid,
"object": {"id": upload.fid, "type": "Tombstone"},
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
{
"toot": "http://joinmastodon.org/ns#",
"Hashtag": "as:Hashtag",
"featured": "toot:featured",
"sensitive": "as:sensitive",
},
],
}
routes.inbox_delete(
payload, context={"actor": actor, "raise_exception": True, "activity": payload},
)
with pytest.raises(upload.track.DoesNotExist):
upload.track.refresh_from_db()
with pytest.raises(upload.DoesNotExist):
upload.refresh_from_db()

View file

@ -123,6 +123,19 @@ def test_track_mutation_apply_outbox(factories, mocker):
)
def test_channel_track_mutation_apply_outbox(factories, mocker):
dispatch = mocker.patch("funkwhale_api.federation.routes.outbox.dispatch")
upload = factories["music.Upload"](channel=True, track__position=4)
mutation = factories["common.Mutation"](
type="update", target=upload.track, payload={"position": 12}
)
mutation.apply()
dispatch.assert_called_once_with(
{"type": "Update", "object": {"type": "Audio"}}, context={"upload": upload}
)
@pytest.mark.parametrize("factory_name", ["music.Artist", "music.Album", "music.Track"])
def test_mutation_set_tags(factory_name, factories, now, mocker):
tags = ["tag1", "tag2"]