1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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 "actor origin containment" do
30 test_with_mock "it rejects objects with a bogus origin",
34 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity.json")
36 refute called(Pleroma.Web.OStatus.fetch_activity_from_url(:_))
39 test_with_mock "it rejects objects when attributedTo is wrong (variant 1)",
43 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity2.json")
45 refute called(Pleroma.Web.OStatus.fetch_activity_from_url(:_))
48 test_with_mock "it rejects objects when attributedTo is wrong (variant 2)",
52 {:error, _} = Fetcher.fetch_object_from_id("https://info.pleroma.site/activity3.json")
54 refute called(Pleroma.Web.OStatus.fetch_activity_from_url(:_))
58 describe "fetching an object" do
59 test "it fetches an object" do
61 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
63 assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
64 assert activity.data["id"]
67 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
69 assert [attachment] = object.data["attachment"]
70 assert is_list(attachment["url"])
72 assert object == object_again
75 test "it works with objects only available via Ostatus" do
76 {:ok, object} = Fetcher.fetch_object_from_id("https://shitposter.club/notice/2827873")
77 assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
78 assert activity.data["id"]
80 {:ok, object_again} = Fetcher.fetch_object_from_id("https://shitposter.club/notice/2827873")
82 assert object == object_again
85 test "it correctly stitches up conversations between ostatus and ap" do
86 last = "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
87 {:ok, object} = Fetcher.fetch_object_from_id(last)
89 object = Object.get_by_ap_id(object.data["inReplyTo"])
94 describe "implementation quirks" do
95 test "it can fetch plume articles" do
97 Fetcher.fetch_object_from_id(
98 "https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/"
104 test "it can fetch peertube videos" do
106 Fetcher.fetch_object_from_id(
107 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
113 test "all objects with fake directions are rejected by the object fetcher" do
115 Fetcher.fetch_and_contain_remote_object_from_id(
116 "https://info.pleroma.site/activity4.json"
120 test "handle HTTP 410 Gone response" do
121 assert {:error, "Object has been deleted"} ==
122 Fetcher.fetch_and_contain_remote_object_from_id(
123 "https://mastodon.example.org/users/userisgone"
127 test "handle HTTP 404 response" do
128 assert {:error, "Object has been deleted"} ==
129 Fetcher.fetch_and_contain_remote_object_from_id(
130 "https://mastodon.example.org/users/userisgone404"
135 describe "pruning" do
136 test "it can refetch pruned objects" do
137 object_id = "http://mastodon.example.org/@admin/99541947525187367"
139 {:ok, object} = Fetcher.fetch_object_from_id(object_id)
143 {:ok, _object} = Object.prune(object)
145 refute Object.get_by_ap_id(object_id)
147 {:ok, %Object{} = object_two} = Fetcher.fetch_object_from_id(object_id)
149 assert object.data["id"] == object_two.data["id"]
150 assert object.id != object_two.id
154 describe "signed fetches" do
155 test_with_mock "it signs fetches when configured to do so",
159 option = Pleroma.Config.get([:activitypub, :sign_object_fetches])
160 Pleroma.Config.put([:activitypub, :sign_object_fetches], true)
162 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
164 assert called(Pleroma.Signature.sign(:_, :_))
166 Pleroma.Config.put([:activitypub, :sign_object_fetches], option)
169 test_with_mock "it doesn't sign fetches when not configured to do so",
173 option = Pleroma.Config.get([:activitypub, :sign_object_fetches])
174 Pleroma.Config.put([:activitypub, :sign_object_fetches], false)
176 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
178 refute called(Pleroma.Signature.sign(:_, :_))
180 Pleroma.Config.put([:activitypub, :sign_object_fetches], option)