def test_inbox_routing(factories, mocker): object = factories["music.Artist"]() target = factories["music.Artist"]() router = activity.InboxRouter() a = factories["federation.Activity"](type="Follow") handler_payload = {} handler_context = {} def handler(payload, context): handler_payload.update(payload) handler_context.update(context) return {"target": target, "object": object} router.connect({"type": "Follow"}, handler) good_message = {"type": "Follow"} router.dispatch(good_message, context={"activity": a}) assert handler_payload == good_message assert handler_context == {"activity": a} a.refresh_from_db() assert a.object == object assert a.target == target
def test_inbox_routing_send_to_channel(factories, mocker): group_send = mocker.patch("funkwhale_api.common.channels.group_send") a = factories["federation.Activity"](type="Follow") ii = factories["federation.InboxItem"](actor__local=True) router = activity.InboxRouter() handler = mocker.stub() router.connect({"type": "Follow"}, handler) good_message = {"type": "Follow"} router.dispatch(good_message, context={ "activity": a, "inbox_items": ii.__class__.objects.all() }) ii.refresh_from_db() group_send.assert_called_once_with( "user.{}.inbox".format(ii.actor.user.pk), { "type": "event.send", "text": "", "data": { "type": "inbox.item_added", "item": api_serializers.InboxItemSerializer(ii).data, }, }, )
def test_inbox_routing_no_handler(factories, mocker): router = activity.InboxRouter() a = factories["federation.Activity"](type="Follow") handler = mocker.Mock() router.connect({"type": "Follow"}, handler) router.dispatch({"type": "Follow"}, context={"activity": a}, call_handlers=False) handler.assert_not_called()