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 test "it adds attachment links to a given text and attachment set" do
19 "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"
22 "url" => [%{"href" => name}]
25 res = Utils.add_attachments("", [attachment])
28 "<br><a href=\"#{name}\" class='attachment'>Sakura Mana – Turned on by a Se…</a>"
31 describe "it confirms the password given is the current users password" do
32 test "incorrect password given" do
33 {:ok, user} = UserBuilder.insert()
35 assert Utils.confirm_current_password(user, "") == {:error, "Invalid password."}
38 test "correct password given" do
39 {:ok, user} = UserBuilder.insert()
40 assert Utils.confirm_current_password(user, "test") == {:ok, user}
44 describe "format_input/3" do
45 test "works for bare text/plain" do
47 expected = "hello world!"
49 {output, [], []} = Utils.format_input(text, "text/plain")
51 assert output == expected
53 text = "hello world!\n\nsecond paragraph!"
54 expected = "hello world!<br><br>second paragraph!"
56 {output, [], []} = Utils.format_input(text, "text/plain")
58 assert output == expected
61 test "works for bare text/html" do
62 text = "<p>hello world!</p>"
63 expected = "<p>hello world!</p>"
65 {output, [], []} = Utils.format_input(text, "text/html")
67 assert output == expected
69 text = "<p>hello world!</p><br/>\n<p>second paragraph</p>"
70 expected = "<p>hello world!</p><br/>\n<p>second paragraph</p>"
72 {output, [], []} = Utils.format_input(text, "text/html")
74 assert output == expected
77 test "works for bare text/markdown" do
78 text = "**hello world**"
79 expected = "<p><strong>hello world</strong></p>"
81 {output, [], []} = Utils.format_input(text, "text/markdown")
83 assert output == expected
85 text = "**hello world**\n\n*another paragraph*"
86 expected = "<p><strong>hello world</strong></p><p><em>another paragraph</em></p>"
88 {output, [], []} = Utils.format_input(text, "text/markdown")
90 assert output == expected
98 expected = "<blockquote><p>cool quote</p></blockquote><p>by someone</p>"
100 {output, [], []} = Utils.format_input(text, "text/markdown")
102 assert output == expected
105 test "works for bare text/bbcode" do
106 text = "[b]hello world[/b]"
107 expected = "<strong>hello world</strong>"
109 {output, [], []} = Utils.format_input(text, "text/bbcode")
111 assert output == expected
113 text = "[b]hello world![/b]\n\nsecond paragraph!"
114 expected = "<strong>hello world!</strong><br><br>second paragraph!"
116 {output, [], []} = Utils.format_input(text, "text/bbcode")
118 assert output == expected
120 text = "[b]hello world![/b]\n\n<strong>second paragraph!</strong>"
123 "<strong>hello world!</strong><br><br><strong>second paragraph!</strong>"
125 {output, [], []} = Utils.format_input(text, "text/bbcode")
127 assert output == expected
130 test "works for text/markdown with mentions" do
132 UserBuilder.insert(%{nickname: "user__test", ap_id: "http://foo.com/user__test"})
134 text = "**hello world**\n\n*another @user__test and @user__test google.com paragraph*"
136 {output, _, _} = Utils.format_input(text, "text/markdown")
139 ~s(<p><strong>hello world</strong></p><p><em>another <span class="h-card"><a class="u-url mention" data-user="#{
141 }" 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="#{
143 }" 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>)
147 describe "context_to_conversation_id" do
148 test "creates a mapping object" do
149 conversation_id = Utils.context_to_conversation_id("random context")
150 object = Object.get_by_ap_id("random context")
152 assert conversation_id == object.id
155 test "returns an existing mapping for an existing object" do
156 {:ok, object} = Object.context_mapping("random context") |> Repo.insert()
157 conversation_id = Utils.context_to_conversation_id("random context")
159 assert conversation_id == object.id
163 describe "formats date to asctime" do
164 test "when date is in ISO 8601 format" do
165 date = DateTime.utc_now() |> DateTime.to_iso8601()
169 |> DateTime.from_iso8601()
171 |> Calendar.Strftime.strftime!("%a %b %d %H:%M:%S %z %Y")
173 assert Utils.date_to_asctime(date) == expected
176 test "when date is a binary in wrong format" do
177 date = DateTime.utc_now()
181 assert capture_log(fn ->
182 assert Utils.date_to_asctime(date) == expected
183 end) =~ "[warn] Date #{date} in wrong format, must be ISO 8601"
186 test "when date is a Unix timestamp" do
187 date = DateTime.utc_now() |> DateTime.to_unix()
191 assert capture_log(fn ->
192 assert Utils.date_to_asctime(date) == expected
193 end) =~ "[warn] Date #{date} in wrong format, must be ISO 8601"
196 test "when date is nil" do
199 assert capture_log(fn ->
200 assert Utils.date_to_asctime(nil) == expected
201 end) =~ "[warn] Date in wrong format, must be ISO 8601"
204 test "when date is a random string" do
205 assert capture_log(fn ->
206 assert Utils.date_to_asctime("foo") == ""
207 end) =~ "[warn] Date foo in wrong format, must be ISO 8601"
211 describe "get_to_and_cc" do
212 test "for public posts, not a reply" do
214 mentioned_user = insert(:user)
215 mentions = [mentioned_user.ap_id]
217 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "public", nil)
219 assert length(to) == 2
220 assert length(cc) == 1
222 assert @public_address in to
223 assert mentioned_user.ap_id in to
224 assert user.follower_address in cc
227 test "for public posts, a reply" do
229 mentioned_user = insert(:user)
230 third_user = insert(:user)
231 {:ok, activity} = CommonAPI.post(third_user, %{"status" => "uguu"})
232 mentions = [mentioned_user.ap_id]
234 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "public", nil)
236 assert length(to) == 3
237 assert length(cc) == 1
239 assert @public_address in to
240 assert mentioned_user.ap_id in to
241 assert third_user.ap_id in to
242 assert user.follower_address in cc
245 test "for unlisted posts, not a reply" do
247 mentioned_user = insert(:user)
248 mentions = [mentioned_user.ap_id]
250 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "unlisted", nil)
252 assert length(to) == 2
253 assert length(cc) == 1
255 assert @public_address in cc
256 assert mentioned_user.ap_id in to
257 assert user.follower_address in to
260 test "for unlisted posts, a reply" do
262 mentioned_user = insert(:user)
263 third_user = insert(:user)
264 {:ok, activity} = CommonAPI.post(third_user, %{"status" => "uguu"})
265 mentions = [mentioned_user.ap_id]
267 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "unlisted", nil)
269 assert length(to) == 3
270 assert length(cc) == 1
272 assert @public_address in cc
273 assert mentioned_user.ap_id in to
274 assert third_user.ap_id in to
275 assert user.follower_address in to
278 test "for private posts, not a reply" do
280 mentioned_user = insert(:user)
281 mentions = [mentioned_user.ap_id]
283 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "private", nil)
284 assert length(to) == 2
285 assert Enum.empty?(cc)
287 assert mentioned_user.ap_id in to
288 assert user.follower_address in to
291 test "for private posts, a reply" do
293 mentioned_user = insert(:user)
294 third_user = insert(:user)
295 {:ok, activity} = CommonAPI.post(third_user, %{"status" => "uguu"})
296 mentions = [mentioned_user.ap_id]
298 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "private", nil)
300 assert length(to) == 3
301 assert Enum.empty?(cc)
303 assert mentioned_user.ap_id in to
304 assert third_user.ap_id in to
305 assert user.follower_address in to
308 test "for direct posts, not a reply" do
310 mentioned_user = insert(:user)
311 mentions = [mentioned_user.ap_id]
313 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "direct", nil)
315 assert length(to) == 1
316 assert Enum.empty?(cc)
318 assert mentioned_user.ap_id in to
321 test "for direct posts, a reply" do
323 mentioned_user = insert(:user)
324 third_user = insert(:user)
325 {:ok, activity} = CommonAPI.post(third_user, %{"status" => "uguu"})
326 mentions = [mentioned_user.ap_id]
328 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "direct", nil)
330 assert length(to) == 2
331 assert Enum.empty?(cc)
333 assert mentioned_user.ap_id in to
334 assert third_user.ap_id in to
338 describe "get_by_id_or_ap_id/1" do
339 test "get activity by id" do
340 activity = insert(:note_activity)
341 %Pleroma.Activity{} = note = Utils.get_by_id_or_ap_id(activity.id)
342 assert note.id == activity.id
345 test "get activity by ap_id" do
346 activity = insert(:note_activity)
347 %Pleroma.Activity{} = note = Utils.get_by_id_or_ap_id(activity.data["object"])
348 assert note.id == activity.id
351 test "get activity by object when type isn't `Create` " do
352 activity = insert(:like_activity)
353 %Pleroma.Activity{} = like = Utils.get_by_id_or_ap_id(activity.id)
354 assert like.data["object"] == activity.data["object"]
358 describe "to_master_date/1" do
359 test "removes microseconds from date (NaiveDateTime)" do
360 assert Utils.to_masto_date(~N[2015-01-23 23:50:07.123]) == "2015-01-23T23:50:07.000Z"
363 test "removes microseconds from date (String)" do
364 assert Utils.to_masto_date("2015-01-23T23:50:07.123Z") == "2015-01-23T23:50:07.000Z"
367 test "returns empty string when date invalid" do
368 assert Utils.to_masto_date("2015-01?23T23:50:07.123Z") == ""
372 describe "conversation_id_to_context/1" do
374 object = insert(:note)
375 assert Utils.conversation_id_to_context(object.id) == object.data["id"]
378 test "returns error if object not found" do
379 assert Utils.conversation_id_to_context("123") == {:error, "No such conversation"}
383 describe "maybe_notify_mentioned_recipients/2" do
384 test "returns recipients when activity is not `Create`" do
385 activity = insert(:like_activity)
386 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == ["test"]
389 test "returns recipients from tag" do
397 %{"type" => "Hashtag"},
399 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
400 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
401 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
406 activity = insert(:note_activity, user: user, note: object)
408 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
410 "https://testing.pleroma.lol/users/lain",
411 "https://shitposter.club/user/5381"
415 test "returns recipients when object is map" do
417 object = insert(:note, user: user)
420 insert(:note_activity,
426 %{"type" => "Hashtag"},
428 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
429 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
430 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
436 Pleroma.Repo.delete(object)
438 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
440 "https://testing.pleroma.lol/users/lain",
441 "https://shitposter.club/user/5381"
445 test "returns recipients when object not found" do
447 object = insert(:note, user: user)
449 activity = insert(:note_activity, user: user, note: object)
450 Pleroma.Repo.delete(object)
452 obj_url = activity.data["object"]
455 %{method: :get, url: ^obj_url} ->
456 %Tesla.Env{status: 404, body: ""}
459 assert Utils.maybe_notify_mentioned_recipients(["test-test"], activity) == [
465 describe "attachments_from_ids_descs/2" do
466 test "returns [] when attachment ids is empty" do
467 assert Utils.attachments_from_ids_descs([], "{}") == []
470 test "returns list attachments with desc" do
471 object = insert(:note)
472 desc = Jason.encode!(%{object.id => "test-desc"})
474 assert Utils.attachments_from_ids_descs(["#{object.id}", "34"], desc) == [
475 Map.merge(object.data, %{"name" => "test-desc"})
480 describe "attachments_from_ids/1" do
481 test "returns attachments with descs" do
482 object = insert(:note)
483 desc = Jason.encode!(%{object.id => "test-desc"})
485 assert Utils.attachments_from_ids(%{
486 "media_ids" => ["#{object.id}"],
487 "descriptions" => desc
489 Map.merge(object.data, %{"name" => "test-desc"})
493 test "returns attachments without descs" do
494 object = insert(:note)
495 assert Utils.attachments_from_ids(%{"media_ids" => ["#{object.id}"]}) == [object.data]
498 test "returns [] when not pass media_ids" do
499 assert Utils.attachments_from_ids(%{}) == []
503 describe "maybe_add_list_data/3" do
504 test "adds list params when found user list" do
506 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
508 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
510 additional: %{"bcc" => [list.ap_id], "listMessage" => list.ap_id},
511 object: %{"listMessage" => list.ap_id}
515 test "returns original params when list not found" do
517 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", insert(:user))
519 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
520 %{additional: %{}, object: %{}}
524 describe "make_note_data/11" do
525 test "returns note data" do
528 user2 = insert(:user)
529 user3 = insert(:user)
531 assert Utils.make_note_data(
535 "<h1>This is :moominmamma: note</h1>",
542 %{"custom_tag" => "test"}
544 "actor" => user.ap_id,
546 "cc" => [user3.ap_id],
547 "content" => "<h1>This is :moominmamma: note</h1>",
549 "sensitive" => false,
550 "summary" => "test summary",
552 "to" => [user2.ap_id],
554 "custom_tag" => "test"
559 describe "maybe_add_attachments/3" do
560 test "returns parsed results when attachment_links is false" do
561 assert Utils.maybe_add_attachments(
562 {"test", [], ["tags"]},
565 ) == {"test", [], ["tags"]}
568 test "adds attachments to parsed results" do
569 attachment = %{"url" => [%{"href" => "SakuraPM.png"}]}
571 assert Utils.maybe_add_attachments(
572 {"test", [], ["tags"]},
576 "test<br><a href=\"SakuraPM.png\" class='attachment'>SakuraPM.png</a>",