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><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 data-user="#{
164 }" 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="#{
166 }" 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>)
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) == 3
324 assert Enum.empty?(cc)
326 assert mentioned_user.ap_id in to
327 assert third_user.ap_id in to
328 assert user.follower_address in to
331 test "for direct posts, not a reply" do
333 mentioned_user = insert(:user)
334 mentions = [mentioned_user.ap_id]
336 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "direct", nil)
338 assert length(to) == 1
339 assert Enum.empty?(cc)
341 assert mentioned_user.ap_id in to
344 test "for direct posts, a reply" do
346 mentioned_user = insert(:user)
347 third_user = insert(:user)
348 {:ok, activity} = CommonAPI.post(third_user, %{"status" => "uguu"})
349 mentions = [mentioned_user.ap_id]
351 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "direct", nil)
353 assert length(to) == 2
354 assert Enum.empty?(cc)
356 assert mentioned_user.ap_id in to
357 assert third_user.ap_id in to
361 describe "to_master_date/1" do
362 test "removes microseconds from date (NaiveDateTime)" do
363 assert Utils.to_masto_date(~N[2015-01-23 23:50:07.123]) == "2015-01-23T23:50:07.000Z"
366 test "removes microseconds from date (String)" do
367 assert Utils.to_masto_date("2015-01-23T23:50:07.123Z") == "2015-01-23T23:50:07.000Z"
370 test "returns empty string when date invalid" do
371 assert Utils.to_masto_date("2015-01?23T23:50:07.123Z") == ""
375 describe "conversation_id_to_context/1" do
377 object = insert(:note)
378 assert Utils.conversation_id_to_context(object.id) == object.data["id"]
381 test "returns error if object not found" do
382 assert Utils.conversation_id_to_context("123") == {:error, "No such conversation"}
386 describe "maybe_notify_mentioned_recipients/2" do
387 test "returns recipients when activity is not `Create`" do
388 activity = insert(:like_activity)
389 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == ["test"]
392 test "returns recipients from tag" do
400 %{"type" => "Hashtag"},
402 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
403 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
404 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
409 activity = insert(:note_activity, user: user, note: object)
411 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
413 "https://testing.pleroma.lol/users/lain",
414 "https://shitposter.club/user/5381"
418 test "returns recipients when object is map" do
420 object = insert(:note, user: user)
423 insert(:note_activity,
429 %{"type" => "Hashtag"},
431 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
432 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
433 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
439 Pleroma.Repo.delete(object)
441 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
443 "https://testing.pleroma.lol/users/lain",
444 "https://shitposter.club/user/5381"
448 test "returns recipients when object not found" do
450 object = insert(:note, user: user)
452 activity = insert(:note_activity, user: user, note: object)
453 Pleroma.Repo.delete(object)
455 assert Utils.maybe_notify_mentioned_recipients(["test-test"], activity) == [
461 describe "attachments_from_ids_descs/2" do
462 test "returns [] when attachment ids is empty" do
463 assert Utils.attachments_from_ids_descs([], "{}") == []
466 test "returns list attachments with desc" do
467 object = insert(:note)
468 desc = Jason.encode!(%{object.id => "test-desc"})
470 assert Utils.attachments_from_ids_descs(["#{object.id}", "34"], desc) == [
471 Map.merge(object.data, %{"name" => "test-desc"})
476 describe "attachments_from_ids/1" do
477 test "returns attachments with descs" do
478 object = insert(:note)
479 desc = Jason.encode!(%{object.id => "test-desc"})
481 assert Utils.attachments_from_ids(%{
482 "media_ids" => ["#{object.id}"],
483 "descriptions" => desc
485 Map.merge(object.data, %{"name" => "test-desc"})
489 test "returns attachments without descs" do
490 object = insert(:note)
491 assert Utils.attachments_from_ids(%{"media_ids" => ["#{object.id}"]}) == [object.data]
494 test "returns [] when not pass media_ids" do
495 assert Utils.attachments_from_ids(%{}) == []
499 describe "maybe_add_list_data/3" do
500 test "adds list params when found user list" do
502 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
504 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
506 additional: %{"bcc" => [list.ap_id], "listMessage" => list.ap_id},
507 object: %{"listMessage" => list.ap_id}
511 test "returns original params when list not found" do
513 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", insert(:user))
515 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
516 %{additional: %{}, object: %{}}
520 describe "make_note_data/11" do
521 test "returns note data" do
524 user2 = insert(:user)
525 user3 = insert(:user)
527 assert Utils.make_note_data(
531 "<h1>This is :moominmamma: note</h1>",
538 %{"custom_tag" => "test"}
540 "actor" => user.ap_id,
542 "cc" => [user3.ap_id],
543 "content" => "<h1>This is :moominmamma: note</h1>",
545 "sensitive" => false,
546 "summary" => "test summary",
548 "to" => [user2.ap_id],
550 "custom_tag" => "test"
555 describe "maybe_add_attachments/3" do
556 test "returns parsed results when attachment_links is false" do
557 assert Utils.maybe_add_attachments(
558 {"test", [], ["tags"]},
561 ) == {"test", [], ["tags"]}
564 test "adds attachments to parsed results" do
565 attachment = %{"url" => [%{"href" => "SakuraPM.png"}]}
567 assert Utils.maybe_add_attachments(
568 {"test", [], ["tags"]},
572 "test<br><a href=\"SakuraPM.png\" class='attachment'>SakuraPM.png</a>",