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.Instances
7 alias Pleroma.ObanHelpers
8 alias Pleroma.Web.CommonAPI
9 alias Pleroma.Web.Federator
10 alias Pleroma.Workers.Publisher, as: PublisherWorker
13 use Oban.Testing, repo: Pleroma.Repo
15 import Pleroma.Factory
19 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
21 config_path = [:instance, :federating]
22 initial_setting = Pleroma.Config.get(config_path)
24 Pleroma.Config.put(config_path, true)
25 on_exit(fn -> Pleroma.Config.put(config_path, initial_setting) end)
30 describe "Publish an activity" do
33 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI"})
36 Pleroma.Web.ActivityPub.Relay,
38 [publish: fn _activity -> send(self(), :relay_publish) end]
41 %{activity: activity, relay_mock: relay_mock}
44 test "with relays active, it publishes to the relay", %{
46 relay_mock: relay_mock
48 with_mocks([relay_mock]) do
49 Federator.publish(activity)
50 ObanHelpers.perform(all_enqueued(worker: PublisherWorker))
53 assert_received :relay_publish
56 test "with relays deactivated, it does not publish to the relay", %{
58 relay_mock: relay_mock
60 Pleroma.Config.put([:instance, :allow_relay], false)
62 with_mocks([relay_mock]) do
63 Federator.publish(activity)
64 ObanHelpers.perform(all_enqueued(worker: PublisherWorker))
67 refute_received :relay_publish
69 Pleroma.Config.put([:instance, :allow_relay], true)
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 policies = Pleroma.Config.get([:instance, :rewrite_policy])
244 mrf_keyword_policy = Pleroma.Config.get(:mrf_keyword)
245 Pleroma.Config.put([:mrf_keyword, :reject], ["lain"])
246 Pleroma.Config.put([:instance, :rewrite_policy], Pleroma.Web.ActivityPub.MRF.KeywordPolicy)
249 File.read!("test/fixtures/mastodon-post-activity.json")
252 assert {:ok, job} = Federator.incoming_ap_doc(params)
253 assert :error = ObanHelpers.perform(job)
255 Pleroma.Config.put([:instance, :rewrite_policy], policies)
256 Pleroma.Config.put(:mrf_keyword, mrf_keyword_policy)