1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.ObjectTest do
7 use Oban.Testing, repo: Pleroma.Repo
8 import ExUnit.CaptureLog
11 alias Pleroma.Activity
14 alias Pleroma.Tests.ObanHelpers
15 alias Pleroma.Web.CommonAPI
18 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
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"])
26 assert object == found_object
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"]}})
35 {:error, _result} = Repo.insert(cs)
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"])
44 assert object == found_object
46 Object.delete(found_object)
48 found_object = Object.get_by_ap_id(object.data["id"])
50 refute object == found_object
52 assert found_object.data["type"] == "Tombstone"
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"])
59 assert object == cached_object
61 Cachex.put(:web_resp_cache, URI.parse(object.data["id"]).path, "cofe")
63 Object.delete(cached_object)
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)
68 cached_object = Object.get_cached_by_ap_id(object.data["id"])
70 refute object == cached_object
72 assert cached_object.data["type"] == "Tombstone"
76 describe "delete attachments" do
77 setup do: clear_config([Pleroma.Upload])
78 setup do: clear_config([:instance, :cleanup_attachments])
80 test "Disabled via config" do
81 Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
82 Pleroma.Config.put([:instance, :cleanup_attachments], false)
85 content_type: "image/jpg",
86 path: Path.absname("test/fixtures/image.jpg"),
87 filename: "an_image.jpg"
92 {:ok, %Object{} = attachment} =
93 Pleroma.Web.ActivityPub.ActivityPub.upload(file, actor: user.ap_id)
95 %{data: %{"attachment" => [%{"url" => [%{"href" => href}]}]}} =
96 note = insert(:note, %{user: user, data: %{"attachment" => [attachment.data]}})
98 uploads_dir = Pleroma.Config.get!([Pleroma.Uploaders.Local, :uploads])
100 path = href |> Path.dirname() |> Path.basename()
102 assert {:ok, ["an_image.jpg"]} == File.ls("#{uploads_dir}/#{path}")
106 ObanHelpers.perform(all_enqueued(worker: Pleroma.Workers.AttachmentsCleanupWorker))
108 assert Object.get_by_id(note.id).data["deleted"]
109 refute Object.get_by_id(attachment.id) == nil
111 assert {:ok, ["an_image.jpg"]} == File.ls("#{uploads_dir}/#{path}")
114 test "in subdirectories" do
115 Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
116 Pleroma.Config.put([:instance, :cleanup_attachments], true)
119 content_type: "image/jpg",
120 path: Path.absname("test/fixtures/image.jpg"),
121 filename: "an_image.jpg"
126 {:ok, %Object{} = attachment} =
127 Pleroma.Web.ActivityPub.ActivityPub.upload(file, actor: user.ap_id)
129 %{data: %{"attachment" => [%{"url" => [%{"href" => href}]}]}} =
130 note = insert(:note, %{user: user, data: %{"attachment" => [attachment.data]}})
132 uploads_dir = Pleroma.Config.get!([Pleroma.Uploaders.Local, :uploads])
134 path = href |> Path.dirname() |> Path.basename()
136 assert {:ok, ["an_image.jpg"]} == File.ls("#{uploads_dir}/#{path}")
140 ObanHelpers.perform(all_enqueued(worker: Pleroma.Workers.AttachmentsCleanupWorker))
142 assert Object.get_by_id(note.id).data["deleted"]
143 assert Object.get_by_id(attachment.id) == nil
145 assert {:ok, []} == File.ls("#{uploads_dir}/#{path}")
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)
153 uploads_dir = Pleroma.Config.get!([Pleroma.Uploaders.Local, :uploads])
155 File.mkdir_p!(uploads_dir)
158 content_type: "image/jpg",
159 path: Path.absname("test/fixtures/image.jpg"),
160 filename: "an_image.jpg"
165 {:ok, %Object{} = attachment} =
166 Pleroma.Web.ActivityPub.ActivityPub.upload(file, actor: user.ap_id)
168 %{data: %{"attachment" => [%{"url" => [%{"href" => href}]}]}} =
169 note = insert(:note, %{user: user, data: %{"attachment" => [attachment.data]}})
171 filename = Path.basename(href)
173 assert {:ok, files} = File.ls(uploads_dir)
174 assert filename in files
178 ObanHelpers.perform(all_enqueued(worker: Pleroma.Workers.AttachmentsCleanupWorker))
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
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)
191 content_type: "image/jpg",
192 path: Path.absname("test/fixtures/image.jpg"),
193 filename: "an_image.jpg"
198 {:ok, %Object{} = attachment} =
199 Pleroma.Web.ActivityPub.ActivityPub.upload(file, actor: user.ap_id)
201 {:ok, %Object{}} = Object.create(%{url: "https://google.com", actor: user.ap_id})
203 %{data: %{"attachment" => [%{"url" => [%{"href" => href}]}]}} =
204 note = insert(:note, %{user: user, data: %{"attachment" => [attachment.data]}})
206 uploads_dir = Pleroma.Config.get!([Pleroma.Uploaders.Local, :uploads])
208 path = href |> Path.dirname() |> Path.basename()
210 assert {:ok, ["an_image.jpg"]} == File.ls("#{uploads_dir}/#{path}")
214 ObanHelpers.perform(all_enqueued(worker: Pleroma.Workers.AttachmentsCleanupWorker))
216 assert Object.get_by_id(note.id).data["deleted"]
217 assert Object.get_by_id(attachment.id) == nil
219 assert {:ok, []} == File.ls("#{uploads_dir}/#{path}")
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)
228 content_type: "image/jpg",
229 path: Path.absname("test/fixtures/image.jpg"),
230 filename: "an_image.jpg"
235 {:ok, %Object{} = attachment} =
236 Pleroma.Web.ActivityPub.ActivityPub.upload(file, actor: user.ap_id)
238 %{data: %{"attachment" => [%{"url" => [%{"href" => href}]}]}} =
239 note = insert(:note, %{user: user, data: %{"attachment" => [attachment.data]}})
241 uploads_dir = Pleroma.Config.get!([Pleroma.Uploaders.Local, :uploads])
243 path = href |> Path.dirname() |> Path.basename()
245 assert {:ok, ["an_image.jpg"]} == File.ls("#{uploads_dir}/#{path}")
249 ObanHelpers.perform(all_enqueued(worker: Pleroma.Workers.AttachmentsCleanupWorker))
251 assert Object.get_by_id(note.id).data["deleted"]
252 assert Object.get_by_id(attachment.id) == nil
254 assert {:ok, []} == File.ls("#{uploads_dir}/#{path}")
258 describe "normalizer" do
259 test "fetches unknown objects by default" do
261 object = Object.normalize("http://mastodon.example.org/@admin/99541947525187367")
263 assert object.data["url"] == "http://mastodon.example.org/@admin/99541947525187367"
266 test "fetches unknown objects when fetch_remote is explicitly true" do
268 object = Object.normalize("http://mastodon.example.org/@admin/99541947525187367", true)
270 assert object.data["url"] == "http://mastodon.example.org/@admin/99541947525187367"
273 test "does not fetch unknown objects when fetch_remote is false" do
275 Object.normalize("http://mastodon.example.org/@admin/99541947525187367", false)
280 describe "get_by_id_and_maybe_refetch" do
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")}
287 apply(HttpRequestMock, :request, [env])
290 mock_modified = fn resp ->
292 %{method: :get, url: "https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d"} ->
296 apply(HttpRequestMock, :request, [env])
300 on_exit(fn -> mock(fn env -> apply(HttpRequestMock, :request, [env]) end) end)
302 [mock_modified: mock_modified]
305 test "refetches if the time since the last refetch is greater than the interval", %{
306 mock_modified: mock_modified
309 object = Object.normalize("https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d")
311 Object.set_cache(object)
313 assert Enum.at(object.data["oneOf"], 0)["replies"]["totalItems"] == 4
314 assert Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 0
316 mock_modified.(%Tesla.Env{
318 body: File.read!("test/fixtures/tesla_mock/poll_modified.json")
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
328 test "returns the old object if refetch fails", %{mock_modified: mock_modified} do
330 object = Object.normalize("https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d")
332 Object.set_cache(object)
334 assert Enum.at(object.data["oneOf"], 0)["replies"]["totalItems"] == 4
335 assert Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 0
337 assert capture_log(fn ->
338 mock_modified.(%Tesla.Env{status: 404, body: ""})
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
346 "[error] Couldn't refresh https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d"
349 test "does not refetch if the time since the last refetch is greater than the interval", %{
350 mock_modified: mock_modified
353 object = Object.normalize("https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d")
355 Object.set_cache(object)
357 assert Enum.at(object.data["oneOf"], 0)["replies"]["totalItems"] == 4
358 assert Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 0
360 mock_modified.(%Tesla.Env{
362 body: File.read!("test/fixtures/tesla_mock/poll_modified.json")
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
372 test "preserves internal fields on refetch", %{mock_modified: mock_modified} do
374 object = Object.normalize("https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d")
376 Object.set_cache(object)
378 assert Enum.at(object.data["oneOf"], 0)["replies"]["totalItems"] == 4
379 assert Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 0
382 activity = Activity.get_create_by_object_ap_id(object.data["id"])
383 {:ok, activity} = CommonAPI.favorite(user, activity.id)
384 object = Object.get_by_ap_id(activity.data["object"])
386 assert object.data["like_count"] == 1
388 mock_modified.(%Tesla.Env{
390 body: File.read!("test/fixtures/tesla_mock/poll_modified.json")
393 updated_object = Object.get_by_id_and_maybe_refetch(object.id, interval: -1)
394 object_in_cache = Object.get_cached_by_ap_id(object.data["id"])
395 assert updated_object == object_in_cache
396 assert Enum.at(updated_object.data["oneOf"], 0)["replies"]["totalItems"] == 8
397 assert Enum.at(updated_object.data["oneOf"], 1)["replies"]["totalItems"] == 3
399 assert updated_object.data["like_count"] == 1