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 "to_master_date/1" do
339 test "removes microseconds from date (NaiveDateTime)" do
340 assert Utils.to_masto_date(~N[2015-01-23 23:50:07.123]) == "2015-01-23T23:50:07.000Z"
343 test "removes microseconds from date (String)" do
344 assert Utils.to_masto_date("2015-01-23T23:50:07.123Z") == "2015-01-23T23:50:07.000Z"
347 test "returns empty string when date invalid" do
348 assert Utils.to_masto_date("2015-01?23T23:50:07.123Z") == ""
352 describe "conversation_id_to_context/1" do
354 object = insert(:note)
355 assert Utils.conversation_id_to_context(object.id) == object.data["id"]
358 test "returns error if object not found" do
359 assert Utils.conversation_id_to_context("123") == {:error, "No such conversation"}
363 describe "maybe_notify_mentioned_recipients/2" do
364 test "returns recipients when activity is not `Create`" do
365 activity = insert(:like_activity)
366 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == ["test"]
369 test "returns recipients from tag" do
377 %{"type" => "Hashtag"},
379 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
380 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
381 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
386 activity = insert(:note_activity, user: user, note: object)
388 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
390 "https://testing.pleroma.lol/users/lain",
391 "https://shitposter.club/user/5381"
395 test "returns recipients when object is map" do
397 object = insert(:note, user: user)
400 insert(:note_activity,
406 %{"type" => "Hashtag"},
408 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
409 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
410 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
416 Pleroma.Repo.delete(object)
418 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
420 "https://testing.pleroma.lol/users/lain",
421 "https://shitposter.club/user/5381"
425 test "returns recipients when object not found" do
427 object = insert(:note, user: user)
429 activity = insert(:note_activity, user: user, note: object)
430 Pleroma.Repo.delete(object)
432 obj_url = activity.data["object"]
435 %{method: :get, url: ^obj_url} ->
436 %Tesla.Env{status: 404, body: ""}
439 assert Utils.maybe_notify_mentioned_recipients(["test-test"], activity) == [
445 describe "attachments_from_ids_descs/2" do
446 test "returns [] when attachment ids is empty" do
447 assert Utils.attachments_from_ids_descs([], "{}") == []
450 test "returns list attachments with desc" do
451 object = insert(:note)
452 desc = Jason.encode!(%{object.id => "test-desc"})
454 assert Utils.attachments_from_ids_descs(["#{object.id}", "34"], desc) == [
455 Map.merge(object.data, %{"name" => "test-desc"})
460 describe "attachments_from_ids/1" do
461 test "returns attachments with descs" do
462 object = insert(:note)
463 desc = Jason.encode!(%{object.id => "test-desc"})
465 assert Utils.attachments_from_ids(%{
466 "media_ids" => ["#{object.id}"],
467 "descriptions" => desc
469 Map.merge(object.data, %{"name" => "test-desc"})
473 test "returns attachments without descs" do
474 object = insert(:note)
475 assert Utils.attachments_from_ids(%{"media_ids" => ["#{object.id}"]}) == [object.data]
478 test "returns [] when not pass media_ids" do
479 assert Utils.attachments_from_ids(%{}) == []
483 describe "maybe_add_list_data/3" do
484 test "adds list params when found user list" do
486 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
488 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
490 additional: %{"bcc" => [list.ap_id], "listMessage" => list.ap_id},
491 object: %{"listMessage" => list.ap_id}
495 test "returns original params when list not found" do
497 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", insert(:user))
499 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
500 %{additional: %{}, object: %{}}
504 describe "make_note_data/11" do
505 test "returns note data" do
508 user2 = insert(:user)
509 user3 = insert(:user)
511 assert Utils.make_note_data(
515 "<h1>This is :moominmamma: note</h1>",
522 %{"custom_tag" => "test"}
524 "actor" => user.ap_id,
526 "cc" => [user3.ap_id],
527 "content" => "<h1>This is :moominmamma: note</h1>",
529 "sensitive" => false,
530 "summary" => "test summary",
532 "to" => [user2.ap_id],
534 "custom_tag" => "test"
539 describe "maybe_add_attachments/3" do
540 test "returns parsed results when attachment_links is false" do
541 assert Utils.maybe_add_attachments(
542 {"test", [], ["tags"]},
545 ) == {"test", [], ["tags"]}
548 test "adds attachments to parsed results" do
549 attachment = %{"url" => [%{"href" => "SakuraPM.png"}]}
551 assert Utils.maybe_add_attachments(
552 {"test", [], ["tags"]},
556 "test<br><a href=\"SakuraPM.png\" class='attachment'>SakuraPM.png</a>",