Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
7.7KB

  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. describe "create_notifications" do
  8. test "notifies someone when they are directly addressed" do
  9. user = insert(:user)
  10. other_user = insert(:user)
  11. third_user = insert(:user)
  12. {:ok, activity} =
  13. TwitterAPI.create_status(user, %{
  14. "status" => "hey @#{other_user.nickname} and @#{third_user.nickname}"
  15. })
  16. {:ok, [notification, other_notification]} = Notification.create_notifications(activity)
  17. notified_ids = Enum.sort([notification.user_id, other_notification.user_id])
  18. assert notified_ids == [other_user.id, third_user.id]
  19. assert notification.activity_id == activity.id
  20. assert other_notification.activity_id == activity.id
  21. end
  22. end
  23. describe "create_notification" do
  24. test "it doesn't create a notification for user if the user blocks the activity author" do
  25. activity = insert(:note_activity)
  26. author = User.get_by_ap_id(activity.data["actor"])
  27. user = insert(:user)
  28. {:ok, user} = User.block(user, author)
  29. assert nil == Notification.create_notification(activity, user)
  30. end
  31. test "it doesn't create a notification for user if he is the activity author" do
  32. activity = insert(:note_activity)
  33. author = User.get_by_ap_id(activity.data["actor"])
  34. assert nil == Notification.create_notification(activity, author)
  35. end
  36. end
  37. describe "get notification" do
  38. test "it gets a notification that belongs to the user" do
  39. user = insert(:user)
  40. other_user = insert(:user)
  41. {:ok, activity} =
  42. TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
  43. {:ok, [notification]} = Notification.create_notifications(activity)
  44. {:ok, notification} = Notification.get(other_user, notification.id)
  45. assert notification.user_id == other_user.id
  46. end
  47. test "it returns error if the notification doesn't belong to the user" do
  48. user = insert(:user)
  49. other_user = insert(:user)
  50. {:ok, activity} =
  51. TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
  52. {:ok, [notification]} = Notification.create_notifications(activity)
  53. {:error, _notification} = Notification.get(user, notification.id)
  54. end
  55. end
  56. describe "dismiss notification" do
  57. test "it dismisses a notification that belongs to the user" do
  58. user = insert(:user)
  59. other_user = insert(:user)
  60. {:ok, activity} =
  61. TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
  62. {:ok, [notification]} = Notification.create_notifications(activity)
  63. {:ok, notification} = Notification.dismiss(other_user, notification.id)
  64. assert notification.user_id == other_user.id
  65. end
  66. test "it returns error if the notification doesn't belong to the user" do
  67. user = insert(:user)
  68. other_user = insert(:user)
  69. {:ok, activity} =
  70. TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
  71. {:ok, [notification]} = Notification.create_notifications(activity)
  72. {:error, _notification} = Notification.dismiss(user, notification.id)
  73. end
  74. end
  75. describe "clear notification" do
  76. test "it clears all notifications belonging to the user" do
  77. user = insert(:user)
  78. other_user = insert(:user)
  79. third_user = insert(:user)
  80. {:ok, activity} =
  81. TwitterAPI.create_status(user, %{
  82. "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
  83. })
  84. {:ok, _notifs} = Notification.create_notifications(activity)
  85. {:ok, activity} =
  86. TwitterAPI.create_status(user, %{
  87. "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
  88. })
  89. {:ok, _notifs} = Notification.create_notifications(activity)
  90. Notification.clear(other_user)
  91. assert Notification.for_user(other_user) == []
  92. assert Notification.for_user(third_user) != []
  93. end
  94. end
  95. describe "notification lifecycle" do
  96. test "liking an activity results in 1 notification, then 0 if the activity is deleted" do
  97. user = insert(:user)
  98. other_user = insert(:user)
  99. {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
  100. assert length(Notification.for_user(user)) == 0
  101. {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
  102. assert length(Notification.for_user(user)) == 1
  103. {:ok, _} = CommonAPI.delete(activity.id, user)
  104. assert length(Notification.for_user(user)) == 0
  105. end
  106. test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
  107. user = insert(:user)
  108. other_user = insert(:user)
  109. {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
  110. assert length(Notification.for_user(user)) == 0
  111. {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
  112. assert length(Notification.for_user(user)) == 1
  113. {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
  114. assert length(Notification.for_user(user)) == 0
  115. end
  116. test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
  117. user = insert(:user)
  118. other_user = insert(:user)
  119. {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
  120. assert length(Notification.for_user(user)) == 0
  121. {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
  122. assert length(Notification.for_user(user)) == 1
  123. {:ok, _} = CommonAPI.delete(activity.id, user)
  124. assert length(Notification.for_user(user)) == 0
  125. end
  126. test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
  127. user = insert(:user)
  128. other_user = insert(:user)
  129. {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
  130. assert length(Notification.for_user(user)) == 0
  131. {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
  132. assert length(Notification.for_user(user)) == 1
  133. {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
  134. assert length(Notification.for_user(user)) == 0
  135. end
  136. test "liking an activity which is already deleted does not generate a notification" do
  137. user = insert(:user)
  138. other_user = insert(:user)
  139. {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
  140. assert length(Notification.for_user(user)) == 0
  141. {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
  142. assert length(Notification.for_user(user)) == 0
  143. {:error, _} = CommonAPI.favorite(activity.id, other_user)
  144. assert length(Notification.for_user(user)) == 0
  145. end
  146. test "repeating an activity which is already deleted does not generate a notification" do
  147. user = insert(:user)
  148. other_user = insert(:user)
  149. {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
  150. assert length(Notification.for_user(user)) == 0
  151. {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
  152. assert length(Notification.for_user(user)) == 0
  153. {:error, _} = CommonAPI.repeat(activity.id, other_user)
  154. assert length(Notification.for_user(user)) == 0
  155. end
  156. test "replying to a deleted post without tagging does not generate a notification" do
  157. user = insert(:user)
  158. other_user = insert(:user)
  159. {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
  160. {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
  161. {:ok, _reply_activity} =
  162. CommonAPI.post(other_user, %{
  163. "status" => "test reply",
  164. "in_reply_to_status_id" => activity.id
  165. })
  166. assert length(Notification.for_user(user)) == 0
  167. end
  168. end
  169. end