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
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>]
183 test "link with local mention" do
184 insert(:user, %{nickname: "lain"})
186 code = "https://example.com/@lain"
187 {result, [], []} = Utils.format_input(code, "text/markdown")
188 assert result == ~s[<p><a href="#{code}">#{code}</a></p>]
191 test "local mentions" do
192 mario = insert(:user, %{nickname: "mario"})
193 luigi = insert(:user, %{nickname: "luigi"})
195 code = "@mario @luigi yo what's up?"
196 {result, _, []} = Utils.format_input(code, "text/markdown")
199 ~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>]
202 test "remote mentions" do
203 mario = insert(:user, %{nickname: "mario@mushroom.world", local: false})
204 luigi = insert(:user, %{nickname: "luigi@mushroom.world", local: false})
206 code = "@mario@mushroom.world @luigi@mushroom.world yo what's up?"
207 {result, _, []} = Utils.format_input(code, "text/markdown")
210 ~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>]
214 code = ~s[<a href="http://example.org/">OwO</a><!-- what's this?-->]
215 {result, [], []} = Utils.format_input(code, "text/markdown")
216 assert result == ~s[<a href="http://example.org/">OwO</a>]
220 code = ~s[before\n\n-----\n\nafter]
221 {result, [], []} = Utils.format_input(code, "text/markdown")
222 assert result == "<p>before</p><hr/><p>after</p>"
226 code = ~s[> whoms't are you quoting?]
227 {result, [], []} = Utils.format_input(code, "text/markdown")
228 assert result == "<blockquote><p>whoms’t are you quoting?</p></blockquote>"
233 {result, [], []} = Utils.format_input(code, "text/markdown")
234 assert result == ~s[<p><code class="inline">mix</code></p>]
237 {result, [], []} = Utils.format_input(code, "text/markdown")
238 assert result == ~s[<p><code class="inline">mix</code></p>]
240 code = ~s[```\nputs "Hello World"\n```]
241 {result, [], []} = Utils.format_input(code, "text/markdown")
242 assert result == ~s[<pre><code>puts "Hello World"</code></pre>]
244 code = ~s[ <div>\n </div>]
245 {result, [], []} = Utils.format_input(code, "text/markdown")
246 assert result == ~s[<pre><code><div>\n</div></code></pre>]
250 code = ~s[- one\n- two\n- three\n- four]
251 {result, [], []} = Utils.format_input(code, "text/markdown")
252 assert result == "<ul><li>one</li><li>two</li><li>three</li><li>four</li></ul>"
254 code = ~s[1. one\n2. two\n3. three\n4. four\n]
255 {result, [], []} = Utils.format_input(code, "text/markdown")
256 assert result == "<ol><li>one</li><li>two</li><li>three</li><li>four</li></ol>"
259 test "delegated renderers" do
261 {result, [], []} = Utils.format_input(code, "text/markdown")
262 assert result == ~s[<p><em>aaaa~</em></p>]
265 {result, [], []} = Utils.format_input(code, "text/markdown")
266 assert result == ~s[<p><strong>aaaa~</strong></p>]
270 {result, [], []} = Utils.format_input(code, "text/markdown")
271 assert result == ~s[<p><del>aaaa</del>~</p>]
275 describe "formats date to asctime" do
276 test "when date is in ISO 8601 format" do
277 date = DateTime.utc_now() |> DateTime.to_iso8601()
281 |> DateTime.from_iso8601()
283 |> Calendar.Strftime.strftime!("%a %b %d %H:%M:%S %z %Y")
285 assert Utils.date_to_asctime(date) == expected
288 test "when date is a binary in wrong format" do
289 date = DateTime.utc_now()
293 assert capture_log(fn ->
294 assert Utils.date_to_asctime(date) == expected
295 end) =~ "[warning] Date #{date} in wrong format, must be ISO 8601"
298 test "when date is a Unix timestamp" do
299 date = DateTime.utc_now() |> DateTime.to_unix()
303 assert capture_log(fn ->
304 assert Utils.date_to_asctime(date) == expected
305 end) =~ "[warning] Date #{date} in wrong format, must be ISO 8601"
308 test "when date is nil" do
311 assert capture_log(fn ->
312 assert Utils.date_to_asctime(nil) == expected
313 end) =~ "[warning] Date in wrong format, must be ISO 8601"
316 test "when date is a random string" do
317 assert capture_log(fn ->
318 assert Utils.date_to_asctime("foo") == ""
319 end) =~ "[warning] Date foo in wrong format, must be ISO 8601"
323 describe "get_to_and_cc" do
324 test "for public posts, not a reply" do
326 mentioned_user = insert(:user)
327 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "public"}
329 {to, cc} = Utils.get_to_and_cc(draft)
331 assert length(to) == 2
332 assert length(cc) == 1
334 assert @public_address in to
335 assert mentioned_user.ap_id in to
336 assert user.follower_address in cc
339 test "for public posts, a reply" do
341 mentioned_user = insert(:user)
342 third_user = insert(:user)
343 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
345 draft = %ActivityDraft{
347 mentions: [mentioned_user.ap_id],
348 visibility: "public",
349 in_reply_to: activity
352 {to, cc} = Utils.get_to_and_cc(draft)
354 assert length(to) == 3
355 assert length(cc) == 1
357 assert @public_address in to
358 assert mentioned_user.ap_id in to
359 assert third_user.ap_id in to
360 assert user.follower_address in cc
363 test "for unlisted posts, not a reply" do
365 mentioned_user = insert(:user)
366 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "unlisted"}
368 {to, cc} = Utils.get_to_and_cc(draft)
370 assert length(to) == 2
371 assert length(cc) == 1
373 assert @public_address in cc
374 assert mentioned_user.ap_id in to
375 assert user.follower_address in to
378 test "for unlisted posts, a reply" do
380 mentioned_user = insert(:user)
381 third_user = insert(:user)
382 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
384 draft = %ActivityDraft{
386 mentions: [mentioned_user.ap_id],
387 visibility: "unlisted",
388 in_reply_to: activity
391 {to, cc} = Utils.get_to_and_cc(draft)
393 assert length(to) == 3
394 assert length(cc) == 1
396 assert @public_address in cc
397 assert mentioned_user.ap_id in to
398 assert third_user.ap_id in to
399 assert user.follower_address in to
402 test "for private posts, not a reply" do
404 mentioned_user = insert(:user)
405 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "private"}
407 {to, cc} = Utils.get_to_and_cc(draft)
408 assert length(to) == 2
409 assert Enum.empty?(cc)
411 assert mentioned_user.ap_id in to
412 assert user.follower_address in to
415 test "for private posts, a reply" do
417 mentioned_user = insert(:user)
418 third_user = insert(:user)
419 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
421 draft = %ActivityDraft{
423 mentions: [mentioned_user.ap_id],
424 visibility: "private",
425 in_reply_to: activity
428 {to, cc} = Utils.get_to_and_cc(draft)
430 assert length(to) == 2
431 assert Enum.empty?(cc)
433 assert mentioned_user.ap_id in to
434 assert user.follower_address in to
437 test "for direct posts, not a reply" do
439 mentioned_user = insert(:user)
440 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "direct"}
442 {to, cc} = Utils.get_to_and_cc(draft)
444 assert length(to) == 1
445 assert Enum.empty?(cc)
447 assert mentioned_user.ap_id in to
450 test "for direct posts, a reply" do
452 mentioned_user = insert(:user)
453 third_user = insert(:user)
454 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
456 draft = %ActivityDraft{
458 mentions: [mentioned_user.ap_id],
459 visibility: "direct",
460 in_reply_to: activity
463 {to, cc} = Utils.get_to_and_cc(draft)
465 assert length(to) == 1
466 assert Enum.empty?(cc)
468 assert mentioned_user.ap_id in to
470 {:ok, direct_activity} = CommonAPI.post(third_user, %{status: "uguu", visibility: "direct"})
472 draft = %ActivityDraft{
474 mentions: [mentioned_user.ap_id],
475 visibility: "direct",
476 in_reply_to: direct_activity
479 {to, cc} = Utils.get_to_and_cc(draft)
481 assert length(to) == 2
482 assert Enum.empty?(cc)
484 assert mentioned_user.ap_id in to
485 assert third_user.ap_id in to
489 describe "to_master_date/1" do
490 test "removes microseconds from date (NaiveDateTime)" do
491 assert Utils.to_masto_date(~N[2015-01-23 23:50:07.123]) == "2015-01-23T23:50:07.000Z"
494 test "removes microseconds from date (String)" do
495 assert Utils.to_masto_date("2015-01-23T23:50:07.123Z") == "2015-01-23T23:50:07.000Z"
498 test "returns empty string when date invalid" do
499 assert Utils.to_masto_date("2015-01?23T23:50:07.123Z") == ""
503 describe "maybe_notify_mentioned_recipients/2" do
504 test "returns recipients when activity is not `Create`" do
505 activity = insert(:like_activity)
506 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == ["test"]
509 test "returns recipients from tag" do
517 %{"type" => "Hashtag"},
519 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
520 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
521 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
526 activity = insert(:note_activity, user: user, note: object)
528 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
530 "https://testing.pleroma.lol/users/lain",
531 "https://shitposter.club/user/5381"
535 test "returns recipients when object is map" do
537 object = insert(:note, user: user)
540 insert(:note_activity,
546 %{"type" => "Hashtag"},
548 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
549 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
550 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
556 Pleroma.Repo.delete(object)
558 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
560 "https://testing.pleroma.lol/users/lain",
561 "https://shitposter.club/user/5381"
565 test "returns recipients when object not found" do
567 object = insert(:note, user: user)
569 activity = insert(:note_activity, user: user, note: object)
570 Pleroma.Repo.delete(object)
572 obj_url = activity.data["object"]
575 %{method: :get, url: ^obj_url} ->
576 %Tesla.Env{status: 404, body: ""}
579 assert Utils.maybe_notify_mentioned_recipients(["test-test"], activity) == [
585 describe "attachments_from_ids_descs/2" do
586 test "returns [] when attachment ids is empty" do
587 assert Utils.attachments_from_ids_descs([], "{}") == []
590 test "returns list attachments with desc" do
591 object = insert(:note)
592 desc = Jason.encode!(%{object.id => "test-desc"})
594 assert Utils.attachments_from_ids_descs(["#{object.id}", "34"], desc) == [
595 Map.merge(object.data, %{"name" => "test-desc"})
600 describe "attachments_from_ids/1" do
601 test "returns attachments with descs" do
602 object = insert(:note)
603 desc = Jason.encode!(%{object.id => "test-desc"})
605 assert Utils.attachments_from_ids(%{
606 media_ids: ["#{object.id}"],
609 Map.merge(object.data, %{"name" => "test-desc"})
613 test "returns attachments without descs" do
614 object = insert(:note)
615 assert Utils.attachments_from_ids(%{media_ids: ["#{object.id}"]}) == [object.data]
618 test "returns [] when not pass media_ids" do
619 assert Utils.attachments_from_ids(%{}) == []
623 describe "maybe_add_list_data/3" do
624 test "adds list params when found user list" do
626 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
628 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
630 additional: %{"bcc" => [list.ap_id], "listMessage" => list.ap_id},
631 object: %{"listMessage" => list.ap_id}
635 test "returns original params when list not found" do
637 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", insert(:user))
639 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
640 %{additional: %{}, object: %{}}
644 describe "maybe_add_attachments/3" do
645 test "returns parsed results when attachment_links is false" do
646 assert Utils.maybe_add_attachments(
647 {"test", [], ["tags"]},
650 ) == {"test", [], ["tags"]}
653 test "adds attachments to parsed results" do
654 attachment = %{"url" => [%{"href" => "SakuraPM.png"}]}
656 assert Utils.maybe_add_attachments(
657 {"test", [], ["tags"]},
661 "test<br><a href=\"SakuraPM.png\" class='attachment'>SakuraPM.png</a>",