Newer
Older
Eliot Berriot
committed
from funkwhale_api.federation import activity, serializers, tasks
Eliot Berriot
committed
def test_accept_follow(mocker, factories):
deliver = mocker.patch("funkwhale_api.federation.activity.deliver")
follow = factories["federation.Follow"](approved=None)
Eliot Berriot
committed
expected_accept = serializers.AcceptFollowSerializer(follow).data
activity.accept_follow(follow)
Eliot Berriot
committed
deliver.assert_called_once_with(
expected_accept, to=[follow.actor.fid], on_behalf_of=follow.target
Eliot Berriot
committed
)
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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