[#2332] Misc. improvements per code change requests.
[akkoma] / test / web / pleroma_api / controllers / pleroma_api_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.PleromaAPI.PleromaAPIControllerTest do
6 use Pleroma.Web.ConnCase
7
8 alias Pleroma.Conversation.Participation
9 alias Pleroma.Notification
10 alias Pleroma.Object
11 alias Pleroma.Repo
12 alias Pleroma.User
13 alias Pleroma.Web.CommonAPI
14
15 import Pleroma.Factory
16
17 test "PUT /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
18 user = insert(:user)
19 other_user = insert(:user)
20
21 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
22
23 result =
24 conn
25 |> assign(:user, other_user)
26 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
27 |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")
28 |> json_response(200)
29
30 # We return the status, but this our implementation detail.
31 assert %{"id" => id} = result
32 assert to_string(activity.id) == id
33
34 assert result["pleroma"]["emoji_reactions"] == [
35 %{"name" => "☕", "count" => 1, "me" => true}
36 ]
37 end
38
39 test "DELETE /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
40 user = insert(:user)
41 other_user = insert(:user)
42
43 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
44 {:ok, activity, _object} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
45
46 result =
47 conn
48 |> assign(:user, other_user)
49 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
50 |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")
51
52 assert %{"id" => id} = json_response(result, 200)
53 assert to_string(activity.id) == id
54
55 object = Object.normalize(activity)
56
57 assert object.data["reaction_count"] == 0
58 end
59
60 test "GET /api/v1/pleroma/statuses/:id/reactions", %{conn: conn} do
61 user = insert(:user)
62 other_user = insert(:user)
63 doomed_user = insert(:user)
64
65 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
66
67 result =
68 conn
69 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
70 |> json_response(200)
71
72 assert result == []
73
74 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
75 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, doomed_user, "🎅")
76
77 User.perform(:delete, doomed_user)
78
79 result =
80 conn
81 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
82 |> json_response(200)
83
84 [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] = result
85
86 assert represented_user["id"] == other_user.id
87
88 result =
89 conn
90 |> assign(:user, other_user)
91 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:statuses"]))
92 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
93 |> json_response(200)
94
95 assert [%{"name" => "🎅", "count" => 1, "accounts" => [_represented_user], "me" => true}] =
96 result
97 end
98
99 test "GET /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
100 user = insert(:user)
101 other_user = insert(:user)
102
103 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
104
105 result =
106 conn
107 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
108 |> json_response(200)
109
110 assert result == []
111
112 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
113 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
114
115 result =
116 conn
117 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
118 |> json_response(200)
119
120 [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] = result
121
122 assert represented_user["id"] == other_user.id
123 end
124
125 test "/api/v1/pleroma/conversations/:id" do
126 user = insert(:user)
127 %{user: other_user, conn: conn} = oauth_access(["read:statuses"])
128
129 {:ok, _activity} =
130 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
131
132 [participation] = Participation.for_user(other_user)
133
134 result =
135 conn
136 |> get("/api/v1/pleroma/conversations/#{participation.id}")
137 |> json_response(200)
138
139 assert result["id"] == participation.id |> to_string()
140 end
141
142 test "/api/v1/pleroma/conversations/:id/statuses" do
143 user = insert(:user)
144 %{user: other_user, conn: conn} = oauth_access(["read:statuses"])
145 third_user = insert(:user)
146
147 {:ok, _activity} =
148 CommonAPI.post(user, %{"status" => "Hi @#{third_user.nickname}!", "visibility" => "direct"})
149
150 {:ok, activity} =
151 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
152
153 [participation] = Participation.for_user(other_user)
154
155 {:ok, activity_two} =
156 CommonAPI.post(other_user, %{
157 "status" => "Hi!",
158 "in_reply_to_status_id" => activity.id,
159 "in_reply_to_conversation_id" => participation.id
160 })
161
162 result =
163 conn
164 |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses")
165 |> json_response(200)
166
167 assert length(result) == 2
168
169 id_one = activity.id
170 id_two = activity_two.id
171 assert [%{"id" => ^id_one}, %{"id" => ^id_two}] = result
172 end
173
174 test "PATCH /api/v1/pleroma/conversations/:id" do
175 %{user: user, conn: conn} = oauth_access(["write:conversations"])
176 other_user = insert(:user)
177
178 {:ok, _activity} = CommonAPI.post(user, %{"status" => "Hi", "visibility" => "direct"})
179
180 [participation] = Participation.for_user(user)
181
182 participation = Repo.preload(participation, :recipients)
183
184 user = User.get_cached_by_id(user.id)
185 assert [user] == participation.recipients
186 assert other_user not in participation.recipients
187
188 result =
189 conn
190 |> patch("/api/v1/pleroma/conversations/#{participation.id}", %{
191 "recipients" => [user.id, other_user.id]
192 })
193 |> json_response(200)
194
195 assert result["id"] == participation.id |> to_string
196
197 [participation] = Participation.for_user(user)
198 participation = Repo.preload(participation, :recipients)
199
200 assert user in participation.recipients
201 assert other_user in participation.recipients
202 end
203
204 test "POST /api/v1/pleroma/conversations/read" do
205 user = insert(:user)
206 %{user: other_user, conn: conn} = oauth_access(["write:notifications"])
207
208 {:ok, _activity} =
209 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}", "visibility" => "direct"})
210
211 {:ok, _activity} =
212 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}", "visibility" => "direct"})
213
214 [participation2, participation1] = Participation.for_user(other_user)
215 assert Participation.get(participation2.id).read == false
216 assert Participation.get(participation1.id).read == false
217 assert User.get_cached_by_id(other_user.id).unread_conversation_count == 2
218
219 [%{"unread" => false}, %{"unread" => false}] =
220 conn
221 |> post("/api/v1/pleroma/conversations/read", %{})
222 |> json_response(200)
223
224 [participation2, participation1] = Participation.for_user(other_user)
225 assert Participation.get(participation2.id).read == true
226 assert Participation.get(participation1.id).read == true
227 assert User.get_cached_by_id(other_user.id).unread_conversation_count == 0
228 end
229
230 describe "POST /api/v1/pleroma/notifications/read" do
231 setup do: oauth_access(["write:notifications"])
232
233 test "it marks a single notification as read", %{user: user1, conn: conn} do
234 user2 = insert(:user)
235 {:ok, activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
236 {:ok, activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
237 {:ok, [notification1]} = Notification.create_notifications(activity1)
238 {:ok, [notification2]} = Notification.create_notifications(activity2)
239
240 response =
241 conn
242 |> post("/api/v1/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
243 |> json_response(:ok)
244
245 assert %{"pleroma" => %{"is_seen" => true}} = response
246 assert Repo.get(Notification, notification1.id).seen
247 refute Repo.get(Notification, notification2.id).seen
248 end
249
250 test "it marks multiple notifications as read", %{user: user1, conn: conn} do
251 user2 = insert(:user)
252 {:ok, _activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
253 {:ok, _activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
254 {:ok, _activity3} = CommonAPI.post(user2, %{"status" => "HIE @#{user1.nickname}"})
255
256 [notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
257
258 [response1, response2] =
259 conn
260 |> post("/api/v1/pleroma/notifications/read", %{"max_id" => "#{notification2.id}"})
261 |> json_response(:ok)
262
263 assert %{"pleroma" => %{"is_seen" => true}} = response1
264 assert %{"pleroma" => %{"is_seen" => true}} = response2
265 assert Repo.get(Notification, notification1.id).seen
266 assert Repo.get(Notification, notification2.id).seen
267 refute Repo.get(Notification, notification3.id).seen
268 end
269
270 test "it returns error when notification not found", %{conn: conn} do
271 response =
272 conn
273 |> post("/api/v1/pleroma/notifications/read", %{"id" => "22222222222222"})
274 |> json_response(:bad_request)
275
276 assert response == %{"error" => "Cannot get notification"}
277 end
278 end
279 end