Expand and authorize streams in Streamer directly (backport from !2519)
[akkoma] / test / web / common_api / common_api_utils_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.CommonAPI.UtilsTest do
6 alias Pleroma.Builders.UserBuilder
7 alias Pleroma.Object
8 alias Pleroma.Web.CommonAPI
9 alias Pleroma.Web.CommonAPI.Utils
10 alias Pleroma.Web.Endpoint
11 use Pleroma.DataCase
12
13 import ExUnit.CaptureLog
14 import Pleroma.Factory
15
16 @public_address "https://www.w3.org/ns/activitystreams#Public"
17
18 test "it adds attachment links to a given text and attachment set" do
19 name =
20 "Sakura%20Mana%20%E2%80%93%20Turned%20on%20by%20a%20Senior%20OL%20with%20a%20Temptating%20Tight%20Skirt-s%20Full%20Hipline%20and%20Panty%20Shot-%20Beautiful%20Thick%20Thighs-%20and%20Erotic%20Ass-%20-2015-%20--%20Oppaitime%208-28-2017%206-50-33%20PM.png"
21
22 attachment = %{
23 "url" => [%{"href" => name}]
24 }
25
26 res = Utils.add_attachments("", [attachment])
27
28 assert res ==
29 "<br><a href=\"#{name}\" class='attachment'>Sakura Mana – Turned on by a Se…</a>"
30 end
31
32 describe "it confirms the password given is the current users password" do
33 test "incorrect password given" do
34 {:ok, user} = UserBuilder.insert()
35
36 assert Utils.confirm_current_password(user, "") == {:error, "Invalid password."}
37 end
38
39 test "correct password given" do
40 {:ok, user} = UserBuilder.insert()
41 assert Utils.confirm_current_password(user, "test") == {:ok, user}
42 end
43 end
44
45 test "parses emoji from name and bio" do
46 {:ok, user} = UserBuilder.insert(%{name: ":blank:", bio: ":firefox:"})
47
48 expected = [
49 %{
50 "type" => "Emoji",
51 "icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}/emoji/Firefox.gif"},
52 "name" => ":firefox:"
53 },
54 %{
55 "type" => "Emoji",
56 "icon" => %{
57 "type" => "Image",
58 "url" => "#{Endpoint.url()}/emoji/blank.png"
59 },
60 "name" => ":blank:"
61 }
62 ]
63
64 assert expected == Utils.emoji_from_profile(user)
65 end
66
67 describe "format_input/3" do
68 test "works for bare text/plain" do
69 text = "hello world!"
70 expected = "hello world!"
71
72 {output, [], []} = Utils.format_input(text, "text/plain")
73
74 assert output == expected
75
76 text = "hello world!\n\nsecond paragraph!"
77 expected = "hello world!<br><br>second paragraph!"
78
79 {output, [], []} = Utils.format_input(text, "text/plain")
80
81 assert output == expected
82 end
83
84 test "works for bare text/html" do
85 text = "<p>hello world!</p>"
86 expected = "<p>hello world!</p>"
87
88 {output, [], []} = Utils.format_input(text, "text/html")
89
90 assert output == expected
91
92 text = "<p>hello world!</p><br/>\n<p>second paragraph</p>"
93 expected = "<p>hello world!</p><br/>\n<p>second paragraph</p>"
94
95 {output, [], []} = Utils.format_input(text, "text/html")
96
97 assert output == expected
98 end
99
100 test "works for bare text/markdown" do
101 text = "**hello world**"
102 expected = "<p><strong>hello world</strong></p>"
103
104 {output, [], []} = Utils.format_input(text, "text/markdown")
105
106 assert output == expected
107
108 text = "**hello world**\n\n*another paragraph*"
109 expected = "<p><strong>hello world</strong></p><p><em>another paragraph</em></p>"
110
111 {output, [], []} = Utils.format_input(text, "text/markdown")
112
113 assert output == expected
114
115 text = """
116 > cool quote
117
118 by someone
119 """
120
121 expected = "<blockquote><p>cool quote</p></blockquote><p>by someone</p>"
122
123 {output, [], []} = Utils.format_input(text, "text/markdown")
124
125 assert output == expected
126 end
127
128 test "works for bare text/bbcode" do
129 text = "[b]hello world[/b]"
130 expected = "<strong>hello world</strong>"
131
132 {output, [], []} = Utils.format_input(text, "text/bbcode")
133
134 assert output == expected
135
136 text = "[b]hello world![/b]\n\nsecond paragraph!"
137 expected = "<strong>hello world!</strong><br><br>second paragraph!"
138
139 {output, [], []} = Utils.format_input(text, "text/bbcode")
140
141 assert output == expected
142
143 text = "[b]hello world![/b]\n\n<strong>second paragraph!</strong>"
144
145 expected =
146 "<strong>hello world!</strong><br><br>&lt;strong&gt;second paragraph!&lt;/strong&gt;"
147
148 {output, [], []} = Utils.format_input(text, "text/bbcode")
149
150 assert output == expected
151 end
152
153 test "works for text/markdown with mentions" do
154 {:ok, user} =
155 UserBuilder.insert(%{nickname: "user__test", ap_id: "http://foo.com/user__test"})
156
157 text = "**hello world**\n\n*another @user__test and @user__test google.com paragraph*"
158
159 {output, _, _} = Utils.format_input(text, "text/markdown")
160
161 assert output ==
162 ~s(<p><strong>hello world</strong></p><p><em>another <span class="h-card"><a data-user="#{
163 user.id
164 }" class="u-url mention" href="http://foo.com/user__test" rel="ugc">@<span>user__test</span></a></span> and <span class="h-card"><a data-user="#{
165 user.id
166 }" class="u-url mention" href="http://foo.com/user__test" rel="ugc">@<span>user__test</span></a></span> <a href="http://google.com" rel="ugc">google.com</a> paragraph</em></p>)
167 end
168 end
169
170 describe "context_to_conversation_id" do
171 test "creates a mapping object" do
172 conversation_id = Utils.context_to_conversation_id("random context")
173 object = Object.get_by_ap_id("random context")
174
175 assert conversation_id == object.id
176 end
177
178 test "returns an existing mapping for an existing object" do
179 {:ok, object} = Object.context_mapping("random context") |> Repo.insert()
180 conversation_id = Utils.context_to_conversation_id("random context")
181
182 assert conversation_id == object.id
183 end
184 end
185
186 describe "formats date to asctime" do
187 test "when date is in ISO 8601 format" do
188 date = DateTime.utc_now() |> DateTime.to_iso8601()
189
190 expected =
191 date
192 |> DateTime.from_iso8601()
193 |> elem(1)
194 |> Calendar.Strftime.strftime!("%a %b %d %H:%M:%S %z %Y")
195
196 assert Utils.date_to_asctime(date) == expected
197 end
198
199 test "when date is a binary in wrong format" do
200 date = DateTime.utc_now()
201
202 expected = ""
203
204 assert capture_log(fn ->
205 assert Utils.date_to_asctime(date) == expected
206 end) =~ "[warn] Date #{date} in wrong format, must be ISO 8601"
207 end
208
209 test "when date is a Unix timestamp" do
210 date = DateTime.utc_now() |> DateTime.to_unix()
211
212 expected = ""
213
214 assert capture_log(fn ->
215 assert Utils.date_to_asctime(date) == expected
216 end) =~ "[warn] Date #{date} in wrong format, must be ISO 8601"
217 end
218
219 test "when date is nil" do
220 expected = ""
221
222 assert capture_log(fn ->
223 assert Utils.date_to_asctime(nil) == expected
224 end) =~ "[warn] Date in wrong format, must be ISO 8601"
225 end
226
227 test "when date is a random string" do
228 assert capture_log(fn ->
229 assert Utils.date_to_asctime("foo") == ""
230 end) =~ "[warn] Date foo in wrong format, must be ISO 8601"
231 end
232 end
233
234 describe "get_to_and_cc" do
235 test "for public posts, not a reply" do
236 user = insert(:user)
237 mentioned_user = insert(:user)
238 mentions = [mentioned_user.ap_id]
239
240 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "public", nil)
241
242 assert length(to) == 2
243 assert length(cc) == 1
244
245 assert @public_address in to
246 assert mentioned_user.ap_id in to
247 assert user.follower_address in cc
248 end
249
250 test "for public posts, a reply" do
251 user = insert(:user)
252 mentioned_user = insert(:user)
253 third_user = insert(:user)
254 {:ok, activity} = CommonAPI.post(third_user, %{"status" => "uguu"})
255 mentions = [mentioned_user.ap_id]
256
257 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "public", nil)
258
259 assert length(to) == 3
260 assert length(cc) == 1
261
262 assert @public_address in to
263 assert mentioned_user.ap_id in to
264 assert third_user.ap_id in to
265 assert user.follower_address in cc
266 end
267
268 test "for unlisted posts, not a reply" do
269 user = insert(:user)
270 mentioned_user = insert(:user)
271 mentions = [mentioned_user.ap_id]
272
273 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "unlisted", nil)
274
275 assert length(to) == 2
276 assert length(cc) == 1
277
278 assert @public_address in cc
279 assert mentioned_user.ap_id in to
280 assert user.follower_address in to
281 end
282
283 test "for unlisted posts, a reply" do
284 user = insert(:user)
285 mentioned_user = insert(:user)
286 third_user = insert(:user)
287 {:ok, activity} = CommonAPI.post(third_user, %{"status" => "uguu"})
288 mentions = [mentioned_user.ap_id]
289
290 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "unlisted", nil)
291
292 assert length(to) == 3
293 assert length(cc) == 1
294
295 assert @public_address in cc
296 assert mentioned_user.ap_id in to
297 assert third_user.ap_id in to
298 assert user.follower_address in to
299 end
300
301 test "for private posts, not a reply" do
302 user = insert(:user)
303 mentioned_user = insert(:user)
304 mentions = [mentioned_user.ap_id]
305
306 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "private", nil)
307 assert length(to) == 2
308 assert Enum.empty?(cc)
309
310 assert mentioned_user.ap_id in to
311 assert user.follower_address in to
312 end
313
314 test "for private posts, a reply" do
315 user = insert(:user)
316 mentioned_user = insert(:user)
317 third_user = insert(:user)
318 {:ok, activity} = CommonAPI.post(third_user, %{"status" => "uguu"})
319 mentions = [mentioned_user.ap_id]
320
321 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "private", nil)
322
323 assert length(to) == 3
324 assert Enum.empty?(cc)
325
326 assert mentioned_user.ap_id in to
327 assert third_user.ap_id in to
328 assert user.follower_address in to
329 end
330
331 test "for direct posts, not a reply" do
332 user = insert(:user)
333 mentioned_user = insert(:user)
334 mentions = [mentioned_user.ap_id]
335
336 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "direct", nil)
337
338 assert length(to) == 1
339 assert Enum.empty?(cc)
340
341 assert mentioned_user.ap_id in to
342 end
343
344 test "for direct posts, a reply" do
345 user = insert(:user)
346 mentioned_user = insert(:user)
347 third_user = insert(:user)
348 {:ok, activity} = CommonAPI.post(third_user, %{"status" => "uguu"})
349 mentions = [mentioned_user.ap_id]
350
351 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "direct", nil)
352
353 assert length(to) == 2
354 assert Enum.empty?(cc)
355
356 assert mentioned_user.ap_id in to
357 assert third_user.ap_id in to
358 end
359 end
360
361 describe "to_master_date/1" do
362 test "removes microseconds from date (NaiveDateTime)" do
363 assert Utils.to_masto_date(~N[2015-01-23 23:50:07.123]) == "2015-01-23T23:50:07.000Z"
364 end
365
366 test "removes microseconds from date (String)" do
367 assert Utils.to_masto_date("2015-01-23T23:50:07.123Z") == "2015-01-23T23:50:07.000Z"
368 end
369
370 test "returns empty string when date invalid" do
371 assert Utils.to_masto_date("2015-01?23T23:50:07.123Z") == ""
372 end
373 end
374
375 describe "conversation_id_to_context/1" do
376 test "returns id" do
377 object = insert(:note)
378 assert Utils.conversation_id_to_context(object.id) == object.data["id"]
379 end
380
381 test "returns error if object not found" do
382 assert Utils.conversation_id_to_context("123") == {:error, "No such conversation"}
383 end
384 end
385
386 describe "maybe_notify_mentioned_recipients/2" do
387 test "returns recipients when activity is not `Create`" do
388 activity = insert(:like_activity)
389 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == ["test"]
390 end
391
392 test "returns recipients from tag" do
393 user = insert(:user)
394
395 object =
396 insert(:note,
397 user: user,
398 data: %{
399 "tag" => [
400 %{"type" => "Hashtag"},
401 "",
402 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
403 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
404 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
405 ]
406 }
407 )
408
409 activity = insert(:note_activity, user: user, note: object)
410
411 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
412 "test",
413 "https://testing.pleroma.lol/users/lain",
414 "https://shitposter.club/user/5381"
415 ]
416 end
417
418 test "returns recipients when object is map" do
419 user = insert(:user)
420 object = insert(:note, user: user)
421
422 activity =
423 insert(:note_activity,
424 user: user,
425 note: object,
426 data_attrs: %{
427 "object" => %{
428 "tag" => [
429 %{"type" => "Hashtag"},
430 "",
431 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
432 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
433 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
434 ]
435 }
436 }
437 )
438
439 Pleroma.Repo.delete(object)
440
441 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
442 "test",
443 "https://testing.pleroma.lol/users/lain",
444 "https://shitposter.club/user/5381"
445 ]
446 end
447
448 test "returns recipients when object not found" do
449 user = insert(:user)
450 object = insert(:note, user: user)
451
452 activity = insert(:note_activity, user: user, note: object)
453 Pleroma.Repo.delete(object)
454
455 assert Utils.maybe_notify_mentioned_recipients(["test-test"], activity) == [
456 "test-test"
457 ]
458 end
459 end
460
461 describe "attachments_from_ids_descs/2" do
462 test "returns [] when attachment ids is empty" do
463 assert Utils.attachments_from_ids_descs([], "{}") == []
464 end
465
466 test "returns list attachments with desc" do
467 object = insert(:note)
468 desc = Jason.encode!(%{object.id => "test-desc"})
469
470 assert Utils.attachments_from_ids_descs(["#{object.id}", "34"], desc) == [
471 Map.merge(object.data, %{"name" => "test-desc"})
472 ]
473 end
474 end
475
476 describe "attachments_from_ids/1" do
477 test "returns attachments with descs" do
478 object = insert(:note)
479 desc = Jason.encode!(%{object.id => "test-desc"})
480
481 assert Utils.attachments_from_ids(%{
482 "media_ids" => ["#{object.id}"],
483 "descriptions" => desc
484 }) == [
485 Map.merge(object.data, %{"name" => "test-desc"})
486 ]
487 end
488
489 test "returns attachments without descs" do
490 object = insert(:note)
491 assert Utils.attachments_from_ids(%{"media_ids" => ["#{object.id}"]}) == [object.data]
492 end
493
494 test "returns [] when not pass media_ids" do
495 assert Utils.attachments_from_ids(%{}) == []
496 end
497 end
498
499 describe "maybe_add_list_data/3" do
500 test "adds list params when found user list" do
501 user = insert(:user)
502 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
503
504 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
505 %{
506 additional: %{"bcc" => [list.ap_id], "listMessage" => list.ap_id},
507 object: %{"listMessage" => list.ap_id}
508 }
509 end
510
511 test "returns original params when list not found" do
512 user = insert(:user)
513 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", insert(:user))
514
515 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
516 %{additional: %{}, object: %{}}
517 end
518 end
519
520 describe "make_note_data/11" do
521 test "returns note data" do
522 user = insert(:user)
523 note = insert(:note)
524 user2 = insert(:user)
525 user3 = insert(:user)
526
527 assert Utils.make_note_data(
528 user.ap_id,
529 [user2.ap_id],
530 "2hu",
531 "<h1>This is :moominmamma: note</h1>",
532 [],
533 note.id,
534 [name: "jimm"],
535 "test summary",
536 [user3.ap_id],
537 false,
538 %{"custom_tag" => "test"}
539 ) == %{
540 "actor" => user.ap_id,
541 "attachment" => [],
542 "cc" => [user3.ap_id],
543 "content" => "<h1>This is :moominmamma: note</h1>",
544 "context" => "2hu",
545 "sensitive" => false,
546 "summary" => "test summary",
547 "tag" => ["jimm"],
548 "to" => [user2.ap_id],
549 "type" => "Note",
550 "custom_tag" => "test"
551 }
552 end
553 end
554
555 describe "maybe_add_attachments/3" do
556 test "returns parsed results when attachment_links is false" do
557 assert Utils.maybe_add_attachments(
558 {"test", [], ["tags"]},
559 [],
560 false
561 ) == {"test", [], ["tags"]}
562 end
563
564 test "adds attachments to parsed results" do
565 attachment = %{"url" => [%{"href" => "SakuraPM.png"}]}
566
567 assert Utils.maybe_add_attachments(
568 {"test", [], ["tags"]},
569 [attachment],
570 true
571 ) == {
572 "test<br><a href=\"SakuraPM.png\" class='attachment'>SakuraPM.png</a>",
573 [],
574 ["tags"]
575 }
576 end
577 end
578 end