static-fe.css: Restore and move to /priv/static/static-fe
[akkoma] / test / object_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.ObjectTest do
6 use Pleroma.DataCase
7 use Oban.Testing, repo: Pleroma.Repo
8 import ExUnit.CaptureLog
9 import Pleroma.Factory
10 import Tesla.Mock
11 alias Pleroma.Activity
12 alias Pleroma.Object
13 alias Pleroma.Repo
14 alias Pleroma.Tests.ObanHelpers
15 alias Pleroma.Web.CommonAPI
16
17 setup do
18 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
19 :ok
20 end
21
22 test "returns an object by it's AP id" do
23 object = insert(:note)
24 found_object = Object.get_by_ap_id(object.data["id"])
25
26 assert object == found_object
27 end
28
29 describe "generic changeset" do
30 test "it ensures uniqueness of the id" do
31 object = insert(:note)
32 cs = Object.change(%Object{}, %{data: %{id: object.data["id"]}})
33 assert cs.valid?
34
35 {:error, _result} = Repo.insert(cs)
36 end
37 end
38
39 describe "deletion function" do
40 test "deletes an object" do
41 object = insert(:note)
42 found_object = Object.get_by_ap_id(object.data["id"])
43
44 assert object == found_object
45
46 Object.delete(found_object)
47
48 found_object = Object.get_by_ap_id(object.data["id"])
49
50 refute object == found_object
51
52 assert found_object.data["type"] == "Tombstone"
53 end
54
55 test "ensures cache is cleared for the object" do
56 object = insert(:note)
57 cached_object = Object.get_cached_by_ap_id(object.data["id"])
58
59 assert object == cached_object
60
61 Cachex.put(:web_resp_cache, URI.parse(object.data["id"]).path, "cofe")
62
63 Object.delete(cached_object)
64
65 {:ok, nil} = Cachex.get(:object_cache, "object:#{object.data["id"]}")
66 {:ok, nil} = Cachex.get(:web_resp_cache, URI.parse(object.data["id"]).path)
67
68 cached_object = Object.get_cached_by_ap_id(object.data["id"])
69
70 refute object == cached_object
71
72 assert cached_object.data["type"] == "Tombstone"
73 end
74 end
75
76 describe "delete attachments" do
77 clear_config([Pleroma.Upload])
78 clear_config([:instance, :cleanup_attachments])
79
80 test "Disabled via config" do
81 Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
82 Pleroma.Config.put([:instance, :cleanup_attachments], false)
83
84 file = %Plug.Upload{
85 content_type: "image/jpg",
86 path: Path.absname("test/fixtures/image.jpg"),
87 filename: "an_image.jpg"
88 }
89
90 user = insert(:user)
91
92 {:ok, %Object{} = attachment} =
93 Pleroma.Web.ActivityPub.ActivityPub.upload(file, actor: user.ap_id)
94
95 %{data: %{"attachment" => [%{"url" => [%{"href" => href}]}]}} =
96 note = insert(:note, %{user: user, data: %{"attachment" => [attachment.data]}})
97
98 uploads_dir = Pleroma.Config.get!([Pleroma.Uploaders.Local, :uploads])
99
100 path = href |> Path.dirname() |> Path.basename()
101
102 assert {:ok, ["an_image.jpg"]} == File.ls("#{uploads_dir}/#{path}")
103
104 Object.delete(note)
105
106 ObanHelpers.perform(all_enqueued(worker: Pleroma.Workers.AttachmentsCleanupWorker))
107
108 assert Object.get_by_id(note.id).data["deleted"]
109 refute Object.get_by_id(attachment.id) == nil
110
111 assert {:ok, ["an_image.jpg"]} == File.ls("#{uploads_dir}/#{path}")
112 end
113
114 test "in subdirectories" do
115 Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
116 Pleroma.Config.put([:instance, :cleanup_attachments], true)
117
118 file = %Plug.Upload{
119 content_type: "image/jpg",
120 path: Path.absname("test/fixtures/image.jpg"),
121 filename: "an_image.jpg"
122 }
123
124 user = insert(:user)
125
126 {:ok, %Object{} = attachment} =
127 Pleroma.Web.ActivityPub.ActivityPub.upload(file, actor: user.ap_id)
128
129 %{data: %{"attachment" => [%{"url" => [%{"href" => href}]}]}} =
130 note = insert(:note, %{user: user, data: %{"attachment" => [attachment.data]}})
131
132 uploads_dir = Pleroma.Config.get!([Pleroma.Uploaders.Local, :uploads])
133
134 path = href |> Path.dirname() |> Path.basename()
135
136 assert {:ok, ["an_image.jpg"]} == File.ls("#{uploads_dir}/#{path}")
137
138 Object.delete(note)
139
140 ObanHelpers.perform(all_enqueued(worker: Pleroma.Workers.AttachmentsCleanupWorker))
141
142 assert Object.get_by_id(note.id).data["deleted"]
143 assert Object.get_by_id(attachment.id) == nil
144
145 assert {:ok, []} == File.ls("#{uploads_dir}/#{path}")
146 end
147
148 test "with dedupe enabled" do
149 Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
150 Pleroma.Config.put([Pleroma.Upload, :filters], [Pleroma.Upload.Filter.Dedupe])
151 Pleroma.Config.put([:instance, :cleanup_attachments], true)
152
153 uploads_dir = Pleroma.Config.get!([Pleroma.Uploaders.Local, :uploads])
154
155 File.mkdir_p!(uploads_dir)
156
157 file = %Plug.Upload{
158 content_type: "image/jpg",
159 path: Path.absname("test/fixtures/image.jpg"),
160 filename: "an_image.jpg"
161 }
162
163 user = insert(:user)
164
165 {:ok, %Object{} = attachment} =
166 Pleroma.Web.ActivityPub.ActivityPub.upload(file, actor: user.ap_id)
167
168 %{data: %{"attachment" => [%{"url" => [%{"href" => href}]}]}} =
169 note = insert(:note, %{user: user, data: %{"attachment" => [attachment.data]}})
170
171 filename = Path.basename(href)
172
173 assert {:ok, files} = File.ls(uploads_dir)
174 assert filename in files
175
176 Object.delete(note)
177
178 ObanHelpers.perform(all_enqueued(worker: Pleroma.Workers.AttachmentsCleanupWorker))
179
180 assert Object.get_by_id(note.id).data["deleted"]
181 assert Object.get_by_id(attachment.id) == nil
182 assert {:ok, files} = File.ls(uploads_dir)
183 refute filename in files
184 end
185
186 test "with objects that have legacy data.url attribute" do
187 Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
188 Pleroma.Config.put([:instance, :cleanup_attachments], true)
189
190 file = %Plug.Upload{
191 content_type: "image/jpg",
192 path: Path.absname("test/fixtures/image.jpg"),
193 filename: "an_image.jpg"
194 }
195
196 user = insert(:user)
197
198 {:ok, %Object{} = attachment} =
199 Pleroma.Web.ActivityPub.ActivityPub.upload(file, actor: user.ap_id)
200
201 {:ok, %Object{}} = Object.create(%{url: "https://google.com", actor: user.ap_id})
202
203 %{data: %{"attachment" => [%{"url" => [%{"href" => href}]}]}} =
204 note = insert(:note, %{user: user, data: %{"attachment" => [attachment.data]}})
205
206 uploads_dir = Pleroma.Config.get!([Pleroma.Uploaders.Local, :uploads])
207
208 path = href |> Path.dirname() |> Path.basename()
209
210 assert {:ok, ["an_image.jpg"]} == File.ls("#{uploads_dir}/#{path}")
211
212 Object.delete(note)
213
214 ObanHelpers.perform(all_enqueued(worker: Pleroma.Workers.AttachmentsCleanupWorker))
215
216 assert Object.get_by_id(note.id).data["deleted"]
217 assert Object.get_by_id(attachment.id) == nil
218
219 assert {:ok, []} == File.ls("#{uploads_dir}/#{path}")
220 end
221
222 test "With custom base_url" do
223 Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
224 Pleroma.Config.put([Pleroma.Upload, :base_url], "https://sub.domain.tld/dir/")
225 Pleroma.Config.put([:instance, :cleanup_attachments], true)
226
227 file = %Plug.Upload{
228 content_type: "image/jpg",
229 path: Path.absname("test/fixtures/image.jpg"),
230 filename: "an_image.jpg"
231 }
232
233 user = insert(:user)
234
235 {:ok, %Object{} = attachment} =
236 Pleroma.Web.ActivityPub.ActivityPub.upload(file, actor: user.ap_id)
237
238 %{data: %{"attachment" => [%{"url" => [%{"href" => href}]}]}} =
239 note = insert(:note, %{user: user, data: %{"attachment" => [attachment.data]}})
240
241 uploads_dir = Pleroma.Config.get!([Pleroma.Uploaders.Local, :uploads])
242
243 path = href |> Path.dirname() |> Path.basename()
244
245 assert {:ok, ["an_image.jpg"]} == File.ls("#{uploads_dir}/#{path}")
246
247 Object.delete(note)
248
249 ObanHelpers.perform(all_enqueued(worker: Pleroma.Workers.AttachmentsCleanupWorker))
250
251 assert Object.get_by_id(note.id).data["deleted"]
252 assert Object.get_by_id(attachment.id) == nil
253
254 assert {:ok, []} == File.ls("#{uploads_dir}/#{path}")
255 end
256 end
257
258 describe "normalizer" do
259 test "fetches unknown objects by default" do
260 %Object{} =
261 object = Object.normalize("http://mastodon.example.org/@admin/99541947525187367")
262
263 assert object.data["url"] == "http://mastodon.example.org/@admin/99541947525187367"
264 end
265
266 test "fetches unknown objects when fetch_remote is explicitly true" do
267 %Object{} =
268 object = Object.normalize("http://mastodon.example.org/@admin/99541947525187367", true)
269
270 assert object.data["url"] == "http://mastodon.example.org/@admin/99541947525187367"
271 end
272
273 test "does not fetch unknown objects when fetch_remote is false" do
274 assert is_nil(
275 Object.normalize("http://mastodon.example.org/@admin/99541947525187367", false)
276 )
277 end
278 end
279
280 describe "get_by_id_and_maybe_refetch" do
281 setup do
282 mock(fn
283 %{method: :get, url: "https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d"} ->
284 %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/poll_original.json")}
285
286 env ->
287 apply(HttpRequestMock, :request, [env])
288 end)
289
290 mock_modified = fn resp ->
291 mock(fn
292 %{method: :get, url: "https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d"} ->
293 resp
294
295 env ->
296 apply(HttpRequestMock, :request, [env])
297 end)
298 end
299
300 on_exit(fn -> mock(fn env -> apply(HttpRequestMock, :request, [env]) end) end)
301
302 [mock_modified: mock_modified]
303 end
304
305 test "refetches if the time since the last refetch is greater than the interval", %{
306 mock_modified: mock_modified
307 } do
308 %Object{} =
309 object = Object.normalize("https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d")
310
311 Object.set_cache(object)
312
313 assert Enum.at(object.data["oneOf"], 0)["replies"]["totalItems"] == 4
314 assert Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 0
315
316 mock_modified.(%Tesla.Env{
317 status: 200,
318 body: File.read!("test/fixtures/tesla_mock/poll_modified.json")
319 })
320
321 updated_object = Object.get_by_id_and_maybe_refetch(object.id, interval: -1)
322 object_in_cache = Object.get_cached_by_ap_id(object.data["id"])
323 assert updated_object == object_in_cache
324 assert Enum.at(updated_object.data["oneOf"], 0)["replies"]["totalItems"] == 8
325 assert Enum.at(updated_object.data["oneOf"], 1)["replies"]["totalItems"] == 3
326 end
327
328 test "returns the old object if refetch fails", %{mock_modified: mock_modified} do
329 %Object{} =
330 object = Object.normalize("https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d")
331
332 Object.set_cache(object)
333
334 assert Enum.at(object.data["oneOf"], 0)["replies"]["totalItems"] == 4
335 assert Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 0
336
337 assert capture_log(fn ->
338 mock_modified.(%Tesla.Env{status: 404, body: ""})
339
340 updated_object = Object.get_by_id_and_maybe_refetch(object.id, interval: -1)
341 object_in_cache = Object.get_cached_by_ap_id(object.data["id"])
342 assert updated_object == object_in_cache
343 assert Enum.at(updated_object.data["oneOf"], 0)["replies"]["totalItems"] == 4
344 assert Enum.at(updated_object.data["oneOf"], 1)["replies"]["totalItems"] == 0
345 end) =~
346 "[error] Couldn't refresh https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d"
347 end
348
349 test "does not refetch if the time since the last refetch is greater than the interval", %{
350 mock_modified: mock_modified
351 } do
352 %Object{} =
353 object = Object.normalize("https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d")
354
355 Object.set_cache(object)
356
357 assert Enum.at(object.data["oneOf"], 0)["replies"]["totalItems"] == 4
358 assert Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 0
359
360 mock_modified.(%Tesla.Env{
361 status: 200,
362 body: File.read!("test/fixtures/tesla_mock/poll_modified.json")
363 })
364
365 updated_object = Object.get_by_id_and_maybe_refetch(object.id, interval: 100)
366 object_in_cache = Object.get_cached_by_ap_id(object.data["id"])
367 assert updated_object == object_in_cache
368 assert Enum.at(updated_object.data["oneOf"], 0)["replies"]["totalItems"] == 4
369 assert Enum.at(updated_object.data["oneOf"], 1)["replies"]["totalItems"] == 0
370 end
371
372 test "preserves internal fields on refetch", %{mock_modified: mock_modified} do
373 %Object{} =
374 object = Object.normalize("https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d")
375
376 Object.set_cache(object)
377
378 assert Enum.at(object.data["oneOf"], 0)["replies"]["totalItems"] == 4
379 assert Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 0
380
381 user = insert(:user)
382 activity = Activity.get_create_by_object_ap_id(object.data["id"])
383 {:ok, _activity, object} = CommonAPI.favorite(activity.id, user)
384
385 assert object.data["like_count"] == 1
386
387 mock_modified.(%Tesla.Env{
388 status: 200,
389 body: File.read!("test/fixtures/tesla_mock/poll_modified.json")
390 })
391
392 updated_object = Object.get_by_id_and_maybe_refetch(object.id, interval: -1)
393 object_in_cache = Object.get_cached_by_ap_id(object.data["id"])
394 assert updated_object == object_in_cache
395 assert Enum.at(updated_object.data["oneOf"], 0)["replies"]["totalItems"] == 8
396 assert Enum.at(updated_object.data["oneOf"], 1)["replies"]["totalItems"] == 3
397
398 assert updated_object.data["like_count"] == 1
399 end
400 end
401 end