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 "does not fetch anything from a rejected instance" do
165 clear_config([:mrf_simple, :reject], [{"evil.example.org", "i said so"}])
167 assert {:reject, _} =
168 Fetcher.fetch_object_from_id("http://evil.example.org/@admin/99541947525187367")
171 test "does not fetch anything if mrf_simple accept is on" do
172 clear_config([:mrf_simple, :accept], [{"mastodon.example.org", "i said so"}])
173 clear_config([:mrf_simple, :reject], [])
175 assert {:reject, _} =
176 Fetcher.fetch_object_from_id(
177 "http://notlisted.example.org/@admin/99541947525187367"
180 assert {:ok, _object} =
181 Fetcher.fetch_object_from_id(
182 "http://mastodon.example.org/@admin/99541947525187367"
186 test "it resets instance reachability on successful fetch" do
187 id = "http://mastodon.example.org/@admin/99541947525187367"
188 Instances.set_consistently_unreachable(id)
189 refute Instances.reachable?(id)
192 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
194 assert Instances.reachable?(id)
198 describe "implementation quirks" do
199 test "it can fetch plume articles" do
201 Fetcher.fetch_object_from_id(
202 "https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/"
208 test "it can fetch peertube videos" do
210 Fetcher.fetch_object_from_id(
211 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
217 test "it can fetch Mobilizon events" do
219 Fetcher.fetch_object_from_id(
220 "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"
226 test "it can fetch wedistribute articles" do
228 Fetcher.fetch_object_from_id("https://wedistribute.org/wp-json/pterotype/v1/object/85810")
233 test "all objects with fake directions are rejected by the object fetcher" do
235 Fetcher.fetch_and_contain_remote_object_from_id(
236 "https://info.pleroma.site/activity4.json"
240 test "handle HTTP 410 Gone response" do
242 {"Object has been deleted", "https://mastodon.example.org/users/userisgone", 410}} ==
243 Fetcher.fetch_and_contain_remote_object_from_id(
244 "https://mastodon.example.org/users/userisgone"
248 test "handle HTTP 404 response" do
250 {"Object has been deleted", "https://mastodon.example.org/users/userisgone404", 404}} ==
251 Fetcher.fetch_and_contain_remote_object_from_id(
252 "https://mastodon.example.org/users/userisgone404"
256 test "it can fetch pleroma polls with attachments" do
258 Fetcher.fetch_object_from_id("https://patch.cx/objects/tesla_mock/poll_attachment")
264 describe "pruning" do
265 test "it can refetch pruned objects" do
266 object_id = "http://mastodon.example.org/@admin/99541947525187367"
268 {:ok, object} = Fetcher.fetch_object_from_id(object_id)
272 {:ok, _object} = Object.prune(object)
274 refute Object.get_by_ap_id(object_id)
276 {:ok, %Object{} = object_two} = Fetcher.fetch_object_from_id(object_id)
278 assert object.data["id"] == object_two.data["id"]
279 assert object.id != object_two.id
283 describe "signed fetches" do
284 setup do: clear_config([:activitypub, :sign_object_fetches])
286 test_with_mock "it signs fetches when configured to do so",
290 clear_config([:activitypub, :sign_object_fetches], true)
292 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
294 assert called(Pleroma.Signature.sign(:_, :_))
297 test_with_mock "it doesn't sign fetches when not configured to do so",
301 clear_config([:activitypub, :sign_object_fetches], false)
303 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
305 refute called(Pleroma.Signature.sign(:_, :_))
309 describe "refetching" do
312 "id" => "https://mastodon.social/1",
313 "actor" => "https://mastodon.social/users/emelie",
314 "attributedTo" => "https://mastodon.social/users/emelie",
316 "content" => "test 1",
325 "id" => "https://mastodon.social/2",
326 "actor" => "https://mastodon.social/users/emelie",
327 "attributedTo" => "https://mastodon.social/users/emelie",
329 "content" => "test 2",
335 "formerRepresentations" => %{
336 "type" => "OrderedCollection",
340 "content" => "orig 2",
341 "actor" => "https://mastodon.social/users/emelie",
342 "attributedTo" => "https://mastodon.social/users/emelie",
357 url: "https://mastodon.social/1"
361 headers: [{"content-type", "application/activity+json"}],
362 body: Jason.encode!(object1)
367 url: "https://mastodon.social/2"
371 headers: [{"content-type", "application/activity+json"}],
372 body: Jason.encode!(object2)
377 url: "https://mastodon.social/users/emelie/collections/featured"
381 headers: [{"content-type", "application/activity+json"}],
384 "id" => "https://mastodon.social/users/emelie/collections/featured",
385 "type" => "OrderedCollection",
386 "actor" => "https://mastodon.social/users/emelie",
387 "attributedTo" => "https://mastodon.social/users/emelie",
388 "orderedItems" => [],
394 apply(HttpRequestMock, :request, [env])
397 %{object1: object1, object2: object2}
400 test "it keeps formerRepresentations if remote does not have this attr", %{object1: object1} do
404 "formerRepresentations" => %{
405 "type" => "OrderedCollection",
409 "content" => "orig 2",
410 "actor" => "https://mastodon.social/users/emelie",
411 "attributedTo" => "https://mastodon.social/users/emelie",
423 {:ok, o} = Object.create(full_object1)
425 assert {:ok, refetched} = Fetcher.refetch_object(o)
427 assert %{"formerRepresentations" => %{"orderedItems" => [%{"content" => "orig 2"}]}} =
431 test "it uses formerRepresentations from remote if possible", %{object2: object2} do
432 {:ok, o} = Object.create(object2)
434 assert {:ok, refetched} = Fetcher.refetch_object(o)
436 assert %{"formerRepresentations" => %{"orderedItems" => [%{"content" => "orig 2"}]}} =
440 test "it replaces formerRepresentations with the one from remote", %{object2: object2} do
444 "content" => "mew mew #def",
445 "formerRepresentations" => %{
446 "type" => "OrderedCollection",
448 %{"type" => "Note", "content" => "mew mew 2"}
454 {:ok, o} = Object.create(full_object2)
456 assert {:ok, refetched} = Fetcher.refetch_object(o)
459 "content" => "test 2",
460 "formerRepresentations" => %{"orderedItems" => [%{"content" => "orig 2"}]}
464 test "it adds to formerRepresentations if the remote does not have one and the object has changed",
465 %{object1: object1} do
469 "content" => "mew mew #def",
470 "formerRepresentations" => %{
471 "type" => "OrderedCollection",
473 %{"type" => "Note", "content" => "mew mew 1"}
479 {:ok, o} = Object.create(full_object1)
481 assert {:ok, refetched} = Fetcher.refetch_object(o)
484 "content" => "test 1",
485 "formerRepresentations" => %{
487 %{"content" => "mew mew #def"},
488 %{"content" => "mew mew 1"}
496 describe "fetch with history" do
499 "id" => "https://mastodon.social/2",
500 "actor" => "https://mastodon.social/users/emelie",
501 "attributedTo" => "https://mastodon.social/users/emelie",
503 "content" => "test 2",
506 "cc" => ["https://mastodon.social/users/emelie/followers"],
509 "formerRepresentations" => %{
510 "type" => "OrderedCollection",
514 "content" => "orig 2",
515 "actor" => "https://mastodon.social/users/emelie",
516 "attributedTo" => "https://mastodon.social/users/emelie",
519 "cc" => ["https://mastodon.social/users/emelie/followers"],
531 url: "https://mastodon.social/2"
535 headers: [{"content-type", "application/activity+json"}],
536 body: Jason.encode!(object2)
541 url: "https://mastodon.social/users/emelie/collections/featured"
545 headers: [{"content-type", "application/activity+json"}],
548 "id" => "https://mastodon.social/users/emelie/collections/featured",
549 "type" => "OrderedCollection",
550 "actor" => "https://mastodon.social/users/emelie",
551 "attributedTo" => "https://mastodon.social/users/emelie",
552 "orderedItems" => [],
558 apply(HttpRequestMock, :request, [env])
564 test "it gets history", %{object2: object2} do
565 {:ok, object} = Fetcher.fetch_object_from_id(object2["id"])
568 "formerRepresentations" => %{
569 "type" => "OrderedCollection",
570 "orderedItems" => [%{}]
576 describe "get_object/1" do
577 test "should return ok if the content type is application/activity+json" do
581 url: "https://mastodon.social/2"
585 headers: [{"content-type", "application/activity+json"}],
590 assert {:ok, "{}"} = Fetcher.get_object("https://mastodon.social/2")
593 test "should return ok if the content type is application/ld+json with a profile" do
597 url: "https://mastodon.social/2"
603 "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""}
609 assert {:ok, "{}"} = Fetcher.get_object("https://mastodon.social/2")
614 url: "https://mastodon.social/2"
620 "application/ld+json; profile=\"http://www.w3.org/ns/activitystreams\""}
626 assert {:ok, "{}"} = Fetcher.get_object("https://mastodon.social/2")
629 test "should not return ok with other content types" do
633 url: "https://mastodon.social/2"
637 headers: [{"content-type", "application/json"}],
642 assert {:error, {:content_type, "application/json"}} =
643 Fetcher.get_object("https://mastodon.social/2")