Merge branch 'systemd-drop-sysadmin-privilege' into 'develop'
[akkoma] / test / notification_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.NotificationTest do
6 use Pleroma.DataCase
7 alias Pleroma.Web.TwitterAPI.TwitterAPI
8 alias Pleroma.Web.CommonAPI
9 alias Pleroma.{User, Notification}
10 alias Pleroma.Web.ActivityPub.Transmogrifier
11 import Pleroma.Factory
12
13 describe "create_notifications" do
14 test "notifies someone when they are directly addressed" do
15 user = insert(:user)
16 other_user = insert(:user)
17 third_user = insert(:user)
18
19 {:ok, activity} =
20 TwitterAPI.create_status(user, %{
21 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname}"
22 })
23
24 {:ok, [notification, other_notification]} = Notification.create_notifications(activity)
25
26 notified_ids = Enum.sort([notification.user_id, other_notification.user_id])
27 assert notified_ids == [other_user.id, third_user.id]
28 assert notification.activity_id == activity.id
29 assert other_notification.activity_id == activity.id
30 end
31 end
32
33 describe "create_notification" do
34 test "it doesn't create a notification for user if the user blocks the activity author" do
35 activity = insert(:note_activity)
36 author = User.get_by_ap_id(activity.data["actor"])
37 user = insert(:user)
38 {:ok, user} = User.block(user, author)
39
40 assert nil == Notification.create_notification(activity, user)
41 end
42
43 test "it doesn't create a notification for user if he is the activity author" do
44 activity = insert(:note_activity)
45 author = User.get_by_ap_id(activity.data["actor"])
46
47 assert nil == Notification.create_notification(activity, author)
48 end
49 end
50
51 describe "get notification" do
52 test "it gets a notification that belongs to the user" do
53 user = insert(:user)
54 other_user = insert(:user)
55
56 {:ok, activity} =
57 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
58
59 {:ok, [notification]} = Notification.create_notifications(activity)
60 {:ok, notification} = Notification.get(other_user, notification.id)
61
62 assert notification.user_id == other_user.id
63 end
64
65 test "it returns error if the notification doesn't belong to the user" do
66 user = insert(:user)
67 other_user = insert(:user)
68
69 {:ok, activity} =
70 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
71
72 {:ok, [notification]} = Notification.create_notifications(activity)
73 {:error, _notification} = Notification.get(user, notification.id)
74 end
75 end
76
77 describe "dismiss notification" do
78 test "it dismisses a notification that belongs to the user" do
79 user = insert(:user)
80 other_user = insert(:user)
81
82 {:ok, activity} =
83 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
84
85 {:ok, [notification]} = Notification.create_notifications(activity)
86 {:ok, notification} = Notification.dismiss(other_user, notification.id)
87
88 assert notification.user_id == other_user.id
89 end
90
91 test "it returns error if the notification doesn't belong to the user" do
92 user = insert(:user)
93 other_user = insert(:user)
94
95 {:ok, activity} =
96 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
97
98 {:ok, [notification]} = Notification.create_notifications(activity)
99 {:error, _notification} = Notification.dismiss(user, notification.id)
100 end
101 end
102
103 describe "clear notification" do
104 test "it clears all notifications belonging to the user" do
105 user = insert(:user)
106 other_user = insert(:user)
107 third_user = insert(:user)
108
109 {:ok, activity} =
110 TwitterAPI.create_status(user, %{
111 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
112 })
113
114 {:ok, _notifs} = Notification.create_notifications(activity)
115
116 {:ok, activity} =
117 TwitterAPI.create_status(user, %{
118 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
119 })
120
121 {:ok, _notifs} = Notification.create_notifications(activity)
122 Notification.clear(other_user)
123
124 assert Notification.for_user(other_user) == []
125 assert Notification.for_user(third_user) != []
126 end
127 end
128
129 describe "set_read_up_to()" do
130 test "it sets all notifications as read up to a specified notification ID" do
131 user = insert(:user)
132 other_user = insert(:user)
133
134 {:ok, _activity} =
135 TwitterAPI.create_status(user, %{
136 "status" => "hey @#{other_user.nickname}!"
137 })
138
139 {:ok, _activity} =
140 TwitterAPI.create_status(user, %{
141 "status" => "hey again @#{other_user.nickname}!"
142 })
143
144 [n2, n1] = notifs = Notification.for_user(other_user)
145 assert length(notifs) == 2
146
147 assert n2.id > n1.id
148
149 {:ok, _activity} =
150 TwitterAPI.create_status(user, %{
151 "status" => "hey yet again @#{other_user.nickname}!"
152 })
153
154 Notification.set_read_up_to(other_user, n2.id)
155
156 [n3, n2, n1] = Notification.for_user(other_user)
157
158 assert n1.seen == true
159 assert n2.seen == true
160 assert n3.seen == false
161 end
162 end
163
164 describe "notification target determination" do
165 test "it sends notifications to addressed users in new messages" do
166 user = insert(:user)
167 other_user = insert(:user)
168
169 {:ok, activity} =
170 CommonAPI.post(user, %{
171 "status" => "hey @#{other_user.nickname}!"
172 })
173
174 assert other_user in Notification.get_notified_from_activity(activity)
175 end
176
177 test "it sends notifications to mentioned users in new messages" do
178 user = insert(:user)
179 other_user = insert(:user)
180
181 create_activity = %{
182 "@context" => "https://www.w3.org/ns/activitystreams",
183 "type" => "Create",
184 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
185 "actor" => user.ap_id,
186 "object" => %{
187 "type" => "Note",
188 "content" => "message with a Mention tag, but no explicit tagging",
189 "tag" => [
190 %{
191 "type" => "Mention",
192 "href" => other_user.ap_id,
193 "name" => other_user.nickname
194 }
195 ],
196 "attributedTo" => user.ap_id
197 }
198 }
199
200 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
201
202 assert other_user in Notification.get_notified_from_activity(activity)
203 end
204
205 test "it does not send notifications to users who are only cc in new messages" do
206 user = insert(:user)
207 other_user = insert(:user)
208
209 create_activity = %{
210 "@context" => "https://www.w3.org/ns/activitystreams",
211 "type" => "Create",
212 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
213 "cc" => [other_user.ap_id],
214 "actor" => user.ap_id,
215 "object" => %{
216 "type" => "Note",
217 "content" => "hi everyone",
218 "attributedTo" => user.ap_id
219 }
220 }
221
222 {:ok, activity} = Transmogrifier.handle_incoming(create_activity)
223
224 assert other_user not in Notification.get_notified_from_activity(activity)
225 end
226
227 test "it does not send notification to mentioned users in likes" do
228 user = insert(:user)
229 other_user = insert(:user)
230 third_user = insert(:user)
231
232 {:ok, activity_one} =
233 CommonAPI.post(user, %{
234 "status" => "hey @#{other_user.nickname}!"
235 })
236
237 {:ok, activity_two, _} = CommonAPI.favorite(activity_one.id, third_user)
238
239 assert other_user not in Notification.get_notified_from_activity(activity_two)
240 end
241
242 test "it does not send notification to mentioned users in announces" do
243 user = insert(:user)
244 other_user = insert(:user)
245 third_user = insert(:user)
246
247 {:ok, activity_one} =
248 CommonAPI.post(user, %{
249 "status" => "hey @#{other_user.nickname}!"
250 })
251
252 {:ok, activity_two, _} = CommonAPI.repeat(activity_one.id, third_user)
253
254 assert other_user not in Notification.get_notified_from_activity(activity_two)
255 end
256 end
257
258 describe "notification lifecycle" do
259 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
260 user = insert(:user)
261 other_user = insert(:user)
262
263 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
264
265 assert length(Notification.for_user(user)) == 0
266
267 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
268
269 assert length(Notification.for_user(user)) == 1
270
271 {:ok, _} = CommonAPI.delete(activity.id, user)
272
273 assert length(Notification.for_user(user)) == 0
274 end
275
276 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
277 user = insert(:user)
278 other_user = insert(:user)
279
280 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
281
282 assert length(Notification.for_user(user)) == 0
283
284 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
285
286 assert length(Notification.for_user(user)) == 1
287
288 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
289
290 assert length(Notification.for_user(user)) == 0
291 end
292
293 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
294 user = insert(:user)
295 other_user = insert(:user)
296
297 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
298
299 assert length(Notification.for_user(user)) == 0
300
301 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
302
303 assert length(Notification.for_user(user)) == 1
304
305 {:ok, _} = CommonAPI.delete(activity.id, user)
306
307 assert length(Notification.for_user(user)) == 0
308 end
309
310 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
311 user = insert(:user)
312 other_user = insert(:user)
313
314 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
315
316 assert length(Notification.for_user(user)) == 0
317
318 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
319
320 assert length(Notification.for_user(user)) == 1
321
322 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
323
324 assert length(Notification.for_user(user)) == 0
325 end
326
327 test "liking an activity which is already deleted does not generate a notification" do
328 user = insert(:user)
329 other_user = insert(:user)
330
331 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
332
333 assert length(Notification.for_user(user)) == 0
334
335 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
336
337 assert length(Notification.for_user(user)) == 0
338
339 {:error, _} = CommonAPI.favorite(activity.id, other_user)
340
341 assert length(Notification.for_user(user)) == 0
342 end
343
344 test "repeating an activity which is already deleted does not generate a notification" do
345 user = insert(:user)
346 other_user = insert(:user)
347
348 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
349
350 assert length(Notification.for_user(user)) == 0
351
352 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
353
354 assert length(Notification.for_user(user)) == 0
355
356 {:error, _} = CommonAPI.repeat(activity.id, other_user)
357
358 assert length(Notification.for_user(user)) == 0
359 end
360
361 test "replying to a deleted post without tagging does not generate a notification" do
362 user = insert(:user)
363 other_user = insert(:user)
364
365 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
366 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
367
368 {:ok, _reply_activity} =
369 CommonAPI.post(other_user, %{
370 "status" => "test reply",
371 "in_reply_to_status_id" => activity.id
372 })
373
374 assert length(Notification.for_user(user)) == 0
375 end
376 end
377 end