Merge branch 'feature/funkwhale-audio' into 'develop'
[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 test "Move notification" do
116 old_user = insert(:user)
117 new_user = insert(:user, also_known_as: [old_user.ap_id])
118 follower = insert(:user)
119
120 User.follow(follower, old_user)
121 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
122 Pleroma.Tests.ObanHelpers.perform_all()
123
124 old_user = refresh_record(old_user)
125 new_user = refresh_record(new_user)
126
127 [notification] = Notification.for_user(follower)
128
129 expected = %{
130 id: to_string(notification.id),
131 pleroma: %{is_seen: false},
132 type: "move",
133 account: AccountView.render("show.json", %{user: old_user, for: follower}),
134 target: AccountView.render("show.json", %{user: new_user, for: follower}),
135 created_at: Utils.to_masto_date(notification.inserted_at)
136 }
137
138 test_notifications_rendering([notification], follower, [expected])
139 end
140
141 test "EmojiReact notification" do
142 user = insert(:user)
143 other_user = insert(:user)
144
145 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
146 {:ok, _activity, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
147
148 activity = Repo.get(Activity, activity.id)
149
150 [notification] = Notification.for_user(user)
151
152 assert notification
153
154 expected = %{
155 id: to_string(notification.id),
156 pleroma: %{is_seen: false},
157 type: "pleroma:emoji_reaction",
158 emoji: "☕",
159 account: AccountView.render("show.json", %{user: other_user, for: user}),
160 status: StatusView.render("show.json", %{activity: activity, for: user}),
161 created_at: Utils.to_masto_date(notification.inserted_at)
162 }
163
164 test_notifications_rendering([notification], user, [expected])
165 end
166 end