65bb22e274b8824ac18afa87d1b52ea9b5315929
[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 %{"name" => "☕", "count" => 1, "me" => true, "url" => nil}
35 ]
36
37 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
38
39 ObanHelpers.perform_all()
40 # Reacting with a custom emoji
41 result =
42 conn
43 |> assign(:user, other_user)
44 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
45 |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/:dinosaur:")
46 |> json_response_and_validate_schema(200)
47
48 # We return the status, but this our implementation detail.
49 assert %{"id" => id} = result
50 assert to_string(activity.id) == id
51
52 assert result["pleroma"]["emoji_reactions"] == [
53 %{
54 "name" => "dinosaur",
55 "count" => 1,
56 "me" => true,
57 "url" => "http://localhost:4001/emoji/dino walking.gif"
58 }
59 ]
60
61 # Reacting with a non-emoji
62 assert conn
63 |> assign(:user, other_user)
64 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
65 |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/x")
66 |> json_response_and_validate_schema(400)
67 end
68
69 test "DELETE /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
70 user = insert(:user)
71 other_user = insert(:user)
72
73 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
74 {:ok, _reaction_activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
75 {:ok, _reaction_activity} = CommonAPI.react_with_emoji(activity.id, other_user, ":dinosaur:")
76
77 ObanHelpers.perform_all()
78
79 result =
80 conn
81 |> assign(:user, other_user)
82 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
83 |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")
84
85 assert %{"id" => id} = json_response_and_validate_schema(result, 200)
86 assert to_string(activity.id) == id
87
88 # Remove custom emoji
89
90 result =
91 conn
92 |> assign(:user, other_user)
93 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
94 |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/:dinosaur:")
95
96 assert %{"id" => id} = json_response_and_validate_schema(result, 200)
97 assert to_string(activity.id) == id
98
99 ObanHelpers.perform_all()
100
101 object = Object.get_by_ap_id(activity.data["object"])
102
103 assert object.data["reaction_count"] == 0
104 end
105
106 test "GET /api/v1/pleroma/statuses/:id/reactions", %{conn: conn} do
107 user = insert(:user)
108 other_user = insert(:user)
109 doomed_user = insert(:user)
110
111 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
112
113 result =
114 conn
115 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
116 |> json_response_and_validate_schema(200)
117
118 assert result == []
119
120 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
121 {:ok, _} = CommonAPI.react_with_emoji(activity.id, doomed_user, "🎅")
122
123 User.perform(:delete, doomed_user)
124
125 result =
126 conn
127 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
128 |> json_response_and_validate_schema(200)
129
130 [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] = result
131
132 assert represented_user["id"] == other_user.id
133
134 result =
135 conn
136 |> assign(:user, other_user)
137 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:statuses"]))
138 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
139 |> json_response_and_validate_schema(200)
140
141 assert [%{"name" => "🎅", "count" => 1, "accounts" => [_represented_user], "me" => true}] =
142 result
143 end
144
145 test "GET /api/v1/pleroma/statuses/:id/reactions?with_muted=true", %{conn: conn} do
146 user = insert(:user)
147 user2 = insert(:user)
148 user3 = insert(:user)
149
150 token = insert(:oauth_token, user: user, scopes: ["read:statuses"])
151
152 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
153
154 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user2, "🎅")
155 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user3, "🎅")
156
157 result =
158 conn
159 |> assign(:user, user)
160 |> assign(:token, token)
161 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
162 |> json_response_and_validate_schema(200)
163
164 assert [%{"name" => "🎅", "count" => 2}] = result
165
166 User.mute(user, user3)
167
168 result =
169 conn
170 |> assign(:user, user)
171 |> assign(:token, token)
172 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
173 |> json_response_and_validate_schema(200)
174
175 assert [%{"name" => "🎅", "count" => 1}] = result
176
177 result =
178 conn
179 |> assign(:user, user)
180 |> assign(:token, token)
181 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions?with_muted=true")
182 |> json_response_and_validate_schema(200)
183
184 assert [%{"name" => "🎅", "count" => 2}] = result
185 end
186
187 test "GET /api/v1/pleroma/statuses/:id/reactions with :show_reactions disabled", %{conn: conn} do
188 clear_config([:instance, :show_reactions], false)
189
190 user = insert(:user)
191 other_user = insert(:user)
192
193 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
194 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
195
196 result =
197 conn
198 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
199 |> json_response_and_validate_schema(200)
200
201 assert result == []
202 end
203
204 test "GET /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
205 user = insert(:user)
206 other_user = insert(:user)
207
208 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
209
210 result =
211 conn
212 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
213 |> json_response_and_validate_schema(200)
214
215 assert result == []
216
217 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
218 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
219
220 assert [
221 %{
222 "name" => "🎅",
223 "count" => 1,
224 "accounts" => [represented_user],
225 "me" => false,
226 "url" => nil
227 }
228 ] =
229 conn
230 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
231 |> json_response_and_validate_schema(200)
232
233 assert represented_user["id"] == other_user.id
234 end
235 end