1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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
7 alias Pleroma.Web.CommonAPI
8 alias Pleroma.Web.CommonAPI.ActivityDraft
9 alias Pleroma.Web.CommonAPI.Utils
12 import ExUnit.CaptureLog
13 import Pleroma.Factory
15 @public_address "https://www.w3.org/ns/activitystreams#Public"
17 describe "add_attachments/2" do
20 "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"
23 "url" => [%{"href" => URI.encode(name)}]
26 %{name: name, attachment: attachment}
29 test "it adds attachment links to a given text and attachment set", %{
31 attachment: attachment
34 clear_config([Pleroma.Upload, :filename_display_max_length], len)
37 "<br><a href=\"#{URI.encode(name)}\" class='attachment'>#{String.slice(name, 0..len)}…</a>"
39 assert Utils.add_attachments("", [attachment]) == expected
42 test "doesn't truncate file name if config for truncate is set to 0", %{
44 attachment: attachment
46 clear_config([Pleroma.Upload, :filename_display_max_length], 0)
48 expected = "<br><a href=\"#{URI.encode(name)}\" class='attachment'>#{name}</a>"
50 assert Utils.add_attachments("", [attachment]) == expected
54 describe "it confirms the password given is the current users password" do
55 test "incorrect password given" do
56 {:ok, user} = UserBuilder.insert()
58 assert Utils.confirm_current_password(user, "") == {:error, "Invalid password."}
61 test "correct password given" do
62 {:ok, user} = UserBuilder.insert()
63 assert Utils.confirm_current_password(user, "test") == {:ok, user}
67 describe "format_input/3" do
68 test "works for bare text/plain" do
70 expected = "hello world!"
72 {output, [], []} = Utils.format_input(text, "text/plain")
74 assert output == expected
76 text = "hello world!\n\nsecond paragraph!"
77 expected = "hello world!<br><br>second paragraph!"
79 {output, [], []} = Utils.format_input(text, "text/plain")
81 assert output == expected
84 test "works for bare text/html" do
85 text = "<p>hello world!</p>"
86 expected = "<p>hello world!</p>"
88 {output, [], []} = Utils.format_input(text, "text/html")
90 assert output == expected
92 text = "<p>hello world!</p><br/>\n<p>second paragraph</p>"
93 expected = "<p>hello world!</p><br/>\n<p>second paragraph</p>"
95 {output, [], []} = Utils.format_input(text, "text/html")
97 assert output == expected
100 test "works for bare text/markdown" do
101 text = "**hello world**"
102 expected = "<p><strong>hello world</strong></p>"
104 {output, [], []} = Utils.format_input(text, "text/markdown")
106 assert output == expected
108 text = "**hello world**\n\n*another paragraph*"
109 expected = "<p><strong>hello world</strong></p><p><em>another paragraph</em></p>"
111 {output, [], []} = Utils.format_input(text, "text/markdown")
113 assert output == expected
121 expected = "<blockquote><p>cool quote</p></blockquote><p>by someone</p>"
123 {output, [], []} = Utils.format_input(text, "text/markdown")
125 assert output == expected
128 test "works for bare text/bbcode" do
129 text = "[b]hello world[/b]"
130 expected = "<strong>hello world</strong>"
132 {output, [], []} = Utils.format_input(text, "text/bbcode")
134 assert output == expected
136 text = "[b]hello world![/b]\n\nsecond paragraph!"
137 expected = "<strong>hello world!</strong><br><br>second paragraph!"
139 {output, [], []} = Utils.format_input(text, "text/bbcode")
141 assert output == expected
143 text = "[b]hello world![/b]\n\n<strong>second paragraph!</strong>"
146 "<strong>hello world!</strong><br><br><strong>second paragraph!</strong>"
148 {output, [], []} = Utils.format_input(text, "text/bbcode")
150 assert output == expected
153 test "works for text/markdown with mentions" do
155 UserBuilder.insert(%{nickname: "user__test", ap_id: "http://foo.com/user__test"})
157 text = "**hello world**\n\n*another @user__test and @user__test google.com paragraph*"
159 {output, _, _} = Utils.format_input(text, "text/markdown")
162 ~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>)
166 describe "format_input/3 with markdown" do
168 code = ~s[Hello\n\nWorld!]
169 {result, [], []} = Utils.format_input(code, "text/markdown")
170 assert result == "<p>Hello</p><p>World!</p>"
174 code = "https://en.wikipedia.org/wiki/Animal_Crossing_(video_game)"
175 {result, [], []} = Utils.format_input(code, "text/markdown")
176 assert result == ~s[<p><a href="#{code}">#{code}</a></p>]
178 code = "https://github.com/pragdave/earmark/"
179 {result, [], []} = Utils.format_input(code, "text/markdown")
180 assert result == ~s[<p><a href="#{code}">#{code}</a></p>]
182 code = "https://github.com/~foo/bar"
183 {result, [], []} = Utils.format_input(code, "text/markdown")
184 assert result == ~s[<p><a href="#{code}">#{code}</a></p>]
187 test "link with local mention" do
188 insert(:user, %{nickname: "lain"})
190 code = "https://example.com/@lain"
191 {result, [], []} = Utils.format_input(code, "text/markdown")
192 assert result == ~s[<p><a href="#{code}">#{code}</a></p>]
195 test "local mentions" do
196 mario = insert(:user, %{nickname: "mario"})
197 luigi = insert(:user, %{nickname: "luigi"})
199 code = "@mario @luigi yo what's up?"
200 {result, _, []} = Utils.format_input(code, "text/markdown")
203 ~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>]
206 test "remote mentions" do
207 mario = insert(:user, %{nickname: "mario@mushroom.world", local: false})
208 luigi = insert(:user, %{nickname: "luigi@mushroom.world", local: false})
210 code = "@mario@mushroom.world @luigi@mushroom.world yo what's up?"
211 {result, _, []} = Utils.format_input(code, "text/markdown")
214 ~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>]
218 code = ~s[<a href="http://example.org/">OwO</a><!-- what's this?-->]
219 {result, [], []} = Utils.format_input(code, "text/markdown")
220 assert result == ~s[<a href="http://example.org/">OwO</a>]
224 code = ~s[before\n\n-----\n\nafter]
225 {result, [], []} = Utils.format_input(code, "text/markdown")
226 assert result == "<p>before</p><hr/><p>after</p>"
230 code = ~s[> whoms't are you quoting?]
231 {result, [], []} = Utils.format_input(code, "text/markdown")
232 assert result == "<blockquote><p>whoms’t are you quoting?</p></blockquote>"
237 {result, [], []} = Utils.format_input(code, "text/markdown")
238 assert result == ~s[<p><code class="inline">mix</code></p>]
241 {result, [], []} = Utils.format_input(code, "text/markdown")
242 assert result == ~s[<p><code class="inline">mix</code></p>]
244 code = ~s[```\nputs "Hello World"\n```]
245 {result, [], []} = Utils.format_input(code, "text/markdown")
246 assert result == ~s[<pre><code>puts "Hello World"</code></pre>]
248 code = ~s[ <div>\n </div>]
249 {result, [], []} = Utils.format_input(code, "text/markdown")
250 assert result == ~s[<pre><code><div>\n</div></code></pre>]
254 code = ~s[- one\n- two\n- three\n- four]
255 {result, [], []} = Utils.format_input(code, "text/markdown")
256 assert result == "<ul><li>one</li><li>two</li><li>three</li><li>four</li></ul>"
258 code = ~s[1. one\n2. two\n3. three\n4. four\n]
259 {result, [], []} = Utils.format_input(code, "text/markdown")
260 assert result == "<ol><li>one</li><li>two</li><li>three</li><li>four</li></ol>"
263 test "delegated renderers" do
265 {result, [], []} = Utils.format_input(code, "text/markdown")
266 assert result == ~s[<p><em>aaaa~</em></p>]
269 {result, [], []} = Utils.format_input(code, "text/markdown")
270 assert result == ~s[<p><strong>aaaa~</strong></p>]
274 {result, [], []} = Utils.format_input(code, "text/markdown")
275 assert result == ~s[<p><del>aaaa</del>~</p>]
279 describe "formats date to asctime" do
280 test "when date is in ISO 8601 format" do
281 date = DateTime.utc_now() |> DateTime.to_iso8601()
285 |> DateTime.from_iso8601()
287 |> Calendar.Strftime.strftime!("%a %b %d %H:%M:%S %z %Y")
289 assert Utils.date_to_asctime(date) == expected
292 test "when date is a binary in wrong format" do
293 date = DateTime.utc_now()
297 assert capture_log(fn ->
298 assert Utils.date_to_asctime(date) == expected
299 end) =~ "Date #{date} in wrong format, must be ISO 8601"
302 test "when date is a Unix timestamp" do
303 date = DateTime.utc_now() |> DateTime.to_unix()
307 assert capture_log(fn ->
308 assert Utils.date_to_asctime(date) == expected
309 end) =~ "Date #{date} in wrong format, must be ISO 8601"
312 test "when date is nil" do
315 assert capture_log(fn ->
316 assert Utils.date_to_asctime(nil) == expected
317 end) =~ "Date in wrong format, must be ISO 8601"
320 test "when date is a random string" do
321 assert capture_log(fn ->
322 assert Utils.date_to_asctime("foo") == ""
323 end) =~ "Date foo in wrong format, must be ISO 8601"
327 describe "get_to_and_cc" do
328 test "for public posts, not a reply" do
330 mentioned_user = insert(:user)
331 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "public"}
333 {to, cc} = Utils.get_to_and_cc(draft)
335 assert length(to) == 2
336 assert length(cc) == 1
338 assert @public_address in to
339 assert mentioned_user.ap_id in to
340 assert user.follower_address in cc
343 test "for public posts, a reply" do
345 mentioned_user = insert(:user)
346 third_user = insert(:user)
347 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
349 draft = %ActivityDraft{
351 mentions: [mentioned_user.ap_id],
352 visibility: "public",
353 in_reply_to: activity
356 {to, cc} = Utils.get_to_and_cc(draft)
358 assert length(to) == 3
359 assert length(cc) == 1
361 assert @public_address in to
362 assert mentioned_user.ap_id in to
363 assert third_user.ap_id in to
364 assert user.follower_address in cc
367 test "for unlisted posts, not a reply" do
369 mentioned_user = insert(:user)
370 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "unlisted"}
372 {to, cc} = Utils.get_to_and_cc(draft)
374 assert length(to) == 2
375 assert length(cc) == 1
377 assert @public_address in cc
378 assert mentioned_user.ap_id in to
379 assert user.follower_address in to
382 test "for unlisted posts, a reply" do
384 mentioned_user = insert(:user)
385 third_user = insert(:user)
386 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
388 draft = %ActivityDraft{
390 mentions: [mentioned_user.ap_id],
391 visibility: "unlisted",
392 in_reply_to: activity
395 {to, cc} = Utils.get_to_and_cc(draft)
397 assert length(to) == 3
398 assert length(cc) == 1
400 assert @public_address in cc
401 assert mentioned_user.ap_id in to
402 assert third_user.ap_id in to
403 assert user.follower_address in to
406 test "for private posts, not a reply" do
408 mentioned_user = insert(:user)
409 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "private"}
411 {to, cc} = Utils.get_to_and_cc(draft)
412 assert length(to) == 2
413 assert Enum.empty?(cc)
415 assert mentioned_user.ap_id in to
416 assert user.follower_address in to
419 test "for private posts, a reply" do
421 mentioned_user = insert(:user)
422 third_user = insert(:user)
423 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
425 draft = %ActivityDraft{
427 mentions: [mentioned_user.ap_id],
428 visibility: "private",
429 in_reply_to: activity
432 {to, cc} = Utils.get_to_and_cc(draft)
434 assert length(to) == 2
435 assert Enum.empty?(cc)
437 assert mentioned_user.ap_id in to
438 assert user.follower_address in to
441 test "for direct posts, not a reply" do
443 mentioned_user = insert(:user)
444 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "direct"}
446 {to, cc} = Utils.get_to_and_cc(draft)
448 assert length(to) == 1
449 assert Enum.empty?(cc)
451 assert mentioned_user.ap_id in to
454 test "for direct posts, a reply" do
456 mentioned_user = insert(:user)
457 third_user = insert(:user)
458 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
460 draft = %ActivityDraft{
462 mentions: [mentioned_user.ap_id],
463 visibility: "direct",
464 in_reply_to: activity
467 {to, cc} = Utils.get_to_and_cc(draft)
469 assert length(to) == 1
470 assert Enum.empty?(cc)
472 assert mentioned_user.ap_id in to
474 {:ok, direct_activity} = CommonAPI.post(third_user, %{status: "uguu", visibility: "direct"})
476 draft = %ActivityDraft{
478 mentions: [mentioned_user.ap_id],
479 visibility: "direct",
480 in_reply_to: direct_activity
483 {to, cc} = Utils.get_to_and_cc(draft)
485 assert length(to) == 2
486 assert Enum.empty?(cc)
488 assert mentioned_user.ap_id in to
489 assert third_user.ap_id in to
493 describe "to_master_date/1" do
494 test "removes microseconds from date (NaiveDateTime)" do
495 assert Utils.to_masto_date(~N[2015-01-23 23:50:07.123]) == "2015-01-23T23:50:07.000Z"
498 test "removes microseconds from date (String)" do
499 assert Utils.to_masto_date("2015-01-23T23:50:07.123Z") == "2015-01-23T23:50:07.000Z"
502 test "returns empty string when date invalid" do
503 assert Utils.to_masto_date("2015-01?23T23:50:07.123Z") == ""
507 describe "maybe_notify_mentioned_recipients/2" do
508 test "returns recipients when activity is not `Create`" do
509 activity = insert(:like_activity)
510 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == ["test"]
513 test "returns recipients from tag" do
521 %{"type" => "Hashtag"},
523 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
524 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
525 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
530 activity = insert(:note_activity, user: user, note: object)
532 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
534 "https://testing.pleroma.lol/users/lain",
535 "https://shitposter.club/user/5381"
539 test "returns recipients when object is map" do
541 object = insert(:note, user: user)
544 insert(:note_activity,
550 %{"type" => "Hashtag"},
552 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
553 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
554 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
560 Pleroma.Repo.delete(object)
562 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
564 "https://testing.pleroma.lol/users/lain",
565 "https://shitposter.club/user/5381"
569 test "returns recipients when object not found" do
571 object = insert(:note, user: user)
573 activity = insert(:note_activity, user: user, note: object)
574 Pleroma.Repo.delete(object)
576 obj_url = activity.data["object"]
579 %{method: :get, url: ^obj_url} ->
580 %Tesla.Env{status: 404, body: ""}
583 assert Utils.maybe_notify_mentioned_recipients(["test-test"], activity) == [
589 describe "attachments_from_ids_descs/2" do
590 test "returns [] when attachment ids is empty" do
591 assert Utils.attachments_from_ids_descs([], "{}") == []
594 test "returns list attachments with desc" do
595 object = insert(:note)
596 desc = Jason.encode!(%{object.id => "test-desc"})
598 assert Utils.attachments_from_ids_descs(["#{object.id}", "34"], desc) == [
599 Map.merge(object.data, %{"name" => "test-desc"})
604 describe "attachments_from_ids/1" do
605 test "returns attachments with descs" do
606 object = insert(:note)
607 desc = Jason.encode!(%{object.id => "test-desc"})
609 assert Utils.attachments_from_ids(%{
610 media_ids: ["#{object.id}"],
613 Map.merge(object.data, %{"name" => "test-desc"})
617 test "returns attachments without descs" do
618 object = insert(:note)
619 assert Utils.attachments_from_ids(%{media_ids: ["#{object.id}"]}) == [object.data]
622 test "returns [] when not pass media_ids" do
623 assert Utils.attachments_from_ids(%{}) == []
627 describe "maybe_add_list_data/3" do
628 test "adds list params when found user list" do
630 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
632 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
634 additional: %{"bcc" => [list.ap_id], "listMessage" => list.ap_id},
635 object: %{"listMessage" => list.ap_id}
639 test "returns original params when list not found" do
641 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", insert(:user))
643 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
644 %{additional: %{}, object: %{}}
648 describe "maybe_add_attachments/3" do
649 test "returns parsed results when attachment_links is false" do
650 assert Utils.maybe_add_attachments(
651 {"test", [], ["tags"]},
654 ) == {"test", [], ["tags"]}
657 test "adds attachments to parsed results" do
658 attachment = %{"url" => [%{"href" => "SakuraPM.png"}]}
660 assert Utils.maybe_add_attachments(
661 {"test", [], ["tags"]},
665 "test<br><a href=\"SakuraPM.png\" class='attachment'>SakuraPM.png</a>",