1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.Transmogrifier.DeleteHandlingTest do
6 use Oban.Testing, repo: Pleroma.Repo
11 alias Pleroma.Tests.ObanHelpers
13 alias Pleroma.Web.ActivityPub.Transmogrifier
15 import Pleroma.Factory
18 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
22 test "it works for incoming deletes" do
23 activity = insert(:note_activity)
24 deleting_user = insert(:user)
27 File.read!("test/fixtures/mastodon-delete.json")
29 |> Map.put("actor", deleting_user.ap_id)
30 |> put_in(["object", "id"], activity.data["object"])
32 {:ok, %Activity{actor: actor, local: false, data: %{"id" => id}}} =
33 Transmogrifier.handle_incoming(data)
35 assert id == data["id"]
37 # We delete the Create activity because we base our timelines on it.
38 # This should be changed after we unify objects and activities
39 refute Activity.get_by_id(activity.id)
40 assert actor == deleting_user.ap_id
42 # Objects are replaced by a tombstone object.
43 object = Object.normalize(activity.data["object"], fetch: false)
44 assert object.data["type"] == "Tombstone"
47 test "it works for incoming when the object has been pruned" do
48 activity = insert(:note_activity)
51 Object.normalize(activity.data["object"], fetch: false)
55 Cachex.del(:object_cache, "object:#{object.data["id"]}")
57 deleting_user = insert(:user)
60 File.read!("test/fixtures/mastodon-delete.json")
62 |> Map.put("actor", deleting_user.ap_id)
63 |> put_in(["object", "id"], activity.data["object"])
65 {:ok, %Activity{actor: actor, local: false, data: %{"id" => id}}} =
66 Transmogrifier.handle_incoming(data)
68 assert id == data["id"]
70 # We delete the Create activity because we base our timelines on it.
71 # This should be changed after we unify objects and activities
72 refute Activity.get_by_id(activity.id)
73 assert actor == deleting_user.ap_id
76 test "it fails for incoming deletes with spoofed origin" do
77 activity = insert(:note_activity)
78 %{ap_id: ap_id} = insert(:user, ap_id: "https://gensokyo.2hu/users/raymoo")
81 File.read!("test/fixtures/mastodon-delete.json")
83 |> Map.put("actor", ap_id)
84 |> put_in(["object", "id"], activity.data["object"])
86 assert match?({:error, _}, Transmogrifier.handle_incoming(data))
89 @tag capture_log: true
90 test "it works for incoming user deletes" do
91 %{ap_id: ap_id} = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
94 File.read!("test/fixtures/mastodon-delete-user.json")
97 {:ok, _} = Transmogrifier.handle_incoming(data)
98 ObanHelpers.perform_all()
100 refute User.get_cached_by_ap_id(ap_id).is_active
103 test "it fails for incoming user deletes with spoofed origin" do
104 %{ap_id: ap_id} = insert(:user)
107 File.read!("test/fixtures/mastodon-delete-user.json")
109 |> Map.put("actor", ap_id)
111 assert match?({:error, _}, Transmogrifier.handle_incoming(data))
113 assert User.get_cached_by_ap_id(ap_id)