1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.CommonAPI.UtilsTest do
6 alias Pleroma.Builders.UserBuilder
8 alias Pleroma.Web.CommonAPI
9 alias Pleroma.Web.CommonAPI.ActivityDraft
10 alias Pleroma.Web.CommonAPI.Utils
13 import ExUnit.CaptureLog
14 import Pleroma.Factory
16 @public_address "https://www.w3.org/ns/activitystreams#Public"
18 describe "add_attachments/2" do
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"
24 "url" => [%{"href" => URI.encode(name)}]
27 %{name: name, attachment: attachment}
30 test "it adds attachment links to a given text and attachment set", %{
32 attachment: attachment
35 clear_config([Pleroma.Upload, :filename_display_max_length], len)
38 "<br><a href=\"#{URI.encode(name)}\" class='attachment'>#{String.slice(name, 0..len)}…</a>"
40 assert Utils.add_attachments("", [attachment]) == expected
43 test "doesn't truncate file name if config for truncate is set to 0", %{
45 attachment: attachment
47 clear_config([Pleroma.Upload, :filename_display_max_length], 0)
49 expected = "<br><a href=\"#{URI.encode(name)}\" class='attachment'>#{name}</a>"
51 assert Utils.add_attachments("", [attachment]) == expected
55 describe "it confirms the password given is the current users password" do
56 test "incorrect password given" do
57 {:ok, user} = UserBuilder.insert()
59 assert Utils.confirm_current_password(user, "") == {:error, "Invalid password."}
62 test "correct password given" do
63 {:ok, user} = UserBuilder.insert()
64 assert Utils.confirm_current_password(user, "test") == {:ok, user}
68 describe "format_input/3" do
69 test "works for bare text/plain" do
71 expected = "hello world!"
73 {output, [], []} = Utils.format_input(text, "text/plain")
75 assert output == expected
77 text = "hello world!\n\nsecond paragraph!"
78 expected = "hello world!<br><br>second paragraph!"
80 {output, [], []} = Utils.format_input(text, "text/plain")
82 assert output == expected
85 test "works for bare text/html" do
86 text = "<p>hello world!</p>"
87 expected = "<p>hello world!</p>"
89 {output, [], []} = Utils.format_input(text, "text/html")
91 assert output == expected
93 text = "<p>hello world!</p><br/>\n<p>second paragraph</p>"
94 expected = "<p>hello world!</p><br/>\n<p>second paragraph</p>"
96 {output, [], []} = Utils.format_input(text, "text/html")
98 assert output == expected
101 test "works for bare text/markdown" do
102 text = "**hello world**"
103 expected = "<p><strong>hello world</strong></p>"
105 {output, [], []} = Utils.format_input(text, "text/markdown")
107 assert output == expected
109 text = "**hello world**\n\n*another paragraph*"
110 expected = "<p><strong>hello world</strong></p><p><em>another paragraph</em></p>"
112 {output, [], []} = Utils.format_input(text, "text/markdown")
114 assert output == expected
122 expected = "<blockquote><p>cool quote</p></blockquote><p>by someone</p>"
124 {output, [], []} = Utils.format_input(text, "text/markdown")
126 assert output == expected
129 test "works for bare text/bbcode" do
130 text = "[b]hello world[/b]"
131 expected = "<strong>hello world</strong>"
133 {output, [], []} = Utils.format_input(text, "text/bbcode")
135 assert output == expected
137 text = "[b]hello world![/b]\n\nsecond paragraph!"
138 expected = "<strong>hello world!</strong><br><br>second paragraph!"
140 {output, [], []} = Utils.format_input(text, "text/bbcode")
142 assert output == expected
144 text = "[b]hello world![/b]\n\n<strong>second paragraph!</strong>"
147 "<strong>hello world!</strong><br><br><strong>second paragraph!</strong>"
149 {output, [], []} = Utils.format_input(text, "text/bbcode")
151 assert output == expected
154 test "works for text/markdown with mentions" do
156 UserBuilder.insert(%{nickname: "user__test", ap_id: "http://foo.com/user__test"})
158 text = "**hello world**\n\n*another @user__test and @user__test google.com paragraph*"
160 {output, _, _} = Utils.format_input(text, "text/markdown")
163 ~s(<p><strong>hello world</strong></p><p><em>another <span class="h-card"><a class="u-url mention" data-user="#{
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="#{
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>)
171 describe "format_input/3 with markdown" do
173 code = ~s[Hello\n\nWorld!]
174 {result, [], []} = Utils.format_input(code, "text/markdown")
175 assert result == "<p>Hello</p><p>World!</p>"
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>]
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>]
188 test "link with local mention" do
189 insert(:user, %{nickname: "lain"})
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>]
196 test "local mentions" do
197 mario = insert(:user, %{nickname: "mario"})
198 luigi = insert(:user, %{nickname: "luigi"})
200 code = "@mario @luigi yo what's up?"
201 {result, _, []} = Utils.format_input(code, "text/markdown")
204 ~s[<p><span class="h-card"><a class="u-url mention" data-user="#{mario.id}" href="#{
206 }" rel="ugc">@<span>mario</span></a></span> <span class="h-card"><a class="u-url mention" data-user="#{
208 }" href="#{luigi.ap_id}" rel="ugc">@<span>luigi</span></a></span> yo what’s up?</p>]
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})
215 code = "@mario@mushroom.world @luigi@mushroom.world yo what's up?"
216 {result, _, []} = Utils.format_input(code, "text/markdown")
219 ~s[<p><span class="h-card"><a class="u-url mention" data-user="#{mario.id}" href="#{
221 }" rel="ugc">@<span>mario</span></a></span> <span class="h-card"><a class="u-url mention" data-user="#{
223 }" href="#{luigi.ap_id}" rel="ugc">@<span>luigi</span></a></span> yo what’s up?</p>]
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>]
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>"
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>"
246 {result, [], []} = Utils.format_input(code, "text/markdown")
247 assert result == ~s[<p><code class="inline">mix</code></p>]
250 {result, [], []} = Utils.format_input(code, "text/markdown")
251 assert result == ~s[<p><code class="inline">mix</code></p>]
253 code = ~s[```\nputs "Hello World"\n```]
254 {result, [], []} = Utils.format_input(code, "text/markdown")
255 assert result == ~s[<pre><code>puts "Hello World"</code></pre>]
257 code = ~s[ <div>\n </div>]
258 {result, [], []} = Utils.format_input(code, "text/markdown")
259 assert result == ~s[<pre><code><div>\n</div></code></pre>]
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>"
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>"
272 test "delegated renderers" do
274 {result, [], []} = Utils.format_input(code, "text/markdown")
275 assert result == ~s[<p><em>aaaa~</em></p>]
278 {result, [], []} = Utils.format_input(code, "text/markdown")
279 assert result == ~s[<p><strong>aaaa~</strong></p>]
283 {result, [], []} = Utils.format_input(code, "text/markdown")
284 assert result == ~s[<p><del>aaaa</del>~</p>]
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")
293 assert conversation_id == object.id
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")
300 assert conversation_id == object.id
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()
310 |> DateTime.from_iso8601()
312 |> Calendar.Strftime.strftime!("%a %b %d %H:%M:%S %z %Y")
314 assert Utils.date_to_asctime(date) == expected
317 test "when date is a binary in wrong format" do
318 date = DateTime.utc_now()
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"
327 test "when date is a Unix timestamp" do
328 date = DateTime.utc_now() |> DateTime.to_unix()
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"
337 test "when date is nil" do
340 assert capture_log(fn ->
341 assert Utils.date_to_asctime(nil) == expected
342 end) =~ "[warn] Date in wrong format, must be ISO 8601"
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"
352 describe "get_to_and_cc" do
353 test "for public posts, not a reply" do
355 mentioned_user = insert(:user)
356 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "public"}
358 {to, cc} = Utils.get_to_and_cc(draft)
360 assert length(to) == 2
361 assert length(cc) == 1
363 assert @public_address in to
364 assert mentioned_user.ap_id in to
365 assert user.follower_address in cc
368 test "for public posts, a reply" do
370 mentioned_user = insert(:user)
371 third_user = insert(:user)
372 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
374 draft = %ActivityDraft{
376 mentions: [mentioned_user.ap_id],
377 visibility: "public",
378 in_reply_to: activity
381 {to, cc} = Utils.get_to_and_cc(draft)
383 assert length(to) == 3
384 assert length(cc) == 1
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
392 test "for unlisted posts, not a reply" do
394 mentioned_user = insert(:user)
395 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "unlisted"}
397 {to, cc} = Utils.get_to_and_cc(draft)
399 assert length(to) == 2
400 assert length(cc) == 1
402 assert @public_address in cc
403 assert mentioned_user.ap_id in to
404 assert user.follower_address in to
407 test "for unlisted posts, a reply" do
409 mentioned_user = insert(:user)
410 third_user = insert(:user)
411 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
413 draft = %ActivityDraft{
415 mentions: [mentioned_user.ap_id],
416 visibility: "unlisted",
417 in_reply_to: activity
420 {to, cc} = Utils.get_to_and_cc(draft)
422 assert length(to) == 3
423 assert length(cc) == 1
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
431 test "for private posts, not a reply" do
433 mentioned_user = insert(:user)
434 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "private"}
436 {to, cc} = Utils.get_to_and_cc(draft)
437 assert length(to) == 2
438 assert Enum.empty?(cc)
440 assert mentioned_user.ap_id in to
441 assert user.follower_address in to
444 test "for private posts, a reply" do
446 mentioned_user = insert(:user)
447 third_user = insert(:user)
448 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
450 draft = %ActivityDraft{
452 mentions: [mentioned_user.ap_id],
453 visibility: "private",
454 in_reply_to: activity
457 {to, cc} = Utils.get_to_and_cc(draft)
459 assert length(to) == 2
460 assert Enum.empty?(cc)
462 assert mentioned_user.ap_id in to
463 assert user.follower_address in to
466 test "for direct posts, not a reply" do
468 mentioned_user = insert(:user)
469 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "direct"}
471 {to, cc} = Utils.get_to_and_cc(draft)
473 assert length(to) == 1
474 assert Enum.empty?(cc)
476 assert mentioned_user.ap_id in to
479 test "for direct posts, a reply" do
481 mentioned_user = insert(:user)
482 third_user = insert(:user)
483 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
485 draft = %ActivityDraft{
487 mentions: [mentioned_user.ap_id],
488 visibility: "direct",
489 in_reply_to: activity
492 {to, cc} = Utils.get_to_and_cc(draft)
494 assert length(to) == 1
495 assert Enum.empty?(cc)
497 assert mentioned_user.ap_id in to
499 {:ok, direct_activity} = CommonAPI.post(third_user, %{status: "uguu", visibility: "direct"})
501 draft = %ActivityDraft{
503 mentions: [mentioned_user.ap_id],
504 visibility: "direct",
505 in_reply_to: direct_activity
508 {to, cc} = Utils.get_to_and_cc(draft)
510 assert length(to) == 2
511 assert Enum.empty?(cc)
513 assert mentioned_user.ap_id in to
514 assert third_user.ap_id in to
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"
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"
527 test "returns empty string when date invalid" do
528 assert Utils.to_masto_date("2015-01?23T23:50:07.123Z") == ""
532 describe "conversation_id_to_context/1" do
534 object = insert(:note)
535 assert Utils.conversation_id_to_context(object.id) == object.data["id"]
538 test "returns error if object not found" do
539 assert Utils.conversation_id_to_context("123") == {:error, "No such conversation"}
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"]
549 test "returns recipients from tag" do
557 %{"type" => "Hashtag"},
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"}
566 activity = insert(:note_activity, user: user, note: object)
568 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
570 "https://testing.pleroma.lol/users/lain",
571 "https://shitposter.club/user/5381"
575 test "returns recipients when object is map" do
577 object = insert(:note, user: user)
580 insert(:note_activity,
586 %{"type" => "Hashtag"},
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"}
596 Pleroma.Repo.delete(object)
598 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
600 "https://testing.pleroma.lol/users/lain",
601 "https://shitposter.club/user/5381"
605 test "returns recipients when object not found" do
607 object = insert(:note, user: user)
609 activity = insert(:note_activity, user: user, note: object)
610 Pleroma.Repo.delete(object)
612 obj_url = activity.data["object"]
615 %{method: :get, url: ^obj_url} ->
616 %Tesla.Env{status: 404, body: ""}
619 assert Utils.maybe_notify_mentioned_recipients(["test-test"], activity) == [
625 describe "attachments_from_ids_descs/2" do
626 test "returns [] when attachment ids is empty" do
627 assert Utils.attachments_from_ids_descs([], "{}") == []
630 test "returns list attachments with desc" do
631 object = insert(:note)
632 desc = Jason.encode!(%{object.id => "test-desc"})
634 assert Utils.attachments_from_ids_descs(["#{object.id}", "34"], desc) == [
635 Map.merge(object.data, %{"name" => "test-desc"})
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"})
645 assert Utils.attachments_from_ids(%{
646 media_ids: ["#{object.id}"],
649 Map.merge(object.data, %{"name" => "test-desc"})
653 test "returns attachments without descs" do
654 object = insert(:note)
655 assert Utils.attachments_from_ids(%{media_ids: ["#{object.id}"]}) == [object.data]
658 test "returns [] when not pass media_ids" do
659 assert Utils.attachments_from_ids(%{}) == []
663 describe "maybe_add_list_data/3" do
664 test "adds list params when found user list" do
666 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
668 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
670 additional: %{"bcc" => [list.ap_id], "listMessage" => list.ap_id},
671 object: %{"listMessage" => list.ap_id}
675 test "returns original params when list not found" do
677 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", insert(:user))
679 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
680 %{additional: %{}, object: %{}}
684 describe "make_note_data/1" do
685 test "returns note data" do
688 user2 = insert(:user)
689 user3 = insert(:user)
691 draft = %ActivityDraft{
695 content_html: "<h1>This is :moominmamma: note</h1>",
696 in_reply_to: note.id,
697 tags: [name: "jimm"],
698 summary: "test summary",
700 extra: %{"custom_tag" => "test"}
703 assert Utils.make_note_data(draft) == %{
704 "actor" => user.ap_id,
706 "cc" => [user3.ap_id],
707 "content" => "<h1>This is :moominmamma: note</h1>",
709 "sensitive" => false,
710 "summary" => "test summary",
712 "to" => [user2.ap_id],
714 "custom_tag" => "test"
719 describe "maybe_add_attachments/3" do
720 test "returns parsed results when attachment_links is false" do
721 assert Utils.maybe_add_attachments(
722 {"test", [], ["tags"]},
725 ) == {"test", [], ["tags"]}
728 test "adds attachments to parsed results" do
729 attachment = %{"url" => [%{"href" => "SakuraPM.png"}]}
731 assert Utils.maybe_add_attachments(
732 {"test", [], ["tags"]},
736 "test<br><a href=\"SakuraPM.png\" class='attachment'>SakuraPM.png</a>",