Merge branch 'openapi/notifications' into 'develop'
[akkoma] / test / web / mastodon_api / controllers / notification_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.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 "does NOT render account/pleroma/relationship if this is disabled by default" do
16 clear_config([:extensions, :output_relationships_in_statuses_by_default], false)
17
18 %{user: user, conn: conn} = oauth_access(["read:notifications"])
19 other_user = insert(:user)
20
21 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
22 {:ok, [_notification]} = Notification.create_notifications(activity)
23
24 response =
25 conn
26 |> assign(:user, user)
27 |> get("/api/v1/notifications")
28 |> json_response_and_validate_schema(200)
29
30 assert Enum.all?(response, fn n ->
31 get_in(n, ["account", "pleroma", "relationship"]) == %{}
32 end)
33 end
34
35 test "list of notifications" do
36 %{user: user, conn: conn} = oauth_access(["read:notifications"])
37 other_user = insert(:user)
38
39 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
40
41 {:ok, [_notification]} = Notification.create_notifications(activity)
42
43 conn =
44 conn
45 |> assign(:user, user)
46 |> get("/api/v1/notifications")
47
48 expected_response =
49 "hi <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{user.id}\" href=\"#{
50 user.ap_id
51 }\" rel=\"ugc\">@<span>#{user.nickname}</span></a></span>"
52
53 assert [%{"status" => %{"content" => response}} | _rest] =
54 json_response_and_validate_schema(conn, 200)
55
56 assert response == expected_response
57 end
58
59 test "getting a single notification" do
60 %{user: user, conn: conn} = oauth_access(["read:notifications"])
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 = get(conn, "/api/v1/notifications/#{notification.id}")
68
69 expected_response =
70 "hi <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{user.id}\" href=\"#{
71 user.ap_id
72 }\" rel=\"ugc\">@<span>#{user.nickname}</span></a></span>"
73
74 assert %{"status" => %{"content" => response}} = json_response_and_validate_schema(conn, 200)
75 assert response == expected_response
76 end
77
78 test "dismissing a single notification (deprecated endpoint)" do
79 %{user: user, conn: conn} = oauth_access(["write:notifications"])
80 other_user = insert(:user)
81
82 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
83
84 {:ok, [notification]} = Notification.create_notifications(activity)
85
86 conn =
87 conn
88 |> assign(:user, user)
89 |> put_req_header("content-type", "application/json")
90 |> post("/api/v1/notifications/dismiss", %{"id" => to_string(notification.id)})
91
92 assert %{} = json_response_and_validate_schema(conn, 200)
93 end
94
95 test "dismissing a single notification" do
96 %{user: user, conn: conn} = oauth_access(["write:notifications"])
97 other_user = insert(:user)
98
99 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
100
101 {:ok, [notification]} = Notification.create_notifications(activity)
102
103 conn =
104 conn
105 |> assign(:user, user)
106 |> post("/api/v1/notifications/#{notification.id}/dismiss")
107
108 assert %{} = json_response_and_validate_schema(conn, 200)
109 end
110
111 test "clearing all notifications" do
112 %{user: user, conn: conn} = oauth_access(["write:notifications", "read:notifications"])
113 other_user = insert(:user)
114
115 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
116
117 {:ok, [_notification]} = Notification.create_notifications(activity)
118
119 ret_conn = post(conn, "/api/v1/notifications/clear")
120
121 assert %{} = json_response_and_validate_schema(ret_conn, 200)
122
123 ret_conn = get(conn, "/api/v1/notifications")
124
125 assert all = json_response_and_validate_schema(ret_conn, 200)
126 assert all == []
127 end
128
129 test "paginates notifications using min_id, since_id, max_id, and limit" do
130 %{user: user, conn: conn} = oauth_access(["read:notifications"])
131 other_user = insert(:user)
132
133 {:ok, activity1} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
134 {:ok, activity2} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
135 {:ok, activity3} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
136 {:ok, activity4} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
137
138 notification1_id = get_notification_id_by_activity(activity1)
139 notification2_id = get_notification_id_by_activity(activity2)
140 notification3_id = get_notification_id_by_activity(activity3)
141 notification4_id = get_notification_id_by_activity(activity4)
142
143 conn = assign(conn, :user, user)
144
145 # min_id
146 result =
147 conn
148 |> get("/api/v1/notifications?limit=2&min_id=#{notification1_id}")
149 |> json_response_and_validate_schema(:ok)
150
151 assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
152
153 # since_id
154 result =
155 conn
156 |> get("/api/v1/notifications?limit=2&since_id=#{notification1_id}")
157 |> json_response_and_validate_schema(:ok)
158
159 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
160
161 # max_id
162 result =
163 conn
164 |> get("/api/v1/notifications?limit=2&max_id=#{notification4_id}")
165 |> json_response_and_validate_schema(:ok)
166
167 assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
168 end
169
170 describe "exclude_visibilities" do
171 test "filters notifications for mentions" do
172 %{user: user, conn: conn} = oauth_access(["read:notifications"])
173 other_user = insert(:user)
174
175 {:ok, public_activity} =
176 CommonAPI.post(other_user, %{"status" => "@#{user.nickname}", "visibility" => "public"})
177
178 {:ok, direct_activity} =
179 CommonAPI.post(other_user, %{"status" => "@#{user.nickname}", "visibility" => "direct"})
180
181 {:ok, unlisted_activity} =
182 CommonAPI.post(other_user, %{"status" => "@#{user.nickname}", "visibility" => "unlisted"})
183
184 {:ok, private_activity} =
185 CommonAPI.post(other_user, %{"status" => "@#{user.nickname}", "visibility" => "private"})
186
187 query = params_to_query(%{exclude_visibilities: ["public", "unlisted", "private"]})
188 conn_res = get(conn, "/api/v1/notifications?" <> query)
189
190 assert [%{"status" => %{"id" => id}}] = json_response_and_validate_schema(conn_res, 200)
191 assert id == direct_activity.id
192
193 query = params_to_query(%{exclude_visibilities: ["public", "unlisted", "direct"]})
194 conn_res = get(conn, "/api/v1/notifications?" <> query)
195
196 assert [%{"status" => %{"id" => id}}] = json_response_and_validate_schema(conn_res, 200)
197 assert id == private_activity.id
198
199 query = params_to_query(%{exclude_visibilities: ["public", "private", "direct"]})
200 conn_res = get(conn, "/api/v1/notifications?" <> query)
201
202 assert [%{"status" => %{"id" => id}}] = json_response_and_validate_schema(conn_res, 200)
203 assert id == unlisted_activity.id
204
205 query = params_to_query(%{exclude_visibilities: ["unlisted", "private", "direct"]})
206 conn_res = get(conn, "/api/v1/notifications?" <> query)
207
208 assert [%{"status" => %{"id" => id}}] = json_response_and_validate_schema(conn_res, 200)
209 assert id == public_activity.id
210 end
211
212 test "filters notifications for Like activities" do
213 user = insert(:user)
214 %{user: other_user, conn: conn} = oauth_access(["read:notifications"])
215
216 {:ok, public_activity} =
217 CommonAPI.post(other_user, %{"status" => ".", "visibility" => "public"})
218
219 {:ok, direct_activity} =
220 CommonAPI.post(other_user, %{"status" => "@#{user.nickname}", "visibility" => "direct"})
221
222 {:ok, unlisted_activity} =
223 CommonAPI.post(other_user, %{"status" => ".", "visibility" => "unlisted"})
224
225 {:ok, private_activity} =
226 CommonAPI.post(other_user, %{"status" => ".", "visibility" => "private"})
227
228 {:ok, _} = CommonAPI.favorite(user, public_activity.id)
229 {:ok, _} = CommonAPI.favorite(user, direct_activity.id)
230 {:ok, _} = CommonAPI.favorite(user, unlisted_activity.id)
231 {:ok, _} = CommonAPI.favorite(user, private_activity.id)
232
233 activity_ids =
234 conn
235 |> get("/api/v1/notifications?exclude_visibilities[]=direct")
236 |> json_response_and_validate_schema(200)
237 |> Enum.map(& &1["status"]["id"])
238
239 assert public_activity.id in activity_ids
240 assert unlisted_activity.id in activity_ids
241 assert private_activity.id in activity_ids
242 refute direct_activity.id in activity_ids
243
244 activity_ids =
245 conn
246 |> get("/api/v1/notifications?exclude_visibilities[]=unlisted")
247 |> json_response_and_validate_schema(200)
248 |> Enum.map(& &1["status"]["id"])
249
250 assert public_activity.id in activity_ids
251 refute unlisted_activity.id in activity_ids
252 assert private_activity.id in activity_ids
253 assert direct_activity.id in activity_ids
254
255 activity_ids =
256 conn
257 |> get("/api/v1/notifications?exclude_visibilities[]=private")
258 |> json_response_and_validate_schema(200)
259 |> Enum.map(& &1["status"]["id"])
260
261 assert public_activity.id in activity_ids
262 assert unlisted_activity.id in activity_ids
263 refute private_activity.id in activity_ids
264 assert direct_activity.id in activity_ids
265
266 activity_ids =
267 conn
268 |> get("/api/v1/notifications?exclude_visibilities[]=public")
269 |> json_response_and_validate_schema(200)
270 |> Enum.map(& &1["status"]["id"])
271
272 refute public_activity.id in activity_ids
273 assert unlisted_activity.id in activity_ids
274 assert private_activity.id in activity_ids
275 assert direct_activity.id in activity_ids
276 end
277
278 test "filters notifications for Announce activities" do
279 user = insert(:user)
280 %{user: other_user, conn: conn} = oauth_access(["read:notifications"])
281
282 {:ok, public_activity} =
283 CommonAPI.post(other_user, %{"status" => ".", "visibility" => "public"})
284
285 {:ok, unlisted_activity} =
286 CommonAPI.post(other_user, %{"status" => ".", "visibility" => "unlisted"})
287
288 {:ok, _, _} = CommonAPI.repeat(public_activity.id, user)
289 {:ok, _, _} = CommonAPI.repeat(unlisted_activity.id, user)
290
291 activity_ids =
292 conn
293 |> get("/api/v1/notifications?exclude_visibilities[]=unlisted")
294 |> json_response_and_validate_schema(200)
295 |> Enum.map(& &1["status"]["id"])
296
297 assert public_activity.id in activity_ids
298 refute unlisted_activity.id in activity_ids
299 end
300 end
301
302 test "filters notifications using exclude_types" do
303 %{user: user, conn: conn} = oauth_access(["read:notifications"])
304 other_user = insert(:user)
305
306 {:ok, mention_activity} = CommonAPI.post(other_user, %{"status" => "hey @#{user.nickname}"})
307 {:ok, create_activity} = CommonAPI.post(user, %{"status" => "hey"})
308 {:ok, favorite_activity} = CommonAPI.favorite(other_user, create_activity.id)
309 {:ok, reblog_activity, _} = CommonAPI.repeat(create_activity.id, other_user)
310 {:ok, _, _, follow_activity} = CommonAPI.follow(other_user, user)
311
312 mention_notification_id = get_notification_id_by_activity(mention_activity)
313 favorite_notification_id = get_notification_id_by_activity(favorite_activity)
314 reblog_notification_id = get_notification_id_by_activity(reblog_activity)
315 follow_notification_id = get_notification_id_by_activity(follow_activity)
316
317 query = params_to_query(%{exclude_types: ["mention", "favourite", "reblog"]})
318 conn_res = get(conn, "/api/v1/notifications?" <> query)
319
320 assert [%{"id" => ^follow_notification_id}] = json_response_and_validate_schema(conn_res, 200)
321
322 query = params_to_query(%{exclude_types: ["favourite", "reblog", "follow"]})
323 conn_res = get(conn, "/api/v1/notifications?" <> query)
324
325 assert [%{"id" => ^mention_notification_id}] =
326 json_response_and_validate_schema(conn_res, 200)
327
328 query = params_to_query(%{exclude_types: ["reblog", "follow", "mention"]})
329 conn_res = get(conn, "/api/v1/notifications?" <> query)
330
331 assert [%{"id" => ^favorite_notification_id}] =
332 json_response_and_validate_schema(conn_res, 200)
333
334 query = params_to_query(%{exclude_types: ["follow", "mention", "favourite"]})
335 conn_res = get(conn, "/api/v1/notifications?" <> query)
336
337 assert [%{"id" => ^reblog_notification_id}] = json_response_and_validate_schema(conn_res, 200)
338 end
339
340 test "filters notifications using include_types" do
341 %{user: user, conn: conn} = oauth_access(["read:notifications"])
342 other_user = insert(:user)
343
344 {:ok, mention_activity} = CommonAPI.post(other_user, %{"status" => "hey @#{user.nickname}"})
345 {:ok, create_activity} = CommonAPI.post(user, %{"status" => "hey"})
346 {:ok, favorite_activity} = CommonAPI.favorite(other_user, create_activity.id)
347 {:ok, reblog_activity, _} = CommonAPI.repeat(create_activity.id, other_user)
348 {:ok, _, _, follow_activity} = CommonAPI.follow(other_user, user)
349
350 mention_notification_id = get_notification_id_by_activity(mention_activity)
351 favorite_notification_id = get_notification_id_by_activity(favorite_activity)
352 reblog_notification_id = get_notification_id_by_activity(reblog_activity)
353 follow_notification_id = get_notification_id_by_activity(follow_activity)
354
355 conn_res = get(conn, "/api/v1/notifications?include_types[]=follow")
356
357 assert [%{"id" => ^follow_notification_id}] = json_response_and_validate_schema(conn_res, 200)
358
359 conn_res = get(conn, "/api/v1/notifications?include_types[]=mention")
360
361 assert [%{"id" => ^mention_notification_id}] =
362 json_response_and_validate_schema(conn_res, 200)
363
364 conn_res = get(conn, "/api/v1/notifications?include_types[]=favourite")
365
366 assert [%{"id" => ^favorite_notification_id}] =
367 json_response_and_validate_schema(conn_res, 200)
368
369 conn_res = get(conn, "/api/v1/notifications?include_types[]=reblog")
370
371 assert [%{"id" => ^reblog_notification_id}] = json_response_and_validate_schema(conn_res, 200)
372
373 result = conn |> get("/api/v1/notifications") |> json_response_and_validate_schema(200)
374
375 assert length(result) == 4
376
377 query = params_to_query(%{include_types: ["follow", "mention", "favourite", "reblog"]})
378
379 result =
380 conn
381 |> get("/api/v1/notifications?" <> query)
382 |> json_response_and_validate_schema(200)
383
384 assert length(result) == 4
385 end
386
387 test "destroy multiple" do
388 %{user: user, conn: conn} = oauth_access(["read:notifications", "write:notifications"])
389 other_user = insert(:user)
390
391 {:ok, activity1} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
392 {:ok, activity2} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
393 {:ok, activity3} = CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}"})
394 {:ok, activity4} = CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}"})
395
396 notification1_id = get_notification_id_by_activity(activity1)
397 notification2_id = get_notification_id_by_activity(activity2)
398 notification3_id = get_notification_id_by_activity(activity3)
399 notification4_id = get_notification_id_by_activity(activity4)
400
401 result =
402 conn
403 |> get("/api/v1/notifications")
404 |> json_response_and_validate_schema(:ok)
405
406 assert [%{"id" => ^notification2_id}, %{"id" => ^notification1_id}] = result
407
408 conn2 =
409 conn
410 |> assign(:user, other_user)
411 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:notifications"]))
412
413 result =
414 conn2
415 |> get("/api/v1/notifications")
416 |> json_response_and_validate_schema(:ok)
417
418 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
419
420 query = params_to_query(%{ids: [notification1_id, notification2_id]})
421 conn_destroy = delete(conn, "/api/v1/notifications/destroy_multiple?" <> query)
422
423 assert json_response_and_validate_schema(conn_destroy, 200) == %{}
424
425 result =
426 conn2
427 |> get("/api/v1/notifications")
428 |> json_response_and_validate_schema(:ok)
429
430 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
431 end
432
433 test "doesn't see notifications after muting user with notifications" do
434 %{user: user, conn: conn} = oauth_access(["read:notifications"])
435 user2 = insert(:user)
436
437 {:ok, _, _, _} = CommonAPI.follow(user, user2)
438 {:ok, _} = CommonAPI.post(user2, %{"status" => "hey @#{user.nickname}"})
439
440 ret_conn = get(conn, "/api/v1/notifications")
441
442 assert length(json_response_and_validate_schema(ret_conn, 200)) == 1
443
444 {:ok, _user_relationships} = User.mute(user, user2)
445
446 conn = get(conn, "/api/v1/notifications")
447
448 assert json_response_and_validate_schema(conn, 200) == []
449 end
450
451 test "see notifications after muting user without notifications" do
452 %{user: user, conn: conn} = oauth_access(["read:notifications"])
453 user2 = insert(:user)
454
455 {:ok, _, _, _} = CommonAPI.follow(user, user2)
456 {:ok, _} = CommonAPI.post(user2, %{"status" => "hey @#{user.nickname}"})
457
458 ret_conn = get(conn, "/api/v1/notifications")
459
460 assert length(json_response_and_validate_schema(ret_conn, 200)) == 1
461
462 {:ok, _user_relationships} = User.mute(user, user2, false)
463
464 conn = get(conn, "/api/v1/notifications")
465
466 assert length(json_response_and_validate_schema(conn, 200)) == 1
467 end
468
469 test "see notifications after muting user with notifications and with_muted parameter" do
470 %{user: user, conn: conn} = oauth_access(["read:notifications"])
471 user2 = insert(:user)
472
473 {:ok, _, _, _} = CommonAPI.follow(user, user2)
474 {:ok, _} = CommonAPI.post(user2, %{"status" => "hey @#{user.nickname}"})
475
476 ret_conn = get(conn, "/api/v1/notifications")
477
478 assert length(json_response_and_validate_schema(ret_conn, 200)) == 1
479
480 {:ok, _user_relationships} = User.mute(user, user2)
481
482 conn = get(conn, "/api/v1/notifications?with_muted=true")
483
484 assert length(json_response_and_validate_schema(conn, 200)) == 1
485 end
486
487 @tag capture_log: true
488 test "see move notifications" do
489 old_user = insert(:user)
490 new_user = insert(:user, also_known_as: [old_user.ap_id])
491 %{user: follower, conn: conn} = oauth_access(["read:notifications"])
492
493 old_user_url = old_user.ap_id
494
495 body =
496 File.read!("test/fixtures/users_mock/localhost.json")
497 |> String.replace("{{nickname}}", old_user.nickname)
498 |> Jason.encode!()
499
500 Tesla.Mock.mock(fn
501 %{method: :get, url: ^old_user_url} ->
502 %Tesla.Env{status: 200, body: body}
503 end)
504
505 User.follow(follower, old_user)
506 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
507 Pleroma.Tests.ObanHelpers.perform_all()
508
509 conn = get(conn, "/api/v1/notifications")
510
511 assert length(json_response_and_validate_schema(conn, 200)) == 1
512 end
513
514 describe "link headers" do
515 test "preserves parameters in link headers" do
516 %{user: user, conn: conn} = oauth_access(["read:notifications"])
517 other_user = insert(:user)
518
519 {:ok, activity1} =
520 CommonAPI.post(other_user, %{
521 "status" => "hi @#{user.nickname}",
522 "visibility" => "public"
523 })
524
525 {:ok, activity2} =
526 CommonAPI.post(other_user, %{
527 "status" => "hi @#{user.nickname}",
528 "visibility" => "public"
529 })
530
531 notification1 = Repo.get_by(Notification, activity_id: activity1.id)
532 notification2 = Repo.get_by(Notification, activity_id: activity2.id)
533
534 conn =
535 conn
536 |> assign(:user, user)
537 |> get("/api/v1/notifications?limit=5")
538
539 assert [link_header] = get_resp_header(conn, "link")
540 assert link_header =~ ~r/limit=5/
541 assert link_header =~ ~r/min_id=#{notification2.id}/
542 assert link_header =~ ~r/max_id=#{notification1.id}/
543 end
544 end
545
546 describe "from specified user" do
547 test "account_id" do
548 %{user: user, conn: conn} = oauth_access(["read:notifications"])
549
550 %{id: account_id} = other_user1 = insert(:user)
551 other_user2 = insert(:user)
552
553 {:ok, _activity} = CommonAPI.post(other_user1, %{"status" => "hi @#{user.nickname}"})
554 {:ok, _activity} = CommonAPI.post(other_user2, %{"status" => "bye @#{user.nickname}"})
555
556 assert [%{"account" => %{"id" => ^account_id}}] =
557 conn
558 |> assign(:user, user)
559 |> get("/api/v1/notifications?account_id=#{account_id}")
560 |> json_response_and_validate_schema(200)
561
562 assert %{"error" => "Account is not found"} =
563 conn
564 |> assign(:user, user)
565 |> get("/api/v1/notifications?account_id=cofe")
566 |> json_response_and_validate_schema(404)
567 end
568 end
569
570 defp get_notification_id_by_activity(%{id: id}) do
571 Notification
572 |> Repo.get_by(activity_id: id)
573 |> Map.get(:id)
574 |> to_string()
575 end
576
577 defp params_to_query(%{} = params) do
578 Enum.map_join(params, "&", fn
579 {k, v} when is_list(v) -> Enum.map_join(v, "&", &"#{k}[]=#{&1}")
580 {k, v} -> k <> "=" <> v
581 end)
582 end
583 end