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.Push.ImplTest do
6 use Pleroma.DataCase, async: true
10 alias Pleroma.Notification
13 alias Pleroma.Web.ActivityPub.ActivityPub
14 alias Pleroma.Web.CommonAPI
15 alias Pleroma.Web.Push.Impl
16 alias Pleroma.Web.Push.Subscription
20 %{method: :post, url: "https://example.com/example/1234"} ->
21 %Tesla.Env{status: 200}
23 %{method: :post, url: "https://example.com/example/not_found"} ->
24 %Tesla.Env{status: 400}
26 %{method: :post, url: "https://example.com/example/bad"} ->
27 %Tesla.Env{status: 100}
34 endpoint: "https://example.com/example/1234",
36 auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
38 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA="
41 @api_key "BASgACIHpN1GYgzSRp"
42 @message "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
44 test "performs sending notifications" do
47 insert(:push_subscription, user: user, data: %{alerts: %{"mention" => true}})
48 insert(:push_subscription, user: user2, data: %{alerts: %{"mention" => true}})
50 insert(:push_subscription,
52 data: %{alerts: %{"follow" => true, "mention" => true}}
55 insert(:push_subscription,
57 data: %{alerts: %{"follow" => true, "mention" => false}}
60 {:ok, activity} = CommonAPI.post(user, %{status: "<Lorem ipsum dolor sit amet."})
69 assert Impl.perform(notif) == {:ok, [:ok, :ok]}
72 @tag capture_log: true
73 test "returns error if notif does not match " do
74 assert Impl.perform(%{}) == {:error, :unknown_type}
77 test "successful message sending" do
78 assert Impl.push_message(@message, @sub, @api_key, %Subscription{}) == :ok
81 @tag capture_log: true
82 test "fail message sending" do
83 assert Impl.push_message(
85 Map.merge(@sub, %{endpoint: "https://example.com/example/bad"}),
91 test "delete subscription if result send message between 400..500" do
92 subscription = insert(:push_subscription)
94 assert Impl.push_message(
96 Map.merge(@sub, %{endpoint: "https://example.com/example/not_found"}),
101 refute Pleroma.Repo.get(Subscription, subscription.id)
104 test "deletes subscription when token has been deleted" do
105 subscription = insert(:push_subscription)
107 Pleroma.Repo.delete(subscription.token)
109 refute Pleroma.Repo.get(Subscription, subscription.id)
112 test "renders title and body for create activity" do
113 user = insert(:user, nickname: "Bob")
116 CommonAPI.post(user, %{
118 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
121 object = Object.normalize(activity)
123 assert Impl.format_body(
130 "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
132 assert Impl.format_title(%{activity: activity, type: "mention"}) ==
136 test "renders title and body for follow activity" do
137 user = insert(:user, nickname: "Bob")
138 other_user = insert(:user)
139 {:ok, _, _, activity} = CommonAPI.follow(user, other_user)
140 object = Object.normalize(activity, false)
142 assert Impl.format_body(%{activity: activity, type: "follow"}, user, object) ==
143 "@Bob has followed you"
145 assert Impl.format_title(%{activity: activity, type: "follow"}) ==
149 test "renders title and body for announce activity" do
153 CommonAPI.post(user, %{
155 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
158 {:ok, announce_activity} = CommonAPI.repeat(activity.id, user)
159 object = Object.normalize(activity)
161 assert Impl.format_body(%{activity: announce_activity}, user, object) ==
162 "@#{user.nickname} repeated: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
164 assert Impl.format_title(%{activity: announce_activity, type: "reblog"}) ==
168 test "renders title and body for like activity" do
169 user = insert(:user, nickname: "Bob")
172 CommonAPI.post(user, %{
174 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
177 {:ok, activity} = CommonAPI.favorite(user, activity.id)
178 object = Object.normalize(activity)
180 assert Impl.format_body(%{activity: activity, type: "favourite"}, user, object) ==
181 "@Bob has favorited your post"
183 assert Impl.format_title(%{activity: activity, type: "favourite"}) ==
187 test "renders title and body for pleroma:emoji_reaction activity" do
188 user = insert(:user, nickname: "Bob")
191 CommonAPI.post(user, %{
192 status: "This post is a really good post!"
195 {:ok, activity} = CommonAPI.react_with_emoji(activity.id, user, "👍")
196 object = Object.normalize(activity)
198 assert Impl.format_body(%{activity: activity, type: "pleroma:emoji_reaction"}, user, object) ==
199 "@Bob reacted with 👍"
201 assert Impl.format_title(%{activity: activity, type: "pleroma:emoji_reaction"}) ==
205 test "renders title for create activity with direct visibility" do
206 user = insert(:user, nickname: "Bob")
209 CommonAPI.post(user, %{
210 visibility: "direct",
211 status: "This is just between you and me, pal"
214 assert Impl.format_title(%{activity: activity}) ==
218 describe "build_content/3" do
219 test "builds content for chat messages" do
221 recipient = insert(:user)
223 {:ok, chat} = CommonAPI.post_chat_message(user, recipient, "hey")
224 object = Object.normalize(chat, false)
225 [notification] = Notification.for_user(recipient)
227 res = Impl.build_content(notification, user, object)
230 body: "@#{user.nickname}: hey",
231 title: "New Chat Message"
235 test "builds content for chat messages with no content" do
237 recipient = insert(:user)
240 content_type: "image/jpeg",
241 path: Path.absname("test/fixtures/image.jpg"),
242 filename: "an_image.jpg"
245 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
247 {:ok, chat} = CommonAPI.post_chat_message(user, recipient, nil, media_id: upload.id)
248 object = Object.normalize(chat, false)
249 [notification] = Notification.for_user(recipient)
251 res = Impl.build_content(notification, user, object)
254 body: "@#{user.nickname}: (Attachment)",
255 title: "New Chat Message"
259 test "hides contents of notifications when option enabled" do
260 user = insert(:user, nickname: "Bob")
263 insert(:user, nickname: "Rob", notification_settings: %{hide_notification_contents: true})
266 CommonAPI.post(user, %{
267 visibility: "direct",
268 status: "<Lorem ipsum dolor sit amet."
271 notif = insert(:notification, user: user2, activity: activity)
273 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
274 object = Object.normalize(activity)
276 assert Impl.build_content(notif, actor, object) == %{
277 body: "New Direct Message"
281 CommonAPI.post(user, %{
282 visibility: "public",
283 status: "<Lorem ipsum dolor sit amet."
286 notif = insert(:notification, user: user2, activity: activity, type: "mention")
288 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
289 object = Object.normalize(activity)
291 assert Impl.build_content(notif, actor, object) == %{
295 {:ok, activity} = CommonAPI.favorite(user, activity.id)
297 notif = insert(:notification, user: user2, activity: activity, type: "favourite")
299 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
300 object = Object.normalize(activity)
302 assert Impl.build_content(notif, actor, object) == %{
307 test "returns regular content when hiding contents option disabled" do
308 user = insert(:user, nickname: "Bob")
311 insert(:user, nickname: "Rob", notification_settings: %{hide_notification_contents: false})
314 CommonAPI.post(user, %{
315 visibility: "direct",
317 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
320 notif = insert(:notification, user: user2, activity: activity)
322 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
323 object = Object.normalize(activity)
325 assert Impl.build_content(notif, actor, object) == %{
327 "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini...",
328 title: "New Direct Message"
332 CommonAPI.post(user, %{
333 visibility: "public",
335 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
338 notif = insert(:notification, user: user2, activity: activity, type: "mention")
340 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
341 object = Object.normalize(activity)
343 assert Impl.build_content(notif, actor, object) == %{
345 "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini...",
349 {:ok, activity} = CommonAPI.favorite(user, activity.id)
351 notif = insert(:notification, user: user2, activity: activity, type: "favourite")
353 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
354 object = Object.normalize(activity)
356 assert Impl.build_content(notif, actor, object) == %{
357 body: "@Bob has favorited your post",
358 title: "New Favorite"