[#582] Optimized federation retirement by reducing the number of SQL calls
[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.Web.{CommonAPI, Federator}
7 alias Pleroma.Instances
8 use Pleroma.DataCase
9 import Pleroma.Factory
10 import Mock
11
12 setup_all do
13 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
14 :ok
15 end
16
17 test "enqueues an element according to priority" do
18 queue = [%{item: 1, priority: 2}]
19
20 new_queue = Federator.enqueue_sorted(queue, 2, 1)
21 assert new_queue == [%{item: 2, priority: 1}, %{item: 1, priority: 2}]
22
23 new_queue = Federator.enqueue_sorted(queue, 2, 3)
24 assert new_queue == [%{item: 1, priority: 2}, %{item: 2, priority: 3}]
25 end
26
27 test "pop first item" do
28 queue = [%{item: 2, priority: 1}, %{item: 1, priority: 2}]
29
30 assert {2, [%{item: 1, priority: 2}]} = Federator.queue_pop(queue)
31 end
32
33 describe "Publish an activity" do
34 setup do
35 user = insert(:user)
36 {:ok, activity} = CommonAPI.post(user, %{"status" => "HI"})
37
38 relay_mock = {
39 Pleroma.Web.ActivityPub.Relay,
40 [],
41 [publish: fn _activity -> send(self(), :relay_publish) end]
42 }
43
44 %{activity: activity, relay_mock: relay_mock}
45 end
46
47 test "with relays active, it publishes to the relay", %{
48 activity: activity,
49 relay_mock: relay_mock
50 } do
51 with_mocks([relay_mock]) do
52 Federator.handle(:publish, activity)
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.handle(:publish, activity)
66 end
67
68 refute_received :relay_publish
69
70 Pleroma.Config.put([:instance, :allow_relay], true)
71 end
72 end
73
74 describe "Targets reachability filtering in `publish`" do
75 test_with_mock "it federates only to reachable instances via AP",
76 Federator,
77 [:passthrough],
78 [] do
79 user = insert(:user)
80
81 {inbox1, inbox2} =
82 {"https://domain.com/users/nick1/inbox", "https://domain2.com/users/nick2/inbox"}
83
84 insert(:user, %{
85 local: false,
86 nickname: "nick1@domain.com",
87 ap_id: "https://domain.com/users/nick1",
88 info: %{ap_enabled: true, source_data: %{"inbox" => inbox1}}
89 })
90
91 insert(:user, %{
92 local: false,
93 nickname: "nick2@domain2.com",
94 ap_id: "https://domain2.com/users/nick2",
95 info: %{ap_enabled: true, source_data: %{"inbox" => inbox2}}
96 })
97
98 Instances.set_unreachable(
99 URI.parse(inbox2).host,
100 Instances.reachability_datetime_threshold()
101 )
102
103 {:ok, _activity} =
104 CommonAPI.post(user, %{"status" => "HI @nick1@domain.com, @nick2@domain2.com!"})
105
106 assert called(Federator.enqueue(:publish_single_ap, %{inbox: inbox1}))
107 refute called(Federator.enqueue(:publish_single_ap, %{inbox: inbox2}))
108 end
109
110 test_with_mock "it federates only to reachable instances via Websub",
111 Federator,
112 [:passthrough],
113 [] do
114 user = insert(:user)
115 websub_topic = Pleroma.Web.OStatus.feed_path(user)
116
117 sub1 =
118 insert(:websub_subscription, %{
119 topic: websub_topic,
120 state: "active",
121 callback: "http://pleroma.soykaf.com/cb"
122 })
123
124 sub2 =
125 insert(:websub_subscription, %{
126 topic: websub_topic,
127 state: "active",
128 callback: "https://pleroma2.soykaf.com/cb"
129 })
130
131 Instances.set_consistently_unreachable(sub1.callback)
132
133 {:ok, _activity} = CommonAPI.post(user, %{"status" => "HI"})
134
135 assert called(Federator.enqueue(:publish_single_websub, %{callback: sub2.callback}))
136 refute called(Federator.enqueue(:publish_single_websub, %{callback: sub1.callback}))
137 end
138
139 test_with_mock "it federates only to reachable instances via Salmon",
140 Federator,
141 [:passthrough],
142 [] do
143 user = insert(:user)
144
145 remote_user1 =
146 insert(:user, %{
147 local: false,
148 nickname: "nick1@domain.com",
149 ap_id: "https://domain.com/users/nick1",
150 info: %{salmon: "https://domain.com/salmon"}
151 })
152
153 remote_user2 =
154 insert(:user, %{
155 local: false,
156 nickname: "nick2@domain2.com",
157 ap_id: "https://domain2.com/users/nick2",
158 info: %{salmon: "https://domain2.com/salmon"}
159 })
160
161 Instances.set_consistently_unreachable("domain.com")
162
163 {:ok, _activity} =
164 CommonAPI.post(user, %{"status" => "HI @nick1@domain.com, @nick2@domain2.com!"})
165
166 assert called(Federator.enqueue(:publish_single_salmon, %{recipient: remote_user2}))
167 refute called(Federator.enqueue(:publish_single_websub, %{recipient: remote_user1}))
168 end
169 end
170
171 describe "Receive an activity" do
172 test "successfully processes incoming AP docs with correct origin" do
173 params = %{
174 "@context" => "https://www.w3.org/ns/activitystreams",
175 "actor" => "http://mastodon.example.org/users/admin",
176 "type" => "Create",
177 "id" => "http://mastodon.example.org/users/admin/activities/1",
178 "object" => %{
179 "type" => "Note",
180 "content" => "hi world!",
181 "id" => "http://mastodon.example.org/users/admin/objects/1",
182 "attributedTo" => "http://mastodon.example.org/users/admin"
183 },
184 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
185 }
186
187 {:ok, _activity} = Federator.handle(:incoming_ap_doc, params)
188 end
189
190 test "rejects incoming AP docs with incorrect origin" do
191 params = %{
192 "@context" => "https://www.w3.org/ns/activitystreams",
193 "actor" => "https://niu.moe/users/rye",
194 "type" => "Create",
195 "id" => "http://mastodon.example.org/users/admin/activities/1",
196 "object" => %{
197 "type" => "Note",
198 "content" => "hi world!",
199 "id" => "http://mastodon.example.org/users/admin/objects/1",
200 "attributedTo" => "http://mastodon.example.org/users/admin"
201 },
202 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
203 }
204
205 :error = Federator.handle(:incoming_ap_doc, params)
206 end
207 end
208 end