logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

notification_view_test.exs (8058B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do
  5. use Pleroma.DataCase
  6. alias Pleroma.Activity
  7. alias Pleroma.Chat
  8. alias Pleroma.Chat.MessageReference
  9. alias Pleroma.Notification
  10. alias Pleroma.Object
  11. alias Pleroma.Repo
  12. alias Pleroma.User
  13. alias Pleroma.Web.CommonAPI
  14. alias Pleroma.Web.CommonAPI.Utils
  15. alias Pleroma.Web.MastodonAPI.AccountView
  16. alias Pleroma.Web.MastodonAPI.NotificationView
  17. alias Pleroma.Web.MastodonAPI.StatusView
  18. alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
  19. import Pleroma.Factory
  20. defp test_notifications_rendering(notifications, user, expected_result) do
  21. result = NotificationView.render("index.json", %{notifications: notifications, for: user})
  22. assert expected_result == result
  23. result =
  24. NotificationView.render("index.json", %{
  25. notifications: notifications,
  26. for: user,
  27. relationships: nil
  28. })
  29. assert expected_result == result
  30. end
  31. test "ChatMessage notification" do
  32. user = insert(:user)
  33. recipient = insert(:user)
  34. {:ok, activity} = CommonAPI.post_chat_message(user, recipient, "what's up my dude")
  35. {:ok, [notification]} = Notification.create_notifications(activity)
  36. object = Object.normalize(activity)
  37. chat = Chat.get(recipient.id, user.ap_id)
  38. cm_ref = MessageReference.for_chat_and_object(chat, object)
  39. expected = %{
  40. id: to_string(notification.id),
  41. pleroma: %{is_seen: false, is_muted: false},
  42. type: "pleroma:chat_mention",
  43. account: AccountView.render("show.json", %{user: user, for: recipient}),
  44. chat_message: MessageReferenceView.render("show.json", %{chat_message_reference: cm_ref}),
  45. created_at: Utils.to_masto_date(notification.inserted_at)
  46. }
  47. test_notifications_rendering([notification], recipient, [expected])
  48. end
  49. test "Mention notification" do
  50. user = insert(:user)
  51. mentioned_user = insert(:user)
  52. {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{mentioned_user.nickname}"})
  53. {:ok, [notification]} = Notification.create_notifications(activity)
  54. user = User.get_cached_by_id(user.id)
  55. expected = %{
  56. id: to_string(notification.id),
  57. pleroma: %{is_seen: false, is_muted: false},
  58. type: "mention",
  59. account:
  60. AccountView.render("show.json", %{
  61. user: user,
  62. for: mentioned_user
  63. }),
  64. status: StatusView.render("show.json", %{activity: activity, for: mentioned_user}),
  65. created_at: Utils.to_masto_date(notification.inserted_at)
  66. }
  67. test_notifications_rendering([notification], mentioned_user, [expected])
  68. end
  69. test "Favourite notification" do
  70. user = insert(:user)
  71. another_user = insert(:user)
  72. {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
  73. {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)
  74. {:ok, [notification]} = Notification.create_notifications(favorite_activity)
  75. create_activity = Activity.get_by_id(create_activity.id)
  76. expected = %{
  77. id: to_string(notification.id),
  78. pleroma: %{is_seen: false, is_muted: false},
  79. type: "favourite",
  80. account: AccountView.render("show.json", %{user: another_user, for: user}),
  81. status: StatusView.render("show.json", %{activity: create_activity, for: user}),
  82. created_at: Utils.to_masto_date(notification.inserted_at)
  83. }
  84. test_notifications_rendering([notification], user, [expected])
  85. end
  86. test "Reblog notification" do
  87. user = insert(:user)
  88. another_user = insert(:user)
  89. {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
  90. {:ok, reblog_activity} = CommonAPI.repeat(create_activity.id, another_user)
  91. {:ok, [notification]} = Notification.create_notifications(reblog_activity)
  92. reblog_activity = Activity.get_by_id(create_activity.id)
  93. expected = %{
  94. id: to_string(notification.id),
  95. pleroma: %{is_seen: false, is_muted: false},
  96. type: "reblog",
  97. account: AccountView.render("show.json", %{user: another_user, for: user}),
  98. status: StatusView.render("show.json", %{activity: reblog_activity, for: user}),
  99. created_at: Utils.to_masto_date(notification.inserted_at)
  100. }
  101. test_notifications_rendering([notification], user, [expected])
  102. end
  103. test "Follow notification" do
  104. follower = insert(:user)
  105. followed = insert(:user)
  106. {:ok, follower, followed, _activity} = CommonAPI.follow(follower, followed)
  107. notification = Notification |> Repo.one() |> Repo.preload(:activity)
  108. expected = %{
  109. id: to_string(notification.id),
  110. pleroma: %{is_seen: false, is_muted: false},
  111. type: "follow",
  112. account: AccountView.render("show.json", %{user: follower, for: followed}),
  113. created_at: Utils.to_masto_date(notification.inserted_at)
  114. }
  115. test_notifications_rendering([notification], followed, [expected])
  116. User.perform(:delete, follower)
  117. refute Repo.one(Notification)
  118. end
  119. @tag capture_log: true
  120. test "Move notification" do
  121. old_user = insert(:user)
  122. new_user = insert(:user, also_known_as: [old_user.ap_id])
  123. follower = insert(:user)
  124. old_user_url = old_user.ap_id
  125. body =
  126. File.read!("test/fixtures/users_mock/localhost.json")
  127. |> String.replace("{{nickname}}", old_user.nickname)
  128. |> Jason.encode!()
  129. Tesla.Mock.mock(fn
  130. %{method: :get, url: ^old_user_url} ->
  131. %Tesla.Env{status: 200, body: body}
  132. end)
  133. User.follow(follower, old_user)
  134. Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
  135. Pleroma.Tests.ObanHelpers.perform_all()
  136. old_user = refresh_record(old_user)
  137. new_user = refresh_record(new_user)
  138. [notification] = Notification.for_user(follower)
  139. expected = %{
  140. id: to_string(notification.id),
  141. pleroma: %{is_seen: false, is_muted: false},
  142. type: "move",
  143. account: AccountView.render("show.json", %{user: old_user, for: follower}),
  144. target: AccountView.render("show.json", %{user: new_user, for: follower}),
  145. created_at: Utils.to_masto_date(notification.inserted_at)
  146. }
  147. test_notifications_rendering([notification], follower, [expected])
  148. end
  149. test "EmojiReact notification" do
  150. user = insert(:user)
  151. other_user = insert(:user)
  152. {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
  153. {:ok, _activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
  154. activity = Repo.get(Activity, activity.id)
  155. [notification] = Notification.for_user(user)
  156. assert notification
  157. expected = %{
  158. id: to_string(notification.id),
  159. pleroma: %{is_seen: false, is_muted: false},
  160. type: "pleroma:emoji_reaction",
  161. emoji: "☕",
  162. account: AccountView.render("show.json", %{user: other_user, for: user}),
  163. status: StatusView.render("show.json", %{activity: activity, for: user}),
  164. created_at: Utils.to_masto_date(notification.inserted_at)
  165. }
  166. test_notifications_rendering([notification], user, [expected])
  167. end
  168. test "muted notification" do
  169. user = insert(:user)
  170. another_user = insert(:user)
  171. {:ok, _} = Pleroma.UserRelationship.create_mute(user, another_user)
  172. {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
  173. {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)
  174. {:ok, [notification]} = Notification.create_notifications(favorite_activity)
  175. create_activity = Activity.get_by_id(create_activity.id)
  176. expected = %{
  177. id: to_string(notification.id),
  178. pleroma: %{is_seen: true, is_muted: true},
  179. type: "favourite",
  180. account: AccountView.render("show.json", %{user: another_user, for: user}),
  181. status: StatusView.render("show.json", %{activity: create_activity, for: user}),
  182. created_at: Utils.to_masto_date(notification.inserted_at)
  183. }
  184. test_notifications_rendering([notification], user, [expected])
  185. end
  186. end