1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do
9 alias Pleroma.Notification
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
19 defp test_notifications_rendering(notifications, user, expected_result) do
20 result = NotificationView.render("index.json", %{notifications: notifications, for: user})
22 assert expected_result == result
25 NotificationView.render("index.json", %{
26 notifications: notifications,
31 assert expected_result == result
34 test "Mention notification" do
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)
42 id: to_string(notification.id),
43 pleroma: %{is_seen: false},
46 AccountView.render("show.json", %{
50 status: StatusView.render("show.json", %{activity: activity, for: mentioned_user}),
51 created_at: Utils.to_masto_date(notification.inserted_at)
54 test_notifications_rendering([notification], mentioned_user, [expected])
57 test "Favourite notification" do
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)
66 id: to_string(notification.id),
67 pleroma: %{is_seen: false},
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)
74 test_notifications_rendering([notification], user, [expected])
77 test "Reblog notification" do
79 another_user = insert(:user)
80 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
81 {:ok, reblog_activity} = 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)
86 id: to_string(notification.id),
87 pleroma: %{is_seen: false},
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)
94 test_notifications_rendering([notification], user, [expected])
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)
104 id: to_string(notification.id),
105 pleroma: %{is_seen: false},
107 account: AccountView.render("show.json", %{user: follower, for: followed}),
108 created_at: Utils.to_masto_date(notification.inserted_at)
111 test_notifications_rendering([notification], followed, [expected])
113 User.perform(:delete, follower)
114 notification = Notification |> Repo.one() |> Repo.preload(:activity)
116 test_notifications_rendering([notification], followed, [])
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)
125 old_user_url = old_user.ap_id
128 File.read!("test/fixtures/users_mock/localhost.json")
129 |> String.replace("{{nickname}}", old_user.nickname)
133 %{method: :get, url: ^old_user_url} ->
134 %Tesla.Env{status: 200, body: body}
137 User.follow(follower, old_user)
138 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
139 Pleroma.Tests.ObanHelpers.perform_all()
141 old_user = refresh_record(old_user)
142 new_user = refresh_record(new_user)
144 [notification] = Notification.for_user(follower)
147 id: to_string(notification.id),
148 pleroma: %{is_seen: false},
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)
155 test_notifications_rendering([notification], follower, [expected])
158 test "EmojiReact notification" do
160 other_user = insert(:user)
162 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
163 {:ok, _activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
165 activity = Repo.get(Activity, activity.id)
167 [notification] = Notification.for_user(user)
172 id: to_string(notification.id),
173 pleroma: %{is_seen: false},
174 type: "pleroma:emoji_reaction",
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)
181 test_notifications_rendering([notification], user, [expected])