1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.FederatorTest do
6 alias Pleroma.Web.{CommonAPI, Federator}
7 alias Pleroma.Instances
13 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
17 test "enqueues an element according to priority" do
18 queue = [%{item: 1, priority: 2}]
20 new_queue = Federator.enqueue_sorted(queue, 2, 1)
21 assert new_queue == [%{item: 2, priority: 1}, %{item: 1, priority: 2}]
23 new_queue = Federator.enqueue_sorted(queue, 2, 3)
24 assert new_queue == [%{item: 1, priority: 2}, %{item: 2, priority: 3}]
27 test "pop first item" do
28 queue = [%{item: 2, priority: 1}, %{item: 1, priority: 2}]
30 assert {2, [%{item: 1, priority: 2}]} = Federator.queue_pop(queue)
33 describe "Publish an activity" do
36 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI"})
39 Pleroma.Web.ActivityPub.Relay,
41 [publish: fn _activity -> send(self(), :relay_publish) end]
44 %{activity: activity, relay_mock: relay_mock}
47 test "with relays active, it publishes to the relay", %{
49 relay_mock: relay_mock
51 with_mocks([relay_mock]) do
52 Federator.handle(:publish, activity)
55 assert_received :relay_publish
58 test "with relays deactivated, it does not publish to the relay", %{
60 relay_mock: relay_mock
62 Pleroma.Config.put([:instance, :allow_relay], false)
64 with_mocks([relay_mock]) do
65 Federator.handle(:publish, activity)
68 refute_received :relay_publish
70 Pleroma.Config.put([:instance, :allow_relay], true)
74 describe "Targets reachability filtering in `publish`" do
75 test_with_mock "it federates only to reachable instances via AP",
82 {"https://domain.com/users/nick1/inbox", "https://domain2.com/users/nick2/inbox"}
86 nickname: "nick1@domain.com",
87 ap_id: "https://domain.com/users/nick1",
88 info: %{ap_enabled: true, source_data: %{"inbox" => inbox1}}
93 nickname: "nick2@domain2.com",
94 ap_id: "https://domain2.com/users/nick2",
95 info: %{ap_enabled: true, source_data: %{"inbox" => inbox2}}
98 Instances.set_unreachable(
99 URI.parse(inbox2).host,
100 Instances.reachability_datetime_threshold()
104 CommonAPI.post(user, %{"status" => "HI @nick1@domain.com, @nick2@domain2.com!"})
106 assert called(Federator.enqueue(:publish_single_ap, %{inbox: inbox1}))
107 refute called(Federator.enqueue(:publish_single_ap, %{inbox: inbox2}))
110 test_with_mock "it federates only to reachable instances via Websub",
115 websub_topic = Pleroma.Web.OStatus.feed_path(user)
118 insert(:websub_subscription, %{
121 callback: "http://pleroma.soykaf.com/cb"
125 insert(:websub_subscription, %{
128 callback: "https://pleroma2.soykaf.com/cb"
131 Instances.set_consistently_unreachable(sub1.callback)
133 {:ok, _activity} = CommonAPI.post(user, %{"status" => "HI"})
135 assert called(Federator.enqueue(:publish_single_websub, %{callback: sub2.callback}))
136 refute called(Federator.enqueue(:publish_single_websub, %{callback: sub1.callback}))
139 test_with_mock "it federates only to reachable instances via Salmon",
148 nickname: "nick1@domain.com",
149 ap_id: "https://domain.com/users/nick1",
150 info: %{salmon: "https://domain.com/salmon"}
156 nickname: "nick2@domain2.com",
157 ap_id: "https://domain2.com/users/nick2",
158 info: %{salmon: "https://domain2.com/salmon"}
161 Instances.set_consistently_unreachable("domain.com")
164 CommonAPI.post(user, %{"status" => "HI @nick1@domain.com, @nick2@domain2.com!"})
166 assert called(Federator.enqueue(:publish_single_salmon, %{recipient: remote_user2}))
167 refute called(Federator.enqueue(:publish_single_websub, %{recipient: remote_user1}))
171 describe "Receive an activity" do
172 test "successfully processes incoming AP docs with correct origin" do
174 "@context" => "https://www.w3.org/ns/activitystreams",
175 "actor" => "http://mastodon.example.org/users/admin",
177 "id" => "http://mastodon.example.org/users/admin/activities/1",
180 "content" => "hi world!",
181 "id" => "http://mastodon.example.org/users/admin/objects/1",
182 "attributedTo" => "http://mastodon.example.org/users/admin"
184 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
187 {:ok, _activity} = Federator.handle(:incoming_ap_doc, params)
190 test "rejects incoming AP docs with incorrect origin" do
192 "@context" => "https://www.w3.org/ns/activitystreams",
193 "actor" => "https://niu.moe/users/rye",
195 "id" => "http://mastodon.example.org/users/admin/activities/1",
198 "content" => "hi world!",
199 "id" => "http://mastodon.example.org/users/admin/objects/1",
200 "attributedTo" => "http://mastodon.example.org/users/admin"
202 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
205 :error = Federator.handle(:incoming_ap_doc, params)