total rebase
[anni] / test / pleroma / web / pleroma_api / controllers / emoji_reaction_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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   setup do
17     Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Config)
18     :ok
19   end
20
21   test "PUT /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
22     user = insert(:user)
23     other_user = insert(:user)
24
25     note = insert(:note, user: user, data: %{"reactions" => [["👍", [other_user.ap_id], nil]]})
26     activity = insert(:note_activity, note: note, user: user)
27
28     result =
29       conn
30       |> assign(:user, other_user)
31       |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
32       |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/\u26A0")
33       |> json_response_and_validate_schema(200)
34
35     assert %{"id" => id} = result
36     assert to_string(activity.id) == id
37
38     assert result["pleroma"]["emoji_reactions"] == [
39              %{
40                "name" => "👍",
41                "count" => 1,
42                "me" => true,
43                "url" => nil,
44                "account_ids" => [other_user.id]
45              },
46              %{
47                "name" => "\u26A0\uFE0F",
48                "count" => 1,
49                "me" => true,
50                "url" => nil,
51                "account_ids" => [other_user.id]
52              }
53            ]
54
55     {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
56
57     ObanHelpers.perform_all()
58
59     # Reacting with a custom emoji
60     result =
61       conn
62       |> assign(:user, other_user)
63       |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
64       |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/:dinosaur:")
65       |> json_response_and_validate_schema(200)
66
67     assert %{"id" => id} = result
68     assert to_string(activity.id) == id
69
70     assert result["pleroma"]["emoji_reactions"] == [
71              %{
72                "name" => "dinosaur",
73                "count" => 1,
74                "me" => true,
75                "url" => "http://localhost:4001/emoji/dino walking.gif",
76                "account_ids" => [other_user.id]
77              }
78            ]
79
80     # Reacting with a remote emoji
81     note =
82       insert(:note,
83         user: user,
84         data: %{
85           "reactions" => [
86             ["👍", [other_user.ap_id], nil],
87             ["wow", [other_user.ap_id], "https://remote/emoji/wow"]
88           ]
89         }
90       )
91
92     activity = insert(:note_activity, note: note, user: user)
93
94     result =
95       conn
96       |> assign(:user, user)
97       |> assign(:token, insert(:oauth_token, user: user, scopes: ["write:statuses"]))
98       |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/:wow@remote:")
99       |> json_response(200)
100
101     assert result["pleroma"]["emoji_reactions"] == [
102              %{
103                "account_ids" => [other_user.id],
104                "count" => 1,
105                "me" => false,
106                "name" => "👍",
107                "url" => nil
108              },
109              %{
110                "name" => "wow@remote",
111                "count" => 2,
112                "me" => true,
113                "url" => "https://remote/emoji/wow",
114                "account_ids" => [user.id, other_user.id]
115              }
116            ]
117
118     # Reacting with a remote custom emoji that hasn't been reacted with yet
119     note =
120       insert(:note,
121         user: user
122       )
123
124     activity = insert(:note_activity, note: note, user: user)
125
126     assert conn
127            |> assign(:user, user)
128            |> assign(:token, insert(:oauth_token, user: user, scopes: ["write:statuses"]))
129            |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/:wow@remote:")
130            |> json_response(400)
131
132     # Reacting with a non-emoji
133     assert conn
134            |> assign(:user, other_user)
135            |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
136            |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/x")
137            |> json_response_and_validate_schema(400)
138   end
139
140   test "DELETE /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
141     user = insert(:user)
142     other_user = insert(:user)
143
144     note =
145       insert(:note,
146         user: user,
147         data: %{"reactions" => [["wow", [user.ap_id], "https://remote/emoji/wow"]]}
148       )
149
150     activity = insert(:note_activity, note: note, user: user)
151
152     ObanHelpers.perform_all()
153
154     {:ok, _reaction_activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
155     {:ok, _reaction_activity} = CommonAPI.react_with_emoji(activity.id, other_user, ":dinosaur:")
156
157     {:ok, _reaction_activity} =
158       CommonAPI.react_with_emoji(activity.id, other_user, ":wow@remote:")
159
160     ObanHelpers.perform_all()
161
162     result =
163       conn
164       |> assign(:user, other_user)
165       |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
166       |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")
167
168     assert %{"id" => id} = json_response_and_validate_schema(result, 200)
169     assert to_string(activity.id) == id
170
171     # Remove custom emoji
172
173     result =
174       conn
175       |> assign(:user, other_user)
176       |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
177       |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/:dinosaur:")
178
179     assert %{"id" => id} = json_response_and_validate_schema(result, 200)
180     assert to_string(activity.id) == id
181
182     ObanHelpers.perform_all()
183
184     object = Object.get_by_ap_id(activity.data["object"])
185
186     assert object.data["reaction_count"] == 2
187
188     # Remove custom remote emoji
189     result =
190       conn
191       |> assign(:user, other_user)
192       |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
193       |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/:wow@remote:")
194       |> json_response(200)
195
196     assert result["pleroma"]["emoji_reactions"] == [
197              %{
198                "name" => "wow@remote",
199                "count" => 1,
200                "me" => false,
201                "url" => "https://remote/emoji/wow",
202                "account_ids" => [user.id]
203              }
204            ]
205
206     # Remove custom remote emoji that hasn't been reacted with yet
207     assert conn
208            |> assign(:user, other_user)
209            |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
210            |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/:zoop@remote:")
211            |> json_response(400)
212   end
213
214   test "GET /api/v1/pleroma/statuses/:id/reactions", %{conn: conn} do
215     user = insert(:user)
216     other_user = insert(:user)
217     doomed_user = insert(:user)
218
219     {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
220
221     result =
222       conn
223       |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
224       |> json_response_and_validate_schema(200)
225
226     assert result == []
227
228     {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
229     {:ok, _} = CommonAPI.react_with_emoji(activity.id, doomed_user, "🎅")
230
231     User.perform(:delete, doomed_user)
232
233     result =
234       conn
235       |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
236       |> json_response_and_validate_schema(200)
237
238     [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] = result
239
240     assert represented_user["id"] == other_user.id
241
242     result =
243       conn
244       |> assign(:user, other_user)
245       |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:statuses"]))
246       |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
247       |> json_response_and_validate_schema(200)
248
249     assert [%{"name" => "🎅", "count" => 1, "accounts" => [_represented_user], "me" => true}] =
250              result
251   end
252
253   test "GET /api/v1/pleroma/statuses/:id/reactions with legacy format", %{conn: conn} do
254     user = insert(:user)
255     other_user = insert(:user)
256
257     note =
258       insert(:note,
259         user: user,
260         data: %{
261           "reactions" => [["😿", [other_user.ap_id]]]
262         }
263       )
264
265     activity = insert(:note_activity, user: user, note: note)
266
267     result =
268       conn
269       |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
270       |> json_response_and_validate_schema(200)
271
272     other_user_id = other_user.id
273
274     assert [
275              %{
276                "name" => "😿",
277                "count" => 1,
278                "me" => false,
279                "url" => nil,
280                "accounts" => [%{"id" => ^other_user_id}]
281              }
282            ] = result
283   end
284
285   test "GET /api/v1/pleroma/statuses/:id/reactions?with_muted=true", %{conn: conn} do
286     user = insert(:user)
287     user2 = insert(:user)
288     user3 = insert(:user)
289
290     token = insert(:oauth_token, user: user, scopes: ["read:statuses"])
291
292     {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
293
294     {:ok, _} = CommonAPI.react_with_emoji(activity.id, user2, "🎅")
295     {:ok, _} = CommonAPI.react_with_emoji(activity.id, user3, "🎅")
296
297     result =
298       conn
299       |> assign(:user, user)
300       |> assign(:token, token)
301       |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
302       |> json_response_and_validate_schema(200)
303
304     assert [%{"name" => "🎅", "count" => 2}] = result
305
306     User.mute(user, user3)
307
308     result =
309       conn
310       |> assign(:user, user)
311       |> assign(:token, token)
312       |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
313       |> json_response_and_validate_schema(200)
314
315     assert [%{"name" => "🎅", "count" => 1}] = result
316
317     result =
318       conn
319       |> assign(:user, user)
320       |> assign(:token, token)
321       |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions?with_muted=true")
322       |> json_response_and_validate_schema(200)
323
324     assert [%{"name" => "🎅", "count" => 2}] = result
325   end
326
327   test "GET /api/v1/pleroma/statuses/:id/reactions with :show_reactions disabled", %{conn: conn} do
328     clear_config([:instance, :show_reactions], false)
329
330     user = insert(:user)
331     other_user = insert(:user)
332
333     {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
334     {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
335
336     result =
337       conn
338       |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
339       |> json_response_and_validate_schema(200)
340
341     assert result == []
342   end
343
344   test "GET /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
345     user = insert(:user)
346     other_user = insert(:user)
347
348     {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
349
350     result =
351       conn
352       |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
353       |> json_response_and_validate_schema(200)
354
355     assert result == []
356
357     {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
358     {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
359
360     assert [
361              %{
362                "name" => "🎅",
363                "count" => 1,
364                "accounts" => [represented_user],
365                "me" => false,
366                "url" => nil
367              }
368            ] =
369              conn
370              |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
371              |> json_response_and_validate_schema(200)
372
373     assert represented_user["id"] == other_user.id
374   end
375 end