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 "max thread distance restriction" do
30 @ap_id "http://mastodon.example.org/@admin/99541947525187367"
32 clear_config([:instance, :federation_incoming_replies_max_depth])
34 test "it returns thread depth exceeded error if thread depth is exceeded" do
35 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
37 assert {:error, "Max thread distance exceeded."} =
38 Fetcher.fetch_object_from_id(@ap_id, depth: 1)
41 test "it fetches object if max thread depth is restricted to 0 and depth is not specified" do
42 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
44 assert {:ok, _} = Fetcher.fetch_object_from_id(@ap_id)
47 test "it fetches object if requested depth does not exceed max thread depth" do
48 Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 10)
50 assert {:ok, _} = Fetcher.fetch_object_from_id(@ap_id, depth: 10)
54 describe "actor origin containment" do
55 test "it rejects objects with a bogus origin" do
56 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity.json")
59 test "it rejects objects when attributedTo is wrong (variant 1)" do
60 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity2.json")
63 test "it rejects objects when attributedTo is wrong (variant 2)" do
64 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity3.json")
68 describe "fetching an object" do
69 test "it fetches an object" do
71 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
73 assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
74 assert activity.data["id"]
77 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
79 assert [attachment] = object.data["attachment"]
80 assert is_list(attachment["url"])
82 assert object == object_again
86 describe "implementation quirks" do
87 test "it can fetch plume articles" do
89 Fetcher.fetch_object_from_id(
90 "https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/"
96 test "it can fetch peertube videos" do
98 Fetcher.fetch_object_from_id(
99 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
105 test "it can fetch Mobilizon events" do
107 Fetcher.fetch_object_from_id(
108 "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"
114 test "it can fetch wedistribute articles" do
116 Fetcher.fetch_object_from_id("https://wedistribute.org/wp-json/pterotype/v1/object/85810")
121 test "all objects with fake directions are rejected by the object fetcher" do
123 Fetcher.fetch_and_contain_remote_object_from_id(
124 "https://info.pleroma.site/activity4.json"
128 test "handle HTTP 410 Gone response" do
129 assert {:error, "Object has been deleted"} ==
130 Fetcher.fetch_and_contain_remote_object_from_id(
131 "https://mastodon.example.org/users/userisgone"
135 test "handle HTTP 404 response" do
136 assert {:error, "Object has been deleted"} ==
137 Fetcher.fetch_and_contain_remote_object_from_id(
138 "https://mastodon.example.org/users/userisgone404"
143 describe "pruning" do
144 test "it can refetch pruned objects" do
145 object_id = "http://mastodon.example.org/@admin/99541947525187367"
147 {:ok, object} = Fetcher.fetch_object_from_id(object_id)
151 {:ok, _object} = Object.prune(object)
153 refute Object.get_by_ap_id(object_id)
155 {:ok, %Object{} = object_two} = Fetcher.fetch_object_from_id(object_id)
157 assert object.data["id"] == object_two.data["id"]
158 assert object.id != object_two.id
162 describe "signed fetches" do
163 clear_config([:activitypub, :sign_object_fetches])
165 test_with_mock "it signs fetches when configured to do so",
169 Pleroma.Config.put([:activitypub, :sign_object_fetches], true)
171 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
173 assert called(Pleroma.Signature.sign(:_, :_))
176 test_with_mock "it doesn't sign fetches when not configured to do so",
180 Pleroma.Config.put([:activitypub, :sign_object_fetches], false)
182 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
184 refute called(Pleroma.Signature.sign(:_, :_))