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.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: 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)
74 }
75
76 test_notifications_rendering([notification], mentioned_user, [expected])
77 end
78
79 test "Favourite notification" do
80 user = insert(:user)
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)
86
87 expected = %{
88 id: to_string(notification.id),
89 pleroma: %{is_seen: false},
90 type: "favourite",
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)
94 }
95
96 test_notifications_rendering([notification], user, [expected])
97 end
98
99 test "Reblog notification" do
100 user = insert(:user)
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)
106
107 expected = %{
108 id: to_string(notification.id),
109 pleroma: %{is_seen: false},
110 type: "reblog",
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)
114 }
115
116 test_notifications_rendering([notification], user, [expected])
117 end
118
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)
124
125 expected = %{
126 id: to_string(notification.id),
127 pleroma: %{is_seen: false},
128 type: "follow",
129 account: AccountView.render("show.json", %{user: follower, for: followed}),
130 created_at: Utils.to_masto_date(notification.inserted_at)
131 }
132
133 test_notifications_rendering([notification], followed, [expected])
134
135 User.perform(:delete, follower)
136 notification = Notification |> Repo.one() |> Repo.preload(:activity)
137
138 test_notifications_rendering([notification], followed, [])
139 end
140
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)
146
147 old_user_url = old_user.ap_id
148
149 body =
150 File.read!("test/fixtures/users_mock/localhost.json")
151 |> String.replace("{{nickname}}", old_user.nickname)
152 |> Jason.encode!()
153
154 Tesla.Mock.mock(fn
155 %{method: :get, url: ^old_user_url} ->
156 %Tesla.Env{status: 200, body: body}
157 end)
158
159 User.follow(follower, old_user)
160 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
161 Pleroma.Tests.ObanHelpers.perform_all()
162
163 old_user = refresh_record(old_user)
164 new_user = refresh_record(new_user)
165
166 [notification] = Notification.for_user(follower)
167
168 expected = %{
169 id: to_string(notification.id),
170 pleroma: %{is_seen: false},
171 type: "move",
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)
175 }
176
177 test_notifications_rendering([notification], follower, [expected])
178 end
179
180 test "EmojiReact notification" do
181 user = insert(:user)
182 other_user = insert(:user)
183
184 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
185 {:ok, _activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
186
187 activity = Repo.get(Activity, activity.id)
188
189 [notification] = Notification.for_user(user)
190
191 assert notification
192
193 expected = %{
194 id: to_string(notification.id),
195 pleroma: %{is_seen: false},
196 type: "pleroma:emoji_reaction",
197 emoji: "☕",
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)
201 }
202
203 test_notifications_rendering([notification], user, [expected])
204 end
205 end