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.

123 lines
4.0KB

  1. defmodule Pleroma.NotificationTest do
  2. use Pleroma.DataCase
  3. alias Pleroma.Web.TwitterAPI.TwitterAPI
  4. alias Pleroma.{User, Notification}
  5. import Pleroma.Factory
  6. describe "create_notifications" do
  7. test "notifies someone when they are directly addressed" do
  8. user = insert(:user)
  9. other_user = insert(:user)
  10. third_user = insert(:user)
  11. {:ok, activity} =
  12. TwitterAPI.create_status(user, %{
  13. "status" => "hey @#{other_user.nickname} and @#{third_user.nickname}"
  14. })
  15. {:ok, [notification, other_notification]} = Notification.create_notifications(activity)
  16. notified_ids = Enum.sort([notification.user_id, other_notification.user_id])
  17. assert notified_ids == [other_user.id, third_user.id]
  18. assert notification.activity_id == activity.id
  19. assert other_notification.activity_id == activity.id
  20. end
  21. end
  22. describe "create_notification" do
  23. test "it doesn't create a notification for user if the user blocks the activity author" do
  24. activity = insert(:note_activity)
  25. author = User.get_by_ap_id(activity.data["actor"])
  26. user = insert(:user)
  27. {:ok, user} = User.block(user, author)
  28. assert nil == Notification.create_notification(activity, user)
  29. end
  30. test "it doesn't create a notification for user if he is the activity author" do
  31. activity = insert(:note_activity)
  32. author = User.get_by_ap_id(activity.data["actor"])
  33. assert nil == Notification.create_notification(activity, author)
  34. end
  35. end
  36. describe "get notification" do
  37. test "it gets a notification that belongs to the user" do
  38. user = insert(:user)
  39. other_user = insert(:user)
  40. {:ok, activity} =
  41. TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
  42. {:ok, [notification]} = Notification.create_notifications(activity)
  43. {:ok, notification} = Notification.get(other_user, notification.id)
  44. assert notification.user_id == other_user.id
  45. end
  46. test "it returns error if the notification doesn't belong to the user" do
  47. user = insert(:user)
  48. other_user = insert(:user)
  49. {:ok, activity} =
  50. TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
  51. {:ok, [notification]} = Notification.create_notifications(activity)
  52. {:error, _notification} = Notification.get(user, notification.id)
  53. end
  54. end
  55. describe "dismiss notification" do
  56. test "it dismisses a notification that belongs to the user" do
  57. user = insert(:user)
  58. other_user = insert(:user)
  59. {:ok, activity} =
  60. TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
  61. {:ok, [notification]} = Notification.create_notifications(activity)
  62. {:ok, notification} = Notification.dismiss(other_user, notification.id)
  63. assert notification.user_id == other_user.id
  64. end
  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. {:ok, activity} =
  69. TwitterAPI.create_status(user, %{"status" => "hey @#{other_user.nickname}"})
  70. {:ok, [notification]} = Notification.create_notifications(activity)
  71. {:error, _notification} = Notification.dismiss(user, notification.id)
  72. end
  73. end
  74. describe "clear notification" do
  75. test "it clears all notifications belonging to the user" do
  76. user = insert(:user)
  77. other_user = insert(:user)
  78. third_user = insert(:user)
  79. {:ok, activity} =
  80. TwitterAPI.create_status(user, %{
  81. "status" => "hey @#{other_user.nickname} and @#{third_user.nickname} !"
  82. })
  83. {:ok, _notifs} = Notification.create_notifications(activity)
  84. {:ok, activity} =
  85. TwitterAPI.create_status(user, %{
  86. "status" => "hey again @#{other_user.nickname} and @#{third_user.nickname} !"
  87. })
  88. {:ok, _notifs} = Notification.create_notifications(activity)
  89. Notification.clear(other_user)
  90. assert Notification.for_user(other_user) == []
  91. assert Notification.for_user(third_user) != []
  92. end
  93. end
  94. end