1 defmodule Pleroma.NotificationTest do
3 alias Pleroma.Web.TwitterAPI.TwitterAPI
4 alias Pleroma.Web.CommonAPI
5 alias Pleroma.{User, Notification}
8 describe "create_notifications" do
9 test "notifies someone when they are directly addressed" do
11 other_user = insert(:user)
12 third_user = insert(:user)
15 TwitterAPI.create_status(user, %{
16 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname}"
19 {:ok, [notification, other_notification]} = Notification.create_notifications(activity)
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
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"])
33 {:ok, user} = User.block(user, author)
35 assert nil == Notification.create_notification(activity, user)
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"])
42 assert nil == Notification.create_notification(activity, author)
46 describe "get notification" do
47 test "it gets a notification that belongs to the user" do
49 other_user = insert(:user)
52 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
54 {:ok, [notification]} = Notification.create_notifications(activity)
55 {:ok, notification} = Notification.get(other_user, notification.id)
57 assert notification.user_id == other_user.id
60 test "it returns error if the notification doesn't belong to the user" do
62 other_user = insert(:user)
65 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
67 {:ok, [notification]} = Notification.create_notifications(activity)
68 {:error, _notification} = Notification.get(user, notification.id)
72 describe "dismiss notification" do
73 test "it dismisses a notification that belongs to the user" do
75 other_user = insert(:user)
78 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
80 {:ok, [notification]} = Notification.create_notifications(activity)
81 {:ok, notification} = Notification.dismiss(other_user, notification.id)
83 assert notification.user_id == other_user.id
86 test "it returns error if the notification doesn't belong to the user" do
88 other_user = insert(:user)
91 TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
93 {:ok, [notification]} = Notification.create_notifications(activity)
94 {:error, _notification} = Notification.dismiss(user, notification.id)
98 describe "clear notification" do
99 test "it clears all notifications belonging to the user" do
101 other_user = insert(:user)
102 third_user = insert(:user)
105 TwitterAPI.create_status(user, %{
106 "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
109 {:ok, _notifs} = Notification.create_notifications(activity)
112 TwitterAPI.create_status(user, %{
113 "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
116 {:ok, _notifs} = Notification.create_notifications(activity)
117 Notification.clear(other_user)
119 assert Notification.for_user(other_user) == []
120 assert Notification.for_user(third_user) != []
124 describe "set_read_up_to()" do
125 test "it sets all notifications as read up to a specified notification ID" do
127 other_user = insert(:user)
130 TwitterAPI.create_status(user, %{
131 "status" => "hey @#{other_user.nickname}!"
135 TwitterAPI.create_status(user, %{
136 "status" => "hey again @#{other_user.nickname}!"
139 [n2, n1] = notifs = Notification.for_user(other_user)
140 assert length(notifs) == 2
145 TwitterAPI.create_status(user, %{
146 "status" => "hey yet again @#{other_user.nickname}!"
149 Notification.set_read_up_to(other_user, n2.id)
151 [n3, n2, n1] = notifs = Notification.for_user(other_user)
153 assert n1.seen == true
154 assert n2.seen == true
155 assert n3.seen == false
159 describe "notification lifecycle" do
160 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
162 other_user = insert(:user)
164 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
166 assert length(Notification.for_user(user)) == 0
168 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
170 assert length(Notification.for_user(user)) == 1
172 {:ok, _} = CommonAPI.delete(activity.id, user)
174 assert length(Notification.for_user(user)) == 0
177 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
179 other_user = insert(:user)
181 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
183 assert length(Notification.for_user(user)) == 0
185 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
187 assert length(Notification.for_user(user)) == 1
189 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
191 assert length(Notification.for_user(user)) == 0
194 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
196 other_user = insert(:user)
198 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
200 assert length(Notification.for_user(user)) == 0
202 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
204 assert length(Notification.for_user(user)) == 1
206 {:ok, _} = CommonAPI.delete(activity.id, user)
208 assert length(Notification.for_user(user)) == 0
211 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
213 other_user = insert(:user)
215 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
217 assert length(Notification.for_user(user)) == 0
219 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
221 assert length(Notification.for_user(user)) == 1
223 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
225 assert length(Notification.for_user(user)) == 0
228 test "liking an activity which is already deleted does not generate a notification" do
230 other_user = insert(:user)
232 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
234 assert length(Notification.for_user(user)) == 0
236 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
238 assert length(Notification.for_user(user)) == 0
240 {:error, _} = CommonAPI.favorite(activity.id, other_user)
242 assert length(Notification.for_user(user)) == 0
245 test "repeating an activity which is already deleted does not generate a notification" do
247 other_user = insert(:user)
249 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
251 assert length(Notification.for_user(user)) == 0
253 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
255 assert length(Notification.for_user(user)) == 0
257 {:error, _} = CommonAPI.repeat(activity.id, other_user)
259 assert length(Notification.for_user(user)) == 0
262 test "replying to a deleted post without tagging does not generate a notification" do
264 other_user = insert(:user)
266 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
267 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
269 {:ok, _reply_activity} =
270 CommonAPI.post(other_user, %{
271 "status" => "test reply",
272 "in_reply_to_status_id" => activity.id
275 assert length(Notification.for_user(user)) == 0