Restricted embedding of relationships where applicable (statuses / notifications...
[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.Notification
10 alias Pleroma.Repo
11 alias Pleroma.User
12 alias Pleroma.Web.CommonAPI
13 alias Pleroma.Web.CommonAPI.Utils
14 alias Pleroma.Web.MastodonAPI.AccountView
15 alias Pleroma.Web.MastodonAPI.NotificationView
16 alias Pleroma.Web.MastodonAPI.StatusView
17 import Pleroma.Factory
18
19 defp test_notifications_rendering(notifications, user, expected_result) do
20 result = NotificationView.render("index.json", %{notifications: notifications, for: user})
21
22 assert expected_result == result
23
24 result =
25 NotificationView.render("index.json", %{
26 notifications: notifications,
27 for: user,
28 relationships: nil
29 })
30
31 assert expected_result == result
32 end
33
34 test "Mention notification" do
35 user = insert(:user)
36 mentioned_user = insert(:user)
37 {:ok, activity} = CommonAPI.post(user, %{"status" => "hey @#{mentioned_user.nickname}"})
38 {:ok, [notification]} = Notification.create_notifications(activity)
39 user = User.get_cached_by_id(user.id)
40
41 expected = %{
42 id: to_string(notification.id),
43 pleroma: %{is_seen: false},
44 type: "mention",
45 account:
46 AccountView.render("show.json", %{
47 user: user,
48 for: mentioned_user,
49 skip_relationships: true
50 }),
51 status: StatusView.render("show.json", %{activity: activity, for: mentioned_user}),
52 created_at: Utils.to_masto_date(notification.inserted_at)
53 }
54
55 test_notifications_rendering([notification], mentioned_user, [expected])
56 end
57
58 test "Favourite notification" do
59 user = insert(:user)
60 another_user = insert(:user)
61 {:ok, create_activity} = CommonAPI.post(user, %{"status" => "hey"})
62 {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)
63 {:ok, [notification]} = Notification.create_notifications(favorite_activity)
64 create_activity = Activity.get_by_id(create_activity.id)
65
66 expected = %{
67 id: to_string(notification.id),
68 pleroma: %{is_seen: false},
69 type: "favourite",
70 account:
71 AccountView.render("show.json", %{user: another_user, for: user, skip_relationships: true}),
72 status: StatusView.render("show.json", %{activity: create_activity, for: user}),
73 created_at: Utils.to_masto_date(notification.inserted_at)
74 }
75
76 test_notifications_rendering([notification], user, [expected])
77 end
78
79 test "Reblog notification" do
80 user = insert(:user)
81 another_user = insert(:user)
82 {:ok, create_activity} = CommonAPI.post(user, %{"status" => "hey"})
83 {:ok, reblog_activity, _object} = CommonAPI.repeat(create_activity.id, another_user)
84 {:ok, [notification]} = Notification.create_notifications(reblog_activity)
85 reblog_activity = Activity.get_by_id(create_activity.id)
86
87 expected = %{
88 id: to_string(notification.id),
89 pleroma: %{is_seen: false},
90 type: "reblog",
91 account:
92 AccountView.render("show.json", %{user: another_user, for: user, skip_relationships: true}),
93 status: StatusView.render("show.json", %{activity: reblog_activity, for: user}),
94 created_at: Utils.to_masto_date(notification.inserted_at)
95 }
96
97 test_notifications_rendering([notification], user, [expected])
98 end
99
100 test "Follow notification" do
101 follower = insert(:user)
102 followed = insert(:user)
103 {:ok, follower, followed, _activity} = CommonAPI.follow(follower, followed)
104 notification = Notification |> Repo.one() |> Repo.preload(:activity)
105
106 expected = %{
107 id: to_string(notification.id),
108 pleroma: %{is_seen: false},
109 type: "follow",
110 account:
111 AccountView.render("show.json", %{user: follower, for: followed, skip_relationships: true}),
112 created_at: Utils.to_masto_date(notification.inserted_at)
113 }
114
115 test_notifications_rendering([notification], followed, [expected])
116
117 User.perform(:delete, follower)
118 notification = Notification |> Repo.one() |> Repo.preload(:activity)
119
120 test_notifications_rendering([notification], followed, [])
121 end
122
123 @tag capture_log: true
124 test "Move notification" do
125 old_user = insert(:user)
126 new_user = insert(:user, also_known_as: [old_user.ap_id])
127 follower = insert(:user)
128
129 old_user_url = old_user.ap_id
130
131 body =
132 File.read!("test/fixtures/users_mock/localhost.json")
133 |> String.replace("{{nickname}}", old_user.nickname)
134 |> Jason.encode!()
135
136 Tesla.Mock.mock(fn
137 %{method: :get, url: ^old_user_url} ->
138 %Tesla.Env{status: 200, body: body}
139 end)
140
141 User.follow(follower, old_user)
142 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
143 Pleroma.Tests.ObanHelpers.perform_all()
144
145 old_user = refresh_record(old_user)
146 new_user = refresh_record(new_user)
147
148 [notification] = Notification.for_user(follower)
149
150 expected = %{
151 id: to_string(notification.id),
152 pleroma: %{is_seen: false},
153 type: "move",
154 account:
155 AccountView.render("show.json", %{user: old_user, for: follower, skip_relationships: true}),
156 target:
157 AccountView.render("show.json", %{user: new_user, for: follower, skip_relationships: true}),
158 created_at: Utils.to_masto_date(notification.inserted_at)
159 }
160
161 test_notifications_rendering([notification], follower, [expected])
162 end
163
164 test "EmojiReact notification" do
165 user = insert(:user)
166 other_user = insert(:user)
167
168 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
169 {:ok, _activity, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
170
171 activity = Repo.get(Activity, activity.id)
172
173 [notification] = Notification.for_user(user)
174
175 assert notification
176
177 expected = %{
178 id: to_string(notification.id),
179 pleroma: %{is_seen: false},
180 type: "pleroma:emoji_reaction",
181 emoji: "☕",
182 account:
183 AccountView.render("show.json", %{user: other_user, for: user, skip_relationships: true}),
184 status: StatusView.render("show.json", %{activity: activity, for: user}),
185 created_at: Utils.to_masto_date(notification.inserted_at)
186 }
187
188 test_notifications_rendering([notification], user, [expected])
189 end
190 end