[#1149] Added more oban workers. Refactoring.
[akkoma] / test / web / federator_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 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.Publisher, as: 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 config_path = [:instance, :federating]
22 initial_setting = Pleroma.Config.get(config_path)
23
24 Pleroma.Config.put(config_path, true)
25 on_exit(fn -> Pleroma.Config.put(config_path, initial_setting) end)
26
27 :ok
28 end
29
30 describe "Publish an activity" do
31 setup do
32 user = insert(:user)
33 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI"})
34
35 relay_mock = {
36 Pleroma.Web.ActivityPub.Relay,
37 [],
38 [publish: fn _activity -> send(self(), :relay_publish) end]
39 }
40
41 %{activity: activity, relay_mock: relay_mock}
42 end
43
44 test "with relays active, it publishes to the relay", %{
45 activity: activity,
46 relay_mock: relay_mock
47 } do
48 with_mocks([relay_mock]) do
49 Federator.publish(activity)
50 ObanHelpers.perform(all_enqueued(worker: PublisherWorker))
51 end
52
53 assert_received :relay_publish
54 end
55
56 test "with relays deactivated, it does not publish to the relay", %{
57 activity: activity,
58 relay_mock: relay_mock
59 } do
60 Pleroma.Config.put([:instance, :allow_relay], false)
61
62 with_mocks([relay_mock]) do
63 Federator.publish(activity)
64 ObanHelpers.perform(all_enqueued(worker: PublisherWorker))
65 end
66
67 refute_received :relay_publish
68
69 Pleroma.Config.put([:instance, :allow_relay], true)
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 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)
247
248 params =
249 File.read!("test/fixtures/mastodon-post-activity.json")
250 |> Poison.decode!()
251
252 assert {:ok, job} = Federator.incoming_ap_doc(params)
253 assert :error = ObanHelpers.perform(job)
254
255 Pleroma.Config.put([:instance, :rewrite_policy], policies)
256 Pleroma.Config.put(:mrf_keyword, mrf_keyword_policy)
257 end
258 end
259 end