[#2497] Specified SHELL in .gitlab-ci.yml as required for `exexec`.
[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 }),
50 status: StatusView.render("show.json", %{activity: activity, for: mentioned_user}),
51 created_at: Utils.to_masto_date(notification.inserted_at)
52 }
53
54 test_notifications_rendering([notification], mentioned_user, [expected])
55 end
56
57 test "Favourite notification" do
58 user = insert(:user)
59 another_user = insert(:user)
60 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
61 {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)
62 {:ok, [notification]} = Notification.create_notifications(favorite_activity)
63 create_activity = Activity.get_by_id(create_activity.id)
64
65 expected = %{
66 id: to_string(notification.id),
67 pleroma: %{is_seen: false},
68 type: "favourite",
69 account: AccountView.render("show.json", %{user: another_user, for: user}),
70 status: StatusView.render("show.json", %{activity: create_activity, for: user}),
71 created_at: Utils.to_masto_date(notification.inserted_at)
72 }
73
74 test_notifications_rendering([notification], user, [expected])
75 end
76
77 test "Reblog notification" do
78 user = insert(:user)
79 another_user = insert(:user)
80 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
81 {:ok, reblog_activity, _object} = CommonAPI.repeat(create_activity.id, another_user)
82 {:ok, [notification]} = Notification.create_notifications(reblog_activity)
83 reblog_activity = Activity.get_by_id(create_activity.id)
84
85 expected = %{
86 id: to_string(notification.id),
87 pleroma: %{is_seen: false},
88 type: "reblog",
89 account: AccountView.render("show.json", %{user: another_user, for: user}),
90 status: StatusView.render("show.json", %{activity: reblog_activity, for: user}),
91 created_at: Utils.to_masto_date(notification.inserted_at)
92 }
93
94 test_notifications_rendering([notification], user, [expected])
95 end
96
97 test "Follow notification" do
98 follower = insert(:user)
99 followed = insert(:user)
100 {:ok, follower, followed, _activity} = CommonAPI.follow(follower, followed)
101 notification = Notification |> Repo.one() |> Repo.preload(:activity)
102
103 expected = %{
104 id: to_string(notification.id),
105 pleroma: %{is_seen: false},
106 type: "follow",
107 account: AccountView.render("show.json", %{user: follower, for: followed}),
108 created_at: Utils.to_masto_date(notification.inserted_at)
109 }
110
111 test_notifications_rendering([notification], followed, [expected])
112
113 User.perform(:delete, follower)
114 notification = Notification |> Repo.one() |> Repo.preload(:activity)
115
116 test_notifications_rendering([notification], followed, [])
117 end
118
119 @tag capture_log: true
120 test "Move notification" do
121 old_user = insert(:user)
122 new_user = insert(:user, also_known_as: [old_user.ap_id])
123 follower = insert(:user)
124
125 old_user_url = old_user.ap_id
126
127 body =
128 File.read!("test/fixtures/users_mock/localhost.json")
129 |> String.replace("{{nickname}}", old_user.nickname)
130 |> Jason.encode!()
131
132 Tesla.Mock.mock(fn
133 %{method: :get, url: ^old_user_url} ->
134 %Tesla.Env{status: 200, body: body}
135 end)
136
137 User.follow(follower, old_user)
138 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
139 Pleroma.Tests.ObanHelpers.perform_all()
140
141 old_user = refresh_record(old_user)
142 new_user = refresh_record(new_user)
143
144 [notification] = Notification.for_user(follower)
145
146 expected = %{
147 id: to_string(notification.id),
148 pleroma: %{is_seen: false},
149 type: "move",
150 account: AccountView.render("show.json", %{user: old_user, for: follower}),
151 target: AccountView.render("show.json", %{user: new_user, for: follower}),
152 created_at: Utils.to_masto_date(notification.inserted_at)
153 }
154
155 test_notifications_rendering([notification], follower, [expected])
156 end
157
158 test "EmojiReact notification" do
159 user = insert(:user)
160 other_user = insert(:user)
161
162 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
163 {:ok, _activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
164
165 activity = Repo.get(Activity, activity.id)
166
167 [notification] = Notification.for_user(user)
168
169 assert notification
170
171 expected = %{
172 id: to_string(notification.id),
173 pleroma: %{is_seen: false},
174 type: "pleroma:emoji_reaction",
175 emoji: "☕",
176 account: AccountView.render("show.json", %{user: other_user, for: user}),
177 status: StatusView.render("show.json", %{activity: activity, for: user}),
178 created_at: Utils.to_masto_date(notification.inserted_at)
179 }
180
181 test_notifications_rendering([notification], user, [expected])
182 end
183 end