d8fec35208bd47d41703a66a3e39f35936ccc0ff
[akkoma] / test / pleroma / web / common_api / utils_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.CommonAPI.UtilsTest do
6 alias Pleroma.Builders.UserBuilder
7 alias Pleroma.Object
8 alias Pleroma.Web.CommonAPI
9 alias Pleroma.Web.CommonAPI.ActivityDraft
10 alias Pleroma.Web.CommonAPI.Utils
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 describe "add_attachments/2" do
19 setup do
20 name =
21 "Sakura Mana – Turned on by a Senior OL with a Temptating Tight Skirt-s Full Hipline and Panty Shot- Beautiful Thick Thighs- and Erotic Ass- -2015- -- Oppaitime 8-28-2017 6-50-33 PM.png"
22
23 attachment = %{
24 "url" => [%{"href" => URI.encode(name)}]
25 }
26
27 %{name: name, attachment: attachment}
28 end
29
30 test "it adds attachment links to a given text and attachment set", %{
31 name: name,
32 attachment: attachment
33 } do
34 len = 10
35 clear_config([Pleroma.Upload, :filename_display_max_length], len)
36
37 expected =
38 "<br><a href=\"#{URI.encode(name)}\" class='attachment'>#{String.slice(name, 0..len)}…</a>"
39
40 assert Utils.add_attachments("", [attachment]) == expected
41 end
42
43 test "doesn't truncate file name if config for truncate is set to 0", %{
44 name: name,
45 attachment: attachment
46 } do
47 clear_config([Pleroma.Upload, :filename_display_max_length], 0)
48
49 expected = "<br><a href=\"#{URI.encode(name)}\" class='attachment'>#{name}</a>"
50
51 assert Utils.add_attachments("", [attachment]) == expected
52 end
53 end
54
55 describe "it confirms the password given is the current users password" do
56 test "incorrect password given" do
57 {:ok, user} = UserBuilder.insert()
58
59 assert Utils.confirm_current_password(user, "") == {:error, "Invalid password."}
60 end
61
62 test "correct password given" do
63 {:ok, user} = UserBuilder.insert()
64 assert Utils.confirm_current_password(user, "test") == {:ok, user}
65 end
66 end
67
68 describe "format_input/3" do
69 test "works for bare text/plain" do
70 text = "hello world!"
71 expected = "hello world!"
72
73 {output, [], []} = Utils.format_input(text, "text/plain")
74
75 assert output == expected
76
77 text = "hello world!\n\nsecond paragraph!"
78 expected = "hello world!<br><br>second paragraph!"
79
80 {output, [], []} = Utils.format_input(text, "text/plain")
81
82 assert output == expected
83 end
84
85 test "works for bare text/html" do
86 text = "<p>hello world!</p>"
87 expected = "<p>hello world!</p>"
88
89 {output, [], []} = Utils.format_input(text, "text/html")
90
91 assert output == expected
92
93 text = "<p>hello world!</p><br/>\n<p>second paragraph</p>"
94 expected = "<p>hello world!</p><br/>\n<p>second paragraph</p>"
95
96 {output, [], []} = Utils.format_input(text, "text/html")
97
98 assert output == expected
99 end
100
101 test "works for bare text/markdown" do
102 text = "**hello world**"
103 expected = "<p><strong>hello world</strong></p>"
104
105 {output, [], []} = Utils.format_input(text, "text/markdown")
106
107 assert output == expected
108
109 text = "**hello world**\n\n*another paragraph*"
110 expected = "<p><strong>hello world</strong></p><p><em>another paragraph</em></p>"
111
112 {output, [], []} = Utils.format_input(text, "text/markdown")
113
114 assert output == expected
115
116 text = """
117 > cool quote
118
119 by someone
120 """
121
122 expected = "<blockquote><p>cool quote</p></blockquote><p>by someone</p>"
123
124 {output, [], []} = Utils.format_input(text, "text/markdown")
125
126 assert output == expected
127 end
128
129 test "works for bare text/bbcode" do
130 text = "[b]hello world[/b]"
131 expected = "<strong>hello world</strong>"
132
133 {output, [], []} = Utils.format_input(text, "text/bbcode")
134
135 assert output == expected
136
137 text = "[b]hello world![/b]\n\nsecond paragraph!"
138 expected = "<strong>hello world!</strong><br><br>second paragraph!"
139
140 {output, [], []} = Utils.format_input(text, "text/bbcode")
141
142 assert output == expected
143
144 text = "[b]hello world![/b]\n\n<strong>second paragraph!</strong>"
145
146 expected =
147 "<strong>hello world!</strong><br><br>&lt;strong&gt;second paragraph!&lt;/strong&gt;"
148
149 {output, [], []} = Utils.format_input(text, "text/bbcode")
150
151 assert output == expected
152 end
153
154 test "works for text/markdown with mentions" do
155 {:ok, user} =
156 UserBuilder.insert(%{nickname: "user__test", ap_id: "http://foo.com/user__test"})
157
158 text = "**hello world**\n\n*another @user__test and @user__test google.com paragraph*"
159
160 {output, _, _} = Utils.format_input(text, "text/markdown")
161
162 assert output ==
163 ~s(<p><strong>hello world</strong></p><p><em>another <span class="h-card"><a class="u-url mention" data-user="#{
164 user.id
165 }" href="http://foo.com/user__test" rel="ugc">@<span>user__test</span></a></span> and <span class="h-card"><a class="u-url mention" data-user="#{
166 user.id
167 }" 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>)
168 end
169 end
170
171 describe "format_input/3 with markdown" do
172 test "Paragraph" do
173 code = ~s[Hello\n\nWorld!]
174 {result, [], []} = Utils.format_input(code, "text/markdown")
175 assert result == "<p>Hello</p><p>World!</p>"
176 end
177
178 test "links" do
179 code = "https://en.wikipedia.org/wiki/Animal_Crossing_(video_game)"
180 {result, [], []} = Utils.format_input(code, "text/markdown")
181 assert result == ~s[<p><a href="#{code}">#{code}</a></p>]
182
183 code = "https://github.com/pragdave/earmark/"
184 {result, [], []} = Utils.format_input(code, "text/markdown")
185 assert result == ~s[<p><a href="#{code}">#{code}</a></p>]
186 end
187
188 test "link with local mention" do
189 insert(:user, %{nickname: "lain"})
190
191 code = "https://example.com/@lain"
192 {result, [], []} = Utils.format_input(code, "text/markdown")
193 assert result == ~s[<p><a href="#{code}">#{code}</a></p>]
194 end
195
196 test "local mentions" do
197 mario = insert(:user, %{nickname: "mario"})
198 luigi = insert(:user, %{nickname: "luigi"})
199
200 code = "@mario @luigi yo what's up?"
201 {result, _, []} = Utils.format_input(code, "text/markdown")
202
203 assert result ==
204 ~s[<p><span class="h-card"><a class="u-url mention" data-user="#{mario.id}" href="#{
205 mario.ap_id
206 }" rel="ugc">@<span>mario</span></a></span> <span class="h-card"><a class="u-url mention" data-user="#{
207 luigi.id
208 }" href="#{luigi.ap_id}" rel="ugc">@<span>luigi</span></a></span> yo what’s up?</p>]
209 end
210
211 test "remote mentions" do
212 mario = insert(:user, %{nickname: "mario@mushroom.world", local: false})
213 luigi = insert(:user, %{nickname: "luigi@mushroom.world", local: false})
214
215 code = "@mario@mushroom.world @luigi@mushroom.world yo what's up?"
216 {result, _, []} = Utils.format_input(code, "text/markdown")
217
218 assert result ==
219 ~s[<p><span class="h-card"><a class="u-url mention" data-user="#{mario.id}" href="#{
220 mario.ap_id
221 }" rel="ugc">@<span>mario</span></a></span> <span class="h-card"><a class="u-url mention" data-user="#{
222 luigi.id
223 }" href="#{luigi.ap_id}" rel="ugc">@<span>luigi</span></a></span> yo what’s up?</p>]
224 end
225
226 test "raw HTML" do
227 code = ~s[<a href="http://example.org/">OwO</a><!-- what's this?-->]
228 {result, [], []} = Utils.format_input(code, "text/markdown")
229 assert result == ~s[<a href="http://example.org/">OwO</a>]
230 end
231
232 test "rulers" do
233 code = ~s[before\n\n-----\n\nafter]
234 {result, [], []} = Utils.format_input(code, "text/markdown")
235 assert result == "<p>before</p><hr/><p>after</p>"
236 end
237
238 test "blockquote" do
239 code = ~s[> whoms't are you quoting?]
240 {result, [], []} = Utils.format_input(code, "text/markdown")
241 assert result == "<blockquote><p>whoms’t are you quoting?</p></blockquote>"
242 end
243
244 test "code" do
245 code = ~s[`mix`]
246 {result, [], []} = Utils.format_input(code, "text/markdown")
247 assert result == ~s[<p><code class="inline">mix</code></p>]
248
249 code = ~s[``mix``]
250 {result, [], []} = Utils.format_input(code, "text/markdown")
251 assert result == ~s[<p><code class="inline">mix</code></p>]
252
253 code = ~s[```\nputs "Hello World"\n```]
254 {result, [], []} = Utils.format_input(code, "text/markdown")
255 assert result == ~s[<pre><code>puts &quot;Hello World&quot;</code></pre>]
256
257 code = ~s[ <div>\n </div>]
258 {result, [], []} = Utils.format_input(code, "text/markdown")
259 assert result == ~s[<pre><code>&lt;div&gt;\n&lt;/div&gt;</code></pre>]
260 end
261
262 test "lists" do
263 code = ~s[- one\n- two\n- three\n- four]
264 {result, [], []} = Utils.format_input(code, "text/markdown")
265 assert result == "<ul><li>one</li><li>two</li><li>three</li><li>four</li></ul>"
266
267 code = ~s[1. one\n2. two\n3. three\n4. four\n]
268 {result, [], []} = Utils.format_input(code, "text/markdown")
269 assert result == "<ol><li>one</li><li>two</li><li>three</li><li>four</li></ol>"
270 end
271
272 test "delegated renderers" do
273 code = ~s[*aaaa~*]
274 {result, [], []} = Utils.format_input(code, "text/markdown")
275 assert result == ~s[<p><em>aaaa~</em></p>]
276
277 code = ~s[**aaaa~**]
278 {result, [], []} = Utils.format_input(code, "text/markdown")
279 assert result == ~s[<p><strong>aaaa~</strong></p>]
280
281 # strikethrough
282 code = ~s[~~aaaa~~~]
283 {result, [], []} = Utils.format_input(code, "text/markdown")
284 assert result == ~s[<p><del>aaaa</del>~</p>]
285 end
286 end
287
288 describe "context_to_conversation_id" do
289 test "creates a mapping object" do
290 conversation_id = Utils.context_to_conversation_id("random context")
291 object = Object.get_by_ap_id("random context")
292
293 assert conversation_id == object.id
294 end
295
296 test "returns an existing mapping for an existing object" do
297 {:ok, object} = Object.context_mapping("random context") |> Repo.insert()
298 conversation_id = Utils.context_to_conversation_id("random context")
299
300 assert conversation_id == object.id
301 end
302 end
303
304 describe "formats date to asctime" do
305 test "when date is in ISO 8601 format" do
306 date = DateTime.utc_now() |> DateTime.to_iso8601()
307
308 expected =
309 date
310 |> DateTime.from_iso8601()
311 |> elem(1)
312 |> Calendar.Strftime.strftime!("%a %b %d %H:%M:%S %z %Y")
313
314 assert Utils.date_to_asctime(date) == expected
315 end
316
317 test "when date is a binary in wrong format" do
318 date = DateTime.utc_now()
319
320 expected = ""
321
322 assert capture_log(fn ->
323 assert Utils.date_to_asctime(date) == expected
324 end) =~ "[warn] Date #{date} in wrong format, must be ISO 8601"
325 end
326
327 test "when date is a Unix timestamp" do
328 date = DateTime.utc_now() |> DateTime.to_unix()
329
330 expected = ""
331
332 assert capture_log(fn ->
333 assert Utils.date_to_asctime(date) == expected
334 end) =~ "[warn] Date #{date} in wrong format, must be ISO 8601"
335 end
336
337 test "when date is nil" do
338 expected = ""
339
340 assert capture_log(fn ->
341 assert Utils.date_to_asctime(nil) == expected
342 end) =~ "[warn] Date in wrong format, must be ISO 8601"
343 end
344
345 test "when date is a random string" do
346 assert capture_log(fn ->
347 assert Utils.date_to_asctime("foo") == ""
348 end) =~ "[warn] Date foo in wrong format, must be ISO 8601"
349 end
350 end
351
352 describe "get_to_and_cc" do
353 test "for public posts, not a reply" do
354 user = insert(:user)
355 mentioned_user = insert(:user)
356 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "public"}
357
358 {to, cc} = Utils.get_to_and_cc(draft)
359
360 assert length(to) == 2
361 assert length(cc) == 1
362
363 assert @public_address in to
364 assert mentioned_user.ap_id in to
365 assert user.follower_address in cc
366 end
367
368 test "for public posts, a reply" do
369 user = insert(:user)
370 mentioned_user = insert(:user)
371 third_user = insert(:user)
372 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
373
374 draft = %ActivityDraft{
375 user: user,
376 mentions: [mentioned_user.ap_id],
377 visibility: "public",
378 in_reply_to: activity
379 }
380
381 {to, cc} = Utils.get_to_and_cc(draft)
382
383 assert length(to) == 3
384 assert length(cc) == 1
385
386 assert @public_address in to
387 assert mentioned_user.ap_id in to
388 assert third_user.ap_id in to
389 assert user.follower_address in cc
390 end
391
392 test "for unlisted posts, not a reply" do
393 user = insert(:user)
394 mentioned_user = insert(:user)
395 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "unlisted"}
396
397 {to, cc} = Utils.get_to_and_cc(draft)
398
399 assert length(to) == 2
400 assert length(cc) == 1
401
402 assert @public_address in cc
403 assert mentioned_user.ap_id in to
404 assert user.follower_address in to
405 end
406
407 test "for unlisted posts, a reply" do
408 user = insert(:user)
409 mentioned_user = insert(:user)
410 third_user = insert(:user)
411 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
412
413 draft = %ActivityDraft{
414 user: user,
415 mentions: [mentioned_user.ap_id],
416 visibility: "unlisted",
417 in_reply_to: activity
418 }
419
420 {to, cc} = Utils.get_to_and_cc(draft)
421
422 assert length(to) == 3
423 assert length(cc) == 1
424
425 assert @public_address in cc
426 assert mentioned_user.ap_id in to
427 assert third_user.ap_id in to
428 assert user.follower_address in to
429 end
430
431 test "for private posts, not a reply" do
432 user = insert(:user)
433 mentioned_user = insert(:user)
434 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "private"}
435
436 {to, cc} = Utils.get_to_and_cc(draft)
437 assert length(to) == 2
438 assert Enum.empty?(cc)
439
440 assert mentioned_user.ap_id in to
441 assert user.follower_address in to
442 end
443
444 test "for private posts, a reply" do
445 user = insert(:user)
446 mentioned_user = insert(:user)
447 third_user = insert(:user)
448 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
449
450 draft = %ActivityDraft{
451 user: user,
452 mentions: [mentioned_user.ap_id],
453 visibility: "private",
454 in_reply_to: activity
455 }
456
457 {to, cc} = Utils.get_to_and_cc(draft)
458
459 assert length(to) == 2
460 assert Enum.empty?(cc)
461
462 assert mentioned_user.ap_id in to
463 assert user.follower_address in to
464 end
465
466 test "for direct posts, not a reply" do
467 user = insert(:user)
468 mentioned_user = insert(:user)
469 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "direct"}
470
471 {to, cc} = Utils.get_to_and_cc(draft)
472
473 assert length(to) == 1
474 assert Enum.empty?(cc)
475
476 assert mentioned_user.ap_id in to
477 end
478
479 test "for direct posts, a reply" do
480 user = insert(:user)
481 mentioned_user = insert(:user)
482 third_user = insert(:user)
483 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
484
485 draft = %ActivityDraft{
486 user: user,
487 mentions: [mentioned_user.ap_id],
488 visibility: "direct",
489 in_reply_to: activity
490 }
491
492 {to, cc} = Utils.get_to_and_cc(draft)
493
494 assert length(to) == 1
495 assert Enum.empty?(cc)
496
497 assert mentioned_user.ap_id in to
498
499 {:ok, direct_activity} = CommonAPI.post(third_user, %{status: "uguu", visibility: "direct"})
500
501 draft = %ActivityDraft{
502 user: user,
503 mentions: [mentioned_user.ap_id],
504 visibility: "direct",
505 in_reply_to: direct_activity
506 }
507
508 {to, cc} = Utils.get_to_and_cc(draft)
509
510 assert length(to) == 2
511 assert Enum.empty?(cc)
512
513 assert mentioned_user.ap_id in to
514 assert third_user.ap_id in to
515 end
516 end
517
518 describe "to_master_date/1" do
519 test "removes microseconds from date (NaiveDateTime)" do
520 assert Utils.to_masto_date(~N[2015-01-23 23:50:07.123]) == "2015-01-23T23:50:07.000Z"
521 end
522
523 test "removes microseconds from date (String)" do
524 assert Utils.to_masto_date("2015-01-23T23:50:07.123Z") == "2015-01-23T23:50:07.000Z"
525 end
526
527 test "returns empty string when date invalid" do
528 assert Utils.to_masto_date("2015-01?23T23:50:07.123Z") == ""
529 end
530 end
531
532 describe "conversation_id_to_context/1" do
533 test "returns id" do
534 object = insert(:note)
535 assert Utils.conversation_id_to_context(object.id) == object.data["id"]
536 end
537
538 test "returns error if object not found" do
539 assert Utils.conversation_id_to_context("123") == {:error, "No such conversation"}
540 end
541 end
542
543 describe "maybe_notify_mentioned_recipients/2" do
544 test "returns recipients when activity is not `Create`" do
545 activity = insert(:like_activity)
546 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == ["test"]
547 end
548
549 test "returns recipients from tag" do
550 user = insert(:user)
551
552 object =
553 insert(:note,
554 user: user,
555 data: %{
556 "tag" => [
557 %{"type" => "Hashtag"},
558 "",
559 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
560 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
561 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
562 ]
563 }
564 )
565
566 activity = insert(:note_activity, user: user, note: object)
567
568 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
569 "test",
570 "https://testing.pleroma.lol/users/lain",
571 "https://shitposter.club/user/5381"
572 ]
573 end
574
575 test "returns recipients when object is map" do
576 user = insert(:user)
577 object = insert(:note, user: user)
578
579 activity =
580 insert(:note_activity,
581 user: user,
582 note: object,
583 data_attrs: %{
584 "object" => %{
585 "tag" => [
586 %{"type" => "Hashtag"},
587 "",
588 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
589 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
590 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
591 ]
592 }
593 }
594 )
595
596 Pleroma.Repo.delete(object)
597
598 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
599 "test",
600 "https://testing.pleroma.lol/users/lain",
601 "https://shitposter.club/user/5381"
602 ]
603 end
604
605 test "returns recipients when object not found" do
606 user = insert(:user)
607 object = insert(:note, user: user)
608
609 activity = insert(:note_activity, user: user, note: object)
610 Pleroma.Repo.delete(object)
611
612 obj_url = activity.data["object"]
613
614 Tesla.Mock.mock(fn
615 %{method: :get, url: ^obj_url} ->
616 %Tesla.Env{status: 404, body: ""}
617 end)
618
619 assert Utils.maybe_notify_mentioned_recipients(["test-test"], activity) == [
620 "test-test"
621 ]
622 end
623 end
624
625 describe "attachments_from_ids_descs/2" do
626 test "returns [] when attachment ids is empty" do
627 assert Utils.attachments_from_ids_descs([], "{}") == []
628 end
629
630 test "returns list attachments with desc" do
631 object = insert(:note)
632 desc = Jason.encode!(%{object.id => "test-desc"})
633
634 assert Utils.attachments_from_ids_descs(["#{object.id}", "34"], desc) == [
635 Map.merge(object.data, %{"name" => "test-desc"})
636 ]
637 end
638 end
639
640 describe "attachments_from_ids/1" do
641 test "returns attachments with descs" do
642 object = insert(:note)
643 desc = Jason.encode!(%{object.id => "test-desc"})
644
645 assert Utils.attachments_from_ids(%{
646 media_ids: ["#{object.id}"],
647 descriptions: desc
648 }) == [
649 Map.merge(object.data, %{"name" => "test-desc"})
650 ]
651 end
652
653 test "returns attachments without descs" do
654 object = insert(:note)
655 assert Utils.attachments_from_ids(%{media_ids: ["#{object.id}"]}) == [object.data]
656 end
657
658 test "returns [] when not pass media_ids" do
659 assert Utils.attachments_from_ids(%{}) == []
660 end
661 end
662
663 describe "maybe_add_list_data/3" do
664 test "adds list params when found user list" do
665 user = insert(:user)
666 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
667
668 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
669 %{
670 additional: %{"bcc" => [list.ap_id], "listMessage" => list.ap_id},
671 object: %{"listMessage" => list.ap_id}
672 }
673 end
674
675 test "returns original params when list not found" do
676 user = insert(:user)
677 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", insert(:user))
678
679 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
680 %{additional: %{}, object: %{}}
681 end
682 end
683
684 describe "maybe_add_attachments/3" do
685 test "returns parsed results when attachment_links is false" do
686 assert Utils.maybe_add_attachments(
687 {"test", [], ["tags"]},
688 [],
689 false
690 ) == {"test", [], ["tags"]}
691 end
692
693 test "adds attachments to parsed results" do
694 attachment = %{"url" => [%{"href" => "SakuraPM.png"}]}
695
696 assert Utils.maybe_add_attachments(
697 {"test", [], ["tags"]},
698 [attachment],
699 true
700 ) == {
701 "test<br><a href=\"SakuraPM.png\" class='attachment'>SakuraPM.png</a>",
702 [],
703 ["tags"]
704 }
705 end
706 end
707 end