1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.FederatorTest do
6 alias Pleroma.Instances
7 alias Pleroma.Tests.ObanHelpers
8 alias Pleroma.Web.CommonAPI
9 alias Pleroma.Web.Federator
10 alias Pleroma.Workers.PublisherWorker
13 use Oban.Testing, repo: Pleroma.Repo
15 import Pleroma.Factory
19 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
24 clear_config_all([:instance, :federating]) do
25 Pleroma.Config.put([:instance, :federating], true)
28 clear_config([:instance, :allow_relay])
29 clear_config([:instance, :rewrite_policy])
30 clear_config([:mrf_keyword])
32 describe "Publish an activity" do
35 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI"})
38 Pleroma.Web.ActivityPub.Relay,
40 [publish: fn _activity -> send(self(), :relay_publish) end]
43 %{activity: activity, relay_mock: relay_mock}
46 test "with relays active, it publishes to the relay", %{
48 relay_mock: relay_mock
50 with_mocks([relay_mock]) do
51 Federator.publish(activity)
52 ObanHelpers.perform(all_enqueued(worker: PublisherWorker))
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.publish(activity)
66 ObanHelpers.perform(all_enqueued(worker: PublisherWorker))
69 refute_received :relay_publish
73 describe "Targets reachability filtering in `publish`" do
74 test "it federates only to reachable instances via AP" do
78 {"https://domain.com/users/nick1/inbox", "https://domain2.com/users/nick2/inbox"}
82 nickname: "nick1@domain.com",
83 ap_id: "https://domain.com/users/nick1",
84 info: %{ap_enabled: true, source_data: %{"inbox" => inbox1}}
89 nickname: "nick2@domain2.com",
90 ap_id: "https://domain2.com/users/nick2",
91 info: %{ap_enabled: true, source_data: %{"inbox" => inbox2}}
94 dt = NaiveDateTime.utc_now()
95 Instances.set_unreachable(inbox1, dt)
97 Instances.set_consistently_unreachable(URI.parse(inbox2).host)
100 CommonAPI.post(user, %{"status" => "HI @nick1@domain.com, @nick2@domain2.com!"})
102 expected_dt = NaiveDateTime.to_iso8601(dt)
104 ObanHelpers.perform(all_enqueued(worker: PublisherWorker))
106 assert ObanHelpers.member?(
108 "op" => "publish_one",
109 "params" => %{"inbox" => inbox1, "unreachable_since" => expected_dt}
111 all_enqueued(worker: PublisherWorker)
115 test "it federates only to reachable instances via Websub" do
117 websub_topic = Pleroma.Web.OStatus.feed_path(user)
120 insert(:websub_subscription, %{
123 callback: "http://pleroma.soykaf.com/cb"
127 insert(:websub_subscription, %{
130 callback: "https://pleroma2.soykaf.com/cb"
133 dt = NaiveDateTime.utc_now()
134 Instances.set_unreachable(sub2.callback, dt)
136 Instances.set_consistently_unreachable(sub1.callback)
138 {:ok, _activity} = CommonAPI.post(user, %{"status" => "HI"})
140 expected_callback = sub2.callback
141 expected_dt = NaiveDateTime.to_iso8601(dt)
143 ObanHelpers.perform(all_enqueued(worker: PublisherWorker))
145 assert ObanHelpers.member?(
147 "op" => "publish_one",
149 "callback" => expected_callback,
150 "unreachable_since" => expected_dt
153 all_enqueued(worker: PublisherWorker)
157 test "it federates only to reachable instances via Salmon" do
163 nickname: "nick1@domain.com",
164 ap_id: "https://domain.com/users/nick1",
165 info: %{salmon: "https://domain.com/salmon"}
171 nickname: "nick2@domain2.com",
172 ap_id: "https://domain2.com/users/nick2",
173 info: %{salmon: "https://domain2.com/salmon"}
176 remote_user2_id = remote_user2.id
178 dt = NaiveDateTime.utc_now()
179 Instances.set_unreachable(remote_user2.ap_id, dt)
181 Instances.set_consistently_unreachable("domain.com")
184 CommonAPI.post(user, %{"status" => "HI @nick1@domain.com, @nick2@domain2.com!"})
186 expected_dt = NaiveDateTime.to_iso8601(dt)
188 ObanHelpers.perform(all_enqueued(worker: PublisherWorker))
190 assert ObanHelpers.member?(
192 "op" => "publish_one",
194 "recipient_id" => remote_user2_id,
195 "unreachable_since" => expected_dt
198 all_enqueued(worker: PublisherWorker)
203 describe "Receive an activity" do
204 test "successfully processes incoming AP docs with correct origin" do
206 "@context" => "https://www.w3.org/ns/activitystreams",
207 "actor" => "http://mastodon.example.org/users/admin",
209 "id" => "http://mastodon.example.org/users/admin/activities/1",
212 "content" => "hi world!",
213 "id" => "http://mastodon.example.org/users/admin/objects/1",
214 "attributedTo" => "http://mastodon.example.org/users/admin"
216 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
219 assert {:ok, job} = Federator.incoming_ap_doc(params)
220 assert {:ok, _activity} = ObanHelpers.perform(job)
223 test "rejects incoming AP docs with incorrect origin" do
225 "@context" => "https://www.w3.org/ns/activitystreams",
226 "actor" => "https://niu.moe/users/rye",
228 "id" => "http://mastodon.example.org/users/admin/activities/1",
231 "content" => "hi world!",
232 "id" => "http://mastodon.example.org/users/admin/objects/1",
233 "attributedTo" => "http://mastodon.example.org/users/admin"
235 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
238 assert {:ok, job} = Federator.incoming_ap_doc(params)
239 assert :error = ObanHelpers.perform(job)
242 test "it does not crash if MRF rejects the post" do
243 Pleroma.Config.put([:mrf_keyword, :reject], ["lain"])
246 [:instance, :rewrite_policy],
247 Pleroma.Web.ActivityPub.MRF.KeywordPolicy
251 File.read!("test/fixtures/mastodon-post-activity.json")
254 assert {:ok, job} = Federator.incoming_ap_doc(params)
255 assert :error = ObanHelpers.perform(job)