1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Push.ImplTest do
10 alias Pleroma.Web.CommonAPI
11 alias Pleroma.Web.Push.Impl
12 alias Pleroma.Web.Push.Subscription
14 import Pleroma.Factory
17 Tesla.Mock.mock_global(fn
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."})
66 assert Impl.perform(notif) == [:ok, :ok]
69 @tag capture_log: true
70 test "returns error if notif does not match " do
71 assert Impl.perform(%{}) == :error
74 test "successful message sending" do
75 assert Impl.push_message(@message, @sub, @api_key, %Subscription{}) == :ok
78 @tag capture_log: true
79 test "fail message sending" do
80 assert Impl.push_message(
82 Map.merge(@sub, %{endpoint: "https://example.com/example/bad"}),
88 test "delete subscription if result send message between 400..500" do
89 subscription = insert(:push_subscription)
91 assert Impl.push_message(
93 Map.merge(@sub, %{endpoint: "https://example.com/example/not_found"}),
98 refute Pleroma.Repo.get(Subscription, subscription.id)
101 test "renders title and body for create activity" do
102 user = insert(:user, nickname: "Bob")
105 CommonAPI.post(user, %{
107 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
110 object = Object.normalize(activity)
112 assert Impl.format_body(
119 "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
121 assert Impl.format_title(%{activity: activity}) ==
125 test "renders title and body for follow activity" do
126 user = insert(:user, nickname: "Bob")
127 other_user = insert(:user)
128 {:ok, _, _, activity} = CommonAPI.follow(user, other_user)
129 object = Object.normalize(activity)
131 assert Impl.format_body(%{activity: activity}, user, object) == "@Bob has followed you"
133 assert Impl.format_title(%{activity: activity}) ==
137 test "renders title and body for announce activity" do
141 CommonAPI.post(user, %{
143 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
146 {:ok, announce_activity, _} = CommonAPI.repeat(activity.id, user)
147 object = Object.normalize(activity)
149 assert Impl.format_body(%{activity: announce_activity}, user, object) ==
150 "@#{user.nickname} repeated: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
152 assert Impl.format_title(%{activity: announce_activity}) ==
156 test "renders title and body for like activity" do
157 user = insert(:user, nickname: "Bob")
160 CommonAPI.post(user, %{
162 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
165 {:ok, activity, _} = CommonAPI.favorite(activity.id, user)
166 object = Object.normalize(activity)
168 assert Impl.format_body(%{activity: activity}, user, object) == "@Bob has favorited your post"
170 assert Impl.format_title(%{activity: activity}) ==
174 test "renders title for create activity with direct visibility" do
175 user = insert(:user, nickname: "Bob")
178 CommonAPI.post(user, %{
179 "visibility" => "direct",
180 "status" => "This is just between you and me, pal"
183 assert Impl.format_title(%{activity: activity}) ==
187 describe "build_content/3" do
188 test "returns info content for direct message with enabled privacy option" do
189 user = insert(:user, nickname: "Bob")
190 user2 = insert(:user, nickname: "Rob", notification_settings: %{privacy_option: true})
193 CommonAPI.post(user, %{
194 "visibility" => "direct",
195 "status" => "<Lorem ipsum dolor sit amet."
198 notif = insert(:notification, user: user2, activity: activity)
200 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
201 object = Object.normalize(activity)
203 assert Impl.build_content(notif, actor, object) == %{
205 title: "New Direct Message"
209 test "returns regular content for direct message with disabled privacy option" do
210 user = insert(:user, nickname: "Bob")
211 user2 = insert(:user, nickname: "Rob", notification_settings: %{privacy_option: false})
214 CommonAPI.post(user, %{
215 "visibility" => "direct",
217 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
220 notif = insert(:notification, user: user2, activity: activity)
222 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
223 object = Object.normalize(activity)
225 assert Impl.build_content(notif, actor, object) == %{
227 "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini...",
228 title: "New Direct Message"