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
10 alias Pleroma.Object.Fetcher
16 %{method: :get, url: "https://mastodon.example.org/users/userisgone"} ->
17 %Tesla.Env{status: 410}
19 %{method: :get, url: "https://mastodon.example.org/users/userisgone404"} ->
20 %Tesla.Env{status: 404}
23 apply(HttpRequestMock, :request, [env])
29 describe "error cases" do
32 %{method: :get, url: "https://social.sakamoto.gq/notice/9wTkLEnuq47B25EehM"} ->
35 body: File.read!("test/fixtures/fetch_mocks/9wTkLEnuq47B25EehM.json")
38 %{method: :get, url: "https://social.sakamoto.gq/users/eal"} ->
41 body: File.read!("test/fixtures/fetch_mocks/eal.json")
44 %{method: :get, url: "https://busshi.moe/users/tuxcrafting/statuses/104410921027210069"} ->
47 body: File.read!("test/fixtures/fetch_mocks/104410921027210069.json")
50 %{method: :get, url: "https://busshi.moe/users/tuxcrafting"} ->
59 @tag capture_log: true
60 test "it works when fetching the OP actor errors out" do
61 # Here we simulate a case where the author of the OP can't be read
63 Fetcher.fetch_object_from_id(
64 "https://social.sakamoto.gq/notice/9wTkLEnuq47B25EehM"
69 describe "max thread distance restriction" do
70 @ap_id "http://mastodon.example.org/@admin/99541947525187367"
71 setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
73 test "it returns thread depth exceeded error if thread depth is exceeded" do
74 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
76 assert {:error, "Max thread distance exceeded."} =
77 Fetcher.fetch_object_from_id(@ap_id, depth: 1)
80 test "it fetches object if max thread depth is restricted to 0 and depth is not specified" do
81 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
83 assert {:ok, _} = Fetcher.fetch_object_from_id(@ap_id)
86 test "it fetches object if requested depth does not exceed max thread depth" do
87 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 10)
89 assert {:ok, _} = Fetcher.fetch_object_from_id(@ap_id, depth: 10)
93 describe "actor origin containment" do
94 test "it rejects objects with a bogus origin" do
95 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity.json")
98 test "it rejects objects when attributedTo is wrong (variant 1)" do
99 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity2.json")
102 test "it rejects objects when attributedTo is wrong (variant 2)" do
103 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity3.json")
107 describe "fetching an object" do
108 test "it fetches an object" do
110 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
112 assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
113 assert activity.data["id"]
115 {:ok, object_again} =
116 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
118 assert [attachment] = object.data["attachment"]
119 assert is_list(attachment["url"])
121 assert object == object_again
125 describe "implementation quirks" do
126 test "it can fetch plume articles" do
128 Fetcher.fetch_object_from_id(
129 "https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/"
135 test "it can fetch peertube videos" do
137 Fetcher.fetch_object_from_id(
138 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
144 test "it can fetch Mobilizon events" do
146 Fetcher.fetch_object_from_id(
147 "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"
153 test "it can fetch wedistribute articles" do
155 Fetcher.fetch_object_from_id("https://wedistribute.org/wp-json/pterotype/v1/object/85810")
160 test "all objects with fake directions are rejected by the object fetcher" do
162 Fetcher.fetch_and_contain_remote_object_from_id(
163 "https://info.pleroma.site/activity4.json"
167 test "handle HTTP 410 Gone response" do
168 assert {:error, "Object has been deleted"} ==
169 Fetcher.fetch_and_contain_remote_object_from_id(
170 "https://mastodon.example.org/users/userisgone"
174 test "handle HTTP 404 response" do
175 assert {:error, "Object has been deleted"} ==
176 Fetcher.fetch_and_contain_remote_object_from_id(
177 "https://mastodon.example.org/users/userisgone404"
181 test "it can fetch pleroma polls with attachments" do
183 Fetcher.fetch_object_from_id("https://patch.cx/objects/tesla_mock/poll_attachment")
189 describe "pruning" do
190 test "it can refetch pruned objects" do
191 object_id = "http://mastodon.example.org/@admin/99541947525187367"
193 {:ok, object} = Fetcher.fetch_object_from_id(object_id)
197 {:ok, _object} = Object.prune(object)
199 refute Object.get_by_ap_id(object_id)
201 {:ok, %Object{} = object_two} = Fetcher.fetch_object_from_id(object_id)
203 assert object.data["id"] == object_two.data["id"]
204 assert object.id != object_two.id
208 describe "signed fetches" do
209 setup do: clear_config([:activitypub, :sign_object_fetches])
211 test_with_mock "it signs fetches when configured to do so",
215 Pleroma.Config.put([:activitypub, :sign_object_fetches], true)
217 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
219 assert called(Pleroma.Signature.sign(:_, :_))
222 test_with_mock "it doesn't sign fetches when not configured to do so",
226 Pleroma.Config.put([:activitypub, :sign_object_fetches], false)
228 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
230 refute called(Pleroma.Signature.sign(:_, :_))