Fetcher: Correctly return MRF reject reason
[akkoma] / test / object / fetcher_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Object.FetcherTest do
6 use Pleroma.DataCase
7
8 alias Pleroma.Activity
9 alias Pleroma.Config
10 alias Pleroma.Object
11 alias Pleroma.Object.Fetcher
12
13 import ExUnit.CaptureLog
14 import Mock
15 import Tesla.Mock
16
17 setup do
18 mock(fn
19 %{method: :get, url: "https://mastodon.example.org/users/userisgone"} ->
20 %Tesla.Env{status: 410}
21
22 %{method: :get, url: "https://mastodon.example.org/users/userisgone404"} ->
23 %Tesla.Env{status: 404}
24
25 env ->
26 apply(HttpRequestMock, :request, [env])
27 end)
28
29 :ok
30 end
31
32 describe "error cases" do
33 setup do
34 mock(fn
35 %{method: :get, url: "https://social.sakamoto.gq/notice/9wTkLEnuq47B25EehM"} ->
36 %Tesla.Env{
37 status: 200,
38 body: File.read!("test/fixtures/fetch_mocks/9wTkLEnuq47B25EehM.json")
39 }
40
41 %{method: :get, url: "https://social.sakamoto.gq/users/eal"} ->
42 %Tesla.Env{
43 status: 200,
44 body: File.read!("test/fixtures/fetch_mocks/eal.json")
45 }
46
47 %{method: :get, url: "https://busshi.moe/users/tuxcrafting/statuses/104410921027210069"} ->
48 %Tesla.Env{
49 status: 200,
50 body: File.read!("test/fixtures/fetch_mocks/104410921027210069.json")
51 }
52
53 %{method: :get, url: "https://busshi.moe/users/tuxcrafting"} ->
54 %Tesla.Env{
55 status: 500
56 }
57 end)
58
59 :ok
60 end
61
62 @tag capture_log: true
63 test "it works when fetching the OP actor errors out" do
64 # Here we simulate a case where the author of the OP can't be read
65 assert {:ok, _} =
66 Fetcher.fetch_object_from_id(
67 "https://social.sakamoto.gq/notice/9wTkLEnuq47B25EehM"
68 )
69 end
70 end
71
72 describe "max thread distance restriction" do
73 @ap_id "http://mastodon.example.org/@admin/99541947525187367"
74 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
75
76 test "it returns thread depth exceeded error if thread depth is exceeded" do
77 Config.put([:instance, :federation_incoming_replies_max_depth], 0)
78
79 assert {:error, "Max thread distance exceeded."} =
80 Fetcher.fetch_object_from_id(@ap_id, depth: 1)
81 end
82
83 test "it fetches object if max thread depth is restricted to 0 and depth is not specified" do
84 Config.put([:instance, :federation_incoming_replies_max_depth], 0)
85
86 assert {:ok, _} = Fetcher.fetch_object_from_id(@ap_id)
87 end
88
89 test "it fetches object if requested depth does not exceed max thread depth" do
90 Config.put([:instance, :federation_incoming_replies_max_depth], 10)
91
92 assert {:ok, _} = Fetcher.fetch_object_from_id(@ap_id, depth: 10)
93 end
94 end
95
96 describe "actor origin containment" do
97 test "it rejects objects with a bogus origin" do
98 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity.json")
99 end
100
101 test "it rejects objects when attributedTo is wrong (variant 1)" do
102 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity2.json")
103 end
104
105 test "it rejects objects when attributedTo is wrong (variant 2)" do
106 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity3.json")
107 end
108 end
109
110 describe "fetching an object" do
111 test "it fetches an object" do
112 {:ok, object} =
113 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
114
115 assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
116 assert activity.data["id"]
117
118 {:ok, object_again} =
119 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
120
121 assert [attachment] = object.data["attachment"]
122 assert is_list(attachment["url"])
123
124 assert object == object_again
125 end
126
127 test "Return MRF reason when fetched status is rejected by one" do
128 clear_config([:mrf_keyword, :reject], ["yeah"])
129 clear_config([:mrf, :policies], [Pleroma.Web.ActivityPub.MRF.KeywordPolicy])
130
131 assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} ==
132 Fetcher.fetch_object_from_id(
133 "http://mastodon.example.org/@admin/99541947525187367"
134 )
135 end
136 end
137
138 describe "implementation quirks" do
139 test "it can fetch plume articles" do
140 {:ok, object} =
141 Fetcher.fetch_object_from_id(
142 "https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/"
143 )
144
145 assert object
146 end
147
148 test "it can fetch peertube videos" do
149 {:ok, object} =
150 Fetcher.fetch_object_from_id(
151 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
152 )
153
154 assert object
155 end
156
157 test "it can fetch Mobilizon events" do
158 {:ok, object} =
159 Fetcher.fetch_object_from_id(
160 "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"
161 )
162
163 assert object
164 end
165
166 test "it can fetch wedistribute articles" do
167 {:ok, object} =
168 Fetcher.fetch_object_from_id("https://wedistribute.org/wp-json/pterotype/v1/object/85810")
169
170 assert object
171 end
172
173 test "all objects with fake directions are rejected by the object fetcher" do
174 assert {:error, _} =
175 Fetcher.fetch_and_contain_remote_object_from_id(
176 "https://info.pleroma.site/activity4.json"
177 )
178 end
179
180 test "handle HTTP 410 Gone response" do
181 assert {:error, "Object has been deleted"} ==
182 Fetcher.fetch_and_contain_remote_object_from_id(
183 "https://mastodon.example.org/users/userisgone"
184 )
185 end
186
187 test "handle HTTP 404 response" do
188 assert {:error, "Object has been deleted"} ==
189 Fetcher.fetch_and_contain_remote_object_from_id(
190 "https://mastodon.example.org/users/userisgone404"
191 )
192 end
193
194 test "it can fetch pleroma polls with attachments" do
195 {:ok, object} =
196 Fetcher.fetch_object_from_id("https://patch.cx/objects/tesla_mock/poll_attachment")
197
198 assert object
199 end
200 end
201
202 describe "pruning" do
203 test "it can refetch pruned objects" do
204 object_id = "http://mastodon.example.org/@admin/99541947525187367"
205
206 {:ok, object} = Fetcher.fetch_object_from_id(object_id)
207
208 assert object
209
210 {:ok, _object} = Object.prune(object)
211
212 refute Object.get_by_ap_id(object_id)
213
214 {:ok, %Object{} = object_two} = Fetcher.fetch_object_from_id(object_id)
215
216 assert object.data["id"] == object_two.data["id"]
217 assert object.id != object_two.id
218 end
219 end
220
221 describe "signed fetches" do
222 setup do: clear_config([:activitypub, :sign_object_fetches])
223
224 test_with_mock "it signs fetches when configured to do so",
225 Pleroma.Signature,
226 [:passthrough],
227 [] do
228 Config.put([:activitypub, :sign_object_fetches], true)
229
230 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
231
232 assert called(Pleroma.Signature.sign(:_, :_))
233 end
234
235 test_with_mock "it doesn't sign fetches when not configured to do so",
236 Pleroma.Signature,
237 [:passthrough],
238 [] do
239 Config.put([:activitypub, :sign_object_fetches], false)
240
241 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
242
243 refute called(Pleroma.Signature.sign(:_, :_))
244 end
245 end
246 end