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
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"} ->
75 @tag capture_log: true
76 test "it works when fetching the OP actor errors out" do
77 # Here we simulate a case where the author of the OP can't be read
79 Fetcher.fetch_object_from_id(
80 "https://social.sakamoto.gq/notice/9wTkLEnuq47B25EehM"
85 describe "max thread distance restriction" do
86 @ap_id "http://mastodon.example.org/@admin/99541947525187367"
87 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
89 test "it returns thread depth exceeded error if thread depth is exceeded" do
90 Config.put([:instance, :federation_incoming_replies_max_depth], 0)
92 assert {:error, "Max thread distance exceeded."} =
93 Fetcher.fetch_object_from_id(@ap_id, depth: 1)
96 test "it fetches object if max thread depth is restricted to 0 and depth is not specified" do
97 Config.put([:instance, :federation_incoming_replies_max_depth], 0)
99 assert {:ok, _} = Fetcher.fetch_object_from_id(@ap_id)
102 test "it fetches object if requested depth does not exceed max thread depth" do
103 Config.put([:instance, :federation_incoming_replies_max_depth], 10)
105 assert {:ok, _} = Fetcher.fetch_object_from_id(@ap_id, depth: 10)
109 describe "actor origin containment" do
110 test "it rejects objects with a bogus origin" do
111 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity.json")
114 test "it rejects objects when attributedTo is wrong (variant 1)" do
115 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity2.json")
118 test "it rejects objects when attributedTo is wrong (variant 2)" do
119 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity3.json")
123 describe "fetching an object" do
124 test "it fetches an object" do
126 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
128 assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
129 assert activity.data["id"]
131 {:ok, object_again} =
132 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
134 assert [attachment] = object.data["attachment"]
135 assert is_list(attachment["url"])
137 assert object == object_again
140 test "Return MRF reason when fetched status is rejected by one" do
141 clear_config([:mrf_keyword, :reject], ["yeah"])
142 clear_config([:mrf, :policies], [Pleroma.Web.ActivityPub.MRF.KeywordPolicy])
144 assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} ==
145 Fetcher.fetch_object_from_id(
146 "http://mastodon.example.org/@admin/99541947525187367"
150 test "it does not fetch a spoofed object uploaded on an instance as an attachment" do
152 Fetcher.fetch_object_from_id(
153 "https://patch.cx/media/03ca3c8b4ac3ddd08bf0f84be7885f2f88de0f709112131a22d83650819e36c2.json"
158 describe "implementation quirks" do
159 test "it can fetch plume articles" do
161 Fetcher.fetch_object_from_id(
162 "https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/"
168 test "it can fetch peertube videos" do
170 Fetcher.fetch_object_from_id(
171 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
177 test "it can fetch Mobilizon events" do
179 Fetcher.fetch_object_from_id(
180 "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"
186 test "it can fetch wedistribute articles" do
188 Fetcher.fetch_object_from_id("https://wedistribute.org/wp-json/pterotype/v1/object/85810")
193 test "all objects with fake directions are rejected by the object fetcher" do
195 Fetcher.fetch_and_contain_remote_object_from_id(
196 "https://info.pleroma.site/activity4.json"
200 test "handle HTTP 410 Gone response" do
201 assert {:error, "Object has been deleted"} ==
202 Fetcher.fetch_and_contain_remote_object_from_id(
203 "https://mastodon.example.org/users/userisgone"
207 test "handle HTTP 404 response" do
208 assert {:error, "Object has been deleted"} ==
209 Fetcher.fetch_and_contain_remote_object_from_id(
210 "https://mastodon.example.org/users/userisgone404"
214 test "it can fetch pleroma polls with attachments" do
216 Fetcher.fetch_object_from_id("https://patch.cx/objects/tesla_mock/poll_attachment")
222 describe "pruning" do
223 test "it can refetch pruned objects" do
224 object_id = "http://mastodon.example.org/@admin/99541947525187367"
226 {:ok, object} = Fetcher.fetch_object_from_id(object_id)
230 {:ok, _object} = Object.prune(object)
232 refute Object.get_by_ap_id(object_id)
234 {:ok, %Object{} = object_two} = Fetcher.fetch_object_from_id(object_id)
236 assert object.data["id"] == object_two.data["id"]
237 assert object.id != object_two.id
241 describe "signed fetches" do
242 setup do: clear_config([:activitypub, :sign_object_fetches])
244 test_with_mock "it signs fetches when configured to do so",
248 Config.put([:activitypub, :sign_object_fetches], true)
250 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
252 assert called(Pleroma.Signature.sign(:_, :_))
255 test_with_mock "it doesn't sign fetches when not configured to do so",
259 Config.put([:activitypub, :sign_object_fetches], false)
261 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
263 refute called(Pleroma.Signature.sign(:_, :_))