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
8 alias Pleroma.Notification
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.CommonAPI
13 alias Pleroma.Web.Push.Impl
14 alias Pleroma.Web.Push.Subscription
16 import Pleroma.Factory
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 for create activity with direct visibility" do
188 user = insert(:user, nickname: "Bob")
191 CommonAPI.post(user, %{
192 visibility: "direct",
193 status: "This is just between you and me, pal"
196 assert Impl.format_title(%{activity: activity}) ==
200 describe "build_content/3" do
201 test "builds content for chat messages" do
203 recipient = insert(:user)
205 {:ok, chat} = CommonAPI.post_chat_message(user, recipient, "hey")
206 object = Object.normalize(chat, false)
207 [notification] = Notification.for_user(recipient)
209 res = Impl.build_content(notification, user, object)
212 body: "@#{user.nickname}: hey",
213 title: "New Chat Message"
217 test "builds content for chat messages with no content" do
219 recipient = insert(:user)
222 content_type: "image/jpg",
223 path: Path.absname("test/fixtures/image.jpg"),
224 filename: "an_image.jpg"
227 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
229 {:ok, chat} = CommonAPI.post_chat_message(user, recipient, nil, media_id: upload.id)
230 object = Object.normalize(chat, false)
231 [notification] = Notification.for_user(recipient)
233 res = Impl.build_content(notification, user, object)
236 body: "@#{user.nickname}: (Attachment)",
237 title: "New Chat Message"
241 test "hides contents of notifications when option enabled" do
242 user = insert(:user, nickname: "Bob")
245 insert(:user, nickname: "Rob", notification_settings: %{hide_notification_contents: true})
248 CommonAPI.post(user, %{
249 visibility: "direct",
250 status: "<Lorem ipsum dolor sit amet."
253 notif = insert(:notification, user: user2, activity: activity)
255 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
256 object = Object.normalize(activity)
258 assert Impl.build_content(notif, actor, object) == %{
259 body: "New Direct Message"
263 CommonAPI.post(user, %{
264 visibility: "public",
265 status: "<Lorem ipsum dolor sit amet."
268 notif = insert(:notification, user: user2, activity: activity, type: "mention")
270 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
271 object = Object.normalize(activity)
273 assert Impl.build_content(notif, actor, object) == %{
277 {:ok, activity} = CommonAPI.favorite(user, activity.id)
279 notif = insert(:notification, user: user2, activity: activity, type: "favourite")
281 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
282 object = Object.normalize(activity)
284 assert Impl.build_content(notif, actor, object) == %{
289 test "returns regular content when hiding contents option disabled" do
290 user = insert(:user, nickname: "Bob")
293 insert(:user, nickname: "Rob", notification_settings: %{hide_notification_contents: false})
296 CommonAPI.post(user, %{
297 visibility: "direct",
299 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
302 notif = insert(:notification, user: user2, activity: activity)
304 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
305 object = Object.normalize(activity)
307 assert Impl.build_content(notif, actor, object) == %{
309 "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini...",
310 title: "New Direct Message"
314 CommonAPI.post(user, %{
315 visibility: "public",
317 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
320 notif = insert(:notification, user: user2, activity: activity, type: "mention")
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...",
331 {:ok, activity} = CommonAPI.favorite(user, activity.id)
333 notif = insert(:notification, user: user2, activity: activity, type: "favourite")
335 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
336 object = Object.normalize(activity)
338 assert Impl.build_content(notif, actor, object) == %{
339 body: "@Bob has favorited your post",
340 title: "New Favorite"