Add compressed background
[akkoma] / test / web / push / impl_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Push.ImplTest do
6 use Pleroma.DataCase
7
8 alias Pleroma.Web.Push.Impl
9 alias Pleroma.Web.Push.Subscription
10
11 import Pleroma.Factory
12
13 setup_all do
14 Tesla.Mock.mock_global(fn
15 %{method: :post, url: "https://example.com/example/1234"} ->
16 %Tesla.Env{status: 200}
17
18 %{method: :post, url: "https://example.com/example/not_found"} ->
19 %Tesla.Env{status: 400}
20
21 %{method: :post, url: "https://example.com/example/bad"} ->
22 %Tesla.Env{status: 100}
23 end)
24
25 :ok
26 end
27
28 @sub %{
29 endpoint: "https://example.com/example/1234",
30 keys: %{
31 auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
32 p256dh:
33 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA="
34 }
35 }
36 @api_key "BASgACIHpN1GYgzSRp"
37 @message "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
38
39 test "performs sending notifications" do
40 user = insert(:user)
41 user2 = insert(:user)
42 insert(:push_subscription, user: user, data: %{alerts: %{"mention" => true}})
43 insert(:push_subscription, user: user2, data: %{alerts: %{"mention" => true}})
44
45 insert(:push_subscription,
46 user: user,
47 data: %{alerts: %{"follow" => true, "mention" => true}}
48 )
49
50 insert(:push_subscription,
51 user: user,
52 data: %{alerts: %{"follow" => true, "mention" => false}}
53 )
54
55 notif =
56 insert(:notification,
57 user: user,
58 activity: %Pleroma.Activity{
59 data: %{
60 "type" => "Create",
61 "actor" => user.ap_id,
62 "object" => %{"content" => "<Lorem ipsum dolor sit amet."}
63 }
64 }
65 )
66
67 assert Impl.perform(notif) == [:ok, :ok]
68 end
69
70 @tag capture_log: true
71 test "returns error if notif does not match " do
72 assert Impl.perform(%{}) == :error
73 end
74
75 test "successful message sending" do
76 assert Impl.push_message(@message, @sub, @api_key, %Subscription{}) == :ok
77 end
78
79 @tag capture_log: true
80 test "fail message sending" do
81 assert Impl.push_message(
82 @message,
83 Map.merge(@sub, %{endpoint: "https://example.com/example/bad"}),
84 @api_key,
85 %Subscription{}
86 ) == :error
87 end
88
89 test "delete subsciption if restult send message between 400..500" do
90 subscription = insert(:push_subscription)
91
92 assert Impl.push_message(
93 @message,
94 Map.merge(@sub, %{endpoint: "https://example.com/example/not_found"}),
95 @api_key,
96 subscription
97 ) == :ok
98
99 refute Pleroma.Repo.get(Subscription, subscription.id)
100 end
101
102 test "renders body for create activity" do
103 assert Impl.format_body(
104 %{
105 activity: %{
106 data: %{
107 "type" => "Create",
108 "object" => %{
109 "content" =>
110 "<span>Lorem ipsum dolor sit amet</span>, consectetur :bear: adipiscing elit. Fusce sagittis finibus turpis."
111 }
112 }
113 }
114 },
115 %{nickname: "Bob"}
116 ) ==
117 "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
118 end
119
120 test "renders body for follow activity" do
121 assert Impl.format_body(%{activity: %{data: %{"type" => "Follow"}}}, %{nickname: "Bob"}) ==
122 "@Bob has followed you"
123 end
124
125 test "renders body for announce activity" do
126 user = insert(:user)
127
128 note =
129 insert(:note, %{
130 data: %{
131 "content" =>
132 "<span>Lorem ipsum dolor sit amet</span>, consectetur :bear: adipiscing elit. Fusce sagittis finibus turpis."
133 }
134 })
135
136 note_activity = insert(:note_activity, %{note: note})
137 announce_activity = insert(:announce_activity, %{user: user, note_activity: note_activity})
138
139 assert Impl.format_body(%{activity: announce_activity}, user) ==
140 "@#{user.nickname} repeated: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
141 end
142
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"
146 end
147 end