Merge remote-tracking branch 'origin/develop' into benchmark-finishing
[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_types", %{conn: conn} do
141 user = insert(:user)
142 other_user = insert(:user)
143
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)
149
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)
154
155 conn = assign(conn, :user, user)
156
157 conn_res =
158 get(conn, "/api/v1/notifications", %{exclude_types: ["mention", "favourite", "reblog"]})
159
160 assert [%{"id" => ^follow_notification_id}] = json_response(conn_res, 200)
161
162 conn_res =
163 get(conn, "/api/v1/notifications", %{exclude_types: ["favourite", "reblog", "follow"]})
164
165 assert [%{"id" => ^mention_notification_id}] = json_response(conn_res, 200)
166
167 conn_res =
168 get(conn, "/api/v1/notifications", %{exclude_types: ["reblog", "follow", "mention"]})
169
170 assert [%{"id" => ^favorite_notification_id}] = json_response(conn_res, 200)
171
172 conn_res =
173 get(conn, "/api/v1/notifications", %{exclude_types: ["follow", "mention", "favourite"]})
174
175 assert [%{"id" => ^reblog_notification_id}] = json_response(conn_res, 200)
176 end
177
178 test "destroy multiple", %{conn: conn} do
179 user = insert(:user)
180 other_user = insert(:user)
181
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}"})
186
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)
191
192 conn = assign(conn, :user, user)
193
194 result =
195 conn
196 |> get("/api/v1/notifications")
197 |> json_response(:ok)
198
199 assert [%{"id" => ^notification2_id}, %{"id" => ^notification1_id}] = result
200
201 conn2 =
202 conn
203 |> assign(:user, other_user)
204
205 result =
206 conn2
207 |> get("/api/v1/notifications")
208 |> json_response(:ok)
209
210 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
211
212 conn_destroy =
213 conn
214 |> delete("/api/v1/notifications/destroy_multiple", %{
215 "ids" => [notification1_id, notification2_id]
216 })
217
218 assert json_response(conn_destroy, 200) == %{}
219
220 result =
221 conn2
222 |> get("/api/v1/notifications")
223 |> json_response(:ok)
224
225 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
226 end
227
228 test "doesn't see notifications after muting user with notifications", %{conn: conn} do
229 user = insert(:user)
230 user2 = insert(:user)
231
232 {:ok, _, _, _} = CommonAPI.follow(user, user2)
233 {:ok, _} = CommonAPI.post(user2, %{"status" => "hey @#{user.nickname}"})
234
235 conn = assign(conn, :user, user)
236
237 conn = get(conn, "/api/v1/notifications")
238
239 assert length(json_response(conn, 200)) == 1
240
241 {:ok, user} = User.mute(user, user2)
242
243 conn = assign(build_conn(), :user, user)
244 conn = get(conn, "/api/v1/notifications")
245
246 assert json_response(conn, 200) == []
247 end
248
249 test "see notifications after muting user without notifications", %{conn: conn} do
250 user = insert(:user)
251 user2 = insert(:user)
252
253 {:ok, _, _, _} = CommonAPI.follow(user, user2)
254 {:ok, _} = CommonAPI.post(user2, %{"status" => "hey @#{user.nickname}"})
255
256 conn = assign(conn, :user, user)
257
258 conn = get(conn, "/api/v1/notifications")
259
260 assert length(json_response(conn, 200)) == 1
261
262 {:ok, user} = User.mute(user, user2, false)
263
264 conn = assign(build_conn(), :user, user)
265 conn = get(conn, "/api/v1/notifications")
266
267 assert length(json_response(conn, 200)) == 1
268 end
269
270 test "see notifications after muting user with notifications and with_muted parameter", %{
271 conn: conn
272 } do
273 user = insert(:user)
274 user2 = insert(:user)
275
276 {:ok, _, _, _} = CommonAPI.follow(user, user2)
277 {:ok, _} = CommonAPI.post(user2, %{"status" => "hey @#{user.nickname}"})
278
279 conn = assign(conn, :user, user)
280
281 conn = get(conn, "/api/v1/notifications")
282
283 assert length(json_response(conn, 200)) == 1
284
285 {:ok, user} = User.mute(user, user2)
286
287 conn = assign(build_conn(), :user, user)
288 conn = get(conn, "/api/v1/notifications", %{"with_muted" => "true"})
289
290 assert length(json_response(conn, 200)) == 1
291 end
292
293 defp get_notification_id_by_activity(%{id: id}) do
294 Notification
295 |> Repo.get_by(activity_id: id)
296 |> Map.get(:id)
297 |> to_string()
298 end
299 end