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 "notification lifecycle" do
125 test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
127 other_user = insert(:user)
129 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
131 assert length(Notification.for_user(user)) == 0
133 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
135 assert length(Notification.for_user(user)) == 1
137 {:ok, _} = CommonAPI.delete(activity.id, user)
139 assert length(Notification.for_user(user)) == 0
142 test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
144 other_user = insert(:user)
146 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
148 assert length(Notification.for_user(user)) == 0
150 {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
152 assert length(Notification.for_user(user)) == 1
154 {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
156 assert length(Notification.for_user(user)) == 0
159 test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
161 other_user = insert(:user)
163 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
165 assert length(Notification.for_user(user)) == 0
167 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
169 assert length(Notification.for_user(user)) == 1
171 {:ok, _} = CommonAPI.delete(activity.id, user)
173 assert length(Notification.for_user(user)) == 0
176 test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
178 other_user = insert(:user)
180 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
182 assert length(Notification.for_user(user)) == 0
184 {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
186 assert length(Notification.for_user(user)) == 1
188 {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
190 assert length(Notification.for_user(user)) == 0
193 test "liking an activity which is already deleted does not generate a notification" do
195 other_user = insert(:user)
197 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
199 assert length(Notification.for_user(user)) == 0
201 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
203 assert length(Notification.for_user(user)) == 0
205 {:error, _} = CommonAPI.favorite(activity.id, other_user)
207 assert length(Notification.for_user(user)) == 0
210 test "repeating an activity which is already deleted does not generate a notification" do
212 other_user = insert(:user)
214 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
216 assert length(Notification.for_user(user)) == 0
218 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
220 assert length(Notification.for_user(user)) == 0
222 {:error, _} = CommonAPI.repeat(activity.id, other_user)
224 assert length(Notification.for_user(user)) == 0
227 test "replying to a deleted post without tagging does not generate a notification" do
229 other_user = insert(:user)
231 {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
232 {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
234 {:ok, _reply_activity} =
235 CommonAPI.post(other_user, %{
236 "status" => "test reply",
237 "in_reply_to_status_id" => activity.id
240 assert length(Notification.for_user(user)) == 0