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.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="#{
164 }" 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="#{
166 }" 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>)
170 describe "context_to_conversation_id" do
171 test "creates a mapping object" do
172 conversation_id = Utils.context_to_conversation_id("random context")
173 object = Object.get_by_ap_id("random context")
175 assert conversation_id == object.id
178 test "returns an existing mapping for an existing object" do
179 {:ok, object} = Object.context_mapping("random context") |> Repo.insert()
180 conversation_id = Utils.context_to_conversation_id("random context")
182 assert conversation_id == object.id
186 describe "formats date to asctime" do
187 test "when date is in ISO 8601 format" do
188 date = DateTime.utc_now() |> DateTime.to_iso8601()
192 |> DateTime.from_iso8601()
194 |> Calendar.Strftime.strftime!("%a %b %d %H:%M:%S %z %Y")
196 assert Utils.date_to_asctime(date) == expected
199 test "when date is a binary in wrong format" do
200 date = DateTime.utc_now()
204 assert capture_log(fn ->
205 assert Utils.date_to_asctime(date) == expected
206 end) =~ "[warn] Date #{date} in wrong format, must be ISO 8601"
209 test "when date is a Unix timestamp" do
210 date = DateTime.utc_now() |> DateTime.to_unix()
214 assert capture_log(fn ->
215 assert Utils.date_to_asctime(date) == expected
216 end) =~ "[warn] Date #{date} in wrong format, must be ISO 8601"
219 test "when date is nil" do
222 assert capture_log(fn ->
223 assert Utils.date_to_asctime(nil) == expected
224 end) =~ "[warn] Date in wrong format, must be ISO 8601"
227 test "when date is a random string" do
228 assert capture_log(fn ->
229 assert Utils.date_to_asctime("foo") == ""
230 end) =~ "[warn] Date foo in wrong format, must be ISO 8601"
234 describe "get_to_and_cc" do
235 test "for public posts, not a reply" do
237 mentioned_user = insert(:user)
238 mentions = [mentioned_user.ap_id]
240 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "public", nil)
242 assert length(to) == 2
243 assert length(cc) == 1
245 assert @public_address in to
246 assert mentioned_user.ap_id in to
247 assert user.follower_address in cc
250 test "for public posts, a reply" do
252 mentioned_user = insert(:user)
253 third_user = insert(:user)
254 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
255 mentions = [mentioned_user.ap_id]
257 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "public", nil)
259 assert length(to) == 3
260 assert length(cc) == 1
262 assert @public_address in to
263 assert mentioned_user.ap_id in to
264 assert third_user.ap_id in to
265 assert user.follower_address in cc
268 test "for unlisted posts, not a reply" do
270 mentioned_user = insert(:user)
271 mentions = [mentioned_user.ap_id]
273 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "unlisted", nil)
275 assert length(to) == 2
276 assert length(cc) == 1
278 assert @public_address in cc
279 assert mentioned_user.ap_id in to
280 assert user.follower_address in to
283 test "for unlisted posts, a reply" do
285 mentioned_user = insert(:user)
286 third_user = insert(:user)
287 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
288 mentions = [mentioned_user.ap_id]
290 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "unlisted", nil)
292 assert length(to) == 3
293 assert length(cc) == 1
295 assert @public_address in cc
296 assert mentioned_user.ap_id in to
297 assert third_user.ap_id in to
298 assert user.follower_address in to
301 test "for private posts, not a reply" do
303 mentioned_user = insert(:user)
304 mentions = [mentioned_user.ap_id]
306 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "private", nil)
307 assert length(to) == 2
308 assert Enum.empty?(cc)
310 assert mentioned_user.ap_id in to
311 assert user.follower_address in to
314 test "for private posts, a reply" do
316 mentioned_user = insert(:user)
317 third_user = insert(:user)
318 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
319 mentions = [mentioned_user.ap_id]
321 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "private", nil)
323 assert length(to) == 2
324 assert Enum.empty?(cc)
326 assert mentioned_user.ap_id in to
327 assert user.follower_address in to
330 test "for direct posts, not a reply" do
332 mentioned_user = insert(:user)
333 mentions = [mentioned_user.ap_id]
335 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "direct", nil)
337 assert length(to) == 1
338 assert Enum.empty?(cc)
340 assert mentioned_user.ap_id in to
343 test "for direct posts, a reply" do
345 mentioned_user = insert(:user)
346 third_user = insert(:user)
347 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
348 mentions = [mentioned_user.ap_id]
350 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "direct", nil)
352 assert length(to) == 1
353 assert Enum.empty?(cc)
355 assert mentioned_user.ap_id in to
357 {:ok, direct_activity} = CommonAPI.post(third_user, %{status: "uguu", visibility: "direct"})
359 {to, cc} = Utils.get_to_and_cc(user, mentions, direct_activity, "direct", nil)
361 assert length(to) == 2
362 assert Enum.empty?(cc)
364 assert mentioned_user.ap_id in to
365 assert third_user.ap_id in to
369 describe "to_master_date/1" do
370 test "removes microseconds from date (NaiveDateTime)" do
371 assert Utils.to_masto_date(~N[2015-01-23 23:50:07.123]) == "2015-01-23T23:50:07.000Z"
374 test "removes microseconds from date (String)" do
375 assert Utils.to_masto_date("2015-01-23T23:50:07.123Z") == "2015-01-23T23:50:07.000Z"
378 test "returns empty string when date invalid" do
379 assert Utils.to_masto_date("2015-01?23T23:50:07.123Z") == ""
383 describe "conversation_id_to_context/1" do
385 object = insert(:note)
386 assert Utils.conversation_id_to_context(object.id) == object.data["id"]
389 test "returns error if object not found" do
390 assert Utils.conversation_id_to_context("123") == {:error, "No such conversation"}
394 describe "maybe_notify_mentioned_recipients/2" do
395 test "returns recipients when activity is not `Create`" do
396 activity = insert(:like_activity)
397 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == ["test"]
400 test "returns recipients from tag" do
408 %{"type" => "Hashtag"},
410 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
411 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
412 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
417 activity = insert(:note_activity, user: user, note: object)
419 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
421 "https://testing.pleroma.lol/users/lain",
422 "https://shitposter.club/user/5381"
426 test "returns recipients when object is map" do
428 object = insert(:note, user: user)
431 insert(:note_activity,
437 %{"type" => "Hashtag"},
439 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
440 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
441 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
447 Pleroma.Repo.delete(object)
449 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
451 "https://testing.pleroma.lol/users/lain",
452 "https://shitposter.club/user/5381"
456 test "returns recipients when object not found" do
458 object = insert(:note, user: user)
460 activity = insert(:note_activity, user: user, note: object)
461 Pleroma.Repo.delete(object)
463 obj_url = activity.data["object"]
466 %{method: :get, url: ^obj_url} ->
467 %Tesla.Env{status: 404, body: ""}
470 assert Utils.maybe_notify_mentioned_recipients(["test-test"], activity) == [
476 describe "attachments_from_ids_descs/2" do
477 test "returns [] when attachment ids is empty" do
478 assert Utils.attachments_from_ids_descs([], "{}") == []
481 test "returns list attachments with desc" do
482 object = insert(:note)
483 desc = Jason.encode!(%{object.id => "test-desc"})
485 assert Utils.attachments_from_ids_descs(["#{object.id}", "34"], desc) == [
486 Map.merge(object.data, %{"name" => "test-desc"})
491 describe "attachments_from_ids/1" do
492 test "returns attachments with descs" do
493 object = insert(:note)
494 desc = Jason.encode!(%{object.id => "test-desc"})
496 assert Utils.attachments_from_ids(%{
497 media_ids: ["#{object.id}"],
500 Map.merge(object.data, %{"name" => "test-desc"})
504 test "returns attachments without descs" do
505 object = insert(:note)
506 assert Utils.attachments_from_ids(%{media_ids: ["#{object.id}"]}) == [object.data]
509 test "returns [] when not pass media_ids" do
510 assert Utils.attachments_from_ids(%{}) == []
514 describe "maybe_add_list_data/3" do
515 test "adds list params when found user list" do
517 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
519 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
521 additional: %{"bcc" => [list.ap_id], "listMessage" => list.ap_id},
522 object: %{"listMessage" => list.ap_id}
526 test "returns original params when list not found" do
528 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", insert(:user))
530 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
531 %{additional: %{}, object: %{}}
535 describe "make_note_data/11" do
536 test "returns note data" do
539 user2 = insert(:user)
540 user3 = insert(:user)
542 assert Utils.make_note_data(
546 "<h1>This is :moominmamma: note</h1>",
553 %{"custom_tag" => "test"}
555 "actor" => user.ap_id,
557 "cc" => [user3.ap_id],
558 "content" => "<h1>This is :moominmamma: note</h1>",
560 "sensitive" => false,
561 "summary" => "test summary",
563 "to" => [user2.ap_id],
565 "custom_tag" => "test"
570 describe "maybe_add_attachments/3" do
571 test "returns parsed results when attachment_links is false" do
572 assert Utils.maybe_add_attachments(
573 {"test", [], ["tags"]},
576 ) == {"test", [], ["tags"]}
579 test "adds attachments to parsed results" do
580 attachment = %{"url" => [%{"href" => "SakuraPM.png"}]}
582 assert Utils.maybe_add_attachments(
583 {"test", [], ["tags"]},
587 "test<br><a href=\"SakuraPM.png\" class='attachment'>SakuraPM.png</a>",