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
220 {"Object has been deleted", "https://mastodon.example.org/users/userisgone", 410}} ==
221 Fetcher.fetch_and_contain_remote_object_from_id(
222 "https://mastodon.example.org/users/userisgone"
226 test "handle HTTP 404 response" do
228 {"Object has been deleted", "https://mastodon.example.org/users/userisgone404", 404}} ==
229 Fetcher.fetch_and_contain_remote_object_from_id(
230 "https://mastodon.example.org/users/userisgone404"
234 test "it can fetch pleroma polls with attachments" do
236 Fetcher.fetch_object_from_id("https://patch.cx/objects/tesla_mock/poll_attachment")
242 describe "pruning" do
243 test "it can refetch pruned objects" do
244 object_id = "http://mastodon.example.org/@admin/99541947525187367"
246 {:ok, object} = Fetcher.fetch_object_from_id(object_id)
250 {:ok, _object} = Object.prune(object)
252 refute Object.get_by_ap_id(object_id)
254 {:ok, %Object{} = object_two} = Fetcher.fetch_object_from_id(object_id)
256 assert object.data["id"] == object_two.data["id"]
257 assert object.id != object_two.id
261 describe "signed fetches" do
262 setup do: clear_config([:activitypub, :sign_object_fetches])
264 test_with_mock "it signs fetches when configured to do so",
268 clear_config([:activitypub, :sign_object_fetches], true)
270 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
272 assert called(Pleroma.Signature.sign(:_, :_))
275 test_with_mock "it doesn't sign fetches when not configured to do so",
279 clear_config([:activitypub, :sign_object_fetches], false)
281 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
283 refute called(Pleroma.Signature.sign(:_, :_))
287 describe "refetching" do
290 "id" => "https://mastodon.social/1",
291 "actor" => "https://mastodon.social/users/emelie",
292 "attributedTo" => "https://mastodon.social/users/emelie",
294 "content" => "test 1",
303 "id" => "https://mastodon.social/2",
304 "actor" => "https://mastodon.social/users/emelie",
305 "attributedTo" => "https://mastodon.social/users/emelie",
307 "content" => "test 2",
313 "formerRepresentations" => %{
314 "type" => "OrderedCollection",
318 "content" => "orig 2",
319 "actor" => "https://mastodon.social/users/emelie",
320 "attributedTo" => "https://mastodon.social/users/emelie",
335 url: "https://mastodon.social/1"
339 headers: [{"content-type", "application/activity+json"}],
340 body: Jason.encode!(object1)
345 url: "https://mastodon.social/2"
349 headers: [{"content-type", "application/activity+json"}],
350 body: Jason.encode!(object2)
355 url: "https://mastodon.social/users/emelie/collections/featured"
359 headers: [{"content-type", "application/activity+json"}],
362 "id" => "https://mastodon.social/users/emelie/collections/featured",
363 "type" => "OrderedCollection",
364 "actor" => "https://mastodon.social/users/emelie",
365 "attributedTo" => "https://mastodon.social/users/emelie",
366 "orderedItems" => [],
372 apply(HttpRequestMock, :request, [env])
375 %{object1: object1, object2: object2}
378 test "it keeps formerRepresentations if remote does not have this attr", %{object1: object1} do
382 "formerRepresentations" => %{
383 "type" => "OrderedCollection",
387 "content" => "orig 2",
388 "actor" => "https://mastodon.social/users/emelie",
389 "attributedTo" => "https://mastodon.social/users/emelie",
401 {:ok, o} = Object.create(full_object1)
403 assert {:ok, refetched} = Fetcher.refetch_object(o)
405 assert %{"formerRepresentations" => %{"orderedItems" => [%{"content" => "orig 2"}]}} =
409 test "it uses formerRepresentations from remote if possible", %{object2: object2} do
410 {:ok, o} = Object.create(object2)
412 assert {:ok, refetched} = Fetcher.refetch_object(o)
414 assert %{"formerRepresentations" => %{"orderedItems" => [%{"content" => "orig 2"}]}} =
418 test "it replaces formerRepresentations with the one from remote", %{object2: object2} do
422 "content" => "mew mew #def",
423 "formerRepresentations" => %{
424 "type" => "OrderedCollection",
426 %{"type" => "Note", "content" => "mew mew 2"}
432 {:ok, o} = Object.create(full_object2)
434 assert {:ok, refetched} = Fetcher.refetch_object(o)
437 "content" => "test 2",
438 "formerRepresentations" => %{"orderedItems" => [%{"content" => "orig 2"}]}
442 test "it adds to formerRepresentations if the remote does not have one and the object has changed",
443 %{object1: object1} do
447 "content" => "mew mew #def",
448 "formerRepresentations" => %{
449 "type" => "OrderedCollection",
451 %{"type" => "Note", "content" => "mew mew 1"}
457 {:ok, o} = Object.create(full_object1)
459 assert {:ok, refetched} = Fetcher.refetch_object(o)
462 "content" => "test 1",
463 "formerRepresentations" => %{
465 %{"content" => "mew mew #def"},
466 %{"content" => "mew mew 1"}
474 describe "fetch with history" do
477 "id" => "https://mastodon.social/2",
478 "actor" => "https://mastodon.social/users/emelie",
479 "attributedTo" => "https://mastodon.social/users/emelie",
481 "content" => "test 2",
484 "cc" => ["https://mastodon.social/users/emelie/followers"],
487 "formerRepresentations" => %{
488 "type" => "OrderedCollection",
492 "content" => "orig 2",
493 "actor" => "https://mastodon.social/users/emelie",
494 "attributedTo" => "https://mastodon.social/users/emelie",
497 "cc" => ["https://mastodon.social/users/emelie/followers"],
509 url: "https://mastodon.social/2"
513 headers: [{"content-type", "application/activity+json"}],
514 body: Jason.encode!(object2)
519 url: "https://mastodon.social/users/emelie/collections/featured"
523 headers: [{"content-type", "application/activity+json"}],
526 "id" => "https://mastodon.social/users/emelie/collections/featured",
527 "type" => "OrderedCollection",
528 "actor" => "https://mastodon.social/users/emelie",
529 "attributedTo" => "https://mastodon.social/users/emelie",
530 "orderedItems" => [],
536 apply(HttpRequestMock, :request, [env])
542 test "it gets history", %{object2: object2} do
543 {:ok, object} = Fetcher.fetch_object_from_id(object2["id"])
546 "formerRepresentations" => %{
547 "type" => "OrderedCollection",
548 "orderedItems" => [%{}]