Add tests, changelog entry
[akkoma] / test / pleroma / web / push / impl_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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, async: true
7
8 import Pleroma.Factory
9
10 alias Pleroma.Object
11 alias Pleroma.User
12 alias Pleroma.Web.CommonAPI
13 alias Pleroma.Web.Push.Impl
14 alias Pleroma.Web.Push.Subscription
15
16 setup do
17 Tesla.Mock.mock(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 type: "mention"
65 )
66
67 assert Impl.perform(notif) == {:ok, [: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, :unknown_type}
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 subscription if result 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 "deletes subscription when token has been deleted" do
103 subscription = insert(:push_subscription)
104
105 Pleroma.Repo.delete(subscription.token)
106
107 refute Pleroma.Repo.get(Subscription, subscription.id)
108 end
109
110 test "renders title and body for create activity" do
111 user = insert(:user, nickname: "Bob")
112
113 {:ok, activity} =
114 CommonAPI.post(user, %{
115 status:
116 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
117 })
118
119 object = Object.normalize(activity, fetch: false)
120
121 assert Impl.format_body(
122 %{
123 activity: activity
124 },
125 user,
126 object
127 ) ==
128 "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
129
130 assert Impl.format_title(%{activity: activity, type: "mention"}) ==
131 "New Mention"
132 end
133
134 test "renders title and body for follow activity" do
135 user = insert(:user, nickname: "Bob")
136 other_user = insert(:user)
137 {:ok, _, _, activity} = CommonAPI.follow(user, other_user)
138 object = Object.normalize(activity, fetch: false)
139
140 assert Impl.format_body(%{activity: activity, type: "follow"}, user, object) ==
141 "@Bob has followed you"
142
143 assert Impl.format_title(%{activity: activity, type: "follow"}) ==
144 "New Follower"
145 end
146
147 test "renders title and body for announce activity" do
148 user = insert(:user)
149
150 {:ok, activity} =
151 CommonAPI.post(user, %{
152 status:
153 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
154 })
155
156 {:ok, announce_activity} = CommonAPI.repeat(activity.id, user)
157 object = Object.normalize(activity, fetch: false)
158
159 assert Impl.format_body(%{activity: announce_activity}, user, object) ==
160 "@#{user.nickname} repeated: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
161
162 assert Impl.format_title(%{activity: announce_activity, type: "reblog"}) ==
163 "New Repeat"
164 end
165
166 test "renders title and body for like activity" do
167 user = insert(:user, nickname: "Bob")
168
169 {:ok, activity} =
170 CommonAPI.post(user, %{
171 status:
172 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
173 })
174
175 {:ok, activity} = CommonAPI.favorite(user, activity.id)
176 object = Object.normalize(activity, fetch: false)
177
178 assert Impl.format_body(%{activity: activity, type: "favourite"}, user, object) ==
179 "@Bob has favorited your post"
180
181 assert Impl.format_title(%{activity: activity, type: "favourite"}) ==
182 "New Favorite"
183 end
184
185 test "renders title and body for pleroma:emoji_reaction activity" do
186 user = insert(:user, nickname: "Bob")
187
188 {:ok, activity} =
189 CommonAPI.post(user, %{
190 status: "This post is a really good post!"
191 })
192
193 {:ok, activity} = CommonAPI.react_with_emoji(activity.id, user, "👍")
194 object = Object.normalize(activity, fetch: false)
195
196 assert Impl.format_body(%{activity: activity, type: "pleroma:emoji_reaction"}, user, object) ==
197 "@Bob reacted with 👍"
198
199 assert Impl.format_title(%{activity: activity, type: "pleroma:emoji_reaction"}) ==
200 "New Reaction"
201 end
202
203 test "renders title and body for update activity" do
204 user = insert(:user)
205
206 {:ok, activity} = CommonAPI.post(user, %{status: "lorem ipsum"})
207
208 {:ok, activity} = CommonAPI.update(user, activity, %{status: "edited status"})
209 object = Object.normalize(activity, fetch: false)
210
211 assert Impl.format_body(%{activity: activity, type: "update"}, user, object) ==
212 "@#{user.nickname} edited a status"
213
214 assert Impl.format_title(%{activity: activity, type: "update"}) ==
215 "New Update"
216 end
217
218 test "renders title for create activity with direct visibility" do
219 user = insert(:user, nickname: "Bob")
220
221 {:ok, activity} =
222 CommonAPI.post(user, %{
223 visibility: "direct",
224 status: "This is just between you and me, pal"
225 })
226
227 assert Impl.format_title(%{activity: activity}) ==
228 "New Direct Message"
229 end
230
231 describe "build_content/3" do
232 test "hides contents of notifications when option enabled" do
233 user = insert(:user, nickname: "Bob")
234
235 user2 =
236 insert(:user, nickname: "Rob", notification_settings: %{hide_notification_contents: true})
237
238 {:ok, activity} =
239 CommonAPI.post(user, %{
240 visibility: "direct",
241 status: "<Lorem ipsum dolor sit amet."
242 })
243
244 notif = insert(:notification, user: user2, activity: activity)
245
246 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
247 object = Object.normalize(activity, fetch: false)
248
249 assert Impl.build_content(notif, actor, object) == %{
250 body: "New Direct Message"
251 }
252
253 {:ok, activity} =
254 CommonAPI.post(user, %{
255 visibility: "public",
256 status: "<Lorem ipsum dolor sit amet."
257 })
258
259 notif = insert(:notification, user: user2, activity: activity, type: "mention")
260
261 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
262 object = Object.normalize(activity, fetch: false)
263
264 assert Impl.build_content(notif, actor, object) == %{
265 body: "New Mention"
266 }
267
268 {:ok, activity} = CommonAPI.favorite(user, activity.id)
269
270 notif = insert(:notification, user: user2, activity: activity, type: "favourite")
271
272 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
273 object = Object.normalize(activity, fetch: false)
274
275 assert Impl.build_content(notif, actor, object) == %{
276 body: "New Favorite"
277 }
278 end
279
280 test "returns regular content when hiding contents option disabled" do
281 user = insert(:user, nickname: "Bob")
282
283 user2 =
284 insert(:user, nickname: "Rob", notification_settings: %{hide_notification_contents: false})
285
286 {:ok, activity} =
287 CommonAPI.post(user, %{
288 visibility: "direct",
289 status:
290 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
291 })
292
293 notif = insert(:notification, user: user2, activity: activity)
294
295 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
296 object = Object.normalize(activity, fetch: false)
297
298 assert Impl.build_content(notif, actor, object) == %{
299 body:
300 "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini...",
301 title: "New Direct Message"
302 }
303
304 {:ok, activity} =
305 CommonAPI.post(user, %{
306 visibility: "public",
307 status:
308 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
309 })
310
311 notif = insert(:notification, user: user2, activity: activity, type: "mention")
312
313 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
314 object = Object.normalize(activity, fetch: false)
315
316 assert Impl.build_content(notif, actor, object) == %{
317 body:
318 "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini...",
319 title: "New Mention"
320 }
321
322 {:ok, activity} = CommonAPI.favorite(user, activity.id)
323
324 notif = insert(:notification, user: user2, activity: activity, type: "favourite")
325
326 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
327 object = Object.normalize(activity, fetch: false)
328
329 assert Impl.build_content(notif, actor, object) == %{
330 body: "@Bob has favorited your post",
331 title: "New Favorite"
332 }
333 end
334 end
335 end