4898179e64e6d38cd06059a054af1489b3786c54
[akkoma] / test / pleroma / web / pleroma_api / controllers / emoji_reaction_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.PleromaAPI.EmojiReactionControllerTest do
6 use Oban.Testing, repo: Pleroma.Repo
7 use Pleroma.Web.ConnCase
8
9 alias Pleroma.Object
10 alias Pleroma.Tests.ObanHelpers
11 alias Pleroma.User
12 alias Pleroma.Web.CommonAPI
13
14 import Pleroma.Factory
15
16 test "PUT /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
17 user = insert(:user)
18 other_user = insert(:user)
19
20 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
21
22 result =
23 conn
24 |> assign(:user, other_user)
25 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
26 |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")
27 |> json_response_and_validate_schema(200)
28
29 # We return the status, but this our implementation detail.
30 assert %{"id" => id} = result
31 assert to_string(activity.id) == id
32
33 assert result["pleroma"]["emoji_reactions"] == [
34 %{
35 "name" => "☕",
36 "count" => 1,
37 "me" => true,
38 "url" => nil,
39 "account_ids" => [other_user.id]
40 }
41 ]
42
43 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
44
45 ObanHelpers.perform_all()
46 # Reacting with a custom emoji
47 result =
48 conn
49 |> assign(:user, other_user)
50 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
51 |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/:dinosaur:")
52 |> json_response_and_validate_schema(200)
53
54 # We return the status, but this our implementation detail.
55 assert %{"id" => id} = result
56 assert to_string(activity.id) == id
57
58 assert result["pleroma"]["emoji_reactions"] == [
59 %{
60 "name" => "dinosaur",
61 "count" => 1,
62 "me" => true,
63 "url" => "http://localhost:4001/emoji/dino walking.gif",
64 "account_ids" => [other_user.id]
65 }
66 ]
67
68 # Reacting with a non-emoji
69 assert conn
70 |> assign(:user, other_user)
71 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
72 |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/x")
73 |> json_response_and_validate_schema(400)
74 end
75
76 test "DELETE /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
77 user = insert(:user)
78 other_user = insert(:user)
79
80 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
81 {:ok, _reaction_activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
82 {:ok, _reaction_activity} = CommonAPI.react_with_emoji(activity.id, other_user, ":dinosaur:")
83
84 ObanHelpers.perform_all()
85
86 result =
87 conn
88 |> assign(:user, other_user)
89 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
90 |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")
91
92 assert %{"id" => id} = json_response_and_validate_schema(result, 200)
93 assert to_string(activity.id) == id
94
95 # Remove custom emoji
96
97 result =
98 conn
99 |> assign(:user, other_user)
100 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
101 |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/:dinosaur:")
102
103 assert %{"id" => id} = json_response_and_validate_schema(result, 200)
104 assert to_string(activity.id) == id
105
106 ObanHelpers.perform_all()
107
108 object = Object.get_by_ap_id(activity.data["object"])
109
110 assert object.data["reaction_count"] == 0
111 end
112
113 test "GET /api/v1/pleroma/statuses/:id/reactions", %{conn: conn} do
114 user = insert(:user)
115 other_user = insert(:user)
116 doomed_user = insert(:user)
117
118 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
119
120 result =
121 conn
122 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
123 |> json_response_and_validate_schema(200)
124
125 assert result == []
126
127 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
128 {:ok, _} = CommonAPI.react_with_emoji(activity.id, doomed_user, "🎅")
129
130 User.perform(:delete, doomed_user)
131
132 result =
133 conn
134 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
135 |> json_response_and_validate_schema(200)
136
137 [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] = result
138
139 assert represented_user["id"] == other_user.id
140
141 result =
142 conn
143 |> assign(:user, other_user)
144 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:statuses"]))
145 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
146 |> json_response_and_validate_schema(200)
147
148 assert [%{"name" => "🎅", "count" => 1, "accounts" => [_represented_user], "me" => true}] =
149 result
150 end
151
152 test "GET /api/v1/pleroma/statuses/:id/reactions?with_muted=true", %{conn: conn} do
153 user = insert(:user)
154 user2 = insert(:user)
155 user3 = insert(:user)
156
157 token = insert(:oauth_token, user: user, scopes: ["read:statuses"])
158
159 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
160
161 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user2, "🎅")
162 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user3, "🎅")
163
164 result =
165 conn
166 |> assign(:user, user)
167 |> assign(:token, token)
168 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
169 |> json_response_and_validate_schema(200)
170
171 assert [%{"name" => "🎅", "count" => 2}] = result
172
173 User.mute(user, user3)
174
175 result =
176 conn
177 |> assign(:user, user)
178 |> assign(:token, token)
179 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
180 |> json_response_and_validate_schema(200)
181
182 assert [%{"name" => "🎅", "count" => 1}] = result
183
184 result =
185 conn
186 |> assign(:user, user)
187 |> assign(:token, token)
188 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions?with_muted=true")
189 |> json_response_and_validate_schema(200)
190
191 assert [%{"name" => "🎅", "count" => 2}] = result
192 end
193
194 test "GET /api/v1/pleroma/statuses/:id/reactions with :show_reactions disabled", %{conn: conn} do
195 clear_config([:instance, :show_reactions], false)
196
197 user = insert(:user)
198 other_user = insert(:user)
199
200 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
201 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
202
203 result =
204 conn
205 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
206 |> json_response_and_validate_schema(200)
207
208 assert result == []
209 end
210
211 test "GET /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
212 user = insert(:user)
213 other_user = insert(:user)
214
215 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
216
217 result =
218 conn
219 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
220 |> json_response_and_validate_schema(200)
221
222 assert result == []
223
224 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
225 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
226
227 assert [
228 %{
229 "name" => "🎅",
230 "count" => 1,
231 "accounts" => [represented_user],
232 "me" => false,
233 "url" => nil
234 }
235 ] =
236 conn
237 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
238 |> json_response_and_validate_schema(200)
239
240 assert represented_user["id"] == other_user.id
241 end
242 end