1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 = ~s[<a href="http://example.org/">OwO</a><!-- what's this?-->]
180 {result, [], []} = Utils.format_input(code, "text/markdown")
181 assert result == ~s[<a href="http://example.org/">OwO</a>]
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>"
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>"
198 {result, [], []} = Utils.format_input(code, "text/markdown")
199 assert result == ~s[<p><code class="inline">mix</code></p>]
202 {result, [], []} = Utils.format_input(code, "text/markdown")
203 assert result == ~s[<p><code class="inline">mix</code></p>]
205 code = ~s[```\nputs "Hello World"\n```]
206 {result, [], []} = Utils.format_input(code, "text/markdown")
207 assert result == ~s[<pre><code>puts "Hello World"</code></pre>]
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>"
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>"
220 test "delegated renderers" do
222 {result, [], []} = Utils.format_input(code, "text/markdown")
223 assert result == ~s[<p><em>aaaa~</em></p>]
226 {result, [], []} = Utils.format_input(code, "text/markdown")
227 assert result == ~s[<p><strong>aaaa~</strong></p>]
231 {result, [], []} = Utils.format_input(code, "text/markdown")
232 assert result == ~s[<p><del>aaaa</del>~</p>]
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")
241 assert conversation_id == object.id
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")
248 assert conversation_id == object.id
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()
258 |> DateTime.from_iso8601()
260 |> Calendar.Strftime.strftime!("%a %b %d %H:%M:%S %z %Y")
262 assert Utils.date_to_asctime(date) == expected
265 test "when date is a binary in wrong format" do
266 date = DateTime.utc_now()
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"
275 test "when date is a Unix timestamp" do
276 date = DateTime.utc_now() |> DateTime.to_unix()
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"
285 test "when date is nil" do
288 assert capture_log(fn ->
289 assert Utils.date_to_asctime(nil) == expected
290 end) =~ "[warn] Date in wrong format, must be ISO 8601"
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"
300 describe "get_to_and_cc" do
301 test "for public posts, not a reply" do
303 mentioned_user = insert(:user)
304 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "public"}
306 {to, cc} = Utils.get_to_and_cc(draft)
308 assert length(to) == 2
309 assert length(cc) == 1
311 assert @public_address in to
312 assert mentioned_user.ap_id in to
313 assert user.follower_address in cc
316 test "for public posts, a reply" do
318 mentioned_user = insert(:user)
319 third_user = insert(:user)
320 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
322 draft = %ActivityDraft{
324 mentions: [mentioned_user.ap_id],
325 visibility: "public",
326 in_reply_to: activity
329 {to, cc} = Utils.get_to_and_cc(draft)
331 assert length(to) == 3
332 assert length(cc) == 1
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
340 test "for unlisted posts, not a reply" do
342 mentioned_user = insert(:user)
343 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "unlisted"}
345 {to, cc} = Utils.get_to_and_cc(draft)
347 assert length(to) == 2
348 assert length(cc) == 1
350 assert @public_address in cc
351 assert mentioned_user.ap_id in to
352 assert user.follower_address in to
355 test "for unlisted posts, a reply" do
357 mentioned_user = insert(:user)
358 third_user = insert(:user)
359 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
361 draft = %ActivityDraft{
363 mentions: [mentioned_user.ap_id],
364 visibility: "unlisted",
365 in_reply_to: activity
368 {to, cc} = Utils.get_to_and_cc(draft)
370 assert length(to) == 3
371 assert length(cc) == 1
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
379 test "for private posts, not a reply" do
381 mentioned_user = insert(:user)
382 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "private"}
384 {to, cc} = Utils.get_to_and_cc(draft)
385 assert length(to) == 2
386 assert Enum.empty?(cc)
388 assert mentioned_user.ap_id in to
389 assert user.follower_address in to
392 test "for private posts, a reply" do
394 mentioned_user = insert(:user)
395 third_user = insert(:user)
396 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
398 draft = %ActivityDraft{
400 mentions: [mentioned_user.ap_id],
401 visibility: "private",
402 in_reply_to: activity
405 {to, cc} = Utils.get_to_and_cc(draft)
407 assert length(to) == 2
408 assert Enum.empty?(cc)
410 assert mentioned_user.ap_id in to
411 assert user.follower_address in to
414 test "for direct posts, not a reply" do
416 mentioned_user = insert(:user)
417 draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "direct"}
419 {to, cc} = Utils.get_to_and_cc(draft)
421 assert length(to) == 1
422 assert Enum.empty?(cc)
424 assert mentioned_user.ap_id in to
427 test "for direct posts, a reply" do
429 mentioned_user = insert(:user)
430 third_user = insert(:user)
431 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
433 draft = %ActivityDraft{
435 mentions: [mentioned_user.ap_id],
436 visibility: "direct",
437 in_reply_to: activity
440 {to, cc} = Utils.get_to_and_cc(draft)
442 assert length(to) == 1
443 assert Enum.empty?(cc)
445 assert mentioned_user.ap_id in to
447 {:ok, direct_activity} = CommonAPI.post(third_user, %{status: "uguu", visibility: "direct"})
449 draft = %ActivityDraft{
451 mentions: [mentioned_user.ap_id],
452 visibility: "direct",
453 in_reply_to: direct_activity
456 {to, cc} = Utils.get_to_and_cc(draft)
458 assert length(to) == 2
459 assert Enum.empty?(cc)
461 assert mentioned_user.ap_id in to
462 assert third_user.ap_id in to
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"
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"
475 test "returns empty string when date invalid" do
476 assert Utils.to_masto_date("2015-01?23T23:50:07.123Z") == ""
480 describe "conversation_id_to_context/1" do
482 object = insert(:note)
483 assert Utils.conversation_id_to_context(object.id) == object.data["id"]
486 test "returns error if object not found" do
487 assert Utils.conversation_id_to_context("123") == {:error, "No such conversation"}
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"]
497 test "returns recipients from tag" do
505 %{"type" => "Hashtag"},
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"}
514 activity = insert(:note_activity, user: user, note: object)
516 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
518 "https://testing.pleroma.lol/users/lain",
519 "https://shitposter.club/user/5381"
523 test "returns recipients when object is map" do
525 object = insert(:note, user: user)
528 insert(:note_activity,
534 %{"type" => "Hashtag"},
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"}
544 Pleroma.Repo.delete(object)
546 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
548 "https://testing.pleroma.lol/users/lain",
549 "https://shitposter.club/user/5381"
553 test "returns recipients when object not found" do
555 object = insert(:note, user: user)
557 activity = insert(:note_activity, user: user, note: object)
558 Pleroma.Repo.delete(object)
560 obj_url = activity.data["object"]
563 %{method: :get, url: ^obj_url} ->
564 %Tesla.Env{status: 404, body: ""}
567 assert Utils.maybe_notify_mentioned_recipients(["test-test"], activity) == [
573 describe "attachments_from_ids_descs/2" do
574 test "returns [] when attachment ids is empty" do
575 assert Utils.attachments_from_ids_descs([], "{}") == []
578 test "returns list attachments with desc" do
579 object = insert(:note)
580 desc = Jason.encode!(%{object.id => "test-desc"})
582 assert Utils.attachments_from_ids_descs(["#{object.id}", "34"], desc) == [
583 Map.merge(object.data, %{"name" => "test-desc"})
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"})
593 assert Utils.attachments_from_ids(%{
594 media_ids: ["#{object.id}"],
597 Map.merge(object.data, %{"name" => "test-desc"})
601 test "returns attachments without descs" do
602 object = insert(:note)
603 assert Utils.attachments_from_ids(%{media_ids: ["#{object.id}"]}) == [object.data]
606 test "returns [] when not pass media_ids" do
607 assert Utils.attachments_from_ids(%{}) == []
611 describe "maybe_add_list_data/3" do
612 test "adds list params when found user list" do
614 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
616 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
618 additional: %{"bcc" => [list.ap_id], "listMessage" => list.ap_id},
619 object: %{"listMessage" => list.ap_id}
623 test "returns original params when list not found" do
625 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", insert(:user))
627 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
628 %{additional: %{}, object: %{}}
632 describe "make_note_data/1" do
633 test "returns note data" do
636 user2 = insert(:user)
637 user3 = insert(:user)
639 draft = %ActivityDraft{
643 content_html: "<h1>This is :moominmamma: note</h1>",
644 in_reply_to: note.id,
645 tags: [name: "jimm"],
646 summary: "test summary",
648 extra: %{"custom_tag" => "test"}
651 assert Utils.make_note_data(draft) == %{
652 "actor" => user.ap_id,
654 "cc" => [user3.ap_id],
655 "content" => "<h1>This is :moominmamma: note</h1>",
657 "sensitive" => false,
658 "summary" => "test summary",
660 "to" => [user2.ap_id],
662 "custom_tag" => "test"
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"]},
673 ) == {"test", [], ["tags"]}
676 test "adds attachments to parsed results" do
677 attachment = %{"url" => [%{"href" => "SakuraPM.png"}]}
679 assert Utils.maybe_add_attachments(
680 {"test", [], ["tags"]},
684 "test<br><a href=\"SakuraPM.png\" class='attachment'>SakuraPM.png</a>",