1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do
10 alias Pleroma.Notification
14 alias Pleroma.Web.CommonAPI
15 alias Pleroma.Web.CommonAPI.Utils
16 alias Pleroma.Web.MastodonAPI.AccountView
17 alias Pleroma.Web.MastodonAPI.NotificationView
18 alias Pleroma.Web.MastodonAPI.StatusView
19 alias Pleroma.Web.PleromaAPI.ChatMessageView
20 import Pleroma.Factory
22 defp test_notifications_rendering(notifications, user, expected_result) do
23 result = NotificationView.render("index.json", %{notifications: notifications, for: user})
25 assert expected_result == result
28 NotificationView.render("index.json", %{
29 notifications: notifications,
34 assert expected_result == result
37 test "ChatMessage notification" do
39 recipient = insert(:user)
40 {:ok, activity} = CommonAPI.post_chat_message(user, recipient, "what's up my dude")
42 {:ok, [notification]} = Notification.create_notifications(activity)
44 object = Object.normalize(activity)
45 chat = Chat.get(recipient.id, user.ap_id)
48 id: to_string(notification.id),
49 pleroma: %{is_seen: false},
50 type: "pleroma:chat_mention",
51 account: AccountView.render("show.json", %{user: user, for: recipient}),
53 ChatMessageView.render("show.json", %{object: object, for: recipient, chat: chat}),
54 created_at: Utils.to_masto_date(notification.inserted_at)
57 test_notifications_rendering([notification], recipient, [expected])
60 test "Mention notification" do
62 mentioned_user = insert(:user)
63 {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{mentioned_user.nickname}"})
64 {:ok, [notification]} = Notification.create_notifications(activity)
65 user = User.get_cached_by_id(user.id)
68 id: to_string(notification.id),
69 pleroma: %{is_seen: false},
72 AccountView.render("show.json", %{
76 status: StatusView.render("show.json", %{activity: activity, for: mentioned_user}),
77 created_at: Utils.to_masto_date(notification.inserted_at)
80 test_notifications_rendering([notification], mentioned_user, [expected])
83 test "Favourite notification" do
85 another_user = insert(:user)
86 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
87 {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)
88 {:ok, [notification]} = Notification.create_notifications(favorite_activity)
89 create_activity = Activity.get_by_id(create_activity.id)
92 id: to_string(notification.id),
93 pleroma: %{is_seen: false},
95 account: AccountView.render("show.json", %{user: another_user, for: user}),
96 status: StatusView.render("show.json", %{activity: create_activity, for: user}),
97 created_at: Utils.to_masto_date(notification.inserted_at)
100 test_notifications_rendering([notification], user, [expected])
103 test "Reblog notification" do
105 another_user = insert(:user)
106 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
107 {:ok, reblog_activity} = CommonAPI.repeat(create_activity.id, another_user)
108 {:ok, [notification]} = Notification.create_notifications(reblog_activity)
109 reblog_activity = Activity.get_by_id(create_activity.id)
112 id: to_string(notification.id),
113 pleroma: %{is_seen: false},
115 account: AccountView.render("show.json", %{user: another_user, for: user}),
116 status: StatusView.render("show.json", %{activity: reblog_activity, for: user}),
117 created_at: Utils.to_masto_date(notification.inserted_at)
120 test_notifications_rendering([notification], user, [expected])
123 test "Follow notification" do
124 follower = insert(:user)
125 followed = insert(:user)
126 {:ok, follower, followed, _activity} = CommonAPI.follow(follower, followed)
127 notification = Notification |> Repo.one() |> Repo.preload(:activity)
130 id: to_string(notification.id),
131 pleroma: %{is_seen: false},
133 account: AccountView.render("show.json", %{user: follower, for: followed}),
134 created_at: Utils.to_masto_date(notification.inserted_at)
137 test_notifications_rendering([notification], followed, [expected])
139 User.perform(:delete, follower)
140 notification = Notification |> Repo.one() |> Repo.preload(:activity)
142 test_notifications_rendering([notification], followed, [])
145 @tag capture_log: true
146 test "Move notification" do
147 old_user = insert(:user)
148 new_user = insert(:user, also_known_as: [old_user.ap_id])
149 follower = insert(:user)
151 old_user_url = old_user.ap_id
154 File.read!("test/fixtures/users_mock/localhost.json")
155 |> String.replace("{{nickname}}", old_user.nickname)
159 %{method: :get, url: ^old_user_url} ->
160 %Tesla.Env{status: 200, body: body}
163 User.follow(follower, old_user)
164 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
165 Pleroma.Tests.ObanHelpers.perform_all()
167 old_user = refresh_record(old_user)
168 new_user = refresh_record(new_user)
170 [notification] = Notification.for_user(follower)
173 id: to_string(notification.id),
174 pleroma: %{is_seen: false},
176 account: AccountView.render("show.json", %{user: old_user, for: follower}),
177 target: AccountView.render("show.json", %{user: new_user, for: follower}),
178 created_at: Utils.to_masto_date(notification.inserted_at)
181 test_notifications_rendering([notification], follower, [expected])
184 test "EmojiReact notification" do
186 other_user = insert(:user)
188 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
189 {:ok, _activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
191 activity = Repo.get(Activity, activity.id)
193 [notification] = Notification.for_user(user)
198 id: to_string(notification.id),
199 pleroma: %{is_seen: false},
200 type: "pleroma:emoji_reaction",
202 account: AccountView.render("show.json", %{user: other_user, for: user}),
203 status: StatusView.render("show.json", %{activity: activity, for: user}),
204 created_at: Utils.to_masto_date(notification.inserted_at)
207 test_notifications_rendering([notification], user, [expected])