1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Object.FetcherTest do
11 alias Pleroma.Object.Fetcher
13 import ExUnit.CaptureLog
19 %{method: :get, url: "https://mastodon.example.org/users/userisgone"} ->
20 %Tesla.Env{status: 410}
22 %{method: :get, url: "https://mastodon.example.org/users/userisgone404"} ->
23 %Tesla.Env{status: 404}
26 apply(HttpRequestMock, :request, [env])
32 describe "error cases" do
35 %{method: :get, url: "https://social.sakamoto.gq/notice/9wTkLEnuq47B25EehM"} ->
38 body: File.read!("test/fixtures/fetch_mocks/9wTkLEnuq47B25EehM.json")
41 %{method: :get, url: "https://social.sakamoto.gq/users/eal"} ->
44 body: File.read!("test/fixtures/fetch_mocks/eal.json")
47 %{method: :get, url: "https://busshi.moe/users/tuxcrafting/statuses/104410921027210069"} ->
50 body: File.read!("test/fixtures/fetch_mocks/104410921027210069.json")
53 %{method: :get, url: "https://busshi.moe/users/tuxcrafting"} ->
62 @tag capture_log: true
63 test "it works when fetching the OP actor errors out" do
64 # Here we simulate a case where the author of the OP can't be read
66 Fetcher.fetch_object_from_id(
67 "https://social.sakamoto.gq/notice/9wTkLEnuq47B25EehM"
72 describe "max thread distance restriction" do
73 @ap_id "http://mastodon.example.org/@admin/99541947525187367"
74 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
76 test "it returns thread depth exceeded error if thread depth is exceeded" do
77 Config.put([:instance, :federation_incoming_replies_max_depth], 0)
79 assert {:error, "Max thread distance exceeded."} =
80 Fetcher.fetch_object_from_id(@ap_id, depth: 1)
83 test "it fetches object if max thread depth is restricted to 0 and depth is not specified" do
84 Config.put([:instance, :federation_incoming_replies_max_depth], 0)
86 assert {:ok, _} = Fetcher.fetch_object_from_id(@ap_id)
89 test "it fetches object if requested depth does not exceed max thread depth" do
90 Config.put([:instance, :federation_incoming_replies_max_depth], 10)
92 assert {:ok, _} = Fetcher.fetch_object_from_id(@ap_id, depth: 10)
96 describe "actor origin containment" do
97 test "it rejects objects with a bogus origin" do
98 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity.json")
101 test "it rejects objects when attributedTo is wrong (variant 1)" do
102 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity2.json")
105 test "it rejects objects when attributedTo is wrong (variant 2)" do
106 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity3.json")
110 describe "fetching an object" do
111 test "it fetches an object" do
113 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
115 assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
116 assert activity.data["id"]
118 {:ok, object_again} =
119 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
121 assert [attachment] = object.data["attachment"]
122 assert is_list(attachment["url"])
124 assert object == object_again
127 test "Return MRF reason when fetched status is rejected by one" do
128 clear_config([:mrf_keyword, :reject], ["yeah"])
129 clear_config([:mrf, :policies], [Pleroma.Web.ActivityPub.MRF.KeywordPolicy])
131 assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} ==
132 Fetcher.fetch_object_from_id(
133 "http://mastodon.example.org/@admin/99541947525187367"
138 describe "implementation quirks" do
139 test "it can fetch plume articles" do
141 Fetcher.fetch_object_from_id(
142 "https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/"
148 test "it can fetch peertube videos" do
150 Fetcher.fetch_object_from_id(
151 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
157 test "it can fetch Mobilizon events" do
159 Fetcher.fetch_object_from_id(
160 "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"
166 test "it can fetch wedistribute articles" do
168 Fetcher.fetch_object_from_id("https://wedistribute.org/wp-json/pterotype/v1/object/85810")
173 test "all objects with fake directions are rejected by the object fetcher" do
175 Fetcher.fetch_and_contain_remote_object_from_id(
176 "https://info.pleroma.site/activity4.json"
180 test "handle HTTP 410 Gone response" do
181 assert {:error, "Object has been deleted"} ==
182 Fetcher.fetch_and_contain_remote_object_from_id(
183 "https://mastodon.example.org/users/userisgone"
187 test "handle HTTP 404 response" do
188 assert {:error, "Object has been deleted"} ==
189 Fetcher.fetch_and_contain_remote_object_from_id(
190 "https://mastodon.example.org/users/userisgone404"
194 test "it can fetch pleroma polls with attachments" do
196 Fetcher.fetch_object_from_id("https://patch.cx/objects/tesla_mock/poll_attachment")
202 describe "pruning" do
203 test "it can refetch pruned objects" do
204 object_id = "http://mastodon.example.org/@admin/99541947525187367"
206 {:ok, object} = Fetcher.fetch_object_from_id(object_id)
210 {:ok, _object} = Object.prune(object)
212 refute Object.get_by_ap_id(object_id)
214 {:ok, %Object{} = object_two} = Fetcher.fetch_object_from_id(object_id)
216 assert object.data["id"] == object_two.data["id"]
217 assert object.id != object_two.id
221 describe "signed fetches" do
222 setup do: clear_config([:activitypub, :sign_object_fetches])
224 test_with_mock "it signs fetches when configured to do so",
228 Config.put([:activitypub, :sign_object_fetches], true)
230 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
232 assert called(Pleroma.Signature.sign(:_, :_))
235 test_with_mock "it doesn't sign fetches when not configured to do so",
239 Config.put([:activitypub, :sign_object_fetches], false)
241 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
243 refute called(Pleroma.Signature.sign(:_, :_))