mirror of
https://code.eliotberriot.com/funkwhale/funkwhale.git
synced 2025-10-05 20:01:54 +02:00
Resolve "Per-user libraries" (use !368 instead)
This commit is contained in:
parent
b0ca181016
commit
2ea21994ee
144 changed files with 6749 additions and 5347 deletions
|
@ -1,32 +1,7 @@
|
|||
|
||||
from funkwhale_api.federation import activity, serializers
|
||||
import pytest
|
||||
|
||||
|
||||
def test_deliver(factories, r_mock, mocker, settings):
|
||||
settings.CELERY_TASK_ALWAYS_EAGER = True
|
||||
to = factories["federation.Actor"]()
|
||||
mocker.patch("funkwhale_api.federation.actors.get_actor", return_value=to)
|
||||
sender = factories["federation.Actor"]()
|
||||
ac = {
|
||||
"id": "http://test.federation/activity",
|
||||
"type": "Create",
|
||||
"actor": sender.url,
|
||||
"object": {
|
||||
"id": "http://test.federation/note",
|
||||
"type": "Note",
|
||||
"content": "Hello",
|
||||
},
|
||||
}
|
||||
|
||||
r_mock.post(to.inbox_url)
|
||||
|
||||
activity.deliver(ac, to=[to.url], on_behalf_of=sender)
|
||||
request = r_mock.request_history[0]
|
||||
|
||||
assert r_mock.called is True
|
||||
assert r_mock.call_count == 1
|
||||
assert request.url == to.inbox_url
|
||||
assert request.headers["content-type"] == "application/activity+json"
|
||||
from funkwhale_api.federation import activity, serializers, tasks
|
||||
|
||||
|
||||
def test_accept_follow(mocker, factories):
|
||||
|
@ -35,5 +10,125 @@ def test_accept_follow(mocker, factories):
|
|||
expected_accept = serializers.AcceptFollowSerializer(follow).data
|
||||
activity.accept_follow(follow)
|
||||
deliver.assert_called_once_with(
|
||||
expected_accept, to=[follow.actor.url], on_behalf_of=follow.target
|
||||
expected_accept, to=[follow.actor.fid], on_behalf_of=follow.target
|
||||
)
|
||||
|
||||
|
||||
def test_receive_validates_basic_attributes_and_stores_activity(factories, now, mocker):
|
||||
mocked_dispatch = mocker.patch(
|
||||
"funkwhale_api.federation.tasks.dispatch_inbox.delay"
|
||||
)
|
||||
local_actor = factories["users.User"]().create_actor()
|
||||
remote_actor = factories["federation.Actor"]()
|
||||
another_actor = factories["federation.Actor"]()
|
||||
a = {
|
||||
"@context": [],
|
||||
"actor": remote_actor.fid,
|
||||
"type": "Noop",
|
||||
"id": "https://test.activity",
|
||||
"to": [local_actor.fid],
|
||||
"cc": [another_actor.fid, activity.PUBLIC_ADDRESS],
|
||||
}
|
||||
|
||||
copy = activity.receive(activity=a, on_behalf_of=remote_actor)
|
||||
|
||||
assert copy.payload == a
|
||||
assert copy.creation_date >= now
|
||||
assert copy.actor == remote_actor
|
||||
assert copy.fid == a["id"]
|
||||
mocked_dispatch.assert_called_once_with(activity_id=copy.pk)
|
||||
|
||||
inbox_item = copy.inbox_items.get(actor__fid=local_actor.fid)
|
||||
assert inbox_item.is_delivered is False
|
||||
|
||||
|
||||
def test_receive_invalid_data(factories):
|
||||
remote_actor = factories["federation.Actor"]()
|
||||
a = {"@context": [], "actor": remote_actor.fid, "id": "https://test.activity"}
|
||||
|
||||
with pytest.raises(serializers.serializers.ValidationError):
|
||||
activity.receive(activity=a, on_behalf_of=remote_actor)
|
||||
|
||||
|
||||
def test_receive_actor_mismatch(factories):
|
||||
remote_actor = factories["federation.Actor"]()
|
||||
a = {
|
||||
"@context": [],
|
||||
"type": "Noop",
|
||||
"actor": "https://hello",
|
||||
"id": "https://test.activity",
|
||||
}
|
||||
|
||||
with pytest.raises(serializers.serializers.ValidationError):
|
||||
activity.receive(activity=a, on_behalf_of=remote_actor)
|
||||
|
||||
|
||||
def test_inbox_routing(mocker):
|
||||
router = activity.InboxRouter()
|
||||
|
||||
handler = mocker.stub(name="handler")
|
||||
router.connect({"type": "Follow"}, handler)
|
||||
|
||||
good_message = {"type": "Follow"}
|
||||
router.dispatch(good_message, context={})
|
||||
|
||||
handler.assert_called_once_with(good_message, context={})
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"route,payload,expected",
|
||||
[
|
||||
({"type": "Follow"}, {"type": "Follow"}, True),
|
||||
({"type": "Follow"}, {"type": "Noop"}, False),
|
||||
({"type": "Follow"}, {"type": "Follow", "id": "https://hello"}, True),
|
||||
],
|
||||
)
|
||||
def test_route_matching(route, payload, expected):
|
||||
assert activity.match_route(route, payload) is expected
|
||||
|
||||
|
||||
def test_outbox_router_dispatch(mocker, factories, now):
|
||||
router = activity.OutboxRouter()
|
||||
recipient = factories["federation.Actor"]()
|
||||
actor = factories["federation.Actor"]()
|
||||
r1 = factories["federation.Actor"]()
|
||||
r2 = factories["federation.Actor"]()
|
||||
mocked_dispatch = mocker.patch("funkwhale_api.common.utils.on_commit")
|
||||
|
||||
def handler(context):
|
||||
yield {
|
||||
"payload": {
|
||||
"type": "Noop",
|
||||
"actor": actor.fid,
|
||||
"summary": context["summary"],
|
||||
},
|
||||
"actor": actor,
|
||||
"to": [r1],
|
||||
"cc": [r2, activity.PUBLIC_ADDRESS],
|
||||
}
|
||||
|
||||
router.connect({"type": "Noop"}, handler)
|
||||
activities = router.dispatch({"type": "Noop"}, {"summary": "hello"})
|
||||
a = activities[0]
|
||||
|
||||
mocked_dispatch.assert_called_once_with(
|
||||
tasks.dispatch_outbox.delay, activity_id=a.pk
|
||||
)
|
||||
|
||||
assert a.payload == {
|
||||
"type": "Noop",
|
||||
"actor": actor.fid,
|
||||
"summary": "hello",
|
||||
"to": [r1.fid],
|
||||
"cc": [r2.fid, activity.PUBLIC_ADDRESS],
|
||||
}
|
||||
assert a.actor == actor
|
||||
assert a.creation_date >= now
|
||||
assert a.uuid is not None
|
||||
|
||||
for recipient, type in [(r1, "to"), (r2, "cc")]:
|
||||
item = a.inbox_items.get(actor=recipient)
|
||||
assert item.is_delivered is False
|
||||
assert item.last_delivery_date is None
|
||||
assert item.delivery_attempts == 0
|
||||
assert item.type == type
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue