Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / test / web / federator_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
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
11
12 use Pleroma.DataCase
13 use Oban.Testing, repo: Pleroma.Repo
14
15 import Pleroma.Factory
16 import Mock
17
18 setup_all do
19 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
20
21 :ok
22 end
23
24 clear_config_all([:instance, :federating]) do
25 Pleroma.Config.put([:instance, :federating], true)
26 end
27
28 clear_config([:instance, :allow_relay])
29 clear_config([:instance, :rewrite_policy])
30 clear_config([:mrf_keyword])
31
32 describe "Publish an activity" do
33 setup do
34 user = insert(:user)
35 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI"})
36
37 relay_mock = {
38 Pleroma.Web.ActivityPub.Relay,
39 [],
40 [publish: fn _activity -> send(self(), :relay_publish) end]
41 }
42
43 %{activity: activity, relay_mock: relay_mock}
44 end
45
46 test "with relays active, it publishes to the relay", %{
47 activity: activity,
48 relay_mock: relay_mock
49 } do
50 with_mocks([relay_mock]) do
51 Federator.publish(activity)
52 ObanHelpers.perform(all_enqueued(worker: PublisherWorker))
53 end
54
55 assert_received :relay_publish
56 end
57
58 test "with relays deactivated, it does not publish to the relay", %{
59 activity: activity,
60 relay_mock: relay_mock
61 } do
62 Pleroma.Config.put([:instance, :allow_relay], false)
63
64 with_mocks([relay_mock]) do
65 Federator.publish(activity)
66 ObanHelpers.perform(all_enqueued(worker: PublisherWorker))
67 end
68
69 refute_received :relay_publish
70 end
71 end
72
73 describe "Targets reachability filtering in `publish`" do
74 test "it federates only to reachable instances via AP" do
75 user = insert(:user)
76
77 {inbox1, inbox2} =
78 {"https://domain.com/users/nick1/inbox", "https://domain2.com/users/nick2/inbox"}
79
80 insert(:user, %{
81 local: false,
82 nickname: "nick1@domain.com",
83 ap_id: "https://domain.com/users/nick1",
84 info: %{ap_enabled: true, source_data: %{"inbox" => inbox1}}
85 })
86
87 insert(:user, %{
88 local: false,
89 nickname: "nick2@domain2.com",
90 ap_id: "https://domain2.com/users/nick2",
91 info: %{ap_enabled: true, source_data: %{"inbox" => inbox2}}
92 })
93
94 dt = NaiveDateTime.utc_now()
95 Instances.set_unreachable(inbox1, dt)
96
97 Instances.set_consistently_unreachable(URI.parse(inbox2).host)
98
99 {:ok, _activity} =
100 CommonAPI.post(user, %{"status" => "HI @nick1@domain.com, @nick2@domain2.com!"})
101
102 expected_dt = NaiveDateTime.to_iso8601(dt)
103
104 ObanHelpers.perform(all_enqueued(worker: PublisherWorker))
105
106 assert ObanHelpers.member?(
107 %{
108 "op" => "publish_one",
109 "params" => %{"inbox" => inbox1, "unreachable_since" => expected_dt}
110 },
111 all_enqueued(worker: PublisherWorker)
112 )
113 end
114
115 test "it federates only to reachable instances via Websub" do
116 user = insert(:user)
117 websub_topic = Pleroma.Web.OStatus.feed_path(user)
118
119 sub1 =
120 insert(:websub_subscription, %{
121 topic: websub_topic,
122 state: "active",
123 callback: "http://pleroma.soykaf.com/cb"
124 })
125
126 sub2 =
127 insert(:websub_subscription, %{
128 topic: websub_topic,
129 state: "active",
130 callback: "https://pleroma2.soykaf.com/cb"
131 })
132
133 dt = NaiveDateTime.utc_now()
134 Instances.set_unreachable(sub2.callback, dt)
135
136 Instances.set_consistently_unreachable(sub1.callback)
137
138 {:ok, _activity} = CommonAPI.post(user, %{"status" => "HI"})
139
140 expected_callback = sub2.callback
141 expected_dt = NaiveDateTime.to_iso8601(dt)
142
143 ObanHelpers.perform(all_enqueued(worker: PublisherWorker))
144
145 assert ObanHelpers.member?(
146 %{
147 "op" => "publish_one",
148 "params" => %{
149 "callback" => expected_callback,
150 "unreachable_since" => expected_dt
151 }
152 },
153 all_enqueued(worker: PublisherWorker)
154 )
155 end
156
157 test "it federates only to reachable instances via Salmon" do
158 user = insert(:user)
159
160 _remote_user1 =
161 insert(:user, %{
162 local: false,
163 nickname: "nick1@domain.com",
164 ap_id: "https://domain.com/users/nick1",
165 info: %{salmon: "https://domain.com/salmon"}
166 })
167
168 remote_user2 =
169 insert(:user, %{
170 local: false,
171 nickname: "nick2@domain2.com",
172 ap_id: "https://domain2.com/users/nick2",
173 info: %{salmon: "https://domain2.com/salmon"}
174 })
175
176 remote_user2_id = remote_user2.id
177
178 dt = NaiveDateTime.utc_now()
179 Instances.set_unreachable(remote_user2.ap_id, dt)
180
181 Instances.set_consistently_unreachable("domain.com")
182
183 {:ok, _activity} =
184 CommonAPI.post(user, %{"status" => "HI @nick1@domain.com, @nick2@domain2.com!"})
185
186 expected_dt = NaiveDateTime.to_iso8601(dt)
187
188 ObanHelpers.perform(all_enqueued(worker: PublisherWorker))
189
190 assert ObanHelpers.member?(
191 %{
192 "op" => "publish_one",
193 "params" => %{
194 "recipient_id" => remote_user2_id,
195 "unreachable_since" => expected_dt
196 }
197 },
198 all_enqueued(worker: PublisherWorker)
199 )
200 end
201 end
202
203 describe "Receive an activity" do
204 test "successfully processes incoming AP docs with correct origin" do
205 params = %{
206 "@context" => "https://www.w3.org/ns/activitystreams",
207 "actor" => "http://mastodon.example.org/users/admin",
208 "type" => "Create",
209 "id" => "http://mastodon.example.org/users/admin/activities/1",
210 "object" => %{
211 "type" => "Note",
212 "content" => "hi world!",
213 "id" => "http://mastodon.example.org/users/admin/objects/1",
214 "attributedTo" => "http://mastodon.example.org/users/admin"
215 },
216 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
217 }
218
219 assert {:ok, job} = Federator.incoming_ap_doc(params)
220 assert {:ok, _activity} = ObanHelpers.perform(job)
221 end
222
223 test "rejects incoming AP docs with incorrect origin" do
224 params = %{
225 "@context" => "https://www.w3.org/ns/activitystreams",
226 "actor" => "https://niu.moe/users/rye",
227 "type" => "Create",
228 "id" => "http://mastodon.example.org/users/admin/activities/1",
229 "object" => %{
230 "type" => "Note",
231 "content" => "hi world!",
232 "id" => "http://mastodon.example.org/users/admin/objects/1",
233 "attributedTo" => "http://mastodon.example.org/users/admin"
234 },
235 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
236 }
237
238 assert {:ok, job} = Federator.incoming_ap_doc(params)
239 assert :error = ObanHelpers.perform(job)
240 end
241
242 test "it does not crash if MRF rejects the post" do
243 Pleroma.Config.put([:mrf_keyword, :reject], ["lain"])
244
245 Pleroma.Config.put(
246 [:instance, :rewrite_policy],
247 Pleroma.Web.ActivityPub.MRF.KeywordPolicy
248 )
249
250 params =
251 File.read!("test/fixtures/mastodon-post-activity.json")
252 |> Poison.decode!()
253
254 assert {:ok, job} = Federator.incoming_ap_doc(params)
255 assert :error = ObanHelpers.perform(job)
256 end
257 end
258 end