1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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 setup_all do: clear_config([:instance, :federating], true)
25 setup do: clear_config([:instance, :allow_relay], true)
26 setup do: clear_config([:mrf, :policies])
27 setup do: clear_config([:mrf_keyword])
29 describe "Publish an activity" do
32 {:ok, activity} = CommonAPI.post(user, %{status: "HI"})
35 Pleroma.Web.ActivityPub.Relay,
37 [publish: fn _activity -> send(self(), :relay_publish) end]
40 %{activity: activity, relay_mock: relay_mock}
43 test "with relays active, it publishes to the relay", %{
45 relay_mock: relay_mock
47 with_mocks([relay_mock]) do
48 Federator.publish(activity)
49 ObanHelpers.perform(all_enqueued(worker: PublisherWorker))
52 assert_received :relay_publish
55 test "with relays deactivated, it does not publish to the relay", %{
57 relay_mock: relay_mock
59 clear_config([:instance, :allow_relay], false)
61 with_mocks([relay_mock]) do
62 Federator.publish(activity)
63 ObanHelpers.perform(all_enqueued(worker: PublisherWorker))
66 refute_received :relay_publish
70 describe "Targets reachability filtering in `publish`" do
71 test "it federates only to reachable instances via AP" do
75 {"https://domain.com/users/nick1/inbox", "https://domain2.com/users/nick2/inbox"}
79 nickname: "nick1@domain.com",
80 ap_id: "https://domain.com/users/nick1",
87 nickname: "nick2@domain2.com",
88 ap_id: "https://domain2.com/users/nick2",
93 dt = NaiveDateTime.utc_now()
94 Instances.set_unreachable(inbox1, dt)
96 Instances.set_consistently_unreachable(URI.parse(inbox2).host)
99 CommonAPI.post(user, %{status: "HI @nick1@domain.com, @nick2@domain2.com!"})
101 expected_dt = NaiveDateTime.to_iso8601(dt)
103 ObanHelpers.perform(all_enqueued(worker: PublisherWorker))
105 assert ObanHelpers.member?(
107 "op" => "publish_one",
108 "params" => %{"inbox" => inbox1, "unreachable_since" => expected_dt}
110 all_enqueued(worker: PublisherWorker)
115 describe "Receive an activity" do
116 test "successfully processes incoming AP docs with correct origin" do
118 "@context" => "https://www.w3.org/ns/activitystreams",
119 "actor" => "http://mastodon.example.org/users/admin",
121 "id" => "http://mastodon.example.org/users/admin/activities/1",
124 "content" => "hi world!",
125 "id" => "http://mastodon.example.org/users/admin/objects/1",
126 "attributedTo" => "http://mastodon.example.org/users/admin",
127 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
129 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
132 assert {:ok, job} = Federator.incoming_ap_doc(params)
133 assert {:ok, _activity} = ObanHelpers.perform(job)
135 assert {:ok, job} = Federator.incoming_ap_doc(params)
136 assert {:error, :already_present} = ObanHelpers.perform(job)
139 test "rejects incoming AP docs with incorrect origin" do
141 "@context" => "https://www.w3.org/ns/activitystreams",
142 "actor" => "https://niu.moe/users/rye",
144 "id" => "http://mastodon.example.org/users/admin/activities/1",
147 "content" => "hi world!",
148 "id" => "http://mastodon.example.org/users/admin/objects/1",
149 "attributedTo" => "http://mastodon.example.org/users/admin",
150 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
152 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
155 assert {:ok, job} = Federator.incoming_ap_doc(params)
156 assert {:discard, :origin_containment_failed} = ObanHelpers.perform(job)
159 test "it does not crash if MRF rejects the post" do
160 clear_config([:mrf_keyword, :reject], ["lain"])
164 Pleroma.Web.ActivityPub.MRF.KeywordPolicy
168 File.read!("test/fixtures/mastodon-post-activity.json")
171 assert {:ok, job} = Federator.incoming_ap_doc(params)
172 assert {:discard, _} = ObanHelpers.perform(job)