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},
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)
50 test_notifications_rendering([notification], mentioned_user, [expected])
53 test "Favourite notification" do
55 another_user = insert(:user)
56 {:ok, create_activity} = CommonAPI.post(user, %{"status" => "hey"})
57 {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)
58 {:ok, [notification]} = Notification.create_notifications(favorite_activity)
59 create_activity = Activity.get_by_id(create_activity.id)
62 id: to_string(notification.id),
63 pleroma: %{is_seen: false},
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)
70 test_notifications_rendering([notification], user, [expected])
73 test "Reblog notification" do
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)
82 id: to_string(notification.id),
83 pleroma: %{is_seen: false},
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)
90 test_notifications_rendering([notification], user, [expected])
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)
100 id: to_string(notification.id),
101 pleroma: %{is_seen: false},
103 account: AccountView.render("show.json", %{user: follower, for: followed}),
104 created_at: Utils.to_masto_date(notification.inserted_at)
107 test_notifications_rendering([notification], followed, [expected])
109 User.perform(:delete, follower)
110 notification = Notification |> Repo.one() |> Repo.preload(:activity)
112 test_notifications_rendering([notification], followed, [])
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)
121 old_user_url = old_user.ap_id
124 File.read!("test/fixtures/users_mock/localhost.json")
125 |> String.replace("{{nickname}}", old_user.nickname)
129 %{method: :get, url: ^old_user_url} ->
130 %Tesla.Env{status: 200, body: body}
133 User.follow(follower, old_user)
134 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
135 Pleroma.Tests.ObanHelpers.perform_all()
137 old_user = refresh_record(old_user)
138 new_user = refresh_record(new_user)
140 [notification] = Notification.for_user(follower)
143 id: to_string(notification.id),
144 pleroma: %{is_seen: false},
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)
151 test_notifications_rendering([notification], follower, [expected])
154 test "EmojiReact notification" do
156 other_user = insert(:user)
158 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
159 {:ok, _activity, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
161 activity = Repo.get(Activity, activity.id)
163 [notification] = Notification.for_user(user)
168 id: to_string(notification.id),
169 pleroma: %{is_seen: false},
170 type: "pleroma:emoji_reaction",
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)
177 test_notifications_rendering([notification], user, [expected])