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
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}
25 apply(HttpRequestMock, :request, [env])
31 describe "error cases" do
34 %{method: :get, url: "https://social.sakamoto.gq/notice/9wTkLEnuq47B25EehM"} ->
37 body: File.read!("test/fixtures/fetch_mocks/9wTkLEnuq47B25EehM.json")
40 %{method: :get, url: "https://social.sakamoto.gq/users/eal"} ->
43 body: File.read!("test/fixtures/fetch_mocks/eal.json")
46 %{method: :get, url: "https://busshi.moe/users/tuxcrafting/statuses/104410921027210069"} ->
49 body: File.read!("test/fixtures/fetch_mocks/104410921027210069.json")
52 %{method: :get, url: "https://busshi.moe/users/tuxcrafting"} ->
61 @tag capture_log: true
62 test "it works when fetching the OP actor errors out" do
63 # Here we simulate a case where the author of the OP can't be read
65 Fetcher.fetch_object_from_id(
66 "https://social.sakamoto.gq/notice/9wTkLEnuq47B25EehM"
71 describe "max thread distance restriction" do
72 @ap_id "http://mastodon.example.org/@admin/99541947525187367"
73 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
75 test "it returns thread depth exceeded error if thread depth is exceeded" do
76 Config.put([:instance, :federation_incoming_replies_max_depth], 0)
78 assert {:error, "Max thread distance exceeded."} =
79 Fetcher.fetch_object_from_id(@ap_id, depth: 1)
82 test "it fetches object if max thread depth is restricted to 0 and depth is not specified" do
83 Config.put([:instance, :federation_incoming_replies_max_depth], 0)
85 assert {:ok, _} = Fetcher.fetch_object_from_id(@ap_id)
88 test "it fetches object if requested depth does not exceed max thread depth" do
89 Config.put([:instance, :federation_incoming_replies_max_depth], 10)
91 assert {:ok, _} = Fetcher.fetch_object_from_id(@ap_id, depth: 10)
95 describe "actor origin containment" do
96 test "it rejects objects with a bogus origin" do
97 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity.json")
100 test "it rejects objects when attributedTo is wrong (variant 1)" do
101 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity2.json")
104 test "it rejects objects when attributedTo is wrong (variant 2)" do
105 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity3.json")
109 describe "fetching an object" do
110 test "it fetches an object" do
112 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
114 assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
115 assert activity.data["id"]
117 {:ok, object_again} =
118 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
120 assert [attachment] = object.data["attachment"]
121 assert is_list(attachment["url"])
123 assert object == object_again
126 test "Return MRF reason when fetched status is rejected by one" do
127 clear_config([:mrf_keyword, :reject], ["yeah"])
128 clear_config([:mrf, :policies], [Pleroma.Web.ActivityPub.MRF.KeywordPolicy])
130 assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} ==
131 Fetcher.fetch_object_from_id(
132 "http://mastodon.example.org/@admin/99541947525187367"
137 describe "implementation quirks" do
138 test "it can fetch plume articles" do
140 Fetcher.fetch_object_from_id(
141 "https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/"
147 test "it can fetch peertube videos" do
149 Fetcher.fetch_object_from_id(
150 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
156 test "it can fetch Mobilizon events" do
158 Fetcher.fetch_object_from_id(
159 "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"
165 test "it can fetch wedistribute articles" do
167 Fetcher.fetch_object_from_id("https://wedistribute.org/wp-json/pterotype/v1/object/85810")
172 test "all objects with fake directions are rejected by the object fetcher" do
174 Fetcher.fetch_and_contain_remote_object_from_id(
175 "https://info.pleroma.site/activity4.json"
179 test "handle HTTP 410 Gone response" do
180 assert {:error, "Object has been deleted"} ==
181 Fetcher.fetch_and_contain_remote_object_from_id(
182 "https://mastodon.example.org/users/userisgone"
186 test "handle HTTP 404 response" do
187 assert {:error, "Object has been deleted"} ==
188 Fetcher.fetch_and_contain_remote_object_from_id(
189 "https://mastodon.example.org/users/userisgone404"
193 test "it can fetch pleroma polls with attachments" do
195 Fetcher.fetch_object_from_id("https://patch.cx/objects/tesla_mock/poll_attachment")
201 describe "pruning" do
202 test "it can refetch pruned objects" do
203 object_id = "http://mastodon.example.org/@admin/99541947525187367"
205 {:ok, object} = Fetcher.fetch_object_from_id(object_id)
209 {:ok, _object} = Object.prune(object)
211 refute Object.get_by_ap_id(object_id)
213 {:ok, %Object{} = object_two} = Fetcher.fetch_object_from_id(object_id)
215 assert object.data["id"] == object_two.data["id"]
216 assert object.id != object_two.id
220 describe "signed fetches" do
221 setup do: clear_config([:activitypub, :sign_object_fetches])
223 test_with_mock "it signs fetches when configured to do so",
227 Config.put([:activitypub, :sign_object_fetches], true)
229 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
231 assert called(Pleroma.Signature.sign(:_, :_))
234 test_with_mock "it doesn't sign fetches when not configured to do so",
238 Config.put([:activitypub, :sign_object_fetches], false)
240 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
242 refute called(Pleroma.Signature.sign(:_, :_))