1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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 clear_config([Pleroma.Upload])
79 test "Disabled via config" do
80 Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
81 Pleroma.Config.put([:instance, :cleanup_attachments], false)
84 content_type: "image/jpg",
85 path: Path.absname("test/fixtures/image.jpg"),
86 filename: "an_image.jpg"
91 {:ok, %Object{} = attachment} =
92 Pleroma.Web.ActivityPub.ActivityPub.upload(file, actor: user.ap_id)
94 %{data: %{"attachment" => [%{"url" => [%{"href" => href}]}]}} =
95 note = insert(:note, %{user: user, data: %{"attachment" => [attachment.data]}})
97 uploads_dir = Pleroma.Config.get!([Pleroma.Uploaders.Local, :uploads])
99 path = href |> Path.dirname() |> Path.basename()
101 assert {:ok, ["an_image.jpg"]} == File.ls("#{uploads_dir}/#{path}")
105 ObanHelpers.perform(all_enqueued(worker: Pleroma.Workers.AttachmentsCleanupWorker))
107 assert Object.get_by_id(note.id).data["deleted"]
108 refute Object.get_by_id(attachment.id) == nil
110 assert {:ok, ["an_image.jpg"]} == File.ls("#{uploads_dir}/#{path}")
113 test "in subdirectories" do
114 Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
115 Pleroma.Config.put([:instance, :cleanup_attachments], true)
118 content_type: "image/jpg",
119 path: Path.absname("test/fixtures/image.jpg"),
120 filename: "an_image.jpg"
125 {:ok, %Object{} = attachment} =
126 Pleroma.Web.ActivityPub.ActivityPub.upload(file, actor: user.ap_id)
128 %{data: %{"attachment" => [%{"url" => [%{"href" => href}]}]}} =
129 note = insert(:note, %{user: user, data: %{"attachment" => [attachment.data]}})
131 uploads_dir = Pleroma.Config.get!([Pleroma.Uploaders.Local, :uploads])
133 path = href |> Path.dirname() |> Path.basename()
135 assert {:ok, ["an_image.jpg"]} == File.ls("#{uploads_dir}/#{path}")
139 ObanHelpers.perform(all_enqueued(worker: Pleroma.Workers.AttachmentsCleanupWorker))
141 assert Object.get_by_id(note.id).data["deleted"]
142 assert Object.get_by_id(attachment.id) == nil
144 assert {:ok, []} == File.ls("#{uploads_dir}/#{path}")
147 test "with dedupe enabled" do
148 Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
149 Pleroma.Config.put([Pleroma.Upload, :filters], [Pleroma.Upload.Filter.Dedupe])
150 Pleroma.Config.put([:instance, :cleanup_attachments], true)
152 uploads_dir = Pleroma.Config.get!([Pleroma.Uploaders.Local, :uploads])
154 File.mkdir_p!(uploads_dir)
157 content_type: "image/jpg",
158 path: Path.absname("test/fixtures/image.jpg"),
159 filename: "an_image.jpg"
164 {:ok, %Object{} = attachment} =
165 Pleroma.Web.ActivityPub.ActivityPub.upload(file, actor: user.ap_id)
167 %{data: %{"attachment" => [%{"url" => [%{"href" => href}]}]}} =
168 note = insert(:note, %{user: user, data: %{"attachment" => [attachment.data]}})
170 filename = Path.basename(href)
172 assert {:ok, files} = File.ls(uploads_dir)
173 assert filename in files
177 ObanHelpers.perform(all_enqueued(worker: Pleroma.Workers.AttachmentsCleanupWorker))
179 assert Object.get_by_id(note.id).data["deleted"]
180 assert Object.get_by_id(attachment.id) == nil
181 assert {:ok, files} = File.ls(uploads_dir)
182 refute filename in files
185 test "with objects that have legacy data.url attribute" do
186 Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
187 Pleroma.Config.put([:instance, :cleanup_attachments], true)
190 content_type: "image/jpg",
191 path: Path.absname("test/fixtures/image.jpg"),
192 filename: "an_image.jpg"
197 {:ok, %Object{} = attachment} =
198 Pleroma.Web.ActivityPub.ActivityPub.upload(file, actor: user.ap_id)
200 {:ok, %Object{}} = Object.create(%{url: "https://google.com", actor: user.ap_id})
202 %{data: %{"attachment" => [%{"url" => [%{"href" => href}]}]}} =
203 note = insert(:note, %{user: user, data: %{"attachment" => [attachment.data]}})
205 uploads_dir = Pleroma.Config.get!([Pleroma.Uploaders.Local, :uploads])
207 path = href |> Path.dirname() |> Path.basename()
209 assert {:ok, ["an_image.jpg"]} == File.ls("#{uploads_dir}/#{path}")
213 ObanHelpers.perform(all_enqueued(worker: Pleroma.Workers.AttachmentsCleanupWorker))
215 assert Object.get_by_id(note.id).data["deleted"]
216 assert Object.get_by_id(attachment.id) == nil
218 assert {:ok, []} == File.ls("#{uploads_dir}/#{path}")
221 test "With custom base_url" do
222 Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
223 Pleroma.Config.put([Pleroma.Upload, :base_url], "https://sub.domain.tld/dir/")
224 Pleroma.Config.put([:instance, :cleanup_attachments], true)
227 content_type: "image/jpg",
228 path: Path.absname("test/fixtures/image.jpg"),
229 filename: "an_image.jpg"
234 {:ok, %Object{} = attachment} =
235 Pleroma.Web.ActivityPub.ActivityPub.upload(file, actor: user.ap_id)
237 %{data: %{"attachment" => [%{"url" => [%{"href" => href}]}]}} =
238 note = insert(:note, %{user: user, data: %{"attachment" => [attachment.data]}})
240 uploads_dir = Pleroma.Config.get!([Pleroma.Uploaders.Local, :uploads])
242 path = href |> Path.dirname() |> Path.basename()
244 assert {:ok, ["an_image.jpg"]} == File.ls("#{uploads_dir}/#{path}")
248 ObanHelpers.perform(all_enqueued(worker: Pleroma.Workers.AttachmentsCleanupWorker))
250 assert Object.get_by_id(note.id).data["deleted"]
251 assert Object.get_by_id(attachment.id) == nil
253 assert {:ok, []} == File.ls("#{uploads_dir}/#{path}")
257 describe "normalizer" do
258 test "fetches unknown objects by default" do
260 object = Object.normalize("http://mastodon.example.org/@admin/99541947525187367")
262 assert object.data["url"] == "http://mastodon.example.org/@admin/99541947525187367"
265 test "fetches unknown objects when fetch_remote is explicitly true" do
267 object = Object.normalize("http://mastodon.example.org/@admin/99541947525187367", true)
269 assert object.data["url"] == "http://mastodon.example.org/@admin/99541947525187367"
272 test "does not fetch unknown objects when fetch_remote is false" do
274 Object.normalize("http://mastodon.example.org/@admin/99541947525187367", false)
279 describe "get_by_id_and_maybe_refetch" do
282 %{method: :get, url: "https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d"} ->
283 %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/poll_original.json")}
286 apply(HttpRequestMock, :request, [env])
289 mock_modified = fn resp ->
291 %{method: :get, url: "https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d"} ->
295 apply(HttpRequestMock, :request, [env])
299 on_exit(fn -> mock(fn env -> apply(HttpRequestMock, :request, [env]) end) end)
301 [mock_modified: mock_modified]
304 test "refetches if the time since the last refetch is greater than the interval", %{
305 mock_modified: mock_modified
308 object = Object.normalize("https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d")
310 Object.set_cache(object)
312 assert Enum.at(object.data["oneOf"], 0)["replies"]["totalItems"] == 4
313 assert Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 0
315 mock_modified.(%Tesla.Env{
317 body: File.read!("test/fixtures/tesla_mock/poll_modified.json")
320 updated_object = Object.get_by_id_and_maybe_refetch(object.id, interval: -1)
321 object_in_cache = Object.get_cached_by_ap_id(object.data["id"])
322 assert updated_object == object_in_cache
323 assert Enum.at(updated_object.data["oneOf"], 0)["replies"]["totalItems"] == 8
324 assert Enum.at(updated_object.data["oneOf"], 1)["replies"]["totalItems"] == 3
327 test "returns the old object if refetch fails", %{mock_modified: mock_modified} do
329 object = Object.normalize("https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d")
331 Object.set_cache(object)
333 assert Enum.at(object.data["oneOf"], 0)["replies"]["totalItems"] == 4
334 assert Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 0
336 assert capture_log(fn ->
337 mock_modified.(%Tesla.Env{status: 404, body: ""})
339 updated_object = Object.get_by_id_and_maybe_refetch(object.id, interval: -1)
340 object_in_cache = Object.get_cached_by_ap_id(object.data["id"])
341 assert updated_object == object_in_cache
342 assert Enum.at(updated_object.data["oneOf"], 0)["replies"]["totalItems"] == 4
343 assert Enum.at(updated_object.data["oneOf"], 1)["replies"]["totalItems"] == 0
345 "[error] Couldn't refresh https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d"
348 test "does not refetch if the time since the last refetch is greater than the interval", %{
349 mock_modified: mock_modified
352 object = Object.normalize("https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d")
354 Object.set_cache(object)
356 assert Enum.at(object.data["oneOf"], 0)["replies"]["totalItems"] == 4
357 assert Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 0
359 mock_modified.(%Tesla.Env{
361 body: File.read!("test/fixtures/tesla_mock/poll_modified.json")
364 updated_object = Object.get_by_id_and_maybe_refetch(object.id, interval: 100)
365 object_in_cache = Object.get_cached_by_ap_id(object.data["id"])
366 assert updated_object == object_in_cache
367 assert Enum.at(updated_object.data["oneOf"], 0)["replies"]["totalItems"] == 4
368 assert Enum.at(updated_object.data["oneOf"], 1)["replies"]["totalItems"] == 0
371 test "preserves internal fields on refetch", %{mock_modified: mock_modified} do
373 object = Object.normalize("https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d")
375 Object.set_cache(object)
377 assert Enum.at(object.data["oneOf"], 0)["replies"]["totalItems"] == 4
378 assert Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 0
381 activity = Activity.get_create_by_object_ap_id(object.data["id"])
382 {:ok, _activity, object} = CommonAPI.favorite(activity.id, user)
384 assert object.data["like_count"] == 1
386 mock_modified.(%Tesla.Env{
388 body: File.read!("test/fixtures/tesla_mock/poll_modified.json")
391 updated_object = Object.get_by_id_and_maybe_refetch(object.id, interval: -1)
392 object_in_cache = Object.get_cached_by_ap_id(object.data["id"])
393 assert updated_object == object_in_cache
394 assert Enum.at(updated_object.data["oneOf"], 0)["replies"]["totalItems"] == 8
395 assert Enum.at(updated_object.data["oneOf"], 1)["replies"]["totalItems"] == 3
397 assert updated_object.data["like_count"] == 1