77c75b560aa037019ac9ca12aa337af6a594bf8f
[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   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              %{"name" => "☕", "count" => 1, "me" => true}
35            ]
36
37     # Reacting with a non-emoji
38     assert conn
39            |> assign(:user, other_user)
40            |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
41            |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/x")
42            |> json_response_and_validate_schema(400)
43   end
44
45   test "DELETE /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
46     user = insert(:user)
47     other_user = insert(:user)
48
49     {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
50     {:ok, _reaction_activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
51
52     ObanHelpers.perform_all()
53
54     result =
55       conn
56       |> assign(:user, other_user)
57       |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
58       |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")
59
60     assert %{"id" => id} = json_response_and_validate_schema(result, 200)
61     assert to_string(activity.id) == id
62
63     ObanHelpers.perform_all()
64
65     object = Object.get_by_ap_id(activity.data["object"])
66
67     assert object.data["reaction_count"] == 0
68   end
69
70   test "GET /api/v1/pleroma/statuses/:id/reactions", %{conn: conn} do
71     user = insert(:user)
72     other_user = insert(:user)
73     doomed_user = insert(:user)
74
75     {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
76
77     result =
78       conn
79       |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
80       |> json_response_and_validate_schema(200)
81
82     assert result == []
83
84     {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
85     {:ok, _} = CommonAPI.react_with_emoji(activity.id, doomed_user, "🎅")
86
87     User.perform(:delete, doomed_user)
88
89     result =
90       conn
91       |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
92       |> json_response_and_validate_schema(200)
93
94     [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] = result
95
96     assert represented_user["id"] == other_user.id
97
98     result =
99       conn
100       |> assign(:user, other_user)
101       |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:statuses"]))
102       |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
103       |> json_response_and_validate_schema(200)
104
105     assert [%{"name" => "🎅", "count" => 1, "accounts" => [_represented_user], "me" => true}] =
106              result
107   end
108
109   test "GET /api/v1/pleroma/statuses/:id/reactions?with_muted=true", %{conn: conn} do
110     user = insert(:user)
111     user2 = insert(:user)
112     user3 = insert(:user)
113
114     token = insert(:oauth_token, user: user, scopes: ["read:statuses"])
115
116     {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
117
118     {:ok, _} = CommonAPI.react_with_emoji(activity.id, user2, "🎅")
119     {:ok, _} = CommonAPI.react_with_emoji(activity.id, user3, "🎅")
120
121     result =
122       conn
123       |> assign(:user, user)
124       |> assign(:token, token)
125       |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
126       |> json_response_and_validate_schema(200)
127
128     assert [%{"name" => "🎅", "count" => 2}] = result
129
130     User.mute(user, user3)
131
132     result =
133       conn
134       |> assign(:user, user)
135       |> assign(:token, token)
136       |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
137       |> json_response_and_validate_schema(200)
138
139     assert [%{"name" => "🎅", "count" => 1}] = result
140
141     result =
142       conn
143       |> assign(:user, user)
144       |> assign(:token, token)
145       |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions?with_muted=true")
146       |> json_response_and_validate_schema(200)
147
148     assert [%{"name" => "🎅", "count" => 2}] = result
149   end
150
151   test "GET /api/v1/pleroma/statuses/:id/reactions with :show_reactions disabled", %{conn: conn} do
152     clear_config([:instance, :show_reactions], false)
153
154     user = insert(:user)
155     other_user = insert(:user)
156
157     {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
158     {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
159
160     result =
161       conn
162       |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
163       |> json_response_and_validate_schema(200)
164
165     assert result == []
166   end
167
168   test "GET /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
169     user = insert(:user)
170     other_user = insert(:user)
171
172     {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
173
174     result =
175       conn
176       |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
177       |> json_response_and_validate_schema(200)
178
179     assert result == []
180
181     {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
182     {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
183
184     assert [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] =
185              conn
186              |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
187              |> json_response_and_validate_schema(200)
188
189     assert represented_user["id"] == other_user.id
190   end
191 end