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