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.ChatMessageReference
11 alias Pleroma.Notification
15 alias Pleroma.Web.CommonAPI
16 alias Pleroma.Web.CommonAPI.Utils
17 alias Pleroma.Web.MastodonAPI.AccountView
18 alias Pleroma.Web.MastodonAPI.NotificationView
19 alias Pleroma.Web.MastodonAPI.StatusView
20 alias Pleroma.Web.PleromaAPI.ChatMessageReferenceView
21 import Pleroma.Factory
23 defp test_notifications_rendering(notifications, user, expected_result) do
24 result = NotificationView.render("index.json", %{notifications: notifications, for: user})
26 assert expected_result == result
29 NotificationView.render("index.json", %{
30 notifications: notifications,
35 assert expected_result == result
38 test "ChatMessage notification" do
40 recipient = insert(:user)
41 {:ok, activity} = CommonAPI.post_chat_message(user, recipient, "what's up my dude")
43 {:ok, [notification]} = Notification.create_notifications(activity)
45 object = Object.normalize(activity)
46 chat = Chat.get(recipient.id, user.ap_id)
48 cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
51 id: to_string(notification.id),
52 pleroma: %{is_seen: false},
53 type: "pleroma:chat_mention",
54 account: AccountView.render("show.json", %{user: user, for: recipient}),
56 ChatMessageReferenceView.render("show.json", %{chat_message_reference: cm_ref}),
57 created_at: Utils.to_masto_date(notification.inserted_at)
60 test_notifications_rendering([notification], recipient, [expected])
63 test "Mention notification" do
65 mentioned_user = insert(:user)
66 {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{mentioned_user.nickname}"})
67 {:ok, [notification]} = Notification.create_notifications(activity)
68 user = User.get_cached_by_id(user.id)
71 id: to_string(notification.id),
72 pleroma: %{is_seen: false},
75 AccountView.render("show.json", %{
79 status: StatusView.render("show.json", %{activity: activity, for: mentioned_user}),
80 created_at: Utils.to_masto_date(notification.inserted_at)
83 test_notifications_rendering([notification], mentioned_user, [expected])
86 test "Favourite notification" do
88 another_user = insert(:user)
89 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
90 {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)
91 {:ok, [notification]} = Notification.create_notifications(favorite_activity)
92 create_activity = Activity.get_by_id(create_activity.id)
95 id: to_string(notification.id),
96 pleroma: %{is_seen: false},
98 account: AccountView.render("show.json", %{user: another_user, for: user}),
99 status: StatusView.render("show.json", %{activity: create_activity, for: user}),
100 created_at: Utils.to_masto_date(notification.inserted_at)
103 test_notifications_rendering([notification], user, [expected])
106 test "Reblog notification" do
108 another_user = insert(:user)
109 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
110 {:ok, reblog_activity} = CommonAPI.repeat(create_activity.id, another_user)
111 {:ok, [notification]} = Notification.create_notifications(reblog_activity)
112 reblog_activity = Activity.get_by_id(create_activity.id)
115 id: to_string(notification.id),
116 pleroma: %{is_seen: false},
118 account: AccountView.render("show.json", %{user: another_user, for: user}),
119 status: StatusView.render("show.json", %{activity: reblog_activity, for: user}),
120 created_at: Utils.to_masto_date(notification.inserted_at)
123 test_notifications_rendering([notification], user, [expected])
126 test "Follow notification" do
127 follower = insert(:user)
128 followed = insert(:user)
129 {:ok, follower, followed, _activity} = CommonAPI.follow(follower, followed)
130 notification = Notification |> Repo.one() |> Repo.preload(:activity)
133 id: to_string(notification.id),
134 pleroma: %{is_seen: false},
136 account: AccountView.render("show.json", %{user: follower, for: followed}),
137 created_at: Utils.to_masto_date(notification.inserted_at)
140 test_notifications_rendering([notification], followed, [expected])
142 User.perform(:delete, follower)
143 notification = Notification |> Repo.one() |> Repo.preload(:activity)
145 test_notifications_rendering([notification], followed, [])
148 @tag capture_log: true
149 test "Move notification" do
150 old_user = insert(:user)
151 new_user = insert(:user, also_known_as: [old_user.ap_id])
152 follower = insert(:user)
154 old_user_url = old_user.ap_id
157 File.read!("test/fixtures/users_mock/localhost.json")
158 |> String.replace("{{nickname}}", old_user.nickname)
162 %{method: :get, url: ^old_user_url} ->
163 %Tesla.Env{status: 200, body: body}
166 User.follow(follower, old_user)
167 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
168 Pleroma.Tests.ObanHelpers.perform_all()
170 old_user = refresh_record(old_user)
171 new_user = refresh_record(new_user)
173 [notification] = Notification.for_user(follower)
176 id: to_string(notification.id),
177 pleroma: %{is_seen: false},
179 account: AccountView.render("show.json", %{user: old_user, for: follower}),
180 target: AccountView.render("show.json", %{user: new_user, for: follower}),
181 created_at: Utils.to_masto_date(notification.inserted_at)
184 test_notifications_rendering([notification], follower, [expected])
187 test "EmojiReact notification" do
189 other_user = insert(:user)
191 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
192 {:ok, _activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
194 activity = Repo.get(Activity, activity.id)
196 [notification] = Notification.for_user(user)
201 id: to_string(notification.id),
202 pleroma: %{is_seen: false},
203 type: "pleroma:emoji_reaction",
205 account: AccountView.render("show.json", %{user: other_user, for: user}),
206 status: StatusView.render("show.json", %{activity: activity, for: user}),
207 created_at: Utils.to_masto_date(notification.inserted_at)
210 test_notifications_rendering([notification], user, [expected])