Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into remake-remodel-dms
[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.ChatMessageReference
11 alias Pleroma.Notification
12 alias Pleroma.Object
13 alias Pleroma.Repo
14 alias Pleroma.User
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
22
23 defp test_notifications_rendering(notifications, user, expected_result) do
24 result = NotificationView.render("index.json", %{notifications: notifications, for: user})
25
26 assert expected_result == result
27
28 result =
29 NotificationView.render("index.json", %{
30 notifications: notifications,
31 for: user,
32 relationships: nil
33 })
34
35 assert expected_result == result
36 end
37
38 test "ChatMessage notification" do
39 user = insert(:user)
40 recipient = insert(:user)
41 {:ok, activity} = CommonAPI.post_chat_message(user, recipient, "what's up my dude")
42
43 {:ok, [notification]} = Notification.create_notifications(activity)
44
45 object = Object.normalize(activity)
46 chat = Chat.get(recipient.id, user.ap_id)
47
48 cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
49
50 expected = %{
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}),
55 chat_message:
56 ChatMessageReferenceView.render("show.json", %{chat_message_reference: cm_ref}),
57 created_at: Utils.to_masto_date(notification.inserted_at)
58 }
59
60 test_notifications_rendering([notification], recipient, [expected])
61 end
62
63 test "Mention notification" do
64 user = insert(:user)
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)
69
70 expected = %{
71 id: to_string(notification.id),
72 pleroma: %{is_seen: false},
73 type: "mention",
74 account:
75 AccountView.render("show.json", %{
76 user: user,
77 for: mentioned_user
78 }),
79 status: StatusView.render("show.json", %{activity: activity, for: mentioned_user}),
80 created_at: Utils.to_masto_date(notification.inserted_at)
81 }
82
83 test_notifications_rendering([notification], mentioned_user, [expected])
84 end
85
86 test "Favourite notification" do
87 user = insert(:user)
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)
93
94 expected = %{
95 id: to_string(notification.id),
96 pleroma: %{is_seen: false},
97 type: "favourite",
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)
101 }
102
103 test_notifications_rendering([notification], user, [expected])
104 end
105
106 test "Reblog notification" do
107 user = insert(:user)
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)
113
114 expected = %{
115 id: to_string(notification.id),
116 pleroma: %{is_seen: false},
117 type: "reblog",
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)
121 }
122
123 test_notifications_rendering([notification], user, [expected])
124 end
125
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)
131
132 expected = %{
133 id: to_string(notification.id),
134 pleroma: %{is_seen: false},
135 type: "follow",
136 account: AccountView.render("show.json", %{user: follower, for: followed}),
137 created_at: Utils.to_masto_date(notification.inserted_at)
138 }
139
140 test_notifications_rendering([notification], followed, [expected])
141
142 User.perform(:delete, follower)
143 notification = Notification |> Repo.one() |> Repo.preload(:activity)
144
145 test_notifications_rendering([notification], followed, [])
146 end
147
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)
153
154 old_user_url = old_user.ap_id
155
156 body =
157 File.read!("test/fixtures/users_mock/localhost.json")
158 |> String.replace("{{nickname}}", old_user.nickname)
159 |> Jason.encode!()
160
161 Tesla.Mock.mock(fn
162 %{method: :get, url: ^old_user_url} ->
163 %Tesla.Env{status: 200, body: body}
164 end)
165
166 User.follow(follower, old_user)
167 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
168 Pleroma.Tests.ObanHelpers.perform_all()
169
170 old_user = refresh_record(old_user)
171 new_user = refresh_record(new_user)
172
173 [notification] = Notification.for_user(follower)
174
175 expected = %{
176 id: to_string(notification.id),
177 pleroma: %{is_seen: false},
178 type: "move",
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)
182 }
183
184 test_notifications_rendering([notification], follower, [expected])
185 end
186
187 test "EmojiReact notification" do
188 user = insert(:user)
189 other_user = insert(:user)
190
191 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
192 {:ok, _activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
193
194 activity = Repo.get(Activity, activity.id)
195
196 [notification] = Notification.for_user(user)
197
198 assert notification
199
200 expected = %{
201 id: to_string(notification.id),
202 pleroma: %{is_seen: false},
203 type: "pleroma:emoji_reaction",
204 emoji: "☕",
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)
208 }
209
210 test_notifications_rendering([notification], user, [expected])
211 end
212 end