remove `unread_conversation_count` from User
[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
181 test "it can fetch pleroma polls with attachments" do
182 {:ok, object} =
183 Fetcher.fetch_object_from_id("https://patch.cx/objects/tesla_mock/poll_attachment")
184
185 assert object
186 end
187 end
188
189 describe "pruning" do
190 test "it can refetch pruned objects" do
191 object_id = "http://mastodon.example.org/@admin/99541947525187367"
192
193 {:ok, object} = Fetcher.fetch_object_from_id(object_id)
194
195 assert object
196
197 {:ok, _object} = Object.prune(object)
198
199 refute Object.get_by_ap_id(object_id)
200
201 {:ok, %Object{} = object_two} = Fetcher.fetch_object_from_id(object_id)
202
203 assert object.data["id"] == object_two.data["id"]
204 assert object.id != object_two.id
205 end
206 end
207
208 describe "signed fetches" do
209 setup do: clear_config([:activitypub, :sign_object_fetches])
210
211 test_with_mock "it signs fetches when configured to do so",
212 Pleroma.Signature,
213 [:passthrough],
214 [] do
215 Pleroma.Config.put([:activitypub, :sign_object_fetches], true)
216
217 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
218
219 assert called(Pleroma.Signature.sign(:_, :_))
220 end
221
222 test_with_mock "it doesn't sign fetches when not configured to do so",
223 Pleroma.Signature,
224 [:passthrough],
225 [] do
226 Pleroma.Config.put([:activitypub, :sign_object_fetches], false)
227
228 Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
229
230 refute called(Pleroma.Signature.sign(:_, :_))
231 end
232 end
233 end