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
9 alias Pleroma.Web.CommonAPI
10 alias Pleroma.Web.Push.Impl
11 alias Pleroma.Web.Push.Subscription
13 import Pleroma.Factory
16 Tesla.Mock.mock_global(fn
17 %{method: :post, url: "https://example.com/example/1234"} ->
18 %Tesla.Env{status: 200}
20 %{method: :post, url: "https://example.com/example/not_found"} ->
21 %Tesla.Env{status: 400}
23 %{method: :post, url: "https://example.com/example/bad"} ->
24 %Tesla.Env{status: 100}
31 endpoint: "https://example.com/example/1234",
33 auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
35 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA="
38 @api_key "BASgACIHpN1GYgzSRp"
39 @message "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
41 test "performs sending notifications" do
44 insert(:push_subscription, user: user, data: %{alerts: %{"mention" => true}})
45 insert(:push_subscription, user: user2, data: %{alerts: %{"mention" => true}})
47 insert(:push_subscription,
49 data: %{alerts: %{"follow" => true, "mention" => true}}
52 insert(:push_subscription,
54 data: %{alerts: %{"follow" => true, "mention" => false}}
57 {:ok, activity} = CommonAPI.post(user, %{"status" => "<Lorem ipsum dolor sit amet."})
65 assert Impl.perform(notif) == [:ok, :ok]
68 @tag capture_log: true
69 test "returns error if notif does not match " do
70 assert Impl.perform(%{}) == :error
73 test "successful message sending" do
74 assert Impl.push_message(@message, @sub, @api_key, %Subscription{}) == :ok
77 @tag capture_log: true
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 user = insert(:user, nickname: "Bob")
104 CommonAPI.post(user, %{
106 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
109 object = Object.normalize(activity)
111 assert Impl.format_body(
118 "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
121 test "renders body for follow activity" do
122 user = insert(:user, nickname: "Bob")
123 other_user = insert(:user)
124 {:ok, _, _, activity} = CommonAPI.follow(user, other_user)
125 object = Object.normalize(activity)
127 assert Impl.format_body(%{activity: activity}, user, object) == "@Bob has followed you"
130 test "renders body for announce activity" do
134 CommonAPI.post(user, %{
136 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
139 {:ok, announce_activity, _} = CommonAPI.repeat(activity.id, user)
140 object = Object.normalize(activity)
142 assert Impl.format_body(%{activity: announce_activity}, user, object) ==
143 "@#{user.nickname} repeated: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
146 test "renders body for like activity" do
147 user = insert(:user, nickname: "Bob")
150 CommonAPI.post(user, %{
152 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
155 {:ok, activity, _} = CommonAPI.favorite(activity.id, user)
156 object = Object.normalize(activity)
158 assert Impl.format_body(%{activity: activity}, user, object) == "@Bob has favorited your post"