c9a53918c38bd1747cdc4122cdf3ee025b35f7cf
[akkoma] / test / pleroma / web / activity_pub / transmogrifier / delete_handling_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.Web.ActivityPub.Transmogrifier.DeleteHandlingTest do
6 use Oban.Testing, repo: Pleroma.Repo
7 use Pleroma.DataCase
8
9 alias Pleroma.Activity
10 alias Pleroma.Object
11 alias Pleroma.Tests.ObanHelpers
12 alias Pleroma.User
13 alias Pleroma.Web.ActivityPub.Transmogrifier
14
15 import Pleroma.Factory
16
17 setup_all do
18 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
19 :ok
20 end
21
22 test "it works for incoming deletes" do
23 activity = insert(:note_activity)
24 deleting_user = insert(:user)
25
26 data =
27 File.read!("test/fixtures/mastodon-delete.json")
28 |> Poison.decode!()
29 |> Map.put("actor", deleting_user.ap_id)
30 |> put_in(["object", "id"], activity.data["object"])
31
32 {:ok, %Activity{actor: actor, local: false, data: %{"id" => id}}} =
33 Transmogrifier.handle_incoming(data)
34
35 assert id == data["id"]
36
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
41
42 # Objects are replaced by a tombstone object.
43 object = Object.normalize(activity.data["object"])
44 assert object.data["type"] == "Tombstone"
45 end
46
47 test "it works for incoming when the object has been pruned" do
48 activity = insert(:note_activity)
49
50 {:ok, object} =
51 Object.normalize(activity.data["object"])
52 |> Repo.delete()
53
54 Cachex.del(:object_cache, "object:#{object.data["id"]}")
55
56 deleting_user = insert(:user)
57
58 data =
59 File.read!("test/fixtures/mastodon-delete.json")
60 |> Poison.decode!()
61 |> Map.put("actor", deleting_user.ap_id)
62 |> put_in(["object", "id"], activity.data["object"])
63
64 {:ok, %Activity{actor: actor, local: false, data: %{"id" => id}}} =
65 Transmogrifier.handle_incoming(data)
66
67 assert id == data["id"]
68
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
73 end
74
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")
78
79 data =
80 File.read!("test/fixtures/mastodon-delete.json")
81 |> Poison.decode!()
82 |> Map.put("actor", ap_id)
83 |> put_in(["object", "id"], activity.data["object"])
84
85 assert match?({:error, _}, Transmogrifier.handle_incoming(data))
86 end
87
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")
91
92 data =
93 File.read!("test/fixtures/mastodon-delete-user.json")
94 |> Poison.decode!()
95
96 {:ok, _} = Transmogrifier.handle_incoming(data)
97 ObanHelpers.perform_all()
98
99 assert User.get_cached_by_ap_id(ap_id).deactivated
100 end
101
102 test "it fails for incoming user deletes with spoofed origin" do
103 %{ap_id: ap_id} = insert(:user)
104
105 data =
106 File.read!("test/fixtures/mastodon-delete-user.json")
107 |> Poison.decode!()
108 |> Map.put("actor", ap_id)
109
110 assert match?({:error, _}, Transmogrifier.handle_incoming(data))
111
112 assert User.get_cached_by_ap_id(ap_id)
113 end
114 end