CI: Bump lint stage to elixir-1.12
[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="#{user.id}" 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="#{user.id}" 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>)
164 end
165 end
166
167 describe "format_input/3 with markdown" do
168 test "Paragraph" do
169 code = ~s[Hello\n\nWorld!]
170 {result, [], []} = Utils.format_input(code, "text/markdown")
171 assert result == "<p>Hello</p><p>World!</p>"
172 end
173
174 test "links" do
175 code = "https://en.wikipedia.org/wiki/Animal_Crossing_(video_game)"
176 {result, [], []} = Utils.format_input(code, "text/markdown")
177 assert result == ~s[<p><a href="#{code}">#{code}</a></p>]
178
179 code = "https://github.com/pragdave/earmark/"
180 {result, [], []} = Utils.format_input(code, "text/markdown")
181 assert result == ~s[<p><a href="#{code}">#{code}</a></p>]
182 end
183
184 test "link with local mention" do
185 insert(:user, %{nickname: "lain"})
186
187 code = "https://example.com/@lain"
188 {result, [], []} = Utils.format_input(code, "text/markdown")
189 assert result == ~s[<p><a href="#{code}">#{code}</a></p>]
190 end
191
192 test "local mentions" do
193 mario = insert(:user, %{nickname: "mario"})
194 luigi = insert(:user, %{nickname: "luigi"})
195
196 code = "@mario @luigi yo what's up?"
197 {result, _, []} = Utils.format_input(code, "text/markdown")
198
199 assert result ==
200 ~s[<p><span class="h-card"><a class="u-url mention" data-user="#{mario.id}" href="#{mario.ap_id}" rel="ugc">@<span>mario</span></a></span> <span class="h-card"><a class="u-url mention" data-user="#{luigi.id}" href="#{luigi.ap_id}" rel="ugc">@<span>luigi</span></a></span> yo what’s up?</p>]
201 end
202
203 test "remote mentions" do
204 mario = insert(:user, %{nickname: "mario@mushroom.world", local: false})
205 luigi = insert(:user, %{nickname: "luigi@mushroom.world", local: false})
206
207 code = "@mario@mushroom.world @luigi@mushroom.world yo what's up?"
208 {result, _, []} = Utils.format_input(code, "text/markdown")
209
210 assert result ==
211 ~s[<p><span class="h-card"><a class="u-url mention" data-user="#{mario.id}" href="#{mario.ap_id}" rel="ugc">@<span>mario</span></a></span> <span class="h-card"><a class="u-url mention" data-user="#{luigi.id}" href="#{luigi.ap_id}" rel="ugc">@<span>luigi</span></a></span> yo what’s up?</p>]
212 end
213
214 test "raw HTML" do
215 code = ~s[<a href="http://example.org/">OwO</a><!-- what's this?-->]
216 {result, [], []} = Utils.format_input(code, "text/markdown")
217 assert result == ~s[<a href="http://example.org/">OwO</a>]
218 end
219
220 test "rulers" do
221 code = ~s[before\n\n-----\n\nafter]
222 {result, [], []} = Utils.format_input(code, "text/markdown")
223 assert result == "<p>before</p><hr/><p>after</p>"
224 end
225
226 test "blockquote" do
227 code = ~s[> whoms't are you quoting?]
228 {result, [], []} = Utils.format_input(code, "text/markdown")
229 assert result == "<blockquote><p>whoms’t are you quoting?</p></blockquote>"
230 end
231
232 test "code" do
233 code = ~s[`mix`]
234 {result, [], []} = Utils.format_input(code, "text/markdown")
235 assert result == ~s[<p><code class="inline">mix</code></p>]
236
237 code = ~s[``mix``]
238 {result, [], []} = Utils.format_input(code, "text/markdown")
239 assert result == ~s[<p><code class="inline">mix</code></p>]
240
241 code = ~s[```\nputs "Hello World"\n```]
242 {result, [], []} = Utils.format_input(code, "text/markdown")
243 assert result == ~s[<pre><code>puts &quot;Hello World&quot;</code></pre>]
244
245 code = ~s[ <div>\n </div>]
246 {result, [], []} = Utils.format_input(code, "text/markdown")
247 assert result == ~s[<pre><code>&lt;div&gt;\n&lt;/div&gt;</code></pre>]
248 end
249
250 test "lists" do
251 code = ~s[- one\n- two\n- three\n- four]
252 {result, [], []} = Utils.format_input(code, "text/markdown")
253 assert result == "<ul><li>one</li><li>two</li><li>three</li><li>four</li></ul>"
254
255 code = ~s[1. one\n2. two\n3. three\n4. four\n]
256 {result, [], []} = Utils.format_input(code, "text/markdown")
257 assert result == "<ol><li>one</li><li>two</li><li>three</li><li>four</li></ol>"
258 end
259
260 test "delegated renderers" do
261 code = ~s[*aaaa~*]
262 {result, [], []} = Utils.format_input(code, "text/markdown")
263 assert result == ~s[<p><em>aaaa~</em></p>]
264
265 code = ~s[**aaaa~**]
266 {result, [], []} = Utils.format_input(code, "text/markdown")
267 assert result == ~s[<p><strong>aaaa~</strong></p>]
268
269 # strikethrough
270 code = ~s[~~aaaa~~~]
271 {result, [], []} = Utils.format_input(code, "text/markdown")
272 assert result == ~s[<p><del>aaaa</del>~</p>]
273 end
274 end
275
276 describe "context_to_conversation_id" do
277 test "creates a mapping object" do
278 conversation_id = Utils.context_to_conversation_id("random context")
279 object = Object.get_by_ap_id("random context")
280
281 assert conversation_id == object.id
282 end
283
284 test "returns an existing mapping for an existing object" do
285 {:ok, object} = Object.context_mapping("random context") |> Repo.insert()
286 conversation_id = Utils.context_to_conversation_id("random context")
287
288 assert conversation_id == object.id
289 end
290 end
291
292 describe "formats date to asctime" do
293 test "when date is in ISO 8601 format" do
294 date = DateTime.utc_now() |> DateTime.to_iso8601()
295
296 expected =
297 date
298 |> DateTime.from_iso8601()
299 |> elem(1)
300 |> Calendar.Strftime.strftime!("%a %b %d %H:%M:%S %z %Y")
301
302 assert Utils.date_to_asctime(date) == expected
303 end
304
305 test "when date is a binary in wrong format" do
306 date = DateTime.utc_now()
307
308 expected = ""
309
310 assert capture_log(fn ->
311 assert Utils.date_to_asctime(date) == expected
312 end) =~ "[warn] Date #{date} in wrong format, must be ISO 8601"
313 end
314
315 test "when date is a Unix timestamp" do
316 date = DateTime.utc_now() |> DateTime.to_unix()
317
318 expected = ""
319
320 assert capture_log(fn ->
321 assert Utils.date_to_asctime(date) == expected
322 end) =~ "[warn] Date #{date} in wrong format, must be ISO 8601"
323 end
324
325 test "when date is nil" do
326 expected = ""
327
328 assert capture_log(fn ->
329 assert Utils.date_to_asctime(nil) == expected
330 end) =~ "[warn] Date in wrong format, must be ISO 8601"
331 end
332
333 test "when date is a random string" do
334 assert capture_log(fn ->
335 assert Utils.date_to_asctime("foo") == ""
336 end) =~ "[warn] Date foo in wrong format, must be ISO 8601"
337 end
338 end
339
340 describe "get_to_and_cc" do
341 test "for public posts, not a reply" do
342 user = insert(:user)
343 mentioned_user = insert(:user)
344 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "public"}
345
346 {to, cc} = Utils.get_to_and_cc(draft)
347
348 assert length(to) == 2
349 assert length(cc) == 1
350
351 assert @public_address in to
352 assert mentioned_user.ap_id in to
353 assert user.follower_address in cc
354 end
355
356 test "for public posts, a reply" do
357 user = insert(:user)
358 mentioned_user = insert(:user)
359 third_user = insert(:user)
360 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
361
362 draft = %ActivityDraft{
363 user: user,
364 mentions: [mentioned_user.ap_id],
365 visibility: "public",
366 in_reply_to: activity
367 }
368
369 {to, cc} = Utils.get_to_and_cc(draft)
370
371 assert length(to) == 3
372 assert length(cc) == 1
373
374 assert @public_address in to
375 assert mentioned_user.ap_id in to
376 assert third_user.ap_id in to
377 assert user.follower_address in cc
378 end
379
380 test "for unlisted posts, not a reply" do
381 user = insert(:user)
382 mentioned_user = insert(:user)
383 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "unlisted"}
384
385 {to, cc} = Utils.get_to_and_cc(draft)
386
387 assert length(to) == 2
388 assert length(cc) == 1
389
390 assert @public_address in cc
391 assert mentioned_user.ap_id in to
392 assert user.follower_address in to
393 end
394
395 test "for unlisted posts, a reply" do
396 user = insert(:user)
397 mentioned_user = insert(:user)
398 third_user = insert(:user)
399 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
400
401 draft = %ActivityDraft{
402 user: user,
403 mentions: [mentioned_user.ap_id],
404 visibility: "unlisted",
405 in_reply_to: activity
406 }
407
408 {to, cc} = Utils.get_to_and_cc(draft)
409
410 assert length(to) == 3
411 assert length(cc) == 1
412
413 assert @public_address in cc
414 assert mentioned_user.ap_id in to
415 assert third_user.ap_id in to
416 assert user.follower_address in to
417 end
418
419 test "for private posts, not a reply" do
420 user = insert(:user)
421 mentioned_user = insert(:user)
422 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "private"}
423
424 {to, cc} = Utils.get_to_and_cc(draft)
425 assert length(to) == 2
426 assert Enum.empty?(cc)
427
428 assert mentioned_user.ap_id in to
429 assert user.follower_address in to
430 end
431
432 test "for private posts, a reply" do
433 user = insert(:user)
434 mentioned_user = insert(:user)
435 third_user = insert(:user)
436 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
437
438 draft = %ActivityDraft{
439 user: user,
440 mentions: [mentioned_user.ap_id],
441 visibility: "private",
442 in_reply_to: activity
443 }
444
445 {to, cc} = Utils.get_to_and_cc(draft)
446
447 assert length(to) == 2
448 assert Enum.empty?(cc)
449
450 assert mentioned_user.ap_id in to
451 assert user.follower_address in to
452 end
453
454 test "for direct posts, not a reply" do
455 user = insert(:user)
456 mentioned_user = insert(:user)
457 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "direct"}
458
459 {to, cc} = Utils.get_to_and_cc(draft)
460
461 assert length(to) == 1
462 assert Enum.empty?(cc)
463
464 assert mentioned_user.ap_id in to
465 end
466
467 test "for direct posts, a reply" do
468 user = insert(:user)
469 mentioned_user = insert(:user)
470 third_user = insert(:user)
471 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
472
473 draft = %ActivityDraft{
474 user: user,
475 mentions: [mentioned_user.ap_id],
476 visibility: "direct",
477 in_reply_to: activity
478 }
479
480 {to, cc} = Utils.get_to_and_cc(draft)
481
482 assert length(to) == 1
483 assert Enum.empty?(cc)
484
485 assert mentioned_user.ap_id in to
486
487 {:ok, direct_activity} = CommonAPI.post(third_user, %{status: "uguu", visibility: "direct"})
488
489 draft = %ActivityDraft{
490 user: user,
491 mentions: [mentioned_user.ap_id],
492 visibility: "direct",
493 in_reply_to: direct_activity
494 }
495
496 {to, cc} = Utils.get_to_and_cc(draft)
497
498 assert length(to) == 2
499 assert Enum.empty?(cc)
500
501 assert mentioned_user.ap_id in to
502 assert third_user.ap_id in to
503 end
504 end
505
506 describe "to_master_date/1" do
507 test "removes microseconds from date (NaiveDateTime)" do
508 assert Utils.to_masto_date(~N[2015-01-23 23:50:07.123]) == "2015-01-23T23:50:07.000Z"
509 end
510
511 test "removes microseconds from date (String)" do
512 assert Utils.to_masto_date("2015-01-23T23:50:07.123Z") == "2015-01-23T23:50:07.000Z"
513 end
514
515 test "returns empty string when date invalid" do
516 assert Utils.to_masto_date("2015-01?23T23:50:07.123Z") == ""
517 end
518 end
519
520 describe "conversation_id_to_context/1" do
521 test "returns id" do
522 object = insert(:note)
523 assert Utils.conversation_id_to_context(object.id) == object.data["id"]
524 end
525
526 test "returns error if object not found" do
527 assert Utils.conversation_id_to_context("123") == {:error, "No such conversation"}
528 end
529 end
530
531 describe "maybe_notify_mentioned_recipients/2" do
532 test "returns recipients when activity is not `Create`" do
533 activity = insert(:like_activity)
534 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == ["test"]
535 end
536
537 test "returns recipients from tag" do
538 user = insert(:user)
539
540 object =
541 insert(:note,
542 user: user,
543 data: %{
544 "tag" => [
545 %{"type" => "Hashtag"},
546 "",
547 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
548 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
549 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
550 ]
551 }
552 )
553
554 activity = insert(:note_activity, user: user, note: object)
555
556 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
557 "test",
558 "https://testing.pleroma.lol/users/lain",
559 "https://shitposter.club/user/5381"
560 ]
561 end
562
563 test "returns recipients when object is map" do
564 user = insert(:user)
565 object = insert(:note, user: user)
566
567 activity =
568 insert(:note_activity,
569 user: user,
570 note: object,
571 data_attrs: %{
572 "object" => %{
573 "tag" => [
574 %{"type" => "Hashtag"},
575 "",
576 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
577 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
578 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
579 ]
580 }
581 }
582 )
583
584 Pleroma.Repo.delete(object)
585
586 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
587 "test",
588 "https://testing.pleroma.lol/users/lain",
589 "https://shitposter.club/user/5381"
590 ]
591 end
592
593 test "returns recipients when object not found" do
594 user = insert(:user)
595 object = insert(:note, user: user)
596
597 activity = insert(:note_activity, user: user, note: object)
598 Pleroma.Repo.delete(object)
599
600 obj_url = activity.data["object"]
601
602 Tesla.Mock.mock(fn
603 %{method: :get, url: ^obj_url} ->
604 %Tesla.Env{status: 404, body: ""}
605 end)
606
607 assert Utils.maybe_notify_mentioned_recipients(["test-test"], activity) == [
608 "test-test"
609 ]
610 end
611 end
612
613 describe "attachments_from_ids_descs/2" do
614 test "returns [] when attachment ids is empty" do
615 assert Utils.attachments_from_ids_descs([], "{}") == []
616 end
617
618 test "returns list attachments with desc" do
619 object = insert(:note)
620 desc = Jason.encode!(%{object.id => "test-desc"})
621
622 assert Utils.attachments_from_ids_descs(["#{object.id}", "34"], desc) == [
623 Map.merge(object.data, %{"name" => "test-desc"})
624 ]
625 end
626 end
627
628 describe "attachments_from_ids/1" do
629 test "returns attachments with descs" do
630 object = insert(:note)
631 desc = Jason.encode!(%{object.id => "test-desc"})
632
633 assert Utils.attachments_from_ids(%{
634 media_ids: ["#{object.id}"],
635 descriptions: desc
636 }) == [
637 Map.merge(object.data, %{"name" => "test-desc"})
638 ]
639 end
640
641 test "returns attachments without descs" do
642 object = insert(:note)
643 assert Utils.attachments_from_ids(%{media_ids: ["#{object.id}"]}) == [object.data]
644 end
645
646 test "returns [] when not pass media_ids" do
647 assert Utils.attachments_from_ids(%{}) == []
648 end
649 end
650
651 describe "maybe_add_list_data/3" do
652 test "adds list params when found user list" do
653 user = insert(:user)
654 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
655
656 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
657 %{
658 additional: %{"bcc" => [list.ap_id], "listMessage" => list.ap_id},
659 object: %{"listMessage" => list.ap_id}
660 }
661 end
662
663 test "returns original params when list not found" do
664 user = insert(:user)
665 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", insert(:user))
666
667 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
668 %{additional: %{}, object: %{}}
669 end
670 end
671
672 describe "maybe_add_attachments/3" do
673 test "returns parsed results when attachment_links is false" do
674 assert Utils.maybe_add_attachments(
675 {"test", [], ["tags"]},
676 [],
677 false
678 ) == {"test", [], ["tags"]}
679 end
680
681 test "adds attachments to parsed results" do
682 attachment = %{"url" => [%{"href" => "SakuraPM.png"}]}
683
684 assert Utils.maybe_add_attachments(
685 {"test", [], ["tags"]},
686 [attachment],
687 true
688 ) == {
689 "test<br><a href=\"SakuraPM.png\" class='attachment'>SakuraPM.png</a>",
690 [],
691 ["tags"]
692 }
693 end
694 end
695 end