remove `unread_conversation_count` from User
[akkoma] / test / pleroma / 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.Config
10 alias Pleroma.Object
11 alias Pleroma.Object.Fetcher
12
13 import Mock
14 import Tesla.Mock
15
16 setup do
17 mock(fn
18 %{method: :get, url: "https://mastodon.example.org/users/userisgone"} ->
19 %Tesla.Env{status: 410}
20
21 %{method: :get, url: "https://mastodon.example.org/users/userisgone404"} ->
22 %Tesla.Env{status: 404}
23
24 env ->
25 apply(HttpRequestMock, :request, [env])
26 end)
27
28 :ok
29 end
30
31 describe "error cases" do
32 setup do
33 mock(fn
34 %{method: :get, url: "https://social.sakamoto.gq/notice/9wTkLEnuq47B25EehM"} ->
35 %Tesla.Env{
36 status: 200,
37 body: File.read!("test/fixtures/fetch_mocks/9wTkLEnuq47B25EehM.json")
38 }
39
40 %{method: :get, url: "https://social.sakamoto.gq/users/eal"} ->
41 %Tesla.Env{
42 status: 200,
43 body: File.read!("test/fixtures/fetch_mocks/eal.json")
44 }
45
46 %{method: :get, url: "https://busshi.moe/users/tuxcrafting/statuses/104410921027210069"} ->
47 %Tesla.Env{
48 status: 200,
49 body: File.read!("test/fixtures/fetch_mocks/104410921027210069.json")
50 }
51
52 %{method: :get, url: "https://busshi.moe/users/tuxcrafting"} ->
53 %Tesla.Env{
54 status: 500
55 }
56 end)
57
58 :ok
59 end
60
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
64 assert {:ok, _} =
65 Fetcher.fetch_object_from_id(
66 "https://social.sakamoto.gq/notice/9wTkLEnuq47B25EehM"
67 )
68 end
69 end
70
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])
74
75 test "it returns thread depth exceeded error if thread depth is exceeded" do
76 Config.put([:instance, :federation_incoming_replies_max_depth], 0)
77
78 assert {:error, "Max thread distance exceeded."} =
79 Fetcher.fetch_object_from_id(@ap_id, depth: 1)
80 end
81
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)
84
85 assert {:ok, _} = Fetcher.fetch_object_from_id(@ap_id)
86 end
87
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)
90
91 assert {:ok, _} = Fetcher.fetch_object_from_id(@ap_id, depth: 10)
92 end
93 end
94
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")
98 end
99
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")
102 end
103
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")
106 end
107 end
108
109 describe "fetching an object" do
110 test "it fetches an object" do
111 {:ok, object} =
112 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
113
114 assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
115 assert activity.data["id"]
116
117 {:ok, object_again} =
118 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
119
120 assert [attachment] = object.data["attachment"]
121 assert is_list(attachment["url"])
122
123 assert object == object_again
124 end
125
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])
129
130 assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} ==
131 Fetcher.fetch_object_from_id(
132 "http://mastodon.example.org/@admin/99541947525187367"
133 )
134 end
135 end
136
137 describe "implementation quirks" do
138 test "it can fetch plume articles" do
139 {:ok, object} =
140 Fetcher.fetch_object_from_id(
141 "https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/"
142 )
143
144 assert object
145 end
146
147 test "it can fetch peertube videos" do
148 {:ok, object} =
149 Fetcher.fetch_object_from_id(
150 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
151 )
152
153 assert object
154 end
155
156 test "it can fetch Mobilizon events" do
157 {:ok, object} =
158 Fetcher.fetch_object_from_id(
159 "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"
160 )
161
162 assert object
163 end
164
165 test "it can fetch wedistribute articles" do
166 {:ok, object} =
167 Fetcher.fetch_object_from_id("https://wedistribute.org/wp-json/pterotype/v1/object/85810")
168
169 assert object
170 end
171
172 test "all objects with fake directions are rejected by the object fetcher" do
173 assert {:error, _} =
174 Fetcher.fetch_and_contain_remote_object_from_id(
175 "https://info.pleroma.site/activity4.json"
176 )
177 end
178
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"
183 )
184 end
185
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"
190 )
191 end
192
193 test "it can fetch pleroma polls with attachments" do
194 {:ok, object} =
195 Fetcher.fetch_object_from_id("https://patch.cx/objects/tesla_mock/poll_attachment")
196
197 assert object
198 end
199 end
200
201 describe "pruning" do
202 test "it can refetch pruned objects" do
203 object_id = "http://mastodon.example.org/@admin/99541947525187367"
204
205 {:ok, object} = Fetcher.fetch_object_from_id(object_id)
206
207 assert object
208
209 {:ok, _object} = Object.prune(object)
210
211 refute Object.get_by_ap_id(object_id)
212
213 {:ok, %Object{} = object_two} = Fetcher.fetch_object_from_id(object_id)
214
215 assert object.data["id"] == object_two.data["id"]
216 assert object.id != object_two.id
217 end
218 end
219
220 describe "signed fetches" do
221 setup do: clear_config([:activitypub, :sign_object_fetches])
222
223 test_with_mock "it signs fetches when configured to do so",
224 Pleroma.Signature,
225 [:passthrough],
226 [] do
227 Config.put([:activitypub, :sign_object_fetches], true)
228
229 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
230
231 assert called(Pleroma.Signature.sign(:_, :_))
232 end
233
234 test_with_mock "it doesn't sign fetches when not configured to do so",
235 Pleroma.Signature,
236 [:passthrough],
237 [] do
238 Config.put([:activitypub, :sign_object_fetches], false)
239
240 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
241
242 refute called(Pleroma.Signature.sign(:_, :_))
243 end
244 end
245 end