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
10 alias Pleroma.Web.Endpoint
13 import ExUnit.CaptureLog
14 import Pleroma.Factory
16 @public_address "https://www.w3.org/ns/activitystreams#Public"
18 test "it adds attachment links to a given text and attachment set" do
20 "Sakura%20Mana%20%E2%80%93%20Turned%20on%20by%20a%20Senior%20OL%20with%20a%20Temptating%20Tight%20Skirt-s%20Full%20Hipline%20and%20Panty%20Shot-%20Beautiful%20Thick%20Thighs-%20and%20Erotic%20Ass-%20-2015-%20--%20Oppaitime%208-28-2017%206-50-33%20PM.png"
23 "url" => [%{"href" => name}]
26 res = Utils.add_attachments("", [attachment])
29 "<br><a href=\"#{name}\" class='attachment'>Sakura Mana – Turned on by a Se…</a>"
32 describe "it confirms the password given is the current users password" do
33 test "incorrect password given" do
34 {:ok, user} = UserBuilder.insert()
36 assert Utils.confirm_current_password(user, "") == {:error, "Invalid password."}
39 test "correct password given" do
40 {:ok, user} = UserBuilder.insert()
41 assert Utils.confirm_current_password(user, "test") == {:ok, user}
45 test "parses emoji from name and bio" do
46 {:ok, user} = UserBuilder.insert(%{name: ":blank:", bio: ":firefox:"})
51 "icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}/emoji/Firefox.gif"},
58 "url" => "#{Endpoint.url()}/emoji/blank.png"
64 assert expected == Utils.emoji_from_profile(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>\n\n<p>second paragraph</p>"
93 expected = "<p>hello world!</p>\n\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>\n"
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>\n<p><em>another paragraph</em></p>\n"
111 {output, [], []} = Utils.format_input(text, "text/markdown")
113 assert output == expected
121 expected = "<blockquote><p>cool quote</p>\n</blockquote>\n<p>by someone</p>\n"
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>\n<br>\nsecond 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>\n<br>\n<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*"
160 ~s(<p><strong>hello world</strong></p>\n<p><em>another <span class="h-card"><a data-user="#{
162 }" class="u-url mention" href="http://foo.com/user__test" rel="ugc">@<span>user__test</span></a></span> and <span class="h-card"><a data-user="#{
164 }" class="u-url mention" 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>\n)
166 {output, _, _} = Utils.format_input(text, "text/markdown")
168 assert output == expected
172 describe "context_to_conversation_id" do
173 test "creates a mapping object" do
174 conversation_id = Utils.context_to_conversation_id("random context")
175 object = Object.get_by_ap_id("random context")
177 assert conversation_id == object.id
180 test "returns an existing mapping for an existing object" do
181 {:ok, object} = Object.context_mapping("random context") |> Repo.insert()
182 conversation_id = Utils.context_to_conversation_id("random context")
184 assert conversation_id == object.id
188 describe "formats date to asctime" do
189 test "when date is in ISO 8601 format" do
190 date = DateTime.utc_now() |> DateTime.to_iso8601()
194 |> DateTime.from_iso8601()
196 |> Calendar.Strftime.strftime!("%a %b %d %H:%M:%S %z %Y")
198 assert Utils.date_to_asctime(date) == expected
201 test "when date is a binary in wrong format" do
202 date = DateTime.utc_now()
206 assert capture_log(fn ->
207 assert Utils.date_to_asctime(date) == expected
208 end) =~ "[warn] Date #{date} in wrong format, must be ISO 8601"
211 test "when date is a Unix timestamp" do
212 date = DateTime.utc_now() |> DateTime.to_unix()
216 assert capture_log(fn ->
217 assert Utils.date_to_asctime(date) == expected
218 end) =~ "[warn] Date #{date} in wrong format, must be ISO 8601"
221 test "when date is nil" do
224 assert capture_log(fn ->
225 assert Utils.date_to_asctime(nil) == expected
226 end) =~ "[warn] Date in wrong format, must be ISO 8601"
229 test "when date is a random string" do
230 assert capture_log(fn ->
231 assert Utils.date_to_asctime("foo") == ""
232 end) =~ "[warn] Date foo in wrong format, must be ISO 8601"
236 describe "get_to_and_cc" do
237 test "for public posts, not a reply" do
239 mentioned_user = insert(:user)
240 mentions = [mentioned_user.ap_id]
242 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "public", nil)
244 assert length(to) == 2
245 assert length(cc) == 1
247 assert @public_address in to
248 assert mentioned_user.ap_id in to
249 assert user.follower_address in cc
252 test "for public posts, a reply" do
254 mentioned_user = insert(:user)
255 third_user = insert(:user)
256 {:ok, activity} = CommonAPI.post(third_user, %{"status" => "uguu"})
257 mentions = [mentioned_user.ap_id]
259 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "public", nil)
261 assert length(to) == 3
262 assert length(cc) == 1
264 assert @public_address in to
265 assert mentioned_user.ap_id in to
266 assert third_user.ap_id in to
267 assert user.follower_address in cc
270 test "for unlisted posts, not a reply" do
272 mentioned_user = insert(:user)
273 mentions = [mentioned_user.ap_id]
275 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "unlisted", nil)
277 assert length(to) == 2
278 assert length(cc) == 1
280 assert @public_address in cc
281 assert mentioned_user.ap_id in to
282 assert user.follower_address in to
285 test "for unlisted posts, a reply" do
287 mentioned_user = insert(:user)
288 third_user = insert(:user)
289 {:ok, activity} = CommonAPI.post(third_user, %{"status" => "uguu"})
290 mentions = [mentioned_user.ap_id]
292 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "unlisted", nil)
294 assert length(to) == 3
295 assert length(cc) == 1
297 assert @public_address in cc
298 assert mentioned_user.ap_id in to
299 assert third_user.ap_id in to
300 assert user.follower_address in to
303 test "for private posts, not a reply" do
305 mentioned_user = insert(:user)
306 mentions = [mentioned_user.ap_id]
308 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "private", nil)
309 assert length(to) == 2
310 assert Enum.empty?(cc)
312 assert mentioned_user.ap_id in to
313 assert user.follower_address in to
316 test "for private posts, a reply" do
318 mentioned_user = insert(:user)
319 third_user = insert(:user)
320 {:ok, activity} = CommonAPI.post(third_user, %{"status" => "uguu"})
321 mentions = [mentioned_user.ap_id]
323 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "private", nil)
325 assert length(to) == 3
326 assert Enum.empty?(cc)
328 assert mentioned_user.ap_id in to
329 assert third_user.ap_id in to
330 assert user.follower_address in to
333 test "for direct posts, not a reply" do
335 mentioned_user = insert(:user)
336 mentions = [mentioned_user.ap_id]
338 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "direct", nil)
340 assert length(to) == 1
341 assert Enum.empty?(cc)
343 assert mentioned_user.ap_id in to
346 test "for direct posts, a reply" do
348 mentioned_user = insert(:user)
349 third_user = insert(:user)
350 {:ok, activity} = CommonAPI.post(third_user, %{"status" => "uguu"})
351 mentions = [mentioned_user.ap_id]
353 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "direct", nil)
355 assert length(to) == 2
356 assert Enum.empty?(cc)
358 assert mentioned_user.ap_id in to
359 assert third_user.ap_id in to
363 describe "get_by_id_or_ap_id/1" do
364 test "get activity by id" do
365 activity = insert(:note_activity)
366 %Pleroma.Activity{} = note = Utils.get_by_id_or_ap_id(activity.id)
367 assert note.id == activity.id
370 test "get activity by ap_id" do
371 activity = insert(:note_activity)
372 %Pleroma.Activity{} = note = Utils.get_by_id_or_ap_id(activity.data["object"])
373 assert note.id == activity.id
376 test "get activity by object when type isn't `Create` " do
377 activity = insert(:like_activity)
378 %Pleroma.Activity{} = like = Utils.get_by_id_or_ap_id(activity.id)
379 assert like.data["object"] == activity.data["object"]
383 describe "to_master_date/1" do
384 test "removes microseconds from date (NaiveDateTime)" do
385 assert Utils.to_masto_date(~N[2015-01-23 23:50:07.123]) == "2015-01-23T23:50:07.000Z"
388 test "removes microseconds from date (String)" do
389 assert Utils.to_masto_date("2015-01-23T23:50:07.123Z") == "2015-01-23T23:50:07.000Z"
392 test "returns empty string when date invalid" do
393 assert Utils.to_masto_date("2015-01?23T23:50:07.123Z") == ""
397 describe "conversation_id_to_context/1" do
399 object = insert(:note)
400 assert Utils.conversation_id_to_context(object.id) == object.data["id"]
403 test "returns error if object not found" do
404 assert Utils.conversation_id_to_context("123") == {:error, "No such conversation"}
408 describe "maybe_notify_mentioned_recipients/2" do
409 test "returns recipients when activity is not `Create`" do
410 activity = insert(:like_activity)
411 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == ["test"]
414 test "returns recipients from tag" do
422 %{"type" => "Hashtag"},
424 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
425 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
426 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
431 activity = insert(:note_activity, user: user, note: object)
433 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
435 "https://testing.pleroma.lol/users/lain",
436 "https://shitposter.club/user/5381"
440 test "returns recipients when object is map" do
442 object = insert(:note, user: user)
445 insert(:note_activity,
451 %{"type" => "Hashtag"},
453 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
454 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
455 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
461 Pleroma.Repo.delete(object)
463 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
465 "https://testing.pleroma.lol/users/lain",
466 "https://shitposter.club/user/5381"
470 test "returns recipients when object not found" do
472 object = insert(:note, user: user)
474 activity = insert(:note_activity, user: user, note: object)
475 Pleroma.Repo.delete(object)
477 assert Utils.maybe_notify_mentioned_recipients(["test-test"], activity) == [
483 describe "attachments_from_ids_descs/2" do
484 test "returns [] when attachment ids is empty" do
485 assert Utils.attachments_from_ids_descs([], "{}") == []
488 test "returns list attachments with desc" do
489 object = insert(:note)
490 desc = Jason.encode!(%{object.id => "test-desc"})
492 assert Utils.attachments_from_ids_descs(["#{object.id}", "34"], desc) == [
493 Map.merge(object.data, %{"name" => "test-desc"})
498 describe "attachments_from_ids/1" do
499 test "returns attachments with descs" do
500 object = insert(:note)
501 desc = Jason.encode!(%{object.id => "test-desc"})
503 assert Utils.attachments_from_ids(%{
504 "media_ids" => ["#{object.id}"],
505 "descriptions" => desc
507 Map.merge(object.data, %{"name" => "test-desc"})
511 test "returns attachments without descs" do
512 object = insert(:note)
513 assert Utils.attachments_from_ids(%{"media_ids" => ["#{object.id}"]}) == [object.data]
516 test "returns [] when not pass media_ids" do
517 assert Utils.attachments_from_ids(%{}) == []
521 describe "maybe_add_list_data/3" do
522 test "adds list params when found user list" do
524 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
526 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
528 additional: %{"bcc" => [list.ap_id], "listMessage" => list.ap_id},
529 object: %{"listMessage" => list.ap_id}
533 test "returns original params when list not found" do
535 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", insert(:user))
537 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
538 %{additional: %{}, object: %{}}
542 describe "make_note_data/11" do
543 test "returns note data" do
546 user2 = insert(:user)
547 user3 = insert(:user)
549 assert Utils.make_note_data(
553 "<h1>This is :moominmamma: note</h1>",
560 %{"custom_tag" => "test"}
562 "actor" => user.ap_id,
564 "cc" => [user3.ap_id],
565 "content" => "<h1>This is :moominmamma: note</h1>",
567 "sensitive" => false,
568 "summary" => "test summary",
570 "to" => [user2.ap_id],
572 "custom_tag" => "test"
577 describe "maybe_add_attachments/3" do
578 test "returns parsed results when attachment_links is false" do
579 assert Utils.maybe_add_attachments(
580 {"test", [], ["tags"]},
583 ) == {"test", [], ["tags"]}
586 test "adds attachments to parsed results" do
587 attachment = %{"url" => [%{"href" => "SakuraPM.png"}]}
589 assert Utils.maybe_add_attachments(
590 {"test", [], ["tags"]},
594 "test<br><a href=\"SakuraPM.png\" class='attachment'>SakuraPM.png</a>",