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", %{
49 skip_relationships: true
51 status: StatusView.render("show.json", %{activity: activity, for: mentioned_user}),
52 created_at: Utils.to_masto_date(notification.inserted_at)
55 test_notifications_rendering([notification], mentioned_user, [expected])
58 test "Favourite notification" do
60 another_user = insert(:user)
61 {:ok, create_activity} = CommonAPI.post(user, %{"status" => "hey"})
62 {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)
63 {:ok, [notification]} = Notification.create_notifications(favorite_activity)
64 create_activity = Activity.get_by_id(create_activity.id)
67 id: to_string(notification.id),
68 pleroma: %{is_seen: false},
71 AccountView.render("show.json", %{user: another_user, for: user, skip_relationships: true}),
72 status: StatusView.render("show.json", %{activity: create_activity, for: user}),
73 created_at: Utils.to_masto_date(notification.inserted_at)
76 test_notifications_rendering([notification], user, [expected])
79 test "Reblog notification" do
81 another_user = insert(:user)
82 {:ok, create_activity} = CommonAPI.post(user, %{"status" => "hey"})
83 {:ok, reblog_activity, _object} = CommonAPI.repeat(create_activity.id, another_user)
84 {:ok, [notification]} = Notification.create_notifications(reblog_activity)
85 reblog_activity = Activity.get_by_id(create_activity.id)
88 id: to_string(notification.id),
89 pleroma: %{is_seen: false},
92 AccountView.render("show.json", %{user: another_user, for: user, skip_relationships: true}),
93 status: StatusView.render("show.json", %{activity: reblog_activity, for: user}),
94 created_at: Utils.to_masto_date(notification.inserted_at)
97 test_notifications_rendering([notification], user, [expected])
100 test "Follow notification" do
101 follower = insert(:user)
102 followed = insert(:user)
103 {:ok, follower, followed, _activity} = CommonAPI.follow(follower, followed)
104 notification = Notification |> Repo.one() |> Repo.preload(:activity)
107 id: to_string(notification.id),
108 pleroma: %{is_seen: false},
111 AccountView.render("show.json", %{user: follower, for: followed, skip_relationships: true}),
112 created_at: Utils.to_masto_date(notification.inserted_at)
115 test_notifications_rendering([notification], followed, [expected])
117 User.perform(:delete, follower)
118 notification = Notification |> Repo.one() |> Repo.preload(:activity)
120 test_notifications_rendering([notification], followed, [])
123 @tag capture_log: true
124 test "Move notification" do
125 old_user = insert(:user)
126 new_user = insert(:user, also_known_as: [old_user.ap_id])
127 follower = insert(:user)
129 old_user_url = old_user.ap_id
132 File.read!("test/fixtures/users_mock/localhost.json")
133 |> String.replace("{{nickname}}", old_user.nickname)
137 %{method: :get, url: ^old_user_url} ->
138 %Tesla.Env{status: 200, body: body}
141 User.follow(follower, old_user)
142 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
143 Pleroma.Tests.ObanHelpers.perform_all()
145 old_user = refresh_record(old_user)
146 new_user = refresh_record(new_user)
148 [notification] = Notification.for_user(follower)
151 id: to_string(notification.id),
152 pleroma: %{is_seen: false},
155 AccountView.render("show.json", %{user: old_user, for: follower, skip_relationships: true}),
157 AccountView.render("show.json", %{user: new_user, for: follower, skip_relationships: true}),
158 created_at: Utils.to_masto_date(notification.inserted_at)
161 test_notifications_rendering([notification], follower, [expected])
164 test "EmojiReact notification" do
166 other_user = insert(:user)
168 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
169 {:ok, _activity, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
171 activity = Repo.get(Activity, activity.id)
173 [notification] = Notification.for_user(user)
178 id: to_string(notification.id),
179 pleroma: %{is_seen: false},
180 type: "pleroma:emoji_reaction",
183 AccountView.render("show.json", %{user: other_user, for: user, skip_relationships: true}),
184 status: StatusView.render("show.json", %{activity: activity, for: user}),
185 created_at: Utils.to_masto_date(notification.inserted_at)
188 test_notifications_rendering([notification], user, [expected])