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="#{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>)
167 describe "format_input/3 with markdown" do
169 code = ~s[Hello\n\nWorld!]
170 {result, [], []} = Utils.format_input(code, "text/markdown")
171 assert result == "<p>Hello</p><p>World!</p>"
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>]
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>]
184 test "link with local mention" do
185 insert(:user, %{nickname: "lain"})
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>]
192 test "local mentions" do
193 mario = insert(:user, %{nickname: "mario"})
194 luigi = insert(:user, %{nickname: "luigi"})
196 code = "@mario @luigi yo what's up?"
197 {result, _, []} = Utils.format_input(code, "text/markdown")
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>]
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})
207 code = "@mario@mushroom.world @luigi@mushroom.world yo what's up?"
208 {result, _, []} = Utils.format_input(code, "text/markdown")
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>]
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>]
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>"
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>"
234 {result, [], []} = Utils.format_input(code, "text/markdown")
235 assert result == ~s[<p><code class="inline">mix</code></p>]
238 {result, [], []} = Utils.format_input(code, "text/markdown")
239 assert result == ~s[<p><code class="inline">mix</code></p>]
241 code = ~s[```\nputs "Hello World"\n```]
242 {result, [], []} = Utils.format_input(code, "text/markdown")
243 assert result == ~s[<pre><code>puts "Hello World"</code></pre>]
245 code = ~s[ <div>\n </div>]
246 {result, [], []} = Utils.format_input(code, "text/markdown")
247 assert result == ~s[<pre><code><div>\n</div></code></pre>]
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>"
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>"
260 test "delegated renderers" do
262 {result, [], []} = Utils.format_input(code, "text/markdown")
263 assert result == ~s[<p><em>aaaa~</em></p>]
266 {result, [], []} = Utils.format_input(code, "text/markdown")
267 assert result == ~s[<p><strong>aaaa~</strong></p>]
271 {result, [], []} = Utils.format_input(code, "text/markdown")
272 assert result == ~s[<p><del>aaaa</del>~</p>]
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")
281 assert conversation_id == object.id
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")
288 assert conversation_id == object.id
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()
298 |> DateTime.from_iso8601()
300 |> Calendar.Strftime.strftime!("%a %b %d %H:%M:%S %z %Y")
302 assert Utils.date_to_asctime(date) == expected
305 test "when date is a binary in wrong format" do
306 date = DateTime.utc_now()
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"
315 test "when date is a Unix timestamp" do
316 date = DateTime.utc_now() |> DateTime.to_unix()
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"
325 test "when date is nil" do
328 assert capture_log(fn ->
329 assert Utils.date_to_asctime(nil) == expected
330 end) =~ "[warn] Date in wrong format, must be ISO 8601"
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"
340 describe "get_to_and_cc" do
341 test "for public posts, not a reply" do
343 mentioned_user = insert(:user)
344 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "public"}
346 {to, cc} = Utils.get_to_and_cc(draft)
348 assert length(to) == 2
349 assert length(cc) == 1
351 assert @public_address in to
352 assert mentioned_user.ap_id in to
353 assert user.follower_address in cc
356 test "for public posts, a reply" do
358 mentioned_user = insert(:user)
359 third_user = insert(:user)
360 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
362 draft = %ActivityDraft{
364 mentions: [mentioned_user.ap_id],
365 visibility: "public",
366 in_reply_to: activity
369 {to, cc} = Utils.get_to_and_cc(draft)
371 assert length(to) == 3
372 assert length(cc) == 1
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
380 test "for unlisted posts, not a reply" do
382 mentioned_user = insert(:user)
383 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "unlisted"}
385 {to, cc} = Utils.get_to_and_cc(draft)
387 assert length(to) == 2
388 assert length(cc) == 1
390 assert @public_address in cc
391 assert mentioned_user.ap_id in to
392 assert user.follower_address in to
395 test "for unlisted posts, a reply" do
397 mentioned_user = insert(:user)
398 third_user = insert(:user)
399 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
401 draft = %ActivityDraft{
403 mentions: [mentioned_user.ap_id],
404 visibility: "unlisted",
405 in_reply_to: activity
408 {to, cc} = Utils.get_to_and_cc(draft)
410 assert length(to) == 3
411 assert length(cc) == 1
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
419 test "for private posts, not a reply" do
421 mentioned_user = insert(:user)
422 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "private"}
424 {to, cc} = Utils.get_to_and_cc(draft)
425 assert length(to) == 2
426 assert Enum.empty?(cc)
428 assert mentioned_user.ap_id in to
429 assert user.follower_address in to
432 test "for private posts, a reply" do
434 mentioned_user = insert(:user)
435 third_user = insert(:user)
436 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
438 draft = %ActivityDraft{
440 mentions: [mentioned_user.ap_id],
441 visibility: "private",
442 in_reply_to: activity
445 {to, cc} = Utils.get_to_and_cc(draft)
447 assert length(to) == 2
448 assert Enum.empty?(cc)
450 assert mentioned_user.ap_id in to
451 assert user.follower_address in to
454 test "for direct posts, not a reply" do
456 mentioned_user = insert(:user)
457 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "direct"}
459 {to, cc} = Utils.get_to_and_cc(draft)
461 assert length(to) == 1
462 assert Enum.empty?(cc)
464 assert mentioned_user.ap_id in to
467 test "for direct posts, a reply" do
469 mentioned_user = insert(:user)
470 third_user = insert(:user)
471 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
473 draft = %ActivityDraft{
475 mentions: [mentioned_user.ap_id],
476 visibility: "direct",
477 in_reply_to: activity
480 {to, cc} = Utils.get_to_and_cc(draft)
482 assert length(to) == 1
483 assert Enum.empty?(cc)
485 assert mentioned_user.ap_id in to
487 {:ok, direct_activity} = CommonAPI.post(third_user, %{status: "uguu", visibility: "direct"})
489 draft = %ActivityDraft{
491 mentions: [mentioned_user.ap_id],
492 visibility: "direct",
493 in_reply_to: direct_activity
496 {to, cc} = Utils.get_to_and_cc(draft)
498 assert length(to) == 2
499 assert Enum.empty?(cc)
501 assert mentioned_user.ap_id in to
502 assert third_user.ap_id in to
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"
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"
515 test "returns empty string when date invalid" do
516 assert Utils.to_masto_date("2015-01?23T23:50:07.123Z") == ""
520 describe "conversation_id_to_context/1" do
522 object = insert(:note)
523 assert Utils.conversation_id_to_context(object.id) == object.data["id"]
526 test "returns error if object not found" do
527 assert Utils.conversation_id_to_context("123") == {:error, "No such conversation"}
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"]
537 test "returns recipients from tag" do
545 %{"type" => "Hashtag"},
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"}
554 activity = insert(:note_activity, user: user, note: object)
556 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
558 "https://testing.pleroma.lol/users/lain",
559 "https://shitposter.club/user/5381"
563 test "returns recipients when object is map" do
565 object = insert(:note, user: user)
568 insert(:note_activity,
574 %{"type" => "Hashtag"},
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"}
584 Pleroma.Repo.delete(object)
586 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
588 "https://testing.pleroma.lol/users/lain",
589 "https://shitposter.club/user/5381"
593 test "returns recipients when object not found" do
595 object = insert(:note, user: user)
597 activity = insert(:note_activity, user: user, note: object)
598 Pleroma.Repo.delete(object)
600 obj_url = activity.data["object"]
603 %{method: :get, url: ^obj_url} ->
604 %Tesla.Env{status: 404, body: ""}
607 assert Utils.maybe_notify_mentioned_recipients(["test-test"], activity) == [
613 describe "attachments_from_ids_descs/2" do
614 test "returns [] when attachment ids is empty" do
615 assert Utils.attachments_from_ids_descs([], "{}") == []
618 test "returns list attachments with desc" do
619 object = insert(:note)
620 desc = Jason.encode!(%{object.id => "test-desc"})
622 assert Utils.attachments_from_ids_descs(["#{object.id}", "34"], desc) == [
623 Map.merge(object.data, %{"name" => "test-desc"})
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"})
633 assert Utils.attachments_from_ids(%{
634 media_ids: ["#{object.id}"],
637 Map.merge(object.data, %{"name" => "test-desc"})
641 test "returns attachments without descs" do
642 object = insert(:note)
643 assert Utils.attachments_from_ids(%{media_ids: ["#{object.id}"]}) == [object.data]
646 test "returns [] when not pass media_ids" do
647 assert Utils.attachments_from_ids(%{}) == []
651 describe "maybe_add_list_data/3" do
652 test "adds list params when found user list" do
654 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
656 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
658 additional: %{"bcc" => [list.ap_id], "listMessage" => list.ap_id},
659 object: %{"listMessage" => list.ap_id}
663 test "returns original params when list not found" do
665 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", insert(:user))
667 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
668 %{additional: %{}, object: %{}}
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"]},
678 ) == {"test", [], ["tags"]}
681 test "adds attachments to parsed results" do
682 attachment = %{"url" => [%{"href" => "SakuraPM.png"}]}
684 assert Utils.maybe_add_attachments(
685 {"test", [], ["tags"]},
689 "test<br><a href=\"SakuraPM.png\" class='attachment'>SakuraPM.png</a>",