Merge remote-tracking branch 'remotes/origin/develop' into authenticated-api-oauth...
[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
173 {:ok, %{id: id_three}} =
174 CommonAPI.post(other_user, %{
175 "status" => "Bye!",
176 "in_reply_to_status_id" => activity.id,
177 "in_reply_to_conversation_id" => participation.id
178 })
179
180 assert [%{"id" => ^id_two}, %{"id" => ^id_three}] =
181 conn
182 |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses?limit=2")
183 |> json_response(:ok)
184
185 assert [%{"id" => ^id_three}] =
186 conn
187 |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses?min_id=#{id_two}")
188 |> json_response(:ok)
189 end
190
191 test "PATCH /api/v1/pleroma/conversations/:id" do
192 %{user: user, conn: conn} = oauth_access(["write:conversations"])
193 other_user = insert(:user)
194
195 {:ok, _activity} = CommonAPI.post(user, %{"status" => "Hi", "visibility" => "direct"})
196
197 [participation] = Participation.for_user(user)
198
199 participation = Repo.preload(participation, :recipients)
200
201 user = User.get_cached_by_id(user.id)
202 assert [user] == participation.recipients
203 assert other_user not in participation.recipients
204
205 result =
206 conn
207 |> patch("/api/v1/pleroma/conversations/#{participation.id}", %{
208 "recipients" => [user.id, other_user.id]
209 })
210 |> json_response(200)
211
212 assert result["id"] == participation.id |> to_string
213
214 [participation] = Participation.for_user(user)
215 participation = Repo.preload(participation, :recipients)
216
217 assert user in participation.recipients
218 assert other_user in participation.recipients
219 end
220
221 test "POST /api/v1/pleroma/conversations/read" do
222 user = insert(:user)
223 %{user: other_user, conn: conn} = oauth_access(["write:conversations"])
224
225 {:ok, _activity} =
226 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}", "visibility" => "direct"})
227
228 {:ok, _activity} =
229 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}", "visibility" => "direct"})
230
231 [participation2, participation1] = Participation.for_user(other_user)
232 assert Participation.get(participation2.id).read == false
233 assert Participation.get(participation1.id).read == false
234 assert User.get_cached_by_id(other_user.id).unread_conversation_count == 2
235
236 [%{"unread" => false}, %{"unread" => false}] =
237 conn
238 |> post("/api/v1/pleroma/conversations/read", %{})
239 |> json_response(200)
240
241 [participation2, participation1] = Participation.for_user(other_user)
242 assert Participation.get(participation2.id).read == true
243 assert Participation.get(participation1.id).read == true
244 assert User.get_cached_by_id(other_user.id).unread_conversation_count == 0
245 end
246
247 describe "POST /api/v1/pleroma/notifications/read" do
248 setup do: oauth_access(["write:notifications"])
249
250 test "it marks a single notification 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, [notification1]} = Notification.create_notifications(activity1)
255 {:ok, [notification2]} = Notification.create_notifications(activity2)
256
257 response =
258 conn
259 |> post("/api/v1/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
260 |> json_response(:ok)
261
262 assert %{"pleroma" => %{"is_seen" => true}} = response
263 assert Repo.get(Notification, notification1.id).seen
264 refute Repo.get(Notification, notification2.id).seen
265 end
266
267 test "it marks multiple notifications as read", %{user: user1, conn: conn} do
268 user2 = insert(:user)
269 {:ok, _activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
270 {:ok, _activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
271 {:ok, _activity3} = CommonAPI.post(user2, %{"status" => "HIE @#{user1.nickname}"})
272
273 [notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
274
275 [response1, response2] =
276 conn
277 |> post("/api/v1/pleroma/notifications/read", %{"max_id" => "#{notification2.id}"})
278 |> json_response(:ok)
279
280 assert %{"pleroma" => %{"is_seen" => true}} = response1
281 assert %{"pleroma" => %{"is_seen" => true}} = response2
282 assert Repo.get(Notification, notification1.id).seen
283 assert Repo.get(Notification, notification2.id).seen
284 refute Repo.get(Notification, notification3.id).seen
285 end
286
287 test "it returns error when notification not found", %{conn: conn} do
288 response =
289 conn
290 |> post("/api/v1/pleroma/notifications/read", %{"id" => "22222222222222"})
291 |> json_response(:bad_request)
292
293 assert response == %{"error" => "Cannot get notification"}
294 end
295 end
296 end