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.UndoHandlingTest do
6 use Pleroma.DataCase, async: true
11 alias Pleroma.Web.ActivityPub.Transmogrifier
12 alias Pleroma.Web.CommonAPI
14 import Pleroma.Factory
16 test "it works for incoming emoji reaction undos" do
19 {:ok, activity} = CommonAPI.post(user, %{status: "hello"})
20 {:ok, reaction_activity} = CommonAPI.react_with_emoji(activity.id, user, "👌")
23 File.read!("test/fixtures/mastodon-undo-like.json")
25 |> Map.put("object", reaction_activity.data["id"])
26 |> Map.put("actor", user.ap_id)
28 {:ok, activity} = Transmogrifier.handle_incoming(data)
30 assert activity.actor == user.ap_id
31 assert activity.data["id"] == data["id"]
32 assert activity.data["type"] == "Undo"
35 test "it returns an error for incoming unlikes wihout a like activity" do
37 {:ok, activity} = CommonAPI.post(user, %{status: "leave a like pls"})
40 File.read!("test/fixtures/mastodon-undo-like.json")
42 |> Map.put("object", activity.data["object"])
44 assert Transmogrifier.handle_incoming(data) == :error
47 test "it works for incoming unlikes with an existing like activity" do
49 {:ok, activity} = CommonAPI.post(user, %{status: "leave a like pls"})
52 File.read!("test/fixtures/mastodon-like.json")
54 |> Map.put("object", activity.data["object"])
56 _liker = insert(:user, ap_id: like_data["actor"], local: false)
58 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
61 File.read!("test/fixtures/mastodon-undo-like.json")
63 |> Map.put("object", like_data)
64 |> Map.put("actor", like_data["actor"])
66 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
68 assert data["actor"] == "http://mastodon.example.org/users/admin"
69 assert data["type"] == "Undo"
70 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
71 assert data["object"] == "http://mastodon.example.org/users/admin#likes/2"
73 note = Object.get_by_ap_id(like_data["object"])
74 assert note.data["like_count"] == 0
75 assert note.data["likes"] == []
78 test "it works for incoming unlikes with an existing like activity and a compact object" do
80 {:ok, activity} = CommonAPI.post(user, %{status: "leave a like pls"})
83 File.read!("test/fixtures/mastodon-like.json")
85 |> Map.put("object", activity.data["object"])
87 _liker = insert(:user, ap_id: like_data["actor"], local: false)
89 {:ok, %Activity{data: like_data, local: false}} = Transmogrifier.handle_incoming(like_data)
92 File.read!("test/fixtures/mastodon-undo-like.json")
94 |> Map.put("object", like_data["id"])
95 |> Map.put("actor", like_data["actor"])
97 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
99 assert data["actor"] == "http://mastodon.example.org/users/admin"
100 assert data["type"] == "Undo"
101 assert data["id"] == "http://mastodon.example.org/users/admin#likes/2/undo"
102 assert data["object"] == "http://mastodon.example.org/users/admin#likes/2"
105 test "it works for incoming unannounces with an existing notice" do
107 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
110 File.read!("test/fixtures/mastodon-announce.json")
112 |> Map.put("object", activity.data["object"])
114 _announcer = insert(:user, ap_id: announce_data["actor"], local: false)
116 {:ok, %Activity{data: announce_data, local: false}} =
117 Transmogrifier.handle_incoming(announce_data)
120 File.read!("test/fixtures/mastodon-undo-announce.json")
122 |> Map.put("object", announce_data)
123 |> Map.put("actor", announce_data["actor"])
125 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
127 assert data["type"] == "Undo"
129 assert data["object"] ==
130 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
133 test "it works for incoming unfollows with an existing follow" do
137 File.read!("test/fixtures/mastodon-follow-activity.json")
139 |> Map.put("object", user.ap_id)
141 _follower = insert(:user, ap_id: follow_data["actor"], local: false)
143 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(follow_data)
146 File.read!("test/fixtures/mastodon-unfollow-activity.json")
148 |> Map.put("object", follow_data)
150 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
152 assert data["type"] == "Undo"
153 assert data["object"]["type"] == "Follow"
154 assert data["object"]["object"] == user.ap_id
155 assert data["actor"] == "http://mastodon.example.org/users/admin"
157 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
160 test "it works for incoming unblocks with an existing block" do
164 File.read!("test/fixtures/mastodon-block-activity.json")
166 |> Map.put("object", user.ap_id)
168 _blocker = insert(:user, ap_id: block_data["actor"], local: false)
170 {:ok, %Activity{data: _, local: false}} = Transmogrifier.handle_incoming(block_data)
173 File.read!("test/fixtures/mastodon-unblock-activity.json")
175 |> Map.put("object", block_data)
177 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
178 assert data["type"] == "Undo"
179 assert data["object"] == block_data["id"]
181 blocker = User.get_cached_by_ap_id(data["actor"])
183 refute User.blocks?(blocker, user)