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 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) == 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 "get_by_id_or_ap_id/1" do
362 test "get activity by id" do
363 activity = insert(:note_activity)
364 %Pleroma.Activity{} = note = Utils.get_by_id_or_ap_id(activity.id)
365 assert note.id == activity.id
368 test "get activity by ap_id" do
369 activity = insert(:note_activity)
370 %Pleroma.Activity{} = note = Utils.get_by_id_or_ap_id(activity.data["object"])
371 assert note.id == activity.id
374 test "get activity by object when type isn't `Create` " do
375 activity = insert(:like_activity)
376 %Pleroma.Activity{} = like = Utils.get_by_id_or_ap_id(activity.id)
377 assert like.data["object"] == activity.data["object"]
381 describe "to_master_date/1" do
382 test "removes microseconds from date (NaiveDateTime)" do
383 assert Utils.to_masto_date(~N[2015-01-23 23:50:07.123]) == "2015-01-23T23:50:07.000Z"
386 test "removes microseconds from date (String)" do
387 assert Utils.to_masto_date("2015-01-23T23:50:07.123Z") == "2015-01-23T23:50:07.000Z"
390 test "returns empty string when date invalid" do
391 assert Utils.to_masto_date("2015-01?23T23:50:07.123Z") == ""
395 describe "conversation_id_to_context/1" do
397 object = insert(:note)
398 assert Utils.conversation_id_to_context(object.id) == object.data["id"]
401 test "returns error if object not found" do
402 assert Utils.conversation_id_to_context("123") == {:error, "No such conversation"}
406 describe "maybe_notify_mentioned_recipients/2" do
407 test "returns recipients when activity is not `Create`" do
408 activity = insert(:like_activity)
409 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == ["test"]
412 test "returns recipients from tag" do
420 %{"type" => "Hashtag"},
422 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
423 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
424 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
429 activity = insert(:note_activity, user: user, note: object)
431 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
433 "https://testing.pleroma.lol/users/lain",
434 "https://shitposter.club/user/5381"
438 test "returns recipients when object is map" do
440 object = insert(:note, user: user)
443 insert(:note_activity,
449 %{"type" => "Hashtag"},
451 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
452 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
453 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
459 Pleroma.Repo.delete(object)
461 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
463 "https://testing.pleroma.lol/users/lain",
464 "https://shitposter.club/user/5381"
468 test "returns recipients when object not found" do
470 object = insert(:note, user: user)
472 activity = insert(:note_activity, user: user, note: object)
473 Pleroma.Repo.delete(object)
475 obj_url = activity.data["object"]
478 %{method: :get, url: ^obj_url} ->
479 %Tesla.Env{status: 404, body: ""}
482 assert Utils.maybe_notify_mentioned_recipients(["test-test"], activity) == [
488 describe "attachments_from_ids_descs/2" do
489 test "returns [] when attachment ids is empty" do
490 assert Utils.attachments_from_ids_descs([], "{}") == []
493 test "returns list attachments with desc" do
494 object = insert(:note)
495 desc = Jason.encode!(%{object.id => "test-desc"})
497 assert Utils.attachments_from_ids_descs(["#{object.id}", "34"], desc) == [
498 Map.merge(object.data, %{"name" => "test-desc"})
503 describe "attachments_from_ids/1" do
504 test "returns attachments with descs" do
505 object = insert(:note)
506 desc = Jason.encode!(%{object.id => "test-desc"})
508 assert Utils.attachments_from_ids(%{
509 "media_ids" => ["#{object.id}"],
510 "descriptions" => desc
512 Map.merge(object.data, %{"name" => "test-desc"})
516 test "returns attachments without descs" do
517 object = insert(:note)
518 assert Utils.attachments_from_ids(%{"media_ids" => ["#{object.id}"]}) == [object.data]
521 test "returns [] when not pass media_ids" do
522 assert Utils.attachments_from_ids(%{}) == []
526 describe "maybe_add_list_data/3" do
527 test "adds list params when found user list" do
529 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
531 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
533 additional: %{"bcc" => [list.ap_id], "listMessage" => list.ap_id},
534 object: %{"listMessage" => list.ap_id}
538 test "returns original params when list not found" do
540 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", insert(:user))
542 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
543 %{additional: %{}, object: %{}}
547 describe "make_note_data/11" do
548 test "returns note data" do
551 user2 = insert(:user)
552 user3 = insert(:user)
554 assert Utils.make_note_data(
558 "<h1>This is :moominmamma: note</h1>",
565 %{"custom_tag" => "test"}
567 "actor" => user.ap_id,
569 "cc" => [user3.ap_id],
570 "content" => "<h1>This is :moominmamma: note</h1>",
572 "sensitive" => false,
573 "summary" => "test summary",
575 "to" => [user2.ap_id],
577 "custom_tag" => "test"
582 describe "maybe_add_attachments/3" do
583 test "returns parsed results when attachment_links is false" do
584 assert Utils.maybe_add_attachments(
585 {"test", [], ["tags"]},
588 ) == {"test", [], ["tags"]}
591 test "adds attachments to parsed results" do
592 attachment = %{"url" => [%{"href" => "SakuraPM.png"}]}
594 assert Utils.maybe_add_attachments(
595 {"test", [], ["tags"]},
599 "test<br><a href=\"SakuraPM.png\" class='attachment'>SakuraPM.png</a>",