1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.NotificationControllerTest do
6 use Pleroma.Web.ConnCase
8 alias Pleroma.Notification
11 alias Pleroma.Web.CommonAPI
13 import Pleroma.Factory
15 test "list of notifications", %{conn: conn} do
17 other_user = insert(:user)
19 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
21 {:ok, [_notification]} = Notification.create_notifications(activity)
25 |> assign(:user, user)
26 |> get("/api/v1/notifications")
29 "hi <span class=\"h-card\"><a data-user=\"#{user.id}\" class=\"u-url mention\" href=\"#{
31 }\" rel=\"ugc\">@<span>#{user.nickname}</span></a></span>"
33 assert [%{"status" => %{"content" => response}} | _rest] = json_response(conn, 200)
34 assert response == expected_response
37 test "getting a single notification", %{conn: conn} do
39 other_user = insert(:user)
41 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
43 {:ok, [notification]} = Notification.create_notifications(activity)
47 |> assign(:user, user)
48 |> get("/api/v1/notifications/#{notification.id}")
51 "hi <span class=\"h-card\"><a data-user=\"#{user.id}\" class=\"u-url mention\" href=\"#{
53 }\" rel=\"ugc\">@<span>#{user.nickname}</span></a></span>"
55 assert %{"status" => %{"content" => response}} = json_response(conn, 200)
56 assert response == expected_response
59 test "dismissing a single notification", %{conn: conn} do
61 other_user = insert(:user)
63 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
65 {:ok, [notification]} = Notification.create_notifications(activity)
69 |> assign(:user, user)
70 |> post("/api/v1/notifications/dismiss", %{"id" => notification.id})
72 assert %{} = json_response(conn, 200)
75 test "clearing all notifications", %{conn: conn} do
77 other_user = insert(:user)
79 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
81 {:ok, [_notification]} = Notification.create_notifications(activity)
85 |> assign(:user, user)
86 |> post("/api/v1/notifications/clear")
88 assert %{} = json_response(conn, 200)
92 |> assign(:user, user)
93 |> get("/api/v1/notifications")
95 assert all = json_response(conn, 200)
99 test "paginates notifications using min_id, since_id, max_id, and limit", %{conn: conn} do
101 other_user = insert(:user)
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}"})
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)
113 conn = assign(conn, :user, user)
118 |> get("/api/v1/notifications?limit=2&min_id=#{notification1_id}")
119 |> json_response(:ok)
121 assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
126 |> get("/api/v1/notifications?limit=2&since_id=#{notification1_id}")
127 |> json_response(:ok)
129 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
134 |> get("/api/v1/notifications?limit=2&max_id=#{notification4_id}")
135 |> json_response(:ok)
137 assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
140 test "filters notifications using exclude_types", %{conn: conn} do
142 other_user = insert(:user)
144 {:ok, mention_activity} = CommonAPI.post(other_user, %{"status" => "hey @#{user.nickname}"})
145 {:ok, create_activity} = CommonAPI.post(user, %{"status" => "hey"})
146 {:ok, favorite_activity, _} = CommonAPI.favorite(create_activity.id, other_user)
147 {:ok, reblog_activity, _} = CommonAPI.repeat(create_activity.id, other_user)
148 {:ok, _, _, follow_activity} = CommonAPI.follow(other_user, user)
150 mention_notification_id = get_notification_id_by_activity(mention_activity)
151 favorite_notification_id = get_notification_id_by_activity(favorite_activity)
152 reblog_notification_id = get_notification_id_by_activity(reblog_activity)
153 follow_notification_id = get_notification_id_by_activity(follow_activity)
155 conn = assign(conn, :user, user)
158 get(conn, "/api/v1/notifications", %{exclude_types: ["mention", "favourite", "reblog"]})
160 assert [%{"id" => ^follow_notification_id}] = json_response(conn_res, 200)
163 get(conn, "/api/v1/notifications", %{exclude_types: ["favourite", "reblog", "follow"]})
165 assert [%{"id" => ^mention_notification_id}] = json_response(conn_res, 200)
168 get(conn, "/api/v1/notifications", %{exclude_types: ["reblog", "follow", "mention"]})
170 assert [%{"id" => ^favorite_notification_id}] = json_response(conn_res, 200)
173 get(conn, "/api/v1/notifications", %{exclude_types: ["follow", "mention", "favourite"]})
175 assert [%{"id" => ^reblog_notification_id}] = json_response(conn_res, 200)
178 test "destroy multiple", %{conn: conn} do
180 other_user = insert(:user)
182 {:ok, activity1} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
183 {:ok, activity2} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
184 {:ok, activity3} = CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}"})
185 {:ok, activity4} = CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}"})
187 notification1_id = get_notification_id_by_activity(activity1)
188 notification2_id = get_notification_id_by_activity(activity2)
189 notification3_id = get_notification_id_by_activity(activity3)
190 notification4_id = get_notification_id_by_activity(activity4)
192 conn = assign(conn, :user, user)
196 |> get("/api/v1/notifications")
197 |> json_response(:ok)
199 assert [%{"id" => ^notification2_id}, %{"id" => ^notification1_id}] = result
203 |> assign(:user, other_user)
207 |> get("/api/v1/notifications")
208 |> json_response(:ok)
210 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
214 |> delete("/api/v1/notifications/destroy_multiple", %{
215 "ids" => [notification1_id, notification2_id]
218 assert json_response(conn_destroy, 200) == %{}
222 |> get("/api/v1/notifications")
223 |> json_response(:ok)
225 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
228 test "doesn't see notifications after muting user with notifications", %{conn: conn} do
230 user2 = insert(:user)
232 {:ok, _, _, _} = CommonAPI.follow(user, user2)
233 {:ok, _} = CommonAPI.post(user2, %{"status" => "hey @#{user.nickname}"})
235 conn = assign(conn, :user, user)
237 conn = get(conn, "/api/v1/notifications")
239 assert length(json_response(conn, 200)) == 1
241 {:ok, user} = User.mute(user, user2)
243 conn = assign(build_conn(), :user, user)
244 conn = get(conn, "/api/v1/notifications")
246 assert json_response(conn, 200) == []
249 test "see notifications after muting user without notifications", %{conn: conn} do
251 user2 = insert(:user)
253 {:ok, _, _, _} = CommonAPI.follow(user, user2)
254 {:ok, _} = CommonAPI.post(user2, %{"status" => "hey @#{user.nickname}"})
256 conn = assign(conn, :user, user)
258 conn = get(conn, "/api/v1/notifications")
260 assert length(json_response(conn, 200)) == 1
262 {:ok, user} = User.mute(user, user2, false)
264 conn = assign(build_conn(), :user, user)
265 conn = get(conn, "/api/v1/notifications")
267 assert length(json_response(conn, 200)) == 1
270 test "see notifications after muting user with notifications and with_muted parameter", %{
274 user2 = insert(:user)
276 {:ok, _, _, _} = CommonAPI.follow(user, user2)
277 {:ok, _} = CommonAPI.post(user2, %{"status" => "hey @#{user.nickname}"})
279 conn = assign(conn, :user, user)
281 conn = get(conn, "/api/v1/notifications")
283 assert length(json_response(conn, 200)) == 1
285 {:ok, user} = User.mute(user, user2)
287 conn = assign(build_conn(), :user, user)
288 conn = get(conn, "/api/v1/notifications", %{"with_muted" => "true"})
290 assert length(json_response(conn, 200)) == 1
293 defp get_notification_id_by_activity(%{id: id}) do
295 |> Repo.get_by(activity_id: id)