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.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"])
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"])
54 Cachex.del(:object_cache, "object:#{object.data["id"]}")
56 deleting_user = insert(:user)
59 File.read!("test/fixtures/mastodon-delete.json")
61 |> Map.put("actor", deleting_user.ap_id)
62 |> put_in(["object", "id"], activity.data["object"])
64 {:ok, %Activity{actor: actor, local: false, data: %{"id" => id}}} =
65 Transmogrifier.handle_incoming(data)
67 assert id == data["id"]
69 # We delete the Create activity because we base our timelines on it.
70 # This should be changed after we unify objects and activities
71 refute Activity.get_by_id(activity.id)
72 assert actor == deleting_user.ap_id
75 test "it fails for incoming deletes with spoofed origin" do
76 activity = insert(:note_activity)
77 %{ap_id: ap_id} = insert(:user, ap_id: "https://gensokyo.2hu/users/raymoo")
80 File.read!("test/fixtures/mastodon-delete.json")
82 |> Map.put("actor", ap_id)
83 |> put_in(["object", "id"], activity.data["object"])
85 assert match?({:error, _}, Transmogrifier.handle_incoming(data))
88 @tag capture_log: true
89 test "it works for incoming user deletes" do
90 %{ap_id: ap_id} = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
93 File.read!("test/fixtures/mastodon-delete-user.json")
96 {:ok, _} = Transmogrifier.handle_incoming(data)
97 ObanHelpers.perform_all()
99 assert User.get_cached_by_ap_id(ap_id).deactivated
102 test "it fails for incoming user deletes with spoofed origin" do
103 %{ap_id: ap_id} = insert(:user)
106 File.read!("test/fixtures/mastodon-delete-user.json")
108 |> Map.put("actor", ap_id)
110 assert match?({:error, _}, Transmogrifier.handle_incoming(data))
112 assert User.get_cached_by_ap_id(ap_id)