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