1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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
12 alias Pleroma.Web.CommonAPI
13 alias Pleroma.Web.Push.Impl
14 alias Pleroma.Web.Push.Subscription
18 %{method: :post, url: "https://example.com/example/1234"} ->
19 %Tesla.Env{status: 200}
21 %{method: :post, url: "https://example.com/example/not_found"} ->
22 %Tesla.Env{status: 400}
24 %{method: :post, url: "https://example.com/example/bad"} ->
25 %Tesla.Env{status: 100}
32 endpoint: "https://example.com/example/1234",
34 auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
36 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA="
39 @api_key "BASgACIHpN1GYgzSRp"
40 @message "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
42 test "performs sending notifications" do
45 insert(:push_subscription, user: user, data: %{alerts: %{"mention" => true}})
46 insert(:push_subscription, user: user2, data: %{alerts: %{"mention" => true}})
48 insert(:push_subscription,
50 data: %{alerts: %{"follow" => true, "mention" => true}}
53 insert(:push_subscription,
55 data: %{alerts: %{"follow" => true, "mention" => false}}
58 {:ok, activity} = CommonAPI.post(user, %{status: "<Lorem ipsum dolor sit amet."})
67 assert Impl.perform(notif) == {:ok, [:ok, :ok]}
70 @tag capture_log: true
71 test "returns error if notif does not match " do
72 assert Impl.perform(%{}) == {:error, :unknown_type}
75 test "successful message sending" do
76 assert Impl.push_message(@message, @sub, @api_key, %Subscription{}) == :ok
79 @tag capture_log: true
80 test "fail message sending" do
81 assert Impl.push_message(
83 Map.merge(@sub, %{endpoint: "https://example.com/example/bad"}),
89 test "delete subscription if result send message between 400..500" do
90 subscription = insert(:push_subscription)
92 assert Impl.push_message(
94 Map.merge(@sub, %{endpoint: "https://example.com/example/not_found"}),
99 refute Pleroma.Repo.get(Subscription, subscription.id)
102 test "deletes subscription when token has been deleted" do
103 subscription = insert(:push_subscription)
105 Pleroma.Repo.delete(subscription.token)
107 refute Pleroma.Repo.get(Subscription, subscription.id)
110 test "renders title and body for create activity" do
111 user = insert(:user, nickname: "Bob")
114 CommonAPI.post(user, %{
116 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
119 object = Object.normalize(activity, fetch: false)
121 assert Impl.format_body(
128 "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
130 assert Impl.format_title(%{activity: activity, type: "mention"}) ==
134 test "renders title and body for follow activity" do
135 user = insert(:user, nickname: "Bob")
136 other_user = insert(:user)
137 {:ok, _, _, activity} = CommonAPI.follow(user, other_user)
138 object = Object.normalize(activity, fetch: false)
140 assert Impl.format_body(%{activity: activity, type: "follow"}, user, object) ==
141 "@Bob has followed you"
143 assert Impl.format_title(%{activity: activity, type: "follow"}) ==
147 test "renders title and body for announce activity" do
151 CommonAPI.post(user, %{
153 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
156 {:ok, announce_activity} = CommonAPI.repeat(activity.id, user)
157 object = Object.normalize(activity, fetch: false)
159 assert Impl.format_body(%{activity: announce_activity}, user, object) ==
160 "@#{user.nickname} repeated: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
162 assert Impl.format_title(%{activity: announce_activity, type: "reblog"}) ==
166 test "renders title and body for like activity" do
167 user = insert(:user, nickname: "Bob")
170 CommonAPI.post(user, %{
172 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
175 {:ok, activity} = CommonAPI.favorite(user, activity.id)
176 object = Object.normalize(activity, fetch: false)
178 assert Impl.format_body(%{activity: activity, type: "favourite"}, user, object) ==
179 "@Bob has favorited your post"
181 assert Impl.format_title(%{activity: activity, type: "favourite"}) ==
185 test "renders title and body for pleroma:emoji_reaction activity" do
186 user = insert(:user, nickname: "Bob")
189 CommonAPI.post(user, %{
190 status: "This post is a really good post!"
193 {:ok, activity} = CommonAPI.react_with_emoji(activity.id, user, "👍")
194 object = Object.normalize(activity, fetch: false)
196 assert Impl.format_body(%{activity: activity, type: "pleroma:emoji_reaction"}, user, object) ==
197 "@Bob reacted with 👍"
199 assert Impl.format_title(%{activity: activity, type: "pleroma:emoji_reaction"}) ==
203 test "renders title and body for update activity" do
206 {:ok, activity} = CommonAPI.post(user, %{status: "lorem ipsum"})
208 {:ok, activity} = CommonAPI.update(user, activity, %{status: "edited status"})
209 object = Object.normalize(activity, fetch: false)
211 assert Impl.format_body(%{activity: activity, type: "update"}, user, object) ==
212 "@#{user.nickname} edited a status"
214 assert Impl.format_title(%{activity: activity, type: "update"}) ==
218 test "renders title for create activity with direct visibility" do
219 user = insert(:user, nickname: "Bob")
222 CommonAPI.post(user, %{
223 visibility: "direct",
224 status: "This is just between you and me, pal"
227 assert Impl.format_title(%{activity: activity}) ==
231 describe "build_content/3" do
232 test "hides contents of notifications when option enabled" do
233 user = insert(:user, nickname: "Bob")
236 insert(:user, nickname: "Rob", notification_settings: %{hide_notification_contents: true})
239 CommonAPI.post(user, %{
240 visibility: "direct",
241 status: "<Lorem ipsum dolor sit amet."
244 notif = insert(:notification, user: user2, activity: activity)
246 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
247 object = Object.normalize(activity, fetch: false)
249 assert Impl.build_content(notif, actor, object) == %{
250 body: "New Direct Message"
254 CommonAPI.post(user, %{
255 visibility: "public",
256 status: "<Lorem ipsum dolor sit amet."
259 notif = insert(:notification, user: user2, activity: activity, type: "mention")
261 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
262 object = Object.normalize(activity, fetch: false)
264 assert Impl.build_content(notif, actor, object) == %{
268 {:ok, activity} = CommonAPI.favorite(user, activity.id)
270 notif = insert(:notification, user: user2, activity: activity, type: "favourite")
272 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
273 object = Object.normalize(activity, fetch: false)
275 assert Impl.build_content(notif, actor, object) == %{
280 test "returns regular content when hiding contents option disabled" do
281 user = insert(:user, nickname: "Bob")
284 insert(:user, nickname: "Rob", notification_settings: %{hide_notification_contents: false})
287 CommonAPI.post(user, %{
288 visibility: "direct",
290 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
293 notif = insert(:notification, user: user2, activity: activity)
295 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
296 object = Object.normalize(activity, fetch: false)
298 assert Impl.build_content(notif, actor, object) == %{
300 "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini...",
301 title: "New Direct Message"
305 CommonAPI.post(user, %{
306 visibility: "public",
308 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
311 notif = insert(:notification, user: user2, activity: activity, type: "mention")
313 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
314 object = Object.normalize(activity, fetch: false)
316 assert Impl.build_content(notif, actor, object) == %{
318 "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini...",
322 {:ok, activity} = CommonAPI.favorite(user, activity.id)
324 notif = insert(:notification, user: user2, activity: activity, type: "favourite")
326 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
327 object = Object.normalize(activity, fetch: false)
329 assert Impl.build_content(notif, actor, object) == %{
330 body: "@Bob has favorited your post",
331 title: "New Favorite"