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_send(notif) == [:ok, :ok]
70 test "returns error if notif does not match " do
71 assert Impl.perform_send(%{}) == :error
74 test "successful message sending" do
75 assert Impl.push_message(@message, @sub, @api_key, %Subscription{}) == :ok
78 test "fail message sending" do
79 assert Impl.push_message(
81 Map.merge(@sub, %{endpoint: "https://example.com/example/bad"}),
87 test "delete subsciption if restult send message between 400..500" do
88 subscription = insert(:push_subscription)
90 assert Impl.push_message(
92 Map.merge(@sub, %{endpoint: "https://example.com/example/not_found"}),
97 refute Pleroma.Repo.get(Subscription, subscription.id)
100 test "renders body for create activity" do
101 assert Impl.format_body(
108 "<span>Lorem ipsum dolor sit amet</span>, consectetur :bear: adipiscing elit. Fusce sagittis finibus turpis."
115 "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
118 test "renders body for follow activity" do
119 assert Impl.format_body(%{activity: %{data: %{"type" => "Follow"}}}, %{nickname: "Bob"}) ==
120 "@Bob has followed you"
123 test "renders body for announce activity" do
130 "<span>Lorem ipsum dolor sit amet</span>, consectetur :bear: adipiscing elit. Fusce sagittis finibus turpis."
134 note_activity = insert(:note_activity, %{note: note})
135 announce_activity = insert(:announce_activity, %{user: user, note_activity: note_activity})
137 assert Impl.format_body(%{activity: announce_activity}, user) ==
138 "@#{user.nickname} repeated: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
141 test "renders body for like activity" do
142 assert Impl.format_body(%{activity: %{data: %{"type" => "Like"}}}, %{nickname: "Bob"}) ==
143 "@Bob has favorited your post"