Merge branch 'bugfix/announce-timeline-flooding' into 'develop'
[akkoma] / test / notification_test.exs
1 defmodule Pleroma.NotificationTest do
2 use Pleroma.DataCase
3 alias Pleroma.Web.TwitterAPI.TwitterAPI
4 alias Pleroma.Web.CommonAPI
5 alias Pleroma.{User, Notification}
6 import Pleroma.Factory
7
8 describe "create_notifications" do
9 test "notifies someone when they are directly addressed" do
10 user = insert(:user)
11 other_user = insert(:user)
12 third_user = insert(:user)
13
14 {:ok, activity} =
15 TwitterAPI.create_status(user, %{
16 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname}"
17 })
18
19 {:ok, [notification, other_notification]} = Notification.create_notifications(activity)
20
21 notified_ids = Enum.sort([notification.user_id, other_notification.user_id])
22 assert notified_ids == [other_user.id, third_user.id]
23 assert notification.activity_id == activity.id
24 assert other_notification.activity_id == activity.id
25 end
26 end
27
28 describe "create_notification" do
29 test "it doesn't create a notification for user if the user blocks the activity author" do
30 activity = insert(:note_activity)
31 author = User.get_by_ap_id(activity.data["actor"])
32 user = insert(:user)
33 {:ok, user} = User.block(user, author)
34
35 assert nil == Notification.create_notification(activity, user)
36 end
37
38 test "it doesn't create a notification for user if he is the activity author" do
39 activity = insert(:note_activity)
40 author = User.get_by_ap_id(activity.data["actor"])
41
42 assert nil == Notification.create_notification(activity, author)
43 end
44 end
45
46 describe "get notification" do
47 test "it gets a notification that belongs to the user" do
48 user = insert(:user)
49 other_user = insert(:user)
50
51 {:ok, activity} =
52 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
53
54 {:ok, [notification]} = Notification.create_notifications(activity)
55 {:ok, notification} = Notification.get(other_user, notification.id)
56
57 assert notification.user_id == other_user.id
58 end
59
60 test "it returns error if the notification doesn't belong to the user" do
61 user = insert(:user)
62 other_user = insert(:user)
63
64 {:ok, activity} =
65 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
66
67 {:ok, [notification]} = Notification.create_notifications(activity)
68 {:error, _notification} = Notification.get(user, notification.id)
69 end
70 end
71
72 describe "dismiss notification" do
73 test "it dismisses a notification that belongs to the user" do
74 user = insert(:user)
75 other_user = insert(:user)
76
77 {:ok, activity} =
78 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
79
80 {:ok, [notification]} = Notification.create_notifications(activity)
81 {:ok, notification} = Notification.dismiss(other_user, notification.id)
82
83 assert notification.user_id == other_user.id
84 end
85
86 test "it returns error if the notification doesn't belong to the user" do
87 user = insert(:user)
88 other_user = insert(:user)
89
90 {:ok, activity} =
91 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
92
93 {:ok, [notification]} = Notification.create_notifications(activity)
94 {:error, _notification} = Notification.dismiss(user, notification.id)
95 end
96 end
97
98 describe "clear notification" do
99 test "it clears all notifications belonging to the user" do
100 user = insert(:user)
101 other_user = insert(:user)
102 third_user = insert(:user)
103
104 {:ok, activity} =
105 TwitterAPI.create_status(user, %{
106 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
107 })
108
109 {:ok, _notifs} = Notification.create_notifications(activity)
110
111 {:ok, activity} =
112 TwitterAPI.create_status(user, %{
113 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
114 })
115
116 {:ok, _notifs} = Notification.create_notifications(activity)
117 Notification.clear(other_user)
118
119 assert Notification.for_user(other_user) == []
120 assert Notification.for_user(third_user) != []
121 end
122 end
123
124 describe "notification lifecycle" do
125 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
126 user = insert(:user)
127 other_user = insert(:user)
128
129 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
130
131 assert length(Notification.for_user(user)) == 0
132
133 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
134
135 assert length(Notification.for_user(user)) == 1
136
137 {:ok, _} = CommonAPI.delete(activity.id, user)
138
139 assert length(Notification.for_user(user)) == 0
140 end
141
142 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
143 user = insert(:user)
144 other_user = insert(:user)
145
146 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
147
148 assert length(Notification.for_user(user)) == 0
149
150 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
151
152 assert length(Notification.for_user(user)) == 1
153
154 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
155
156 assert length(Notification.for_user(user)) == 0
157 end
158
159 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
160 user = insert(:user)
161 other_user = insert(:user)
162
163 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
164
165 assert length(Notification.for_user(user)) == 0
166
167 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
168
169 assert length(Notification.for_user(user)) == 1
170
171 {:ok, _} = CommonAPI.delete(activity.id, user)
172
173 assert length(Notification.for_user(user)) == 0
174 end
175
176 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
177 user = insert(:user)
178 other_user = insert(:user)
179
180 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
181
182 assert length(Notification.for_user(user)) == 0
183
184 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
185
186 assert length(Notification.for_user(user)) == 1
187
188 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
189
190 assert length(Notification.for_user(user)) == 0
191 end
192
193 test "liking an activity which is already deleted does not generate a notification" do
194 user = insert(:user)
195 other_user = insert(:user)
196
197 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
198
199 assert length(Notification.for_user(user)) == 0
200
201 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
202
203 assert length(Notification.for_user(user)) == 0
204
205 {:error, _} = CommonAPI.favorite(activity.id, other_user)
206
207 assert length(Notification.for_user(user)) == 0
208 end
209
210 test "repeating an activity which is already deleted does not generate a notification" do
211 user = insert(:user)
212 other_user = insert(:user)
213
214 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
215
216 assert length(Notification.for_user(user)) == 0
217
218 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
219
220 assert length(Notification.for_user(user)) == 0
221
222 {:error, _} = CommonAPI.repeat(activity.id, other_user)
223
224 assert length(Notification.for_user(user)) == 0
225 end
226
227 test "replying to a deleted post without tagging does not generate a notification" do
228 user = insert(:user)
229 other_user = insert(:user)
230
231 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
232 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
233
234 {:ok, _reply_activity} =
235 CommonAPI.post(other_user, %{
236 "status" => "test reply",
237 "in_reply_to_status_id" => activity.id
238 })
239
240 assert length(Notification.for_user(user)) == 0
241 end
242 end
243 end