1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Object.FetcherTest do
9 alias Pleroma.Instances
11 alias Pleroma.Object.Fetcher
18 %{method: :get, url: "https://mastodon.example.org/users/userisgone"} ->
19 %Tesla.Env{status: 410}
21 %{method: :get, url: "https://mastodon.example.org/users/userisgone404"} ->
22 %Tesla.Env{status: 404}
27 "https://patch.cx/media/03ca3c8b4ac3ddd08bf0f84be7885f2f88de0f709112131a22d83650819e36c2.json"
31 headers: [{"content-type", "application/json"}],
32 body: File.read!("test/fixtures/spoofed-object.json")
36 apply(HttpRequestMock, :request, [env])
42 describe "error cases" do
45 %{method: :get, url: "https://social.sakamoto.gq/notice/9wTkLEnuq47B25EehM"} ->
48 body: File.read!("test/fixtures/fetch_mocks/9wTkLEnuq47B25EehM.json"),
49 headers: HttpRequestMock.activitypub_object_headers()
52 %{method: :get, url: "https://social.sakamoto.gq/users/eal"} ->
55 body: File.read!("test/fixtures/fetch_mocks/eal.json"),
56 headers: HttpRequestMock.activitypub_object_headers()
59 %{method: :get, url: "https://busshi.moe/users/tuxcrafting/statuses/104410921027210069"} ->
62 body: File.read!("test/fixtures/fetch_mocks/104410921027210069.json"),
63 headers: HttpRequestMock.activitypub_object_headers()
66 %{method: :get, url: "https://busshi.moe/users/tuxcrafting"} ->
73 url: "https://stereophonic.space/objects/02997b83-3ea7-4b63-94af-ef3aa2d4ed17"
83 @tag capture_log: true
84 test "it works when fetching the OP actor errors out" do
85 # Here we simulate a case where the author of the OP can't be read
87 Fetcher.fetch_object_from_id(
88 "https://social.sakamoto.gq/notice/9wTkLEnuq47B25EehM"
93 describe "max thread distance restriction" do
94 @ap_id "http://mastodon.example.org/@admin/99541947525187367"
95 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
97 test "it returns thread depth exceeded error if thread depth is exceeded" do
98 clear_config([:instance, :federation_incoming_replies_max_depth], 0)
100 assert {:error, "Max thread distance exceeded."} =
101 Fetcher.fetch_object_from_id(@ap_id, depth: 1)
104 test "it fetches object if max thread depth is restricted to 0 and depth is not specified" do
105 clear_config([:instance, :federation_incoming_replies_max_depth], 0)
107 assert {:ok, _} = Fetcher.fetch_object_from_id(@ap_id)
110 test "it fetches object if requested depth does not exceed max thread depth" do
111 clear_config([:instance, :federation_incoming_replies_max_depth], 10)
113 assert {:ok, _} = Fetcher.fetch_object_from_id(@ap_id, depth: 10)
117 describe "actor origin containment" do
118 test "it rejects objects with a bogus origin" do
119 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity.json")
122 test "it rejects objects when attributedTo is wrong (variant 1)" do
123 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity2.json")
126 test "it rejects objects when attributedTo is wrong (variant 2)" do
127 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity3.json")
131 describe "fetching an object" do
132 test "it fetches an object" do
134 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
136 assert _activity = Activity.get_create_by_object_ap_id(object.data["id"])
138 {:ok, object_again} =
139 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
141 assert [attachment] = object.data["attachment"]
142 assert is_list(attachment["url"])
144 assert object == object_again
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])
151 assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} ==
152 Fetcher.fetch_object_from_id(
153 "http://mastodon.example.org/@admin/99541947525187367"
157 test "it does not fetch a spoofed object uploaded on an instance as an attachment" do
159 Fetcher.fetch_object_from_id(
160 "https://patch.cx/media/03ca3c8b4ac3ddd08bf0f84be7885f2f88de0f709112131a22d83650819e36c2.json"
164 test "it resets instance reachability on successful fetch" do
165 id = "http://mastodon.example.org/@admin/99541947525187367"
166 Instances.set_consistently_unreachable(id)
167 refute Instances.reachable?(id)
170 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
172 assert Instances.reachable?(id)
176 describe "implementation quirks" do
177 test "it can fetch plume articles" do
179 Fetcher.fetch_object_from_id(
180 "https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/"
186 test "it can fetch peertube videos" do
188 Fetcher.fetch_object_from_id(
189 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
195 test "it can fetch Mobilizon events" do
197 Fetcher.fetch_object_from_id(
198 "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"
204 test "it can fetch wedistribute articles" do
206 Fetcher.fetch_object_from_id("https://wedistribute.org/wp-json/pterotype/v1/object/85810")
211 test "all objects with fake directions are rejected by the object fetcher" do
213 Fetcher.fetch_and_contain_remote_object_from_id(
214 "https://info.pleroma.site/activity4.json"
218 test "handle HTTP 410 Gone response" do
219 assert {:error, "Object has been deleted"} ==
220 Fetcher.fetch_and_contain_remote_object_from_id(
221 "https://mastodon.example.org/users/userisgone"
225 test "handle HTTP 404 response" do
226 assert {:error, "Object has been deleted"} ==
227 Fetcher.fetch_and_contain_remote_object_from_id(
228 "https://mastodon.example.org/users/userisgone404"
232 test "it can fetch pleroma polls with attachments" do
234 Fetcher.fetch_object_from_id("https://patch.cx/objects/tesla_mock/poll_attachment")
240 describe "pruning" do
241 test "it can refetch pruned objects" do
242 object_id = "http://mastodon.example.org/@admin/99541947525187367"
244 {:ok, object} = Fetcher.fetch_object_from_id(object_id)
248 {:ok, _object} = Object.prune(object)
250 refute Object.get_by_ap_id(object_id)
252 {:ok, %Object{} = object_two} = Fetcher.fetch_object_from_id(object_id)
254 assert object.data["id"] == object_two.data["id"]
255 assert object.id != object_two.id
259 describe "signed fetches" do
260 setup do: clear_config([:activitypub, :sign_object_fetches])
262 test_with_mock "it signs fetches when configured to do so",
266 clear_config([:activitypub, :sign_object_fetches], true)
268 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
270 assert called(Pleroma.Signature.sign(:_, :_))
273 test_with_mock "it doesn't sign fetches when not configured to do so",
277 clear_config([:activitypub, :sign_object_fetches], false)
279 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
281 refute called(Pleroma.Signature.sign(:_, :_))
285 describe "refetching" do
288 "id" => "https://mastodon.social/1",
289 "actor" => "https://mastodon.social/users/emelie",
290 "attributedTo" => "https://mastodon.social/users/emelie",
292 "content" => "test 1",
301 "id" => "https://mastodon.social/2",
302 "actor" => "https://mastodon.social/users/emelie",
303 "attributedTo" => "https://mastodon.social/users/emelie",
305 "content" => "test 2",
311 "formerRepresentations" => %{
312 "type" => "OrderedCollection",
316 "content" => "orig 2",
317 "actor" => "https://mastodon.social/users/emelie",
318 "attributedTo" => "https://mastodon.social/users/emelie",
333 url: "https://mastodon.social/1"
337 headers: [{"content-type", "application/activity+json"}],
338 body: Jason.encode!(object1)
343 url: "https://mastodon.social/2"
347 headers: [{"content-type", "application/activity+json"}],
348 body: Jason.encode!(object2)
353 url: "https://mastodon.social/users/emelie/collections/featured"
357 headers: [{"content-type", "application/activity+json"}],
360 "id" => "https://mastodon.social/users/emelie/collections/featured",
361 "type" => "OrderedCollection",
362 "actor" => "https://mastodon.social/users/emelie",
363 "attributedTo" => "https://mastodon.social/users/emelie",
364 "orderedItems" => [],
370 apply(HttpRequestMock, :request, [env])
373 %{object1: object1, object2: object2}
376 test "it keeps formerRepresentations if remote does not have this attr", %{object1: object1} do
380 "formerRepresentations" => %{
381 "type" => "OrderedCollection",
385 "content" => "orig 2",
386 "actor" => "https://mastodon.social/users/emelie",
387 "attributedTo" => "https://mastodon.social/users/emelie",
399 {:ok, o} = Object.create(full_object1)
401 assert {:ok, refetched} = Fetcher.refetch_object(o)
403 assert %{"formerRepresentations" => %{"orderedItems" => [%{"content" => "orig 2"}]}} =
407 test "it uses formerRepresentations from remote if possible", %{object2: object2} do
408 {:ok, o} = Object.create(object2)
410 assert {:ok, refetched} = Fetcher.refetch_object(o)
412 assert %{"formerRepresentations" => %{"orderedItems" => [%{"content" => "orig 2"}]}} =
416 test "it replaces formerRepresentations with the one from remote", %{object2: object2} do
420 "content" => "mew mew #def",
421 "formerRepresentations" => %{
422 "type" => "OrderedCollection",
424 %{"type" => "Note", "content" => "mew mew 2"}
430 {:ok, o} = Object.create(full_object2)
432 assert {:ok, refetched} = Fetcher.refetch_object(o)
435 "content" => "test 2",
436 "formerRepresentations" => %{"orderedItems" => [%{"content" => "orig 2"}]}
440 test "it adds to formerRepresentations if the remote does not have one and the object has changed",
441 %{object1: object1} do
445 "content" => "mew mew #def",
446 "formerRepresentations" => %{
447 "type" => "OrderedCollection",
449 %{"type" => "Note", "content" => "mew mew 1"}
455 {:ok, o} = Object.create(full_object1)
457 assert {:ok, refetched} = Fetcher.refetch_object(o)
460 "content" => "test 1",
461 "formerRepresentations" => %{
463 %{"content" => "mew mew #def"},
464 %{"content" => "mew mew 1"}
472 describe "fetch with history" do
475 "id" => "https://mastodon.social/2",
476 "actor" => "https://mastodon.social/users/emelie",
477 "attributedTo" => "https://mastodon.social/users/emelie",
479 "content" => "test 2",
482 "cc" => ["https://mastodon.social/users/emelie/followers"],
485 "formerRepresentations" => %{
486 "type" => "OrderedCollection",
490 "content" => "orig 2",
491 "actor" => "https://mastodon.social/users/emelie",
492 "attributedTo" => "https://mastodon.social/users/emelie",
495 "cc" => ["https://mastodon.social/users/emelie/followers"],
507 url: "https://mastodon.social/2"
511 headers: [{"content-type", "application/activity+json"}],
512 body: Jason.encode!(object2)
517 url: "https://mastodon.social/users/emelie/collections/featured"
521 headers: [{"content-type", "application/activity+json"}],
524 "id" => "https://mastodon.social/users/emelie/collections/featured",
525 "type" => "OrderedCollection",
526 "actor" => "https://mastodon.social/users/emelie",
527 "attributedTo" => "https://mastodon.social/users/emelie",
528 "orderedItems" => [],
534 apply(HttpRequestMock, :request, [env])
540 test "it gets history", %{object2: object2} do
541 {:ok, object} = Fetcher.fetch_object_from_id(object2["id"])
544 "formerRepresentations" => %{
545 "type" => "OrderedCollection",
546 "orderedItems" => [%{}]