Merge branch 'issue/1878' into 'develop'
[akkoma] / test / 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
7
8 alias Pleroma.Notification
9 alias Pleroma.Object
10 alias Pleroma.User
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.CommonAPI
13 alias Pleroma.Web.Push.Impl
14 alias Pleroma.Web.Push.Subscription
15
16 import Pleroma.Factory
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 for create activity with direct visibility" do
188 user = insert(:user, nickname: "Bob")
189
190 {:ok, activity} =
191 CommonAPI.post(user, %{
192 visibility: "direct",
193 status: "This is just between you and me, pal"
194 })
195
196 assert Impl.format_title(%{activity: activity}) ==
197 "New Direct Message"
198 end
199
200 describe "build_content/3" do
201 test "builds content for chat messages" do
202 user = insert(:user)
203 recipient = insert(:user)
204
205 {:ok, chat} = CommonAPI.post_chat_message(user, recipient, "hey")
206 object = Object.normalize(chat, false)
207 [notification] = Notification.for_user(recipient)
208
209 res = Impl.build_content(notification, user, object)
210
211 assert res == %{
212 body: "@#{user.nickname}: hey",
213 title: "New Chat Message"
214 }
215 end
216
217 test "builds content for chat messages with no content" do
218 user = insert(:user)
219 recipient = insert(:user)
220
221 file = %Plug.Upload{
222 content_type: "image/jpg",
223 path: Path.absname("test/fixtures/image.jpg"),
224 filename: "an_image.jpg"
225 }
226
227 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
228
229 {:ok, chat} = CommonAPI.post_chat_message(user, recipient, nil, media_id: upload.id)
230 object = Object.normalize(chat, false)
231 [notification] = Notification.for_user(recipient)
232
233 res = Impl.build_content(notification, user, object)
234
235 assert res == %{
236 body: "@#{user.nickname}: (Attachment)",
237 title: "New Chat Message"
238 }
239 end
240
241 test "hides contents of notifications when option enabled" do
242 user = insert(:user, nickname: "Bob")
243
244 user2 =
245 insert(:user, nickname: "Rob", notification_settings: %{hide_notification_contents: true})
246
247 {:ok, activity} =
248 CommonAPI.post(user, %{
249 visibility: "direct",
250 status: "<Lorem ipsum dolor sit amet."
251 })
252
253 notif = insert(:notification, user: user2, activity: activity)
254
255 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
256 object = Object.normalize(activity)
257
258 assert Impl.build_content(notif, actor, object) == %{
259 body: "New Direct Message"
260 }
261
262 {:ok, activity} =
263 CommonAPI.post(user, %{
264 visibility: "public",
265 status: "<Lorem ipsum dolor sit amet."
266 })
267
268 notif = insert(:notification, user: user2, activity: activity, type: "mention")
269
270 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
271 object = Object.normalize(activity)
272
273 assert Impl.build_content(notif, actor, object) == %{
274 body: "New Mention"
275 }
276
277 {:ok, activity} = CommonAPI.favorite(user, activity.id)
278
279 notif = insert(:notification, user: user2, activity: activity, type: "favourite")
280
281 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
282 object = Object.normalize(activity)
283
284 assert Impl.build_content(notif, actor, object) == %{
285 body: "New Favorite"
286 }
287 end
288
289 test "returns regular content when hiding contents option disabled" do
290 user = insert(:user, nickname: "Bob")
291
292 user2 =
293 insert(:user, nickname: "Rob", notification_settings: %{hide_notification_contents: false})
294
295 {:ok, activity} =
296 CommonAPI.post(user, %{
297 visibility: "direct",
298 status:
299 "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
300 })
301
302 notif = insert(:notification, user: user2, activity: activity)
303
304 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
305 object = Object.normalize(activity)
306
307 assert Impl.build_content(notif, actor, object) == %{
308 body:
309 "@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini...",
310 title: "New Direct Message"
311 }
312
313 {:ok, activity} =
314 CommonAPI.post(user, %{
315 visibility: "public",
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, type: "mention")
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 Mention"
329 }
330
331 {:ok, activity} = CommonAPI.favorite(user, activity.id)
332
333 notif = insert(:notification, user: user2, activity: activity, type: "favourite")
334
335 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
336 object = Object.normalize(activity)
337
338 assert Impl.build_content(notif, actor, object) == %{
339 body: "@Bob has favorited your post",
340 title: "New Favorite"
341 }
342 end
343 end
344 end