1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Push.ImplTest do
8 alias Pleroma.Web.Push.Impl
9 alias Pleroma.Web.Push.Subscription
11 import Pleroma.Factory
14 Tesla.Mock.mock_global(fn
15 %{method: :post, url: "https://example.com/example/1234"} ->
16 %Tesla.Env{status: 200}
18 %{method: :post, url: "https://example.com/example/not_found"} ->
19 %Tesla.Env{status: 400}
21 %{method: :post, url: "https://example.com/example/bad"} ->
22 %Tesla.Env{status: 100}
29 endpoint: "https://example.com/example/1234",
31 auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
33 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA="
36 @api_key "BASgACIHpN1GYgzSRp"
37 @message "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
39 test "performs sending notifications" do
42 insert(:push_subscription, user: user, data: %{alerts: %{"mention" => true}})
43 insert(:push_subscription, user: user2, data: %{alerts: %{"mention" => true}})
45 insert(:push_subscription,
47 data: %{alerts: %{"follow" => true, "mention" => true}}
50 insert(:push_subscription,
52 data: %{alerts: %{"follow" => true, "mention" => false}}
58 activity: %Pleroma.Activity{
61 "actor" => user.ap_id,
62 "object" => %{"content" => "<Lorem ipsum dolor sit amet."}
67 assert Impl.perform(notif) == [:ok, :ok]
70 @tag capture_log: true
71 test "returns error if notif does not match " do
72 assert Impl.perform(%{}) == :error
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 subsciption if restult 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 "renders body for create activity" do
103 assert Impl.format_body(
110 "<span>Lorem ipsum dolor sit amet</span>, consectetur :bear: adipiscing elit. Fusce sagittis finibus turpis."
117 "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
120 test "renders body for follow activity" do
121 assert Impl.format_body(%{activity: %{data: %{"type" => "Follow"}}}, %{nickname: "Bob"}) ==
122 "@Bob has followed you"
125 test "renders body for announce activity" do
132 "<span>Lorem ipsum dolor sit amet</span>, consectetur :bear: adipiscing elit. Fusce sagittis finibus turpis."
136 note_activity = insert(:note_activity, %{note: note})
137 announce_activity = insert(:announce_activity, %{user: user, note_activity: note_activity})
139 assert Impl.format_body(%{activity: announce_activity}, user) ==
140 "@#{user.nickname} repeated: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
143 test "renders body for like activity" do
144 assert Impl.format_body(%{activity: %{data: %{"type" => "Like"}}}, %{nickname: "Bob"}) ==
145 "@Bob has favorited your post"