1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Push.ImplTest do
6 use Pleroma.DataCase, async: true
11 alias Pleroma.Notification
13 alias Pleroma.UnstubbedConfigMock, as: ConfigMock
15 alias Pleroma.Web.ActivityPub.ActivityPub
16 alias Pleroma.Web.CommonAPI
17 alias Pleroma.Web.Push.Impl
18 alias Pleroma.Web.Push.Subscription
22 %{method: :post, url: "https://example.com/example/1234"} ->
23 %Tesla.Env{status: 200}
25 %{method: :post, url: "https://example.com/example/not_found"} ->
26 %Tesla.Env{status: 400}
28 %{method: :post, url: "https://example.com/example/bad"} ->
29 %Tesla.Env{status: 100}
36 endpoint: "https://example.com/example/1234",
38 auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
40 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA="
43 @api_key "BASgACIHpN1GYgzSRp"
44 @message "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
46 test "performs sending notifications" do
49 insert(:push_subscription, user: user, data: %{alerts: %{"mention" => true}})
50 insert(:push_subscription, user: user2, data: %{alerts: %{"mention" => true}})
52 insert(:push_subscription,
54 data: %{alerts: %{"follow" => true, "mention" => true}}
57 insert(:push_subscription,
59 data: %{alerts: %{"follow" => true, "mention" => false}}
62 {:ok, activity} = CommonAPI.post(user, %{status: "<Lorem ipsum dolor sit amet."})
71 assert Impl.perform(notif) == {:ok, [:ok, :ok]}
74 @tag capture_log: true
75 test "returns error if notif does not match " do
76 assert Impl.perform(%{}) == {:error, :unknown_type}
79 test "successful message sending" do
80 assert Impl.push_message(@message, @sub, @api_key, %Subscription{}) == :ok
83 @tag capture_log: true
84 test "fail message sending" do
85 assert Impl.push_message(
87 Map.merge(@sub, %{endpoint: "https://example.com/example/bad"}),
93 test "delete subscription if result send message between 400..500" do
94 subscription = insert(:push_subscription)
96 assert Impl.push_message(
98 Map.merge(@sub, %{endpoint: "https://example.com/example/not_found"}),
103 refute Pleroma.Repo.get(Subscription, subscription.id)
106 test "deletes subscription when token has been deleted" do
107 subscription = insert(:push_subscription)
109 Pleroma.Repo.delete(subscription.token)
111 refute Pleroma.Repo.get(Subscription, subscription.id)
114 test "renders title and body for create activity" do
115 user = insert(:user, nickname: "Bob")
118 CommonAPI.post(user, %{
120 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
123 object = Object.normalize(activity, fetch: false)
125 assert Impl.format_body(
132 "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
134 assert Impl.format_title(%{activity: activity, type: "mention"}) ==
138 test "renders title and body for follow activity" do
139 user = insert(:user, nickname: "Bob")
140 other_user = insert(:user)
141 {:ok, _, _, activity} = CommonAPI.follow(user, other_user)
142 object = Object.normalize(activity, fetch: false)
144 assert Impl.format_body(%{activity: activity, type: "follow"}, user, object) ==
145 "@Bob has followed you"
147 assert Impl.format_title(%{activity: activity, type: "follow"}) ==
151 test "renders title and body for announce activity" do
155 CommonAPI.post(user, %{
157 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
160 {:ok, announce_activity} = CommonAPI.repeat(activity.id, user)
161 object = Object.normalize(activity, fetch: false)
163 assert Impl.format_body(%{activity: announce_activity}, user, object) ==
164 "@#{user.nickname} repeated: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
166 assert Impl.format_title(%{activity: announce_activity, type: "reblog"}) ==
170 test "renders title and body for like activity" do
171 user = insert(:user, nickname: "Bob")
174 CommonAPI.post(user, %{
176 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
179 {:ok, activity} = CommonAPI.favorite(user, activity.id)
180 object = Object.normalize(activity, fetch: false)
182 assert Impl.format_body(%{activity: activity, type: "favourite"}, user, object) ==
183 "@Bob has favorited your post"
185 assert Impl.format_title(%{activity: activity, type: "favourite"}) ==
189 test "renders title and body for pleroma:emoji_reaction activity" do
190 user = insert(:user, nickname: "Bob")
193 CommonAPI.post(user, %{
194 status: "This post is a really good post!"
197 {:ok, activity} = CommonAPI.react_with_emoji(activity.id, user, "👍")
198 object = Object.normalize(activity, fetch: false)
200 assert Impl.format_body(%{activity: activity, type: "pleroma:emoji_reaction"}, user, object) ==
201 "@Bob reacted with 👍"
203 assert Impl.format_title(%{activity: activity, type: "pleroma:emoji_reaction"}) ==
207 test "renders title and body for update activity" do
210 {:ok, activity} = CommonAPI.post(user, %{status: "lorem ipsum"})
212 {:ok, activity} = CommonAPI.update(user, activity, %{status: "edited status"})
213 object = Object.normalize(activity, fetch: false)
215 assert Impl.format_body(%{activity: activity, type: "update"}, user, object) ==
216 "@#{user.nickname} edited a status"
218 assert Impl.format_title(%{activity: activity, type: "update"}) ==
222 test "renders title for create activity with direct visibility" do
223 user = insert(:user, nickname: "Bob")
226 CommonAPI.post(user, %{
227 visibility: "direct",
228 status: "This is just between you and me, pal"
231 assert Impl.format_title(%{activity: activity}) ==
235 describe "build_content/3" do
236 test "builds content for chat messages" do
238 recipient = insert(:user)
240 {:ok, chat} = CommonAPI.post_chat_message(user, recipient, "hey")
241 object = Object.normalize(chat, fetch: false)
242 [notification] = Notification.for_user(recipient)
244 res = Impl.build_content(notification, user, object)
247 body: "@#{user.nickname}: hey",
248 title: "New Chat Message"
252 test "builds content for chat messages with no content" do
254 recipient = insert(:user)
257 content_type: "image/jpeg",
258 path: Path.absname("test/fixtures/image.jpg"),
259 filename: "an_image.jpg"
263 |> stub_with(Pleroma.Test.StaticConfig)
265 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
267 {:ok, chat} = CommonAPI.post_chat_message(user, recipient, nil, media_id: upload.id)
268 object = Object.normalize(chat, fetch: false)
269 [notification] = Notification.for_user(recipient)
271 res = Impl.build_content(notification, user, object)
274 body: "@#{user.nickname}: (Attachment)",
275 title: "New Chat Message"
279 test "hides contents of notifications when option enabled" do
280 user = insert(:user, nickname: "Bob")
283 insert(:user, nickname: "Rob", notification_settings: %{hide_notification_contents: true})
286 CommonAPI.post(user, %{
287 visibility: "direct",
288 status: "<Lorem ipsum dolor sit amet."
291 notif = insert(:notification, user: user2, activity: activity)
293 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
294 object = Object.normalize(activity, fetch: false)
296 assert Impl.build_content(notif, actor, object) == %{
297 body: "New Direct Message"
301 CommonAPI.post(user, %{
302 visibility: "public",
303 status: "<Lorem ipsum dolor sit amet."
306 notif = insert(:notification, user: user2, activity: activity, type: "mention")
308 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
309 object = Object.normalize(activity, fetch: false)
311 assert Impl.build_content(notif, actor, object) == %{
315 {:ok, activity} = CommonAPI.favorite(user, activity.id)
317 notif = insert(:notification, user: user2, activity: activity, type: "favourite")
319 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
320 object = Object.normalize(activity, fetch: false)
322 assert Impl.build_content(notif, actor, object) == %{
327 test "returns regular content when hiding contents option disabled" do
328 user = insert(:user, nickname: "Bob")
331 insert(:user, nickname: "Rob", notification_settings: %{hide_notification_contents: false})
334 CommonAPI.post(user, %{
335 visibility: "direct",
337 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
340 notif = insert(:notification, user: user2, activity: activity)
342 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
343 object = Object.normalize(activity, fetch: false)
345 assert Impl.build_content(notif, actor, object) == %{
347 "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini...",
348 title: "New Direct Message"
352 CommonAPI.post(user, %{
353 visibility: "public",
355 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
358 notif = insert(:notification, user: user2, activity: activity, type: "mention")
360 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
361 object = Object.normalize(activity, fetch: false)
363 assert Impl.build_content(notif, actor, object) == %{
365 "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini...",
369 {:ok, activity} = CommonAPI.favorite(user, activity.id)
371 notif = insert(:notification, user: user2, activity: activity, type: "favourite")
373 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
374 object = Object.normalize(activity, fetch: false)
376 assert Impl.build_content(notif, actor, object) == %{
377 body: "@Bob has favorited your post",
378 title: "New Favorite"