Merge branch 'develop' into 'cleanup/masto_fe-default_settings'
[akkoma] / test / object / fetcher_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Object.FetcherTest do
6 use Pleroma.DataCase
7
8 alias Pleroma.Activity
9 alias Pleroma.Object
10 alias Pleroma.Object.Fetcher
11 import Tesla.Mock
12 import Mock
13
14 setup do
15 mock(fn
16 %{method: :get, url: "https://mastodon.example.org/users/userisgone"} ->
17 %Tesla.Env{status: 410}
18
19 %{method: :get, url: "https://mastodon.example.org/users/userisgone404"} ->
20 %Tesla.Env{status: 404}
21
22 env ->
23 apply(HttpRequestMock, :request, [env])
24 end)
25
26 :ok
27 end
28
29 describe "error cases" do
30 setup do
31 mock(fn
32 %{method: :get, url: "https://social.sakamoto.gq/notice/9wTkLEnuq47B25EehM"} ->
33 %Tesla.Env{
34 status: 200,
35 body: File.read!("test/fixtures/fetch_mocks/9wTkLEnuq47B25EehM.json")
36 }
37
38 %{method: :get, url: "https://social.sakamoto.gq/users/eal"} ->
39 %Tesla.Env{
40 status: 200,
41 body: File.read!("test/fixtures/fetch_mocks/eal.json")
42 }
43
44 %{method: :get, url: "https://busshi.moe/users/tuxcrafting/statuses/104410921027210069"} ->
45 %Tesla.Env{
46 status: 200,
47 body: File.read!("test/fixtures/fetch_mocks/104410921027210069.json")
48 }
49
50 %{method: :get, url: "https://busshi.moe/users/tuxcrafting"} ->
51 %Tesla.Env{
52 status: 500
53 }
54 end)
55
56 :ok
57 end
58
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
62 assert {:ok, _} =
63 Fetcher.fetch_object_from_id(
64 "https://social.sakamoto.gq/notice/9wTkLEnuq47B25EehM"
65 )
66 end
67 end
68
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])
72
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)
75
76 assert {:error, "Max thread distance exceeded."} =
77 Fetcher.fetch_object_from_id(@ap_id, depth: 1)
78 end
79
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)
82
83 assert {:ok, _} = Fetcher.fetch_object_from_id(@ap_id)
84 end
85
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)
88
89 assert {:ok, _} = Fetcher.fetch_object_from_id(@ap_id, depth: 10)
90 end
91 end
92
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")
96 end
97
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")
100 end
101
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")
104 end
105 end
106
107 describe "fetching an object" do
108 test "it fetches an object" do
109 {:ok, object} =
110 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
111
112 assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
113 assert activity.data["id"]
114
115 {:ok, object_again} =
116 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
117
118 assert [attachment] = object.data["attachment"]
119 assert is_list(attachment["url"])
120
121 assert object == object_again
122 end
123 end
124
125 describe "implementation quirks" do
126 test "it can fetch plume articles" do
127 {:ok, object} =
128 Fetcher.fetch_object_from_id(
129 "https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/"
130 )
131
132 assert object
133 end
134
135 test "it can fetch peertube videos" do
136 {:ok, object} =
137 Fetcher.fetch_object_from_id(
138 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
139 )
140
141 assert object
142 end
143
144 test "it can fetch Mobilizon events" do
145 {:ok, object} =
146 Fetcher.fetch_object_from_id(
147 "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"
148 )
149
150 assert object
151 end
152
153 test "it can fetch wedistribute articles" do
154 {:ok, object} =
155 Fetcher.fetch_object_from_id("https://wedistribute.org/wp-json/pterotype/v1/object/85810")
156
157 assert object
158 end
159
160 test "all objects with fake directions are rejected by the object fetcher" do
161 assert {:error, _} =
162 Fetcher.fetch_and_contain_remote_object_from_id(
163 "https://info.pleroma.site/activity4.json"
164 )
165 end
166
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"
171 )
172 end
173
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"
178 )
179 end
180 end
181
182 describe "pruning" do
183 test "it can refetch pruned objects" do
184 object_id = "http://mastodon.example.org/@admin/99541947525187367"
185
186 {:ok, object} = Fetcher.fetch_object_from_id(object_id)
187
188 assert object
189
190 {:ok, _object} = Object.prune(object)
191
192 refute Object.get_by_ap_id(object_id)
193
194 {:ok, %Object{} = object_two} = Fetcher.fetch_object_from_id(object_id)
195
196 assert object.data["id"] == object_two.data["id"]
197 assert object.id != object_two.id
198 end
199 end
200
201 describe "signed fetches" do
202 setup do: clear_config([:activitypub, :sign_object_fetches])
203
204 test_with_mock "it signs fetches when configured to do so",
205 Pleroma.Signature,
206 [:passthrough],
207 [] do
208 Pleroma.Config.put([:activitypub, :sign_object_fetches], true)
209
210 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
211
212 assert called(Pleroma.Signature.sign(:_, :_))
213 end
214
215 test_with_mock "it doesn't sign fetches when not configured to do so",
216 Pleroma.Signature,
217 [:passthrough],
218 [] do
219 Pleroma.Config.put([:activitypub, :sign_object_fetches], false)
220
221 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
222
223 refute called(Pleroma.Signature.sign(:_, :_))
224 end
225 end
226 end