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