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},
71 account: AccountView.render("show.json", %{user: user, for: mentioned_user}),
72 status: StatusView.render("show.json", %{activity: activity, for: mentioned_user}),
73 created_at: Utils.to_masto_date(notification.inserted_at)
76 test_notifications_rendering([notification], mentioned_user, [expected])
79 test "Favourite notification" do
81 another_user = insert(:user)
82 {:ok, create_activity} = CommonAPI.post(user, %{"status" => "hey"})
83 {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)
84 {:ok, [notification]} = Notification.create_notifications(favorite_activity)
85 create_activity = Activity.get_by_id(create_activity.id)
88 id: to_string(notification.id),
89 pleroma: %{is_seen: false},
91 account: AccountView.render("show.json", %{user: another_user, for: user}),
92 status: StatusView.render("show.json", %{activity: create_activity, for: user}),
93 created_at: Utils.to_masto_date(notification.inserted_at)
96 test_notifications_rendering([notification], user, [expected])
99 test "Reblog notification" do
101 another_user = insert(:user)
102 {:ok, create_activity} = CommonAPI.post(user, %{"status" => "hey"})
103 {:ok, reblog_activity, _object} = CommonAPI.repeat(create_activity.id, another_user)
104 {:ok, [notification]} = Notification.create_notifications(reblog_activity)
105 reblog_activity = Activity.get_by_id(create_activity.id)
108 id: to_string(notification.id),
109 pleroma: %{is_seen: false},
111 account: AccountView.render("show.json", %{user: another_user, for: user}),
112 status: StatusView.render("show.json", %{activity: reblog_activity, for: user}),
113 created_at: Utils.to_masto_date(notification.inserted_at)
116 test_notifications_rendering([notification], user, [expected])
119 test "Follow notification" do
120 follower = insert(:user)
121 followed = insert(:user)
122 {:ok, follower, followed, _activity} = CommonAPI.follow(follower, followed)
123 notification = Notification |> Repo.one() |> Repo.preload(:activity)
126 id: to_string(notification.id),
127 pleroma: %{is_seen: false},
129 account: AccountView.render("show.json", %{user: follower, for: followed}),
130 created_at: Utils.to_masto_date(notification.inserted_at)
133 test_notifications_rendering([notification], followed, [expected])
135 User.perform(:delete, follower)
136 notification = Notification |> Repo.one() |> Repo.preload(:activity)
138 test_notifications_rendering([notification], followed, [])
141 @tag capture_log: true
142 test "Move notification" do
143 old_user = insert(:user)
144 new_user = insert(:user, also_known_as: [old_user.ap_id])
145 follower = insert(:user)
147 old_user_url = old_user.ap_id
150 File.read!("test/fixtures/users_mock/localhost.json")
151 |> String.replace("{{nickname}}", old_user.nickname)
155 %{method: :get, url: ^old_user_url} ->
156 %Tesla.Env{status: 200, body: body}
159 User.follow(follower, old_user)
160 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
161 Pleroma.Tests.ObanHelpers.perform_all()
163 old_user = refresh_record(old_user)
164 new_user = refresh_record(new_user)
166 [notification] = Notification.for_user(follower)
169 id: to_string(notification.id),
170 pleroma: %{is_seen: false},
172 account: AccountView.render("show.json", %{user: old_user, for: follower}),
173 target: AccountView.render("show.json", %{user: new_user, for: follower}),
174 created_at: Utils.to_masto_date(notification.inserted_at)
177 test_notifications_rendering([notification], follower, [expected])
180 test "EmojiReact notification" do
182 other_user = insert(:user)
184 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
185 {:ok, _activity, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
187 activity = Repo.get(Activity, activity.id)
189 [notification] = Notification.for_user(user)
194 id: to_string(notification.id),
195 pleroma: %{is_seen: false},
196 type: "pleroma:emoji_reaction",
198 account: AccountView.render("show.json", %{user: other_user, for: user}),
199 status: StatusView.render("show.json", %{activity: activity, for: user}),
200 created_at: Utils.to_masto_date(notification.inserted_at)
203 test_notifications_rendering([notification], user, [expected])