326a67963410e71c17ef096c14a25827b95c038f
[akkoma] / test / pleroma / web / push / impl_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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.Notification
11 alias Pleroma.Object
12 alias Pleroma.User
13 alias Pleroma.Web.ActivityPub.ActivityPub
14 alias Pleroma.Web.CommonAPI
15 alias Pleroma.Web.Push.Impl
16 alias Pleroma.Web.Push.Subscription
17
18 setup do
19 Tesla.Mock.mock(fn
20 %{method: :post, url: "https://example.com/example/1234"} ->
21 %Tesla.Env{status: 200}
22
23 %{method: :post, url: "https://example.com/example/not_found"} ->
24 %Tesla.Env{status: 400}
25
26 %{method: :post, url: "https://example.com/example/bad"} ->
27 %Tesla.Env{status: 100}
28 end)
29
30 :ok
31 end
32
33 @sub %{
34 endpoint: "https://example.com/example/1234",
35 keys: %{
36 auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
37 p256dh:
38 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA="
39 }
40 }
41 @api_key "BASgACIHpN1GYgzSRp"
42 @message "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
43
44 test "performs sending notifications" do
45 user = insert(:user)
46 user2 = insert(:user)
47 insert(:push_subscription, user: user, data: %{alerts: %{"mention" => true}})
48 insert(:push_subscription, user: user2, data: %{alerts: %{"mention" => true}})
49
50 insert(:push_subscription,
51 user: user,
52 data: %{alerts: %{"follow" => true, "mention" => true}}
53 )
54
55 insert(:push_subscription,
56 user: user,
57 data: %{alerts: %{"follow" => true, "mention" => false}}
58 )
59
60 {:ok, activity} = CommonAPI.post(user, %{status: "<Lorem ipsum dolor sit amet."})
61
62 notif =
63 insert(:notification,
64 user: user,
65 activity: activity,
66 type: "mention"
67 )
68
69 assert Impl.perform(notif) == {:ok, [:ok, :ok]}
70 end
71
72 @tag capture_log: true
73 test "returns error if notif does not match " do
74 assert Impl.perform(%{}) == {:error, :unknown_type}
75 end
76
77 test "successful message sending" do
78 assert Impl.push_message(@message, @sub, @api_key, %Subscription{}) == :ok
79 end
80
81 @tag capture_log: true
82 test "fail message sending" do
83 assert Impl.push_message(
84 @message,
85 Map.merge(@sub, %{endpoint: "https://example.com/example/bad"}),
86 @api_key,
87 %Subscription{}
88 ) == :error
89 end
90
91 test "delete subscription if result send message between 400..500" do
92 subscription = insert(:push_subscription)
93
94 assert Impl.push_message(
95 @message,
96 Map.merge(@sub, %{endpoint: "https://example.com/example/not_found"}),
97 @api_key,
98 subscription
99 ) == :ok
100
101 refute Pleroma.Repo.get(Subscription, subscription.id)
102 end
103
104 test "deletes subscription when token has been deleted" do
105 subscription = insert(:push_subscription)
106
107 Pleroma.Repo.delete(subscription.token)
108
109 refute Pleroma.Repo.get(Subscription, subscription.id)
110 end
111
112 test "renders title and body for create activity" do
113 user = insert(:user, nickname: "Bob")
114
115 {:ok, activity} =
116 CommonAPI.post(user, %{
117 status:
118 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
119 })
120
121 object = Object.normalize(activity)
122
123 assert Impl.format_body(
124 %{
125 activity: activity
126 },
127 user,
128 object
129 ) ==
130 "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
131
132 assert Impl.format_title(%{activity: activity, type: "mention"}) ==
133 "New Mention"
134 end
135
136 test "renders title and body for follow activity" do
137 user = insert(:user, nickname: "Bob")
138 other_user = insert(:user)
139 {:ok, _, _, activity} = CommonAPI.follow(user, other_user)
140 object = Object.normalize(activity, false)
141
142 assert Impl.format_body(%{activity: activity, type: "follow"}, user, object) ==
143 "@Bob has followed you"
144
145 assert Impl.format_title(%{activity: activity, type: "follow"}) ==
146 "New Follower"
147 end
148
149 test "renders title and body for announce activity" do
150 user = insert(:user)
151
152 {:ok, activity} =
153 CommonAPI.post(user, %{
154 status:
155 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
156 })
157
158 {:ok, announce_activity} = CommonAPI.repeat(activity.id, user)
159 object = Object.normalize(activity)
160
161 assert Impl.format_body(%{activity: announce_activity}, user, object) ==
162 "@#{user.nickname} repeated: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
163
164 assert Impl.format_title(%{activity: announce_activity, type: "reblog"}) ==
165 "New Repeat"
166 end
167
168 test "renders title and body for like activity" do
169 user = insert(:user, nickname: "Bob")
170
171 {:ok, activity} =
172 CommonAPI.post(user, %{
173 status:
174 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
175 })
176
177 {:ok, activity} = CommonAPI.favorite(user, activity.id)
178 object = Object.normalize(activity)
179
180 assert Impl.format_body(%{activity: activity, type: "favourite"}, user, object) ==
181 "@Bob has favorited your post"
182
183 assert Impl.format_title(%{activity: activity, type: "favourite"}) ==
184 "New Favorite"
185 end
186
187 test "renders title and body for pleroma:emoji_reaction activity" do
188 user = insert(:user, nickname: "Bob")
189
190 {:ok, activity} =
191 CommonAPI.post(user, %{
192 status: "This post is a really good post!"
193 })
194
195 {:ok, activity} = CommonAPI.react_with_emoji(activity.id, user, "👍")
196 object = Object.normalize(activity)
197
198 assert Impl.format_body(%{activity: activity, type: "pleroma:emoji_reaction"}, user, object) ==
199 "@Bob reacted with 👍"
200
201 assert Impl.format_title(%{activity: activity, type: "pleroma:emoji_reaction"}) ==
202 "New Reaction"
203 end
204
205 test "renders title for create activity with direct visibility" do
206 user = insert(:user, nickname: "Bob")
207
208 {:ok, activity} =
209 CommonAPI.post(user, %{
210 visibility: "direct",
211 status: "This is just between you and me, pal"
212 })
213
214 assert Impl.format_title(%{activity: activity}) ==
215 "New Direct Message"
216 end
217
218 describe "build_content/3" do
219 test "builds content for chat messages" do
220 user = insert(:user)
221 recipient = insert(:user)
222
223 {:ok, chat} = CommonAPI.post_chat_message(user, recipient, "hey")
224 object = Object.normalize(chat, false)
225 [notification] = Notification.for_user(recipient)
226
227 res = Impl.build_content(notification, user, object)
228
229 assert res == %{
230 body: "@#{user.nickname}: hey",
231 title: "New Chat Message"
232 }
233 end
234
235 test "builds content for chat messages with no content" do
236 user = insert(:user)
237 recipient = insert(:user)
238
239 file = %Plug.Upload{
240 content_type: "image/jpeg",
241 path: Path.absname("test/fixtures/image.jpg"),
242 filename: "an_image.jpg"
243 }
244
245 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
246
247 {:ok, chat} = CommonAPI.post_chat_message(user, recipient, nil, media_id: upload.id)
248 object = Object.normalize(chat, false)
249 [notification] = Notification.for_user(recipient)
250
251 res = Impl.build_content(notification, user, object)
252
253 assert res == %{
254 body: "@#{user.nickname}: (Attachment)",
255 title: "New Chat Message"
256 }
257 end
258
259 test "hides contents of notifications when option enabled" do
260 user = insert(:user, nickname: "Bob")
261
262 user2 =
263 insert(:user, nickname: "Rob", notification_settings: %{hide_notification_contents: true})
264
265 {:ok, activity} =
266 CommonAPI.post(user, %{
267 visibility: "direct",
268 status: "<Lorem ipsum dolor sit amet."
269 })
270
271 notif = insert(:notification, user: user2, activity: activity)
272
273 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
274 object = Object.normalize(activity)
275
276 assert Impl.build_content(notif, actor, object) == %{
277 body: "New Direct Message"
278 }
279
280 {:ok, activity} =
281 CommonAPI.post(user, %{
282 visibility: "public",
283 status: "<Lorem ipsum dolor sit amet."
284 })
285
286 notif = insert(:notification, user: user2, activity: activity, type: "mention")
287
288 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
289 object = Object.normalize(activity)
290
291 assert Impl.build_content(notif, actor, object) == %{
292 body: "New Mention"
293 }
294
295 {:ok, activity} = CommonAPI.favorite(user, activity.id)
296
297 notif = insert(:notification, user: user2, activity: activity, type: "favourite")
298
299 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
300 object = Object.normalize(activity)
301
302 assert Impl.build_content(notif, actor, object) == %{
303 body: "New Favorite"
304 }
305 end
306
307 test "returns regular content when hiding contents option disabled" do
308 user = insert(:user, nickname: "Bob")
309
310 user2 =
311 insert(:user, nickname: "Rob", notification_settings: %{hide_notification_contents: false})
312
313 {:ok, activity} =
314 CommonAPI.post(user, %{
315 visibility: "direct",
316 status:
317 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
318 })
319
320 notif = insert(:notification, user: user2, activity: activity)
321
322 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
323 object = Object.normalize(activity)
324
325 assert Impl.build_content(notif, actor, object) == %{
326 body:
327 "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini...",
328 title: "New Direct Message"
329 }
330
331 {:ok, activity} =
332 CommonAPI.post(user, %{
333 visibility: "public",
334 status:
335 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
336 })
337
338 notif = insert(:notification, user: user2, activity: activity, type: "mention")
339
340 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
341 object = Object.normalize(activity)
342
343 assert Impl.build_content(notif, actor, object) == %{
344 body:
345 "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini...",
346 title: "New Mention"
347 }
348
349 {:ok, activity} = CommonAPI.favorite(user, activity.id)
350
351 notif = insert(:notification, user: user2, activity: activity, type: "favourite")
352
353 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
354 object = Object.normalize(activity)
355
356 assert Impl.build_content(notif, actor, object) == %{
357 body: "@Bob has favorited your post",
358 title: "New Favorite"
359 }
360 end
361 end
362 end