Merge branch 'develop' into issue/1383
[akkoma] / test / web / push / impl_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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.Object
9 alias Pleroma.User
10 alias Pleroma.Web.CommonAPI
11 alias Pleroma.Web.Push.Impl
12 alias Pleroma.Web.Push.Subscription
13
14 import Pleroma.Factory
15
16 setup_all do
17 Tesla.Mock.mock_global(fn
18 %{method: :post, url: "https://example.com/example/1234"} ->
19 %Tesla.Env{status: 200}
20
21 %{method: :post, url: "https://example.com/example/not_found"} ->
22 %Tesla.Env{status: 400}
23
24 %{method: :post, url: "https://example.com/example/bad"} ->
25 %Tesla.Env{status: 100}
26 end)
27
28 :ok
29 end
30
31 @sub %{
32 endpoint: "https://example.com/example/1234",
33 keys: %{
34 auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
35 p256dh:
36 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA="
37 }
38 }
39 @api_key "BASgACIHpN1GYgzSRp"
40 @message "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
41
42 test "performs sending notifications" do
43 user = insert(:user)
44 user2 = insert(:user)
45 insert(:push_subscription, user: user, data: %{alerts: %{"mention" => true}})
46 insert(:push_subscription, user: user2, data: %{alerts: %{"mention" => true}})
47
48 insert(:push_subscription,
49 user: user,
50 data: %{alerts: %{"follow" => true, "mention" => true}}
51 )
52
53 insert(:push_subscription,
54 user: user,
55 data: %{alerts: %{"follow" => true, "mention" => false}}
56 )
57
58 {:ok, activity} = CommonAPI.post(user, %{"status" => "<Lorem ipsum dolor sit amet."})
59
60 notif =
61 insert(:notification,
62 user: user,
63 activity: activity
64 )
65
66 assert Impl.perform(notif) == [:ok, :ok]
67 end
68
69 @tag capture_log: true
70 test "returns error if notif does not match " do
71 assert Impl.perform(%{}) == :error
72 end
73
74 test "successful message sending" do
75 assert Impl.push_message(@message, @sub, @api_key, %Subscription{}) == :ok
76 end
77
78 @tag capture_log: true
79 test "fail message sending" do
80 assert Impl.push_message(
81 @message,
82 Map.merge(@sub, %{endpoint: "https://example.com/example/bad"}),
83 @api_key,
84 %Subscription{}
85 ) == :error
86 end
87
88 test "delete subscription if result send message between 400..500" do
89 subscription = insert(:push_subscription)
90
91 assert Impl.push_message(
92 @message,
93 Map.merge(@sub, %{endpoint: "https://example.com/example/not_found"}),
94 @api_key,
95 subscription
96 ) == :ok
97
98 refute Pleroma.Repo.get(Subscription, subscription.id)
99 end
100
101 test "renders title and body for create activity" do
102 user = insert(:user, nickname: "Bob")
103
104 {:ok, activity} =
105 CommonAPI.post(user, %{
106 "status" =>
107 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
108 })
109
110 object = Object.normalize(activity)
111
112 assert Impl.format_body(
113 %{
114 activity: activity
115 },
116 user,
117 object
118 ) ==
119 "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
120
121 assert Impl.format_title(%{activity: activity}) ==
122 "New Mention"
123 end
124
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)
130
131 assert Impl.format_body(%{activity: activity}, user, object) == "@Bob has followed you"
132
133 assert Impl.format_title(%{activity: activity}) ==
134 "New Follower"
135 end
136
137 test "renders title and body for announce activity" do
138 user = insert(:user)
139
140 {:ok, activity} =
141 CommonAPI.post(user, %{
142 "status" =>
143 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
144 })
145
146 {:ok, announce_activity, _} = CommonAPI.repeat(activity.id, user)
147 object = Object.normalize(activity)
148
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..."
151
152 assert Impl.format_title(%{activity: announce_activity}) ==
153 "New Repeat"
154 end
155
156 test "renders title and body for like activity" do
157 user = insert(:user, nickname: "Bob")
158
159 {:ok, activity} =
160 CommonAPI.post(user, %{
161 "status" =>
162 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
163 })
164
165 {:ok, activity, _} = CommonAPI.favorite(activity.id, user)
166 object = Object.normalize(activity)
167
168 assert Impl.format_body(%{activity: activity}, user, object) == "@Bob has favorited your post"
169
170 assert Impl.format_title(%{activity: activity}) ==
171 "New Favorite"
172 end
173
174 test "renders title for create activity with direct visibility" do
175 user = insert(:user, nickname: "Bob")
176
177 {:ok, activity} =
178 CommonAPI.post(user, %{
179 "visibility" => "direct",
180 "status" => "This is just between you and me, pal"
181 })
182
183 assert Impl.format_title(%{activity: activity}) ==
184 "New Direct Message"
185 end
186
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})
191
192 {:ok, activity} =
193 CommonAPI.post(user, %{
194 "visibility" => "direct",
195 "status" => "<Lorem ipsum dolor sit amet."
196 })
197
198 notif = insert(:notification, user: user2, activity: activity)
199
200 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
201 object = Object.normalize(activity)
202
203 assert Impl.build_content(notif, actor, object) == %{
204 body: "@Bob",
205 title: "New Direct Message"
206 }
207 end
208
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})
212
213 {:ok, activity} =
214 CommonAPI.post(user, %{
215 "visibility" => "direct",
216 "status" =>
217 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
218 })
219
220 notif = insert(:notification, user: user2, activity: activity)
221
222 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
223 object = Object.normalize(activity)
224
225 assert Impl.build_content(notif, actor, object) == %{
226 body:
227 "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini...",
228 title: "New Direct Message"
229 }
230 end
231 end
232 end