2018-04-07 09:29:40 +00:00
|
|
|
import arrow
|
2018-04-01 20:11:46 +00:00
|
|
|
import pytest
|
2018-03-31 13:47:21 +00:00
|
|
|
from django.urls import reverse
|
2018-04-02 16:07:46 +00:00
|
|
|
from django.utils import timezone
|
2018-04-01 20:11:46 +00:00
|
|
|
from rest_framework import exceptions
|
2018-03-31 13:47:21 +00:00
|
|
|
|
2018-06-10 08:55:16 +00:00
|
|
|
from funkwhale_api.federation import actors, models, serializers, utils
|
2018-04-16 19:59:13 +00:00
|
|
|
from funkwhale_api.music import models as music_models
|
|
|
|
from funkwhale_api.music import tasks as music_tasks
|
2018-03-31 13:47:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_actor_fetching(r_mock):
|
|
|
|
payload = {
|
2018-06-09 13:36:16 +00:00
|
|
|
"id": "https://actor.mock/users/actor#main-key",
|
|
|
|
"owner": "test",
|
|
|
|
"publicKeyPem": "test_pem",
|
2018-03-31 13:47:21 +00:00
|
|
|
}
|
2018-06-09 13:36:16 +00:00
|
|
|
actor_url = "https://actor.mock/"
|
2018-03-31 13:47:21 +00:00
|
|
|
r_mock.get(actor_url, json=payload)
|
|
|
|
r = actors.get_actor_data(actor_url)
|
|
|
|
|
|
|
|
assert r == payload
|
|
|
|
|
|
|
|
|
2018-04-27 22:25:47 +00:00
|
|
|
def test_get_actor(factories, r_mock):
|
2018-06-09 13:36:16 +00:00
|
|
|
actor = factories["federation.Actor"].build()
|
2018-04-27 22:25:47 +00:00
|
|
|
payload = serializers.ActorSerializer(actor).data
|
|
|
|
r_mock.get(actor.url, json=payload)
|
|
|
|
new_actor = actors.get_actor(actor.url)
|
|
|
|
|
|
|
|
assert new_actor.pk is not None
|
|
|
|
assert serializers.ActorSerializer(new_actor).data == payload
|
|
|
|
|
|
|
|
|
2018-04-28 03:55:21 +00:00
|
|
|
def test_get_actor_use_existing(factories, preferences, mocker):
|
2018-06-09 13:36:16 +00:00
|
|
|
preferences["federation__actor_fetch_delay"] = 60
|
|
|
|
actor = factories["federation.Actor"]()
|
|
|
|
get_data = mocker.patch("funkwhale_api.federation.actors.get_actor_data")
|
2018-04-27 22:25:47 +00:00
|
|
|
new_actor = actors.get_actor(actor.url)
|
|
|
|
|
|
|
|
assert new_actor == actor
|
|
|
|
get_data.assert_not_called()
|
|
|
|
|
|
|
|
|
2018-04-28 03:55:21 +00:00
|
|
|
def test_get_actor_refresh(factories, preferences, mocker):
|
2018-06-09 13:36:16 +00:00
|
|
|
preferences["federation__actor_fetch_delay"] = 0
|
|
|
|
actor = factories["federation.Actor"]()
|
2018-04-27 22:25:47 +00:00
|
|
|
payload = serializers.ActorSerializer(actor).data
|
|
|
|
# actor changed their username in the meantime
|
2018-06-09 13:36:16 +00:00
|
|
|
payload["preferredUsername"] = "New me"
|
2018-06-10 09:27:38 +00:00
|
|
|
mocker.patch("funkwhale_api.federation.actors.get_actor_data", return_value=payload)
|
2018-04-27 22:25:47 +00:00
|
|
|
new_actor = actors.get_actor(actor.url)
|
|
|
|
|
|
|
|
assert new_actor == actor
|
|
|
|
assert new_actor.last_fetch_date > actor.last_fetch_date
|
2018-06-09 13:36:16 +00:00
|
|
|
assert new_actor.preferred_username == "New me"
|
2018-04-27 22:25:47 +00:00
|
|
|
|
|
|
|
|
2018-04-07 13:34:35 +00:00
|
|
|
def test_get_library(db, settings, mocker):
|
2018-06-10 09:27:38 +00:00
|
|
|
mocker.patch(
|
2018-06-09 13:36:16 +00:00
|
|
|
"funkwhale_api.federation.keys.get_key_pair",
|
|
|
|
return_value=(b"private", b"public"),
|
|
|
|
)
|
2018-03-31 13:47:21 +00:00
|
|
|
expected = {
|
2018-06-09 13:36:16 +00:00
|
|
|
"preferred_username": "library",
|
|
|
|
"domain": settings.FEDERATION_HOSTNAME,
|
|
|
|
"type": "Person",
|
|
|
|
"name": "{}'s library".format(settings.FEDERATION_HOSTNAME),
|
|
|
|
"manually_approves_followers": True,
|
|
|
|
"public_key": "public",
|
|
|
|
"url": utils.full_url(
|
|
|
|
reverse("federation:instance-actors-detail", kwargs={"actor": "library"})
|
|
|
|
),
|
|
|
|
"shared_inbox_url": utils.full_url(
|
|
|
|
reverse("federation:instance-actors-inbox", kwargs={"actor": "library"})
|
|
|
|
),
|
|
|
|
"inbox_url": utils.full_url(
|
|
|
|
reverse("federation:instance-actors-inbox", kwargs={"actor": "library"})
|
|
|
|
),
|
|
|
|
"outbox_url": utils.full_url(
|
|
|
|
reverse("federation:instance-actors-outbox", kwargs={"actor": "library"})
|
|
|
|
),
|
|
|
|
"summary": "Bot account to federate with {}'s library".format(
|
|
|
|
settings.FEDERATION_HOSTNAME
|
|
|
|
),
|
2018-03-31 13:47:21 +00:00
|
|
|
}
|
2018-06-09 13:36:16 +00:00
|
|
|
actor = actors.SYSTEM_ACTORS["library"].get_actor_instance()
|
2018-03-31 13:47:21 +00:00
|
|
|
for key, value in expected.items():
|
|
|
|
assert getattr(actor, key) == value
|
2018-04-01 20:11:46 +00:00
|
|
|
|
|
|
|
|
2018-04-07 13:34:35 +00:00
|
|
|
def test_get_test(db, mocker, settings):
|
2018-06-10 09:27:38 +00:00
|
|
|
mocker.patch(
|
2018-06-09 13:36:16 +00:00
|
|
|
"funkwhale_api.federation.keys.get_key_pair",
|
|
|
|
return_value=(b"private", b"public"),
|
|
|
|
)
|
2018-04-01 20:11:46 +00:00
|
|
|
expected = {
|
2018-06-09 13:36:16 +00:00
|
|
|
"preferred_username": "test",
|
|
|
|
"domain": settings.FEDERATION_HOSTNAME,
|
|
|
|
"type": "Person",
|
|
|
|
"name": "{}'s test account".format(settings.FEDERATION_HOSTNAME),
|
|
|
|
"manually_approves_followers": False,
|
|
|
|
"public_key": "public",
|
|
|
|
"url": utils.full_url(
|
|
|
|
reverse("federation:instance-actors-detail", kwargs={"actor": "test"})
|
|
|
|
),
|
|
|
|
"shared_inbox_url": utils.full_url(
|
|
|
|
reverse("federation:instance-actors-inbox", kwargs={"actor": "test"})
|
|
|
|
),
|
|
|
|
"inbox_url": utils.full_url(
|
|
|
|
reverse("federation:instance-actors-inbox", kwargs={"actor": "test"})
|
|
|
|
),
|
|
|
|
"outbox_url": utils.full_url(
|
|
|
|
reverse("federation:instance-actors-outbox", kwargs={"actor": "test"})
|
|
|
|
),
|
|
|
|
"summary": "Bot account to test federation with {}. Send me /ping and I'll answer you.".format(
|
|
|
|
settings.FEDERATION_HOSTNAME
|
|
|
|
),
|
2018-04-01 20:11:46 +00:00
|
|
|
}
|
2018-06-09 13:36:16 +00:00
|
|
|
actor = actors.SYSTEM_ACTORS["test"].get_actor_instance()
|
2018-04-01 20:11:46 +00:00
|
|
|
for key, value in expected.items():
|
|
|
|
assert getattr(actor, key) == value
|
|
|
|
|
|
|
|
|
|
|
|
def test_test_get_outbox():
|
|
|
|
expected = {
|
2018-04-04 17:38:55 +00:00
|
|
|
"@context": [
|
|
|
|
"https://www.w3.org/ns/activitystreams",
|
|
|
|
"https://w3id.org/security/v1",
|
2018-06-09 13:36:16 +00:00
|
|
|
{},
|
2018-04-04 17:38:55 +00:00
|
|
|
],
|
|
|
|
"id": utils.full_url(
|
2018-06-09 13:36:16 +00:00
|
|
|
reverse("federation:instance-actors-outbox", kwargs={"actor": "test"})
|
|
|
|
),
|
2018-04-04 17:38:55 +00:00
|
|
|
"type": "OrderedCollection",
|
|
|
|
"totalItems": 0,
|
2018-06-09 13:36:16 +00:00
|
|
|
"orderedItems": [],
|
2018-04-01 20:11:46 +00:00
|
|
|
}
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
data = actors.SYSTEM_ACTORS["test"].get_outbox({}, actor=None)
|
2018-04-01 20:11:46 +00:00
|
|
|
|
|
|
|
assert data == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_test_post_inbox_requires_authenticated_actor():
|
|
|
|
with pytest.raises(exceptions.PermissionDenied):
|
2018-06-09 13:36:16 +00:00
|
|
|
actors.SYSTEM_ACTORS["test"].post_inbox({}, actor=None)
|
2018-04-01 20:11:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_test_post_outbox_validates_actor(nodb_factories):
|
2018-06-09 13:36:16 +00:00
|
|
|
actor = nodb_factories["federation.Actor"]()
|
|
|
|
data = {"actor": "noop"}
|
2018-04-01 20:11:46 +00:00
|
|
|
with pytest.raises(exceptions.ValidationError) as exc_info:
|
2018-06-09 13:36:16 +00:00
|
|
|
actors.SYSTEM_ACTORS["test"].post_inbox(data, actor=actor)
|
|
|
|
msg = "The actor making the request do not match"
|
2018-04-01 20:11:46 +00:00
|
|
|
assert msg in exc_info.value
|
|
|
|
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
def test_test_post_inbox_handles_create_note(settings, mocker, factories):
|
|
|
|
deliver = mocker.patch("funkwhale_api.federation.activity.deliver")
|
|
|
|
actor = factories["federation.Actor"]()
|
2018-04-02 16:07:46 +00:00
|
|
|
now = timezone.now()
|
2018-06-09 13:36:16 +00:00
|
|
|
mocker.patch("django.utils.timezone.now", return_value=now)
|
2018-04-01 20:11:46 +00:00
|
|
|
data = {
|
2018-06-09 13:36:16 +00:00
|
|
|
"actor": actor.url,
|
|
|
|
"type": "Create",
|
|
|
|
"id": "http://test.federation/activity",
|
|
|
|
"object": {
|
|
|
|
"type": "Note",
|
|
|
|
"id": "http://test.federation/object",
|
|
|
|
"content": "<p><a>@mention</a> /ping</p>",
|
|
|
|
},
|
2018-04-01 20:11:46 +00:00
|
|
|
}
|
2018-06-09 13:36:16 +00:00
|
|
|
test_actor = actors.SYSTEM_ACTORS["test"].get_actor_instance()
|
|
|
|
expected_note = factories["federation.Note"](
|
|
|
|
id="https://test.federation/activities/note/{}".format(now.timestamp()),
|
|
|
|
content="Pong!",
|
2018-04-02 16:07:46 +00:00
|
|
|
published=now.isoformat(),
|
2018-06-09 13:36:16 +00:00
|
|
|
inReplyTo=data["object"]["id"],
|
2018-04-02 17:15:27 +00:00
|
|
|
cc=[],
|
|
|
|
summary=None,
|
|
|
|
sensitive=False,
|
|
|
|
attributedTo=test_actor.url,
|
|
|
|
attachment=[],
|
|
|
|
to=[actor.url],
|
2018-06-09 13:36:16 +00:00
|
|
|
url="https://{}/activities/note/{}".format(
|
2018-04-02 17:15:27 +00:00
|
|
|
settings.FEDERATION_HOSTNAME, now.timestamp()
|
|
|
|
),
|
2018-06-09 13:36:16 +00:00
|
|
|
tag=[{"href": actor.url, "name": actor.mention_username, "type": "Mention"}],
|
2018-04-02 16:07:46 +00:00
|
|
|
)
|
|
|
|
expected_activity = {
|
2018-06-09 13:36:16 +00:00
|
|
|
"@context": serializers.AP_CONTEXT,
|
|
|
|
"actor": test_actor.url,
|
|
|
|
"id": "https://{}/activities/note/{}/activity".format(
|
2018-04-02 17:15:27 +00:00
|
|
|
settings.FEDERATION_HOSTNAME, now.timestamp()
|
2018-04-02 16:07:46 +00:00
|
|
|
),
|
2018-06-09 13:36:16 +00:00
|
|
|
"to": actor.url,
|
|
|
|
"type": "Create",
|
|
|
|
"published": now.isoformat(),
|
|
|
|
"object": expected_note,
|
|
|
|
"cc": [],
|
2018-04-02 16:07:46 +00:00
|
|
|
}
|
2018-06-09 13:36:16 +00:00
|
|
|
actors.SYSTEM_ACTORS["test"].post_inbox(data, actor=actor)
|
2018-04-01 20:11:46 +00:00
|
|
|
deliver.assert_called_once_with(
|
2018-04-02 16:07:46 +00:00
|
|
|
expected_activity,
|
2018-04-01 20:11:46 +00:00
|
|
|
to=[actor.url],
|
2018-06-09 13:36:16 +00:00
|
|
|
on_behalf_of=actors.SYSTEM_ACTORS["test"].get_actor_instance(),
|
2018-04-01 20:11:46 +00:00
|
|
|
)
|
2018-04-02 20:12:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_getting_actor_instance_persists_in_db(db):
|
2018-06-09 13:36:16 +00:00
|
|
|
test = actors.SYSTEM_ACTORS["test"].get_actor_instance()
|
2018-04-02 20:12:28 +00:00
|
|
|
from_db = models.Actor.objects.get(url=test.url)
|
|
|
|
|
|
|
|
for f in test._meta.fields:
|
|
|
|
assert getattr(from_db, f.name) == getattr(test, f.name)
|
2018-04-02 20:43:59 +00:00
|
|
|
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"username,domain,expected",
|
|
|
|
[("test", "wrongdomain.com", False), ("notsystem", "", False), ("test", "", True)],
|
|
|
|
)
|
|
|
|
def test_actor_is_system(username, domain, expected, nodb_factories, settings):
|
2018-04-02 20:43:59 +00:00
|
|
|
if not domain:
|
|
|
|
domain = settings.FEDERATION_HOSTNAME
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
actor = nodb_factories["federation.Actor"](
|
|
|
|
preferred_username=username, domain=domain
|
2018-04-02 20:43:59 +00:00
|
|
|
)
|
|
|
|
assert actor.is_system is expected
|
|
|
|
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"username,domain,expected",
|
|
|
|
[
|
|
|
|
("test", "wrongdomain.com", None),
|
|
|
|
("notsystem", "", None),
|
|
|
|
("test", "", actors.SYSTEM_ACTORS["test"]),
|
|
|
|
],
|
|
|
|
)
|
2018-06-10 09:27:38 +00:00
|
|
|
def test_actor_system_conf(username, domain, expected, nodb_factories, settings):
|
2018-04-03 15:36:03 +00:00
|
|
|
if not domain:
|
|
|
|
domain = settings.FEDERATION_HOSTNAME
|
2018-06-09 13:36:16 +00:00
|
|
|
actor = nodb_factories["federation.Actor"](
|
|
|
|
preferred_username=username, domain=domain
|
2018-04-03 15:36:03 +00:00
|
|
|
)
|
|
|
|
assert actor.system_conf == expected
|
|
|
|
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
@pytest.mark.parametrize("value", [False, True])
|
|
|
|
def test_library_actor_manually_approves_based_on_preference(value, preferences):
|
|
|
|
preferences["federation__music_needs_approval"] = value
|
|
|
|
library_conf = actors.SYSTEM_ACTORS["library"]
|
2018-04-02 20:43:59 +00:00
|
|
|
assert library_conf.manually_approves_followers is value
|
2018-04-03 17:48:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_system_actor_handle(mocker, nodb_factories):
|
2018-06-09 13:36:16 +00:00
|
|
|
handler = mocker.patch("funkwhale_api.federation.actors.TestActor.handle_create")
|
|
|
|
actor = nodb_factories["federation.Actor"]()
|
|
|
|
activity = nodb_factories["federation.Activity"](type="Create", actor=actor.url)
|
|
|
|
serializer = serializers.ActivitySerializer(data=activity)
|
2018-04-03 17:48:50 +00:00
|
|
|
assert serializer.is_valid()
|
2018-06-09 13:36:16 +00:00
|
|
|
actors.SYSTEM_ACTORS["test"].handle(activity, actor)
|
2018-04-04 17:38:55 +00:00
|
|
|
handler.assert_called_once_with(activity, actor)
|
2018-04-03 17:48:50 +00:00
|
|
|
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
def test_test_actor_handles_follow(settings, mocker, factories):
|
|
|
|
deliver = mocker.patch("funkwhale_api.federation.activity.deliver")
|
|
|
|
actor = factories["federation.Actor"]()
|
|
|
|
accept_follow = mocker.patch("funkwhale_api.federation.activity.accept_follow")
|
|
|
|
test_actor = actors.SYSTEM_ACTORS["test"].get_actor_instance()
|
2018-04-03 17:48:50 +00:00
|
|
|
data = {
|
2018-06-09 13:36:16 +00:00
|
|
|
"actor": actor.url,
|
|
|
|
"type": "Follow",
|
|
|
|
"id": "http://test.federation/user#follows/267",
|
|
|
|
"object": test_actor.url,
|
2018-04-03 17:48:50 +00:00
|
|
|
}
|
2018-06-09 13:36:16 +00:00
|
|
|
actors.SYSTEM_ACTORS["test"].post_inbox(data, actor=actor)
|
2018-04-10 19:25:35 +00:00
|
|
|
follow = models.Follow.objects.get(target=test_actor, approved=True)
|
|
|
|
follow_back = models.Follow.objects.get(actor=test_actor, approved=None)
|
|
|
|
accept_follow.assert_called_once_with(follow)
|
|
|
|
deliver.assert_called_once_with(
|
|
|
|
serializers.FollowSerializer(follow_back).data,
|
|
|
|
on_behalf_of=test_actor,
|
2018-06-09 13:36:16 +00:00
|
|
|
to=[actor.url],
|
2018-04-04 17:38:55 +00:00
|
|
|
)
|
2018-04-03 19:30:15 +00:00
|
|
|
|
2018-04-03 21:25:44 +00:00
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
def test_test_actor_handles_undo_follow(settings, mocker, factories):
|
|
|
|
deliver = mocker.patch("funkwhale_api.federation.activity.deliver")
|
|
|
|
test_actor = actors.SYSTEM_ACTORS["test"].get_actor_instance()
|
|
|
|
follow = factories["federation.Follow"](target=test_actor)
|
|
|
|
reverse_follow = factories["federation.Follow"](
|
|
|
|
actor=test_actor, target=follow.actor
|
|
|
|
)
|
2018-04-03 21:25:44 +00:00
|
|
|
follow_serializer = serializers.FollowSerializer(follow)
|
2018-06-09 13:36:16 +00:00
|
|
|
reverse_follow_serializer = serializers.FollowSerializer(reverse_follow)
|
2018-04-03 21:25:44 +00:00
|
|
|
undo = {
|
2018-06-09 13:36:16 +00:00
|
|
|
"@context": serializers.AP_CONTEXT,
|
|
|
|
"type": "Undo",
|
|
|
|
"id": follow_serializer.data["id"] + "/undo",
|
|
|
|
"actor": follow.actor.url,
|
|
|
|
"object": follow_serializer.data,
|
2018-04-03 21:25:44 +00:00
|
|
|
}
|
|
|
|
expected_undo = {
|
2018-06-09 13:36:16 +00:00
|
|
|
"@context": serializers.AP_CONTEXT,
|
|
|
|
"type": "Undo",
|
|
|
|
"id": reverse_follow_serializer.data["id"] + "/undo",
|
|
|
|
"actor": reverse_follow.actor.url,
|
|
|
|
"object": reverse_follow_serializer.data,
|
2018-04-03 21:25:44 +00:00
|
|
|
}
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
actors.SYSTEM_ACTORS["test"].post_inbox(undo, actor=follow.actor)
|
2018-04-03 21:25:44 +00:00
|
|
|
deliver.assert_called_once_with(
|
2018-06-09 13:36:16 +00:00
|
|
|
expected_undo, to=[follow.actor.url], on_behalf_of=test_actor
|
|
|
|
)
|
2018-04-03 21:25:44 +00:00
|
|
|
|
|
|
|
assert models.Follow.objects.count() == 0
|
2018-04-04 17:38:55 +00:00
|
|
|
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
def test_library_actor_handles_follow_manual_approval(preferences, mocker, factories):
|
|
|
|
preferences["federation__music_needs_approval"] = True
|
|
|
|
actor = factories["federation.Actor"]()
|
2018-04-04 17:38:55 +00:00
|
|
|
now = timezone.now()
|
2018-06-09 13:36:16 +00:00
|
|
|
mocker.patch("django.utils.timezone.now", return_value=now)
|
|
|
|
library_actor = actors.SYSTEM_ACTORS["library"].get_actor_instance()
|
2018-04-04 17:38:55 +00:00
|
|
|
data = {
|
2018-06-09 13:36:16 +00:00
|
|
|
"actor": actor.url,
|
|
|
|
"type": "Follow",
|
|
|
|
"id": "http://test.federation/user#follows/267",
|
|
|
|
"object": library_actor.url,
|
2018-04-04 17:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
library_actor.system_conf.post_inbox(data, actor=actor)
|
2018-04-10 19:25:35 +00:00
|
|
|
follow = library_actor.received_follows.first()
|
2018-04-04 17:38:55 +00:00
|
|
|
|
2018-04-10 19:25:35 +00:00
|
|
|
assert follow.actor == actor
|
|
|
|
assert follow.approved is None
|
2018-04-04 17:38:55 +00:00
|
|
|
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
def test_library_actor_handles_follow_auto_approval(preferences, mocker, factories):
|
|
|
|
preferences["federation__music_needs_approval"] = False
|
|
|
|
actor = factories["federation.Actor"]()
|
2018-06-09 15:41:59 +00:00
|
|
|
mocker.patch("funkwhale_api.federation.activity.accept_follow")
|
2018-06-09 13:36:16 +00:00
|
|
|
library_actor = actors.SYSTEM_ACTORS["library"].get_actor_instance()
|
2018-04-04 17:38:55 +00:00
|
|
|
data = {
|
2018-06-09 13:36:16 +00:00
|
|
|
"actor": actor.url,
|
|
|
|
"type": "Follow",
|
|
|
|
"id": "http://test.federation/user#follows/267",
|
|
|
|
"object": library_actor.url,
|
2018-04-04 17:38:55 +00:00
|
|
|
}
|
|
|
|
library_actor.system_conf.post_inbox(data, actor=actor)
|
2018-04-04 21:12:41 +00:00
|
|
|
|
2018-04-10 19:25:35 +00:00
|
|
|
follow = library_actor.received_follows.first()
|
|
|
|
|
|
|
|
assert follow.actor == actor
|
|
|
|
assert follow.approved is True
|
|
|
|
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
def test_library_actor_handles_accept(mocker, factories):
|
|
|
|
library_actor = actors.SYSTEM_ACTORS["library"].get_actor_instance()
|
|
|
|
actor = factories["federation.Actor"]()
|
|
|
|
pending_follow = factories["federation.Follow"](
|
|
|
|
actor=library_actor, target=actor, approved=None
|
2018-04-04 21:12:41 +00:00
|
|
|
)
|
2018-04-10 19:25:35 +00:00
|
|
|
serializer = serializers.AcceptFollowSerializer(pending_follow)
|
|
|
|
library_actor.system_conf.post_inbox(serializer.data, actor=actor)
|
|
|
|
|
|
|
|
pending_follow.refresh_from_db()
|
|
|
|
|
|
|
|
assert pending_follow.approved is True
|
2018-04-06 12:45:06 +00:00
|
|
|
|
|
|
|
|
2018-04-06 16:49:29 +00:00
|
|
|
def test_library_actor_handle_create_audio_no_library(mocker, factories):
|
2018-04-06 12:45:06 +00:00
|
|
|
# when we receive inbox create audio, we should not do anything
|
2018-04-06 16:49:29 +00:00
|
|
|
# if we don't have a configured library matching the sender
|
2018-04-06 12:45:06 +00:00
|
|
|
mocked_create = mocker.patch(
|
2018-06-09 13:36:16 +00:00
|
|
|
"funkwhale_api.federation.serializers.AudioSerializer.create"
|
2018-04-06 12:45:06 +00:00
|
|
|
)
|
2018-06-09 13:36:16 +00:00
|
|
|
actor = factories["federation.Actor"]()
|
|
|
|
library_actor = actors.SYSTEM_ACTORS["library"].get_actor_instance()
|
2018-04-06 12:45:06 +00:00
|
|
|
data = {
|
2018-06-09 13:36:16 +00:00
|
|
|
"actor": actor.url,
|
|
|
|
"type": "Create",
|
|
|
|
"id": "http://test.federation/audio/create",
|
|
|
|
"object": {
|
|
|
|
"id": "https://batch.import",
|
|
|
|
"type": "Collection",
|
|
|
|
"totalItems": 2,
|
|
|
|
"items": factories["federation.Audio"].create_batch(size=2),
|
2018-04-06 12:45:06 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
library_actor.system_conf.post_inbox(data, actor=actor)
|
|
|
|
|
|
|
|
mocked_create.assert_not_called()
|
2018-04-07 09:29:40 +00:00
|
|
|
models.LibraryTrack.objects.count() == 0
|
2018-04-06 12:45:06 +00:00
|
|
|
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
def test_library_actor_handle_create_audio_no_library_enabled(mocker, factories):
|
2018-04-06 16:49:29 +00:00
|
|
|
# when we receive inbox create audio, we should not do anything
|
|
|
|
# if we don't have an enabled library
|
|
|
|
mocked_create = mocker.patch(
|
2018-06-09 13:36:16 +00:00
|
|
|
"funkwhale_api.federation.serializers.AudioSerializer.create"
|
2018-04-06 16:49:29 +00:00
|
|
|
)
|
2018-06-09 13:36:16 +00:00
|
|
|
disabled_library = factories["federation.Library"](federation_enabled=False)
|
2018-04-06 16:49:29 +00:00
|
|
|
actor = disabled_library.actor
|
2018-06-09 13:36:16 +00:00
|
|
|
library_actor = actors.SYSTEM_ACTORS["library"].get_actor_instance()
|
2018-04-06 16:49:29 +00:00
|
|
|
data = {
|
2018-06-09 13:36:16 +00:00
|
|
|
"actor": actor.url,
|
|
|
|
"type": "Create",
|
|
|
|
"id": "http://test.federation/audio/create",
|
|
|
|
"object": {
|
|
|
|
"id": "https://batch.import",
|
|
|
|
"type": "Collection",
|
|
|
|
"totalItems": 2,
|
|
|
|
"items": factories["federation.Audio"].create_batch(size=2),
|
2018-04-06 16:49:29 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
library_actor.system_conf.post_inbox(data, actor=actor)
|
|
|
|
|
|
|
|
mocked_create.assert_not_called()
|
2018-04-07 09:29:40 +00:00
|
|
|
models.LibraryTrack.objects.count() == 0
|
2018-04-06 16:49:29 +00:00
|
|
|
|
|
|
|
|
2018-04-06 12:45:06 +00:00
|
|
|
def test_library_actor_handle_create_audio(mocker, factories):
|
2018-06-09 13:36:16 +00:00
|
|
|
library_actor = actors.SYSTEM_ACTORS["library"].get_actor_instance()
|
|
|
|
remote_library = factories["federation.Library"](federation_enabled=True)
|
2018-04-06 12:45:06 +00:00
|
|
|
|
|
|
|
data = {
|
2018-06-09 13:36:16 +00:00
|
|
|
"actor": remote_library.actor.url,
|
|
|
|
"type": "Create",
|
|
|
|
"id": "http://test.federation/audio/create",
|
|
|
|
"object": {
|
|
|
|
"id": "https://batch.import",
|
|
|
|
"type": "Collection",
|
|
|
|
"totalItems": 2,
|
|
|
|
"items": factories["federation.Audio"].create_batch(size=2),
|
2018-04-06 12:45:06 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-04-06 16:49:29 +00:00
|
|
|
library_actor.system_conf.post_inbox(data, actor=remote_library.actor)
|
2018-04-06 12:45:06 +00:00
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
lts = list(remote_library.tracks.order_by("id"))
|
2018-04-06 12:45:06 +00:00
|
|
|
|
2018-04-07 09:29:40 +00:00
|
|
|
assert len(lts) == 2
|
2018-04-06 12:45:06 +00:00
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
for i, a in enumerate(data["object"]["items"]):
|
2018-04-07 09:29:40 +00:00
|
|
|
lt = lts[i]
|
|
|
|
assert lt.pk is not None
|
2018-06-09 13:36:16 +00:00
|
|
|
assert lt.url == a["id"]
|
2018-04-07 09:29:40 +00:00
|
|
|
assert lt.library == remote_library
|
2018-06-09 13:36:16 +00:00
|
|
|
assert lt.audio_url == a["url"]["href"]
|
|
|
|
assert lt.audio_mimetype == a["url"]["mediaType"]
|
|
|
|
assert lt.metadata == a["metadata"]
|
|
|
|
assert lt.title == a["metadata"]["recording"]["title"]
|
|
|
|
assert lt.artist_name == a["metadata"]["artist"]["name"]
|
|
|
|
assert lt.album_title == a["metadata"]["release"]["title"]
|
|
|
|
assert lt.published_date == arrow.get(a["published"])
|
2018-04-16 19:59:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_library_actor_handle_create_audio_autoimport(mocker, factories):
|
2018-06-09 13:36:16 +00:00
|
|
|
mocked_import = mocker.patch("funkwhale_api.common.utils.on_commit")
|
|
|
|
library_actor = actors.SYSTEM_ACTORS["library"].get_actor_instance()
|
|
|
|
remote_library = factories["federation.Library"](
|
|
|
|
federation_enabled=True, autoimport=True
|
2018-04-16 19:59:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
data = {
|
2018-06-09 13:36:16 +00:00
|
|
|
"actor": remote_library.actor.url,
|
|
|
|
"type": "Create",
|
|
|
|
"id": "http://test.federation/audio/create",
|
|
|
|
"object": {
|
|
|
|
"id": "https://batch.import",
|
|
|
|
"type": "Collection",
|
|
|
|
"totalItems": 2,
|
|
|
|
"items": factories["federation.Audio"].create_batch(size=2),
|
2018-04-16 19:59:13 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
library_actor.system_conf.post_inbox(data, actor=remote_library.actor)
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
lts = list(remote_library.tracks.order_by("id"))
|
2018-04-16 19:59:13 +00:00
|
|
|
|
|
|
|
assert len(lts) == 2
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
for i, a in enumerate(data["object"]["items"]):
|
2018-04-16 19:59:13 +00:00
|
|
|
lt = lts[i]
|
|
|
|
assert lt.pk is not None
|
2018-06-09 13:36:16 +00:00
|
|
|
assert lt.url == a["id"]
|
2018-04-16 19:59:13 +00:00
|
|
|
assert lt.library == remote_library
|
2018-06-09 13:36:16 +00:00
|
|
|
assert lt.audio_url == a["url"]["href"]
|
|
|
|
assert lt.audio_mimetype == a["url"]["mediaType"]
|
|
|
|
assert lt.metadata == a["metadata"]
|
|
|
|
assert lt.title == a["metadata"]["recording"]["title"]
|
|
|
|
assert lt.artist_name == a["metadata"]["artist"]["name"]
|
|
|
|
assert lt.album_title == a["metadata"]["release"]["title"]
|
|
|
|
assert lt.published_date == arrow.get(a["published"])
|
2018-04-16 19:59:13 +00:00
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
batch = music_models.ImportBatch.objects.latest("id")
|
2018-04-16 19:59:13 +00:00
|
|
|
|
|
|
|
assert batch.jobs.count() == len(lts)
|
2018-06-09 13:36:16 +00:00
|
|
|
assert batch.source == "federation"
|
2018-04-16 19:59:13 +00:00
|
|
|
assert batch.submitted_by is None
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
for i, job in enumerate(batch.jobs.order_by("id")):
|
2018-04-16 19:59:13 +00:00
|
|
|
lt = lts[i]
|
|
|
|
assert job.library_track == lt
|
|
|
|
assert job.mbid == lt.mbid
|
|
|
|
assert job.source == lt.url
|
|
|
|
|
|
|
|
mocked_import.assert_any_call(
|
2018-06-09 13:36:16 +00:00
|
|
|
music_tasks.import_job_run.delay, import_job_id=job.pk, use_acoustid=False
|
2018-04-16 19:59:13 +00:00
|
|
|
)
|