8db6a237944e25e3260a4df96011c74214464ea0
[akkoma] / test / web / mastodon_api / views / notification_view_test.exs
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
5 defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do
6 use Pleroma.DataCase
7
8 alias Pleroma.Activity
9 alias Pleroma.Chat
10 alias Pleroma.Notification
11 alias Pleroma.Object
12 alias Pleroma.Repo
13 alias Pleroma.User
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
21
22 defp test_notifications_rendering(notifications, user, expected_result) do
23 result = NotificationView.render("index.json", %{notifications: notifications, for: user})
24
25 assert expected_result == result
26
27 result =
28 NotificationView.render("index.json", %{
29 notifications: notifications,
30 for: user,
31 relationships: nil
32 })
33
34 assert expected_result == result
35 end
36
37 test "ChatMessage notification" do
38 user = insert(:user)
39 recipient = insert(:user)
40 {:ok, activity} = CommonAPI.post_chat_message(user, recipient, "what's up my dude")
41
42 {:ok, [notification]} = Notification.create_notifications(activity)
43
44 object = Object.normalize(activity)
45 chat = Chat.get(recipient.id, user.ap_id)
46
47 expected = %{
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}),
52 chat_message:
53 ChatMessageView.render("show.json", %{object: object, for: recipient, chat: chat}),
54 created_at: Utils.to_masto_date(notification.inserted_at)
55 }
56
57 test_notifications_rendering([notification], recipient, [expected])
58 end
59
60 test "Mention notification" do
61 user = insert(:user)
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)
66
67 expected = %{
68 id: to_string(notification.id),
69 pleroma: %{is_seen: false},
70 type: "mention",
71 account:
72 AccountView.render("show.json", %{
73 user: user,
74 for: mentioned_user
75 }),
76 status: StatusView.render("show.json", %{activity: activity, for: mentioned_user}),
77 created_at: Utils.to_masto_date(notification.inserted_at)
78 }
79
80 test_notifications_rendering([notification], mentioned_user, [expected])
81 end
82
83 test "Favourite notification" do
84 user = insert(:user)
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)
90
91 expected = %{
92 id: to_string(notification.id),
93 pleroma: %{is_seen: false},
94 type: "favourite",
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)
98 }
99
100 test_notifications_rendering([notification], user, [expected])
101 end
102
103 test "Reblog notification" do
104 user = insert(:user)
105 another_user = insert(:user)
106 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
107 {:ok, reblog_activity, _object} = 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)
110
111 expected = %{
112 id: to_string(notification.id),
113 pleroma: %{is_seen: false},
114 type: "reblog",
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)
118 }
119
120 test_notifications_rendering([notification], user, [expected])
121 end
122
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)
128
129 expected = %{
130 id: to_string(notification.id),
131 pleroma: %{is_seen: false},
132 type: "follow",
133 account: AccountView.render("show.json", %{user: follower, for: followed}),
134 created_at: Utils.to_masto_date(notification.inserted_at)
135 }
136
137 test_notifications_rendering([notification], followed, [expected])
138
139 User.perform(:delete, follower)
140 notification = Notification |> Repo.one() |> Repo.preload(:activity)
141
142 test_notifications_rendering([notification], followed, [])
143 end
144
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)
150
151 old_user_url = old_user.ap_id
152
153 body =
154 File.read!("test/fixtures/users_mock/localhost.json")
155 |> String.replace("{{nickname}}", old_user.nickname)
156 |> Jason.encode!()
157
158 Tesla.Mock.mock(fn
159 %{method: :get, url: ^old_user_url} ->
160 %Tesla.Env{status: 200, body: body}
161 end)
162
163 User.follow(follower, old_user)
164 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
165 Pleroma.Tests.ObanHelpers.perform_all()
166
167 old_user = refresh_record(old_user)
168 new_user = refresh_record(new_user)
169
170 [notification] = Notification.for_user(follower)
171
172 expected = %{
173 id: to_string(notification.id),
174 pleroma: %{is_seen: false},
175 type: "move",
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)
179 }
180
181 test_notifications_rendering([notification], follower, [expected])
182 end
183
184 test "EmojiReact notification" do
185 user = insert(:user)
186 other_user = insert(:user)
187
188 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
189 {:ok, _activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
190
191 activity = Repo.get(Activity, activity.id)
192
193 [notification] = Notification.for_user(user)
194
195 assert notification
196
197 expected = %{
198 id: to_string(notification.id),
199 pleroma: %{is_seen: false},
200 type: "pleroma:emoji_reaction",
201 emoji: "☕",
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)
205 }
206
207 test_notifications_rendering([notification], user, [expected])
208 end
209 end