Merge remote-tracking branch 'origin/develop' into reactions
[akkoma] / test / web / mastodon_api / controllers / notification_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.NotificationControllerTest do
6 use Pleroma.Web.ConnCase
7
8 alias Pleroma.Notification
9 alias Pleroma.Repo
10 alias Pleroma.User
11 alias Pleroma.Web.CommonAPI
12
13 import Pleroma.Factory
14
15 test "list of notifications", %{conn: conn} do
16 user = insert(:user)
17 other_user = insert(:user)
18
19 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
20
21 {:ok, [_notification]} = Notification.create_notifications(activity)
22
23 conn =
24 conn
25 |> assign(:user, user)
26 |> get("/api/v1/notifications")
27
28 expected_response =
29 "hi <span class=\"h-card\"><a data-user=\"#{user.id}\" class=\"u-url mention\" href=\"#{
30 user.ap_id
31 }\" rel=\"ugc\">@<span>#{user.nickname}</span></a></span>"
32
33 assert [%{"status" => %{"content" => response}} | _rest] = json_response(conn, 200)
34 assert response == expected_response
35 end
36
37 test "getting a single notification", %{conn: conn} do
38 user = insert(:user)
39 other_user = insert(:user)
40
41 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
42
43 {:ok, [notification]} = Notification.create_notifications(activity)
44
45 conn =
46 conn
47 |> assign(:user, user)
48 |> get("/api/v1/notifications/#{notification.id}")
49
50 expected_response =
51 "hi <span class=\"h-card\"><a data-user=\"#{user.id}\" class=\"u-url mention\" href=\"#{
52 user.ap_id
53 }\" rel=\"ugc\">@<span>#{user.nickname}</span></a></span>"
54
55 assert %{"status" => %{"content" => response}} = json_response(conn, 200)
56 assert response == expected_response
57 end
58
59 test "dismissing a single notification", %{conn: conn} do
60 user = insert(:user)
61 other_user = insert(:user)
62
63 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
64
65 {:ok, [notification]} = Notification.create_notifications(activity)
66
67 conn =
68 conn
69 |> assign(:user, user)
70 |> post("/api/v1/notifications/dismiss", %{"id" => notification.id})
71
72 assert %{} = json_response(conn, 200)
73 end
74
75 test "clearing all notifications", %{conn: conn} do
76 user = insert(:user)
77 other_user = insert(:user)
78
79 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
80
81 {:ok, [_notification]} = Notification.create_notifications(activity)
82
83 conn =
84 conn
85 |> assign(:user, user)
86 |> post("/api/v1/notifications/clear")
87
88 assert %{} = json_response(conn, 200)
89
90 conn =
91 build_conn()
92 |> assign(:user, user)
93 |> get("/api/v1/notifications")
94
95 assert all = json_response(conn, 200)
96 assert all == []
97 end
98
99 test "paginates notifications using min_id, since_id, max_id, and limit", %{conn: conn} do
100 user = insert(:user)
101 other_user = insert(:user)
102
103 {:ok, activity1} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
104 {:ok, activity2} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
105 {:ok, activity3} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
106 {:ok, activity4} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
107
108 notification1_id = get_notification_id_by_activity(activity1)
109 notification2_id = get_notification_id_by_activity(activity2)
110 notification3_id = get_notification_id_by_activity(activity3)
111 notification4_id = get_notification_id_by_activity(activity4)
112
113 conn = assign(conn, :user, user)
114
115 # min_id
116 result =
117 conn
118 |> get("/api/v1/notifications?limit=2&min_id=#{notification1_id}")
119 |> json_response(:ok)
120
121 assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
122
123 # since_id
124 result =
125 conn
126 |> get("/api/v1/notifications?limit=2&since_id=#{notification1_id}")
127 |> json_response(:ok)
128
129 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
130
131 # max_id
132 result =
133 conn
134 |> get("/api/v1/notifications?limit=2&max_id=#{notification4_id}")
135 |> json_response(:ok)
136
137 assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
138 end
139
140 test "filters notifications using exclude_visibilities", %{conn: conn} do
141 user = insert(:user)
142 other_user = insert(:user)
143
144 {:ok, public_activity} =
145 CommonAPI.post(other_user, %{"status" => "@#{user.nickname}", "visibility" => "public"})
146
147 {:ok, direct_activity} =
148 CommonAPI.post(other_user, %{"status" => "@#{user.nickname}", "visibility" => "direct"})
149
150 {:ok, unlisted_activity} =
151 CommonAPI.post(other_user, %{"status" => "@#{user.nickname}", "visibility" => "unlisted"})
152
153 {:ok, private_activity} =
154 CommonAPI.post(other_user, %{"status" => "@#{user.nickname}", "visibility" => "private"})
155
156 conn = assign(conn, :user, user)
157
158 conn_res =
159 get(conn, "/api/v1/notifications", %{
160 exclude_visibilities: ["public", "unlisted", "private"]
161 })
162
163 assert [%{"status" => %{"id" => id}}] = json_response(conn_res, 200)
164 assert id == direct_activity.id
165
166 conn_res =
167 get(conn, "/api/v1/notifications", %{
168 exclude_visibilities: ["public", "unlisted", "direct"]
169 })
170
171 assert [%{"status" => %{"id" => id}}] = json_response(conn_res, 200)
172 assert id == private_activity.id
173
174 conn_res =
175 get(conn, "/api/v1/notifications", %{
176 exclude_visibilities: ["public", "private", "direct"]
177 })
178
179 assert [%{"status" => %{"id" => id}}] = json_response(conn_res, 200)
180 assert id == unlisted_activity.id
181
182 conn_res =
183 get(conn, "/api/v1/notifications", %{
184 exclude_visibilities: ["unlisted", "private", "direct"]
185 })
186
187 assert [%{"status" => %{"id" => id}}] = json_response(conn_res, 200)
188 assert id == public_activity.id
189 end
190
191 test "filters notifications using exclude_types", %{conn: conn} do
192 user = insert(:user)
193 other_user = insert(:user)
194
195 {:ok, mention_activity} = CommonAPI.post(other_user, %{"status" => "hey @#{user.nickname}"})
196 {:ok, create_activity} = CommonAPI.post(user, %{"status" => "hey"})
197 {:ok, favorite_activity, _} = CommonAPI.favorite(create_activity.id, other_user)
198 {:ok, reblog_activity, _} = CommonAPI.repeat(create_activity.id, other_user)
199 {:ok, _, _, follow_activity} = CommonAPI.follow(other_user, user)
200
201 mention_notification_id = get_notification_id_by_activity(mention_activity)
202 favorite_notification_id = get_notification_id_by_activity(favorite_activity)
203 reblog_notification_id = get_notification_id_by_activity(reblog_activity)
204 follow_notification_id = get_notification_id_by_activity(follow_activity)
205
206 conn = assign(conn, :user, user)
207
208 conn_res =
209 get(conn, "/api/v1/notifications", %{exclude_types: ["mention", "favourite", "reblog"]})
210
211 assert [%{"id" => ^follow_notification_id}] = json_response(conn_res, 200)
212
213 conn_res =
214 get(conn, "/api/v1/notifications", %{exclude_types: ["favourite", "reblog", "follow"]})
215
216 assert [%{"id" => ^mention_notification_id}] = json_response(conn_res, 200)
217
218 conn_res =
219 get(conn, "/api/v1/notifications", %{exclude_types: ["reblog", "follow", "mention"]})
220
221 assert [%{"id" => ^favorite_notification_id}] = json_response(conn_res, 200)
222
223 conn_res =
224 get(conn, "/api/v1/notifications", %{exclude_types: ["follow", "mention", "favourite"]})
225
226 assert [%{"id" => ^reblog_notification_id}] = json_response(conn_res, 200)
227 end
228
229 test "destroy multiple", %{conn: conn} do
230 user = insert(:user)
231 other_user = insert(:user)
232
233 {:ok, activity1} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
234 {:ok, activity2} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
235 {:ok, activity3} = CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}"})
236 {:ok, activity4} = CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}"})
237
238 notification1_id = get_notification_id_by_activity(activity1)
239 notification2_id = get_notification_id_by_activity(activity2)
240 notification3_id = get_notification_id_by_activity(activity3)
241 notification4_id = get_notification_id_by_activity(activity4)
242
243 conn = assign(conn, :user, user)
244
245 result =
246 conn
247 |> get("/api/v1/notifications")
248 |> json_response(:ok)
249
250 assert [%{"id" => ^notification2_id}, %{"id" => ^notification1_id}] = result
251
252 conn2 =
253 conn
254 |> assign(:user, other_user)
255
256 result =
257 conn2
258 |> get("/api/v1/notifications")
259 |> json_response(:ok)
260
261 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
262
263 conn_destroy =
264 conn
265 |> delete("/api/v1/notifications/destroy_multiple", %{
266 "ids" => [notification1_id, notification2_id]
267 })
268
269 assert json_response(conn_destroy, 200) == %{}
270
271 result =
272 conn2
273 |> get("/api/v1/notifications")
274 |> json_response(:ok)
275
276 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
277 end
278
279 test "doesn't see notifications after muting user with notifications", %{conn: conn} do
280 user = insert(:user)
281 user2 = insert(:user)
282
283 {:ok, _, _, _} = CommonAPI.follow(user, user2)
284 {:ok, _} = CommonAPI.post(user2, %{"status" => "hey @#{user.nickname}"})
285
286 conn = assign(conn, :user, user)
287
288 conn = get(conn, "/api/v1/notifications")
289
290 assert length(json_response(conn, 200)) == 1
291
292 {:ok, user} = User.mute(user, user2)
293
294 conn = assign(build_conn(), :user, user)
295 conn = get(conn, "/api/v1/notifications")
296
297 assert json_response(conn, 200) == []
298 end
299
300 test "see notifications after muting user without notifications", %{conn: conn} do
301 user = insert(:user)
302 user2 = insert(:user)
303
304 {:ok, _, _, _} = CommonAPI.follow(user, user2)
305 {:ok, _} = CommonAPI.post(user2, %{"status" => "hey @#{user.nickname}"})
306
307 conn = assign(conn, :user, user)
308
309 conn = get(conn, "/api/v1/notifications")
310
311 assert length(json_response(conn, 200)) == 1
312
313 {:ok, user} = User.mute(user, user2, false)
314
315 conn = assign(build_conn(), :user, user)
316 conn = get(conn, "/api/v1/notifications")
317
318 assert length(json_response(conn, 200)) == 1
319 end
320
321 test "see notifications after muting user with notifications and with_muted parameter", %{
322 conn: conn
323 } do
324 user = insert(:user)
325 user2 = insert(:user)
326
327 {:ok, _, _, _} = CommonAPI.follow(user, user2)
328 {:ok, _} = CommonAPI.post(user2, %{"status" => "hey @#{user.nickname}"})
329
330 conn = assign(conn, :user, user)
331
332 conn = get(conn, "/api/v1/notifications")
333
334 assert length(json_response(conn, 200)) == 1
335
336 {:ok, user} = User.mute(user, user2)
337
338 conn = assign(build_conn(), :user, user)
339 conn = get(conn, "/api/v1/notifications", %{"with_muted" => "true"})
340
341 assert length(json_response(conn, 200)) == 1
342 end
343
344 defp get_notification_id_by_activity(%{id: id}) do
345 Notification
346 |> Repo.get_by(activity_id: id)
347 |> Map.get(:id)
348 |> to_string()
349 end
350 end