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