1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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
12 alias Pleroma.Web.ActivityPub.ObjectValidator
14 require Pleroma.Constants
17 import Pleroma.Factory
22 %{method: :get, url: "https://mastodon.example.org/users/userisgone"} ->
23 %Tesla.Env{status: 410}
25 %{method: :get, url: "https://mastodon.example.org/users/userisgone404"} ->
26 %Tesla.Env{status: 404}
31 "https://patch.cx/media/03ca3c8b4ac3ddd08bf0f84be7885f2f88de0f709112131a22d83650819e36c2.json"
35 headers: [{"content-type", "application/json"}],
36 body: File.read!("test/fixtures/spoofed-object.json")
40 apply(HttpRequestMock, :request, [env])
46 describe "error cases" do
49 %{method: :get, url: "https://social.sakamoto.gq/notice/9wTkLEnuq47B25EehM"} ->
52 body: File.read!("test/fixtures/fetch_mocks/9wTkLEnuq47B25EehM.json"),
53 headers: HttpRequestMock.activitypub_object_headers()
56 %{method: :get, url: "https://social.sakamoto.gq/users/eal"} ->
59 body: File.read!("test/fixtures/fetch_mocks/eal.json"),
60 headers: HttpRequestMock.activitypub_object_headers()
63 %{method: :get, url: "https://busshi.moe/users/tuxcrafting/statuses/104410921027210069"} ->
66 body: File.read!("test/fixtures/fetch_mocks/104410921027210069.json"),
67 headers: HttpRequestMock.activitypub_object_headers()
70 %{method: :get, url: "https://busshi.moe/users/tuxcrafting"} ->
77 url: "https://stereophonic.space/objects/02997b83-3ea7-4b63-94af-ef3aa2d4ed17"
87 @tag capture_log: true
88 test "it works when fetching the OP actor errors out" do
89 # Here we simulate a case where the author of the OP can't be read
91 Fetcher.fetch_object_from_id(
92 "https://social.sakamoto.gq/notice/9wTkLEnuq47B25EehM"
97 describe "max thread distance restriction" do
98 @ap_id "http://mastodon.example.org/@admin/99541947525187367"
99 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
101 test "it returns thread depth exceeded error if thread depth is exceeded" do
102 clear_config([:instance, :federation_incoming_replies_max_depth], 0)
104 assert {:error, "Max thread distance exceeded."} =
105 Fetcher.fetch_object_from_id(@ap_id, depth: 1)
108 test "it fetches object if max thread depth is restricted to 0 and depth is not specified" do
109 clear_config([:instance, :federation_incoming_replies_max_depth], 0)
111 assert {:ok, _} = Fetcher.fetch_object_from_id(@ap_id)
114 test "it fetches object if requested depth does not exceed max thread depth" do
115 clear_config([:instance, :federation_incoming_replies_max_depth], 10)
117 assert {:ok, _} = Fetcher.fetch_object_from_id(@ap_id, depth: 10)
121 describe "actor origin containment" do
122 test "it rejects objects with a bogus origin" do
123 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity.json")
126 test "it rejects objects when attributedTo is wrong (variant 1)" do
127 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity2.json")
130 test "it rejects objects when attributedTo is wrong (variant 2)" do
131 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity3.json")
135 describe "fetching an object" do
136 test "it fetches an object" do
138 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
140 assert _activity = Activity.get_create_by_object_ap_id(object.data["id"])
142 {:ok, object_again} =
143 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
145 assert [attachment] = object.data["attachment"]
146 assert is_list(attachment["url"])
148 assert object == object_again
151 test "Return MRF reason when fetched status is rejected by one" do
152 clear_config([:mrf_keyword, :reject], ["yeah"])
153 clear_config([:mrf, :policies], [Pleroma.Web.ActivityPub.MRF.KeywordPolicy])
155 assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} ==
156 Fetcher.fetch_object_from_id(
157 "http://mastodon.example.org/@admin/99541947525187367"
161 test "it does not fetch a spoofed object uploaded on an instance as an attachment" do
163 Fetcher.fetch_object_from_id(
164 "https://patch.cx/media/03ca3c8b4ac3ddd08bf0f84be7885f2f88de0f709112131a22d83650819e36c2.json"
168 test "it resets instance reachability on successful fetch" do
169 id = "http://mastodon.example.org/@admin/99541947525187367"
170 Instances.set_consistently_unreachable(id)
171 refute Instances.reachable?(id)
174 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
176 assert Instances.reachable?(id)
180 describe "implementation quirks" do
181 test "it can fetch plume articles" do
183 Fetcher.fetch_object_from_id(
184 "https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/"
190 test "it can fetch peertube videos" do
192 Fetcher.fetch_object_from_id(
193 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
199 test "it can fetch Mobilizon events" do
201 Fetcher.fetch_object_from_id(
202 "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"
208 test "it can fetch wedistribute articles" do
210 Fetcher.fetch_object_from_id("https://wedistribute.org/wp-json/pterotype/v1/object/85810")
215 test "all objects with fake directions are rejected by the object fetcher" do
217 Fetcher.fetch_and_contain_remote_object_from_id(
218 "https://info.pleroma.site/activity4.json"
222 test "handle HTTP 410 Gone response" do
223 assert {:error, "Object has been deleted"} ==
224 Fetcher.fetch_and_contain_remote_object_from_id(
225 "https://mastodon.example.org/users/userisgone"
229 test "handle HTTP 404 response" do
230 assert {:error, "Object has been deleted"} ==
231 Fetcher.fetch_and_contain_remote_object_from_id(
232 "https://mastodon.example.org/users/userisgone404"
236 test "it can fetch pleroma polls with attachments" do
238 Fetcher.fetch_object_from_id("https://patch.cx/objects/tesla_mock/poll_attachment")
244 describe "pruning" do
245 test "it can refetch pruned objects" do
246 object_id = "http://mastodon.example.org/@admin/99541947525187367"
248 {:ok, object} = Fetcher.fetch_object_from_id(object_id)
252 {:ok, _object} = Object.prune(object)
254 refute Object.get_by_ap_id(object_id)
256 {:ok, %Object{} = object_two} = Fetcher.fetch_object_from_id(object_id)
258 assert object.data["id"] == object_two.data["id"]
259 assert object.id != object_two.id
263 describe "signed fetches" do
264 setup do: clear_config([:activitypub, :sign_object_fetches])
266 test_with_mock "it signs fetches when configured to do so",
270 clear_config([:activitypub, :sign_object_fetches], true)
272 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
274 assert called(Pleroma.Signature.sign(:_, :_))
277 test_with_mock "it doesn't sign fetches when not configured to do so",
281 clear_config([:activitypub, :sign_object_fetches], false)
283 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
285 refute called(Pleroma.Signature.sign(:_, :_))
289 describe "refetching" do
291 insert(:user, ap_id: "https://mastodon.social/users/emelie")
294 "id" => "https://mastodon.social/1",
295 "actor" => "https://mastodon.social/users/emelie",
296 "attributedTo" => "https://mastodon.social/users/emelie",
298 "content" => "test 1",
302 "to" => [Pleroma.Constants.as_public()],
304 "published" => "2023-05-08 23:43:20Z",
305 "updated" => "2023-05-09 23:43:20Z"
308 {:ok, local_object1, _} = ObjectValidator.validate(object1, [])
311 "id" => "https://mastodon.social/2",
312 "actor" => "https://mastodon.social/users/emelie",
313 "attributedTo" => "https://mastodon.social/users/emelie",
315 "content" => "test 2",
319 "to" => [Pleroma.Constants.as_public()],
321 "published" => "2023-05-08 23:43:20Z",
322 "updated" => "2023-05-09 23:43:25Z",
323 "formerRepresentations" => %{
324 "type" => "OrderedCollection",
328 "content" => "orig 2",
329 "actor" => "https://mastodon.social/users/emelie",
330 "attributedTo" => "https://mastodon.social/users/emelie",
334 "to" => [Pleroma.Constants.as_public()],
336 "published" => "2023-05-08 23:43:20Z",
337 "updated" => "2023-05-09 23:43:21Z"
344 {:ok, local_object2, _} = ObjectValidator.validate(object2, [])
349 url: "https://mastodon.social/1"
353 headers: [{"content-type", "application/activity+json"}],
354 body: Jason.encode!(object1 |> Map.put("updated", "2023-05-09 23:44:20Z"))
359 url: "https://mastodon.social/2"
363 headers: [{"content-type", "application/activity+json"}],
364 body: Jason.encode!(object2 |> Map.put("updated", "2023-05-09 23:44:20Z"))
369 url: "https://mastodon.social/users/emelie/collections/featured"
373 headers: [{"content-type", "application/activity+json"}],
376 "id" => "https://mastodon.social/users/emelie/collections/featured",
377 "type" => "OrderedCollection",
378 "actor" => "https://mastodon.social/users/emelie",
379 "attributedTo" => "https://mastodon.social/users/emelie",
380 "orderedItems" => [],
386 apply(HttpRequestMock, :request, [env])
389 %{object1: local_object1, object2: local_object2}
392 test "it keeps formerRepresentations if remote does not have this attr", %{object1: object1} do
396 "formerRepresentations" => %{
397 "type" => "OrderedCollection",
401 "content" => "orig 2",
402 "actor" => "https://mastodon.social/users/emelie",
403 "attributedTo" => "https://mastodon.social/users/emelie",
407 "to" => [Pleroma.Constants.as_public()],
409 "published" => "2023-05-08 23:43:20Z"
416 {:ok, o} = Object.create(full_object1)
418 assert {:ok, refetched} = Fetcher.refetch_object(o)
420 assert %{"formerRepresentations" => %{"orderedItems" => [%{"content" => "orig 2"}]}} =
424 test "it uses formerRepresentations from remote if possible", %{object2: object2} do
425 {:ok, o} = Object.create(object2)
427 assert {:ok, refetched} = Fetcher.refetch_object(o)
429 assert %{"formerRepresentations" => %{"orderedItems" => [%{"content" => "orig 2"}]}} =
433 test "it replaces formerRepresentations with the one from remote", %{object2: object2} do
437 "content" => "mew mew #def",
438 "formerRepresentations" => %{
439 "type" => "OrderedCollection",
441 %{"type" => "Note", "content" => "mew mew 2"}
447 {:ok, o} = Object.create(full_object2)
449 assert {:ok, refetched} = Fetcher.refetch_object(o)
452 "content" => "test 2",
453 "formerRepresentations" => %{"orderedItems" => [%{"content" => "orig 2"}]}
457 test "it adds to formerRepresentations if the remote does not have one and the object has changed",
458 %{object1: object1} do
462 "content" => "mew mew #def",
463 "formerRepresentations" => %{
464 "type" => "OrderedCollection",
466 %{"type" => "Note", "content" => "mew mew 1"}
472 {:ok, o} = Object.create(full_object1)
474 assert {:ok, refetched} = Fetcher.refetch_object(o)
477 "content" => "test 1",
478 "formerRepresentations" => %{
480 %{"content" => "mew mew #def"},
481 %{"content" => "mew mew 1"}
488 test "it keeps the history intact if only updated time has changed",
489 %{object1: object1} do
493 "updated" => "2023-05-08 23:43:47Z",
494 "formerRepresentations" => %{
495 "type" => "OrderedCollection",
497 %{"type" => "Note", "content" => "mew mew 1"}
503 {:ok, o} = Object.create(full_object1)
505 assert {:ok, refetched} = Fetcher.refetch_object(o)
508 "content" => "test 1",
509 "formerRepresentations" => %{
511 %{"content" => "mew mew 1"}
518 test "it goes through ObjectValidator and MRF", %{object2: object2} do
519 with_mock Pleroma.Web.ActivityPub.MRF, [:passthrough],
521 %{"type" => "Note"} = object ->
522 {:ok, Map.put(object, "content", "MRFd content")}
527 {:ok, o} = Object.create(object2)
529 assert {:ok, refetched} = Fetcher.refetch_object(o)
531 assert %{"content" => "MRFd content"} = refetched.data
536 describe "fetch with history" do
539 "id" => "https://mastodon.social/2",
540 "actor" => "https://mastodon.social/users/emelie",
541 "attributedTo" => "https://mastodon.social/users/emelie",
543 "content" => "test 2",
546 "cc" => ["https://mastodon.social/users/emelie/followers"],
549 "formerRepresentations" => %{
550 "type" => "OrderedCollection",
554 "content" => "orig 2",
555 "actor" => "https://mastodon.social/users/emelie",
556 "attributedTo" => "https://mastodon.social/users/emelie",
559 "cc" => ["https://mastodon.social/users/emelie/followers"],
571 url: "https://mastodon.social/2"
575 headers: [{"content-type", "application/activity+json"}],
576 body: Jason.encode!(object2)
581 url: "https://mastodon.social/users/emelie/collections/featured"
585 headers: [{"content-type", "application/activity+json"}],
588 "id" => "https://mastodon.social/users/emelie/collections/featured",
589 "type" => "OrderedCollection",
590 "actor" => "https://mastodon.social/users/emelie",
591 "attributedTo" => "https://mastodon.social/users/emelie",
592 "orderedItems" => [],
598 apply(HttpRequestMock, :request, [env])
604 test "it gets history", %{object2: object2} do
605 {:ok, object} = Fetcher.fetch_object_from_id(object2["id"])
608 "formerRepresentations" => %{
609 "type" => "OrderedCollection",
610 "orderedItems" => [%{}]