logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

utils_test.exs (22139B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.CommonAPI.UtilsTest do
  5. alias Pleroma.Builders.UserBuilder
  6. alias Pleroma.Web.CommonAPI
  7. alias Pleroma.Web.CommonAPI.ActivityDraft
  8. alias Pleroma.Web.CommonAPI.Utils
  9. use Pleroma.DataCase
  10. import ExUnit.CaptureLog
  11. import Pleroma.Factory
  12. @public_address "https://www.w3.org/ns/activitystreams#Public"
  13. describe "add_attachments/2" do
  14. setup do
  15. name =
  16. "Sakura Mana – Turned on by a Senior OL with a Temptating Tight Skirt-s Full Hipline and Panty Shot- Beautiful Thick Thighs- and Erotic Ass- -2015- -- Oppaitime 8-28-2017 6-50-33 PM.png"
  17. attachment = %{
  18. "url" => [%{"href" => URI.encode(name)}]
  19. }
  20. %{name: name, attachment: attachment}
  21. end
  22. test "it adds attachment links to a given text and attachment set", %{
  23. name: name,
  24. attachment: attachment
  25. } do
  26. len = 10
  27. clear_config([Pleroma.Upload, :filename_display_max_length], len)
  28. expected =
  29. "<br><a href=\"#{URI.encode(name)}\" class='attachment'>#{String.slice(name, 0..len)}…</a>"
  30. assert Utils.add_attachments("", [attachment]) == expected
  31. end
  32. test "doesn't truncate file name if config for truncate is set to 0", %{
  33. name: name,
  34. attachment: attachment
  35. } do
  36. clear_config([Pleroma.Upload, :filename_display_max_length], 0)
  37. expected = "<br><a href=\"#{URI.encode(name)}\" class='attachment'>#{name}</a>"
  38. assert Utils.add_attachments("", [attachment]) == expected
  39. end
  40. end
  41. describe "it confirms the password given is the current users password" do
  42. test "incorrect password given" do
  43. {:ok, user} = UserBuilder.insert()
  44. assert Utils.confirm_current_password(user, "") == {:error, "Invalid password."}
  45. end
  46. test "correct password given" do
  47. {:ok, user} = UserBuilder.insert()
  48. assert Utils.confirm_current_password(user, "test") == {:ok, user}
  49. end
  50. end
  51. describe "format_input/3" do
  52. test "works for bare text/plain" do
  53. text = "hello world!"
  54. expected = "hello world!"
  55. {output, [], []} = Utils.format_input(text, "text/plain")
  56. assert output == expected
  57. text = "hello world!\n\nsecond paragraph!"
  58. expected = "hello world!<br><br>second paragraph!"
  59. {output, [], []} = Utils.format_input(text, "text/plain")
  60. assert output == expected
  61. end
  62. test "works for bare text/html" do
  63. text = "<p>hello world!</p>"
  64. expected = "<p>hello world!</p>"
  65. {output, [], []} = Utils.format_input(text, "text/html")
  66. assert output == expected
  67. text = "<p>hello world!</p><br/>\n<p>second paragraph</p>"
  68. expected = "<p>hello world!</p><br/>\n<p>second paragraph</p>"
  69. {output, [], []} = Utils.format_input(text, "text/html")
  70. assert output == expected
  71. end
  72. test "works for bare text/markdown" do
  73. text = "**hello world**"
  74. expected = "<p><strong>hello world</strong></p>"
  75. {output, [], []} = Utils.format_input(text, "text/markdown")
  76. assert output == expected
  77. text = "**hello world**\n\n*another paragraph*"
  78. expected = "<p><strong>hello world</strong></p><p><em>another paragraph</em></p>"
  79. {output, [], []} = Utils.format_input(text, "text/markdown")
  80. assert output == expected
  81. text = """
  82. > cool quote
  83. by someone
  84. """
  85. expected = "<blockquote><p>cool quote</p></blockquote><p>by someone</p>"
  86. {output, [], []} = Utils.format_input(text, "text/markdown")
  87. assert output == expected
  88. end
  89. test "works for bare text/bbcode" do
  90. text = "[b]hello world[/b]"
  91. expected = "<strong>hello world</strong>"
  92. {output, [], []} = Utils.format_input(text, "text/bbcode")
  93. assert output == expected
  94. text = "[b]hello world![/b]\n\nsecond paragraph!"
  95. expected = "<strong>hello world!</strong><br><br>second paragraph!"
  96. {output, [], []} = Utils.format_input(text, "text/bbcode")
  97. assert output == expected
  98. text = "[b]hello world![/b]\n\n<strong>second paragraph!</strong>"
  99. expected =
  100. "<strong>hello world!</strong><br><br>&lt;strong&gt;second paragraph!&lt;/strong&gt;"
  101. {output, [], []} = Utils.format_input(text, "text/bbcode")
  102. assert output == expected
  103. end
  104. test "works for text/markdown with mentions" do
  105. {:ok, user} =
  106. UserBuilder.insert(%{nickname: "user__test", ap_id: "http://foo.com/user__test"})
  107. text =
  108. "**hello world**\n\n*another @user__test and @user__test http://google.com paragraph*"
  109. {output, _, _} = Utils.format_input(text, "text/markdown")
  110. assert output ==
  111. ~s(<p><strong>hello world</strong></p><p><em>another <span class="h-card"><a class="u-url mention" data-user="#{user.id}" 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="#{user.id}" href="http://foo.com/user__test" rel="ugc">@<span>user__test</span></a></span> <a href="http://google.com">http://google.com</a> paragraph</em></p>)
  112. end
  113. end
  114. describe "format_input/3 with markdown" do
  115. test "Paragraph" do
  116. code = ~s[Hello\n\nWorld!]
  117. {result, [], []} = Utils.format_input(code, "text/markdown")
  118. assert result == "<p>Hello</p><p>World!</p>"
  119. end
  120. test "links" do
  121. code = "https://en.wikipedia.org/wiki/Animal_Crossing_(video_game)"
  122. {result, [], []} = Utils.format_input(code, "text/markdown")
  123. assert result == ~s[<p><a href="#{code}">#{code}</a></p>]
  124. code = "https://github.com/pragdave/earmark/"
  125. {result, [], []} = Utils.format_input(code, "text/markdown")
  126. assert result == ~s[<p><a href="#{code}">#{code}</a></p>]
  127. code = "https://github.com/~foo/bar"
  128. {result, [], []} = Utils.format_input(code, "text/markdown")
  129. assert result == ~s[<p><a href="#{code}">#{code}</a></p>]
  130. end
  131. test "link with local mention" do
  132. insert(:user, %{nickname: "lain"})
  133. code = "https://example.com/@lain"
  134. {result, [], []} = Utils.format_input(code, "text/markdown")
  135. assert result == ~s[<p><a href="#{code}">#{code}</a></p>]
  136. end
  137. test "local mentions" do
  138. mario = insert(:user, %{nickname: "mario"})
  139. luigi = insert(:user, %{nickname: "luigi"})
  140. code = "@mario @luigi yo what's up?"
  141. {result, _, []} = Utils.format_input(code, "text/markdown")
  142. assert result ==
  143. ~s[<p><span class="h-card"><a class="u-url mention" data-user="#{mario.id}" href="#{mario.ap_id}" rel="ugc">@<span>mario</span></a></span> <span class="h-card"><a class="u-url mention" data-user="#{luigi.id}" href="#{luigi.ap_id}" rel="ugc">@<span>luigi</span></a></span> yo what&#39;s up?</p>]
  144. end
  145. test "remote mentions" do
  146. mario = insert(:user, %{nickname: "mario@mushroom.world", local: false})
  147. luigi = insert(:user, %{nickname: "luigi@mushroom.world", local: false})
  148. code = "@mario@mushroom.world @luigi@mushroom.world yo what's up?"
  149. {result, _, []} = Utils.format_input(code, "text/markdown")
  150. assert result ==
  151. ~s[<p><span class="h-card"><a class="u-url mention" data-user="#{mario.id}" href="#{mario.ap_id}" rel="ugc">@<span>mario</span></a></span> <span class="h-card"><a class="u-url mention" data-user="#{luigi.id}" href="#{luigi.ap_id}" rel="ugc">@<span>luigi</span></a></span> yo what&#39;s up?</p>]
  152. end
  153. test "raw HTML" do
  154. code = ~s[<a href="http://example.org/">OwO</a><!-- what's this?-->]
  155. {result, [], []} = Utils.format_input(code, "text/markdown")
  156. assert result == ~s[<a href="http://example.org/">OwO</a>]
  157. end
  158. test "rulers" do
  159. code = ~s[before\n\n-----\n\nafter]
  160. {result, [], []} = Utils.format_input(code, "text/markdown")
  161. assert result == "<p>before</p><hr/><p>after</p>"
  162. end
  163. test "blockquote" do
  164. code = ~s[> whoms't are you quoting?]
  165. {result, [], []} = Utils.format_input(code, "text/markdown")
  166. assert result == "<blockquote><p>whoms&#39;t are you quoting?</p></blockquote>"
  167. end
  168. test "code" do
  169. code = ~s[`mix`]
  170. {result, [], []} = Utils.format_input(code, "text/markdown")
  171. assert result == ~s[<p><code class="inline">mix</code></p>]
  172. code = ~s[``mix``]
  173. {result, [], []} = Utils.format_input(code, "text/markdown")
  174. assert result == ~s[<p><code class="inline">mix</code></p>]
  175. code = ~s[```\nputs "Hello World"\n```]
  176. {result, [], []} = Utils.format_input(code, "text/markdown")
  177. assert result == ~s[<pre><code>puts &quot;Hello World&quot;</code></pre>]
  178. code = ~s[ <div>\n </div>]
  179. {result, [], []} = Utils.format_input(code, "text/markdown")
  180. assert result == ~s[<pre><code>&lt;div&gt;\n&lt;/div&gt;</code></pre>]
  181. end
  182. test "lists" do
  183. code = ~s[- one\n- two\n- three\n- four]
  184. {result, [], []} = Utils.format_input(code, "text/markdown")
  185. assert result == "<ul><li>one</li><li>two</li><li>three</li><li>four</li></ul>"
  186. code = ~s[1. one\n2. two\n3. three\n4. four\n]
  187. {result, [], []} = Utils.format_input(code, "text/markdown")
  188. assert result == "<ol><li>one</li><li>two</li><li>three</li><li>four</li></ol>"
  189. end
  190. test "delegated renderers" do
  191. code = ~s[*aaaa~*]
  192. {result, [], []} = Utils.format_input(code, "text/markdown")
  193. assert result == ~s[<p><em>aaaa~</em></p>]
  194. code = ~s[**aaaa~**]
  195. {result, [], []} = Utils.format_input(code, "text/markdown")
  196. assert result == ~s[<p><strong>aaaa~</strong></p>]
  197. # strikethrough
  198. code = ~s[~~aaaa~~~]
  199. {result, [], []} = Utils.format_input(code, "text/markdown")
  200. assert result == ~s[<p><del>aaaa</del>~</p>]
  201. end
  202. end
  203. describe "formats date to asctime" do
  204. test "when date is in ISO 8601 format" do
  205. date = DateTime.utc_now() |> DateTime.to_iso8601()
  206. expected =
  207. date
  208. |> DateTime.from_iso8601()
  209. |> elem(1)
  210. |> Calendar.Strftime.strftime!("%a %b %d %H:%M:%S %z %Y")
  211. assert Utils.date_to_asctime(date) == expected
  212. end
  213. test "when date is a binary in wrong format" do
  214. date = DateTime.utc_now()
  215. expected = ""
  216. assert capture_log(fn ->
  217. assert Utils.date_to_asctime(date) == expected
  218. end) =~ "Date #{date} in wrong format, must be ISO 8601"
  219. end
  220. test "when date is a Unix timestamp" do
  221. date = DateTime.utc_now() |> DateTime.to_unix()
  222. expected = ""
  223. assert capture_log(fn ->
  224. assert Utils.date_to_asctime(date) == expected
  225. end) =~ "Date #{date} in wrong format, must be ISO 8601"
  226. end
  227. test "when date is nil" do
  228. expected = ""
  229. assert capture_log(fn ->
  230. assert Utils.date_to_asctime(nil) == expected
  231. end) =~ "Date in wrong format, must be ISO 8601"
  232. end
  233. test "when date is a random string" do
  234. assert capture_log(fn ->
  235. assert Utils.date_to_asctime("foo") == ""
  236. end) =~ "Date foo in wrong format, must be ISO 8601"
  237. end
  238. end
  239. describe "get_to_and_cc" do
  240. test "for public posts, not a reply" do
  241. user = insert(:user)
  242. mentioned_user = insert(:user)
  243. draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "public"}
  244. {to, cc} = Utils.get_to_and_cc(draft)
  245. assert length(to) == 2
  246. 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
  250. end
  251. test "for public posts, a reply" do
  252. user = insert(:user)
  253. mentioned_user = insert(:user)
  254. third_user = insert(:user)
  255. {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
  256. draft = %ActivityDraft{
  257. user: user,
  258. mentions: [mentioned_user.ap_id],
  259. visibility: "public",
  260. in_reply_to: activity
  261. }
  262. {to, cc} = Utils.get_to_and_cc(draft)
  263. assert length(to) == 3
  264. assert length(cc) == 1
  265. assert @public_address in to
  266. assert mentioned_user.ap_id in to
  267. assert third_user.ap_id in to
  268. assert user.follower_address in cc
  269. end
  270. test "for unlisted posts, not a reply" do
  271. user = insert(:user)
  272. mentioned_user = insert(:user)
  273. draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "unlisted"}
  274. {to, cc} = Utils.get_to_and_cc(draft)
  275. assert length(to) == 2
  276. assert length(cc) == 1
  277. assert @public_address in cc
  278. assert mentioned_user.ap_id in to
  279. assert user.follower_address in to
  280. end
  281. test "for unlisted posts, a reply" do
  282. user = insert(:user)
  283. mentioned_user = insert(:user)
  284. third_user = insert(:user)
  285. {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
  286. draft = %ActivityDraft{
  287. user: user,
  288. mentions: [mentioned_user.ap_id],
  289. visibility: "unlisted",
  290. in_reply_to: activity
  291. }
  292. {to, cc} = Utils.get_to_and_cc(draft)
  293. assert length(to) == 3
  294. 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
  299. end
  300. test "for private posts, not a reply" do
  301. user = insert(:user)
  302. mentioned_user = insert(:user)
  303. draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "private"}
  304. {to, cc} = Utils.get_to_and_cc(draft)
  305. assert length(to) == 2
  306. assert Enum.empty?(cc)
  307. assert mentioned_user.ap_id in to
  308. assert user.follower_address in to
  309. end
  310. test "for private posts, a reply" do
  311. user = insert(:user)
  312. mentioned_user = insert(:user)
  313. third_user = insert(:user)
  314. {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
  315. draft = %ActivityDraft{
  316. user: user,
  317. mentions: [mentioned_user.ap_id],
  318. visibility: "private",
  319. in_reply_to: activity
  320. }
  321. {to, cc} = Utils.get_to_and_cc(draft)
  322. assert length(to) == 2
  323. assert Enum.empty?(cc)
  324. assert mentioned_user.ap_id in to
  325. assert user.follower_address in to
  326. end
  327. test "for direct posts, not a reply" do
  328. user = insert(:user)
  329. mentioned_user = insert(:user)
  330. draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "direct"}
  331. {to, cc} = Utils.get_to_and_cc(draft)
  332. assert length(to) == 1
  333. assert Enum.empty?(cc)
  334. assert mentioned_user.ap_id in to
  335. end
  336. test "for direct posts, a reply" do
  337. user = insert(:user)
  338. mentioned_user = insert(:user)
  339. third_user = insert(:user)
  340. {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
  341. draft = %ActivityDraft{
  342. user: user,
  343. mentions: [mentioned_user.ap_id],
  344. visibility: "direct",
  345. in_reply_to: activity
  346. }
  347. {to, cc} = Utils.get_to_and_cc(draft)
  348. assert length(to) == 1
  349. assert Enum.empty?(cc)
  350. assert mentioned_user.ap_id in to
  351. {:ok, direct_activity} = CommonAPI.post(third_user, %{status: "uguu", visibility: "direct"})
  352. draft = %ActivityDraft{
  353. user: user,
  354. mentions: [mentioned_user.ap_id],
  355. visibility: "direct",
  356. in_reply_to: direct_activity
  357. }
  358. {to, cc} = Utils.get_to_and_cc(draft)
  359. assert length(to) == 2
  360. assert Enum.empty?(cc)
  361. assert mentioned_user.ap_id in to
  362. assert third_user.ap_id in to
  363. end
  364. end
  365. describe "to_master_date/1" do
  366. test "removes microseconds from date (NaiveDateTime)" do
  367. assert Utils.to_masto_date(~N[2015-01-23 23:50:07.123]) == "2015-01-23T23:50:07.000Z"
  368. end
  369. test "removes microseconds from date (String)" do
  370. assert Utils.to_masto_date("2015-01-23T23:50:07.123Z") == "2015-01-23T23:50:07.000Z"
  371. end
  372. test "returns empty string when date invalid" do
  373. assert Utils.to_masto_date("2015-01?23T23:50:07.123Z") == ""
  374. end
  375. end
  376. describe "maybe_notify_mentioned_recipients/2" do
  377. test "returns recipients when activity is not `Create`" do
  378. activity = insert(:like_activity)
  379. assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == ["test"]
  380. end
  381. test "returns recipients from tag" do
  382. user = insert(:user)
  383. object =
  384. insert(:note,
  385. user: user,
  386. data: %{
  387. "tag" => [
  388. %{"type" => "Hashtag"},
  389. "",
  390. %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
  391. %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
  392. %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
  393. ]
  394. }
  395. )
  396. activity = insert(:note_activity, user: user, note: object)
  397. assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
  398. "test",
  399. "https://testing.pleroma.lol/users/lain",
  400. "https://shitposter.club/user/5381"
  401. ]
  402. end
  403. test "returns recipients when object is map" do
  404. user = insert(:user)
  405. object = insert(:note, user: user)
  406. activity =
  407. insert(:note_activity,
  408. user: user,
  409. note: object,
  410. data_attrs: %{
  411. "object" => %{
  412. "tag" => [
  413. %{"type" => "Hashtag"},
  414. "",
  415. %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
  416. %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
  417. %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
  418. ]
  419. }
  420. }
  421. )
  422. Pleroma.Repo.delete(object)
  423. assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
  424. "test",
  425. "https://testing.pleroma.lol/users/lain",
  426. "https://shitposter.club/user/5381"
  427. ]
  428. end
  429. test "returns recipients when object not found" do
  430. user = insert(:user)
  431. object = insert(:note, user: user)
  432. activity = insert(:note_activity, user: user, note: object)
  433. Pleroma.Repo.delete(object)
  434. obj_url = activity.data["object"]
  435. Tesla.Mock.mock(fn
  436. %{method: :get, url: ^obj_url} ->
  437. %Tesla.Env{status: 404, body: ""}
  438. end)
  439. assert Utils.maybe_notify_mentioned_recipients(["test-test"], activity) == [
  440. "test-test"
  441. ]
  442. end
  443. end
  444. describe "attachments_from_ids_descs/3" do
  445. test "returns [] when attachment ids is empty" do
  446. assert Utils.attachments_from_ids_descs([], "{}", nil) == []
  447. end
  448. test "returns list attachments with desc" do
  449. user = insert(:user)
  450. object = insert(:attachment, %{user: user})
  451. desc = Jason.encode!(%{object.id => "test-desc"})
  452. assert Utils.attachments_from_ids_descs(["#{object.id}", "34"], desc, user) == [
  453. Map.merge(object.data, %{"name" => "test-desc"})
  454. ]
  455. end
  456. end
  457. describe "attachments_from_ids/2" do
  458. test "returns attachments with descs" do
  459. user = insert(:user)
  460. object = insert(:attachment, %{user: user})
  461. desc = Jason.encode!(%{object.id => "test-desc"})
  462. assert Utils.attachments_from_ids(
  463. %{
  464. media_ids: ["#{object.id}"],
  465. descriptions: desc
  466. },
  467. user
  468. ) == [
  469. Map.merge(object.data, %{"name" => "test-desc"})
  470. ]
  471. end
  472. test "returns attachments without descs" do
  473. user = insert(:user)
  474. object = insert(:attachment, %{user: user})
  475. assert Utils.attachments_from_ids(%{media_ids: ["#{object.id}"]}, user) == [object.data]
  476. end
  477. test "returns [] when not pass media_ids" do
  478. assert Utils.attachments_from_ids(%{}, nil) == []
  479. end
  480. test "returns [] when media_ids not belong to current user" do
  481. user = insert(:user)
  482. user2 = insert(:user)
  483. object = insert(:attachment, %{user: user})
  484. assert Utils.attachments_from_ids(%{media_ids: ["#{object.id}"]}, user2) == []
  485. end
  486. test "checks that the object is of upload type" do
  487. object = insert(:note)
  488. assert Utils.attachments_from_ids(%{media_ids: ["#{object.id}"]}, nil) == []
  489. end
  490. end
  491. describe "maybe_add_list_data/3" do
  492. test "adds list params when found user list" do
  493. user = insert(:user)
  494. {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
  495. assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
  496. %{
  497. additional: %{"bcc" => [list.ap_id], "listMessage" => list.ap_id},
  498. object: %{"listMessage" => list.ap_id}
  499. }
  500. end
  501. test "returns original params when list not found" do
  502. user = insert(:user)
  503. {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", insert(:user))
  504. assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
  505. %{additional: %{}, object: %{}}
  506. end
  507. end
  508. describe "maybe_add_attachments/3" do
  509. test "returns parsed results when attachment_links is false" do
  510. assert Utils.maybe_add_attachments(
  511. {"test", [], ["tags"]},
  512. [],
  513. false
  514. ) == {"test", [], ["tags"]}
  515. end
  516. test "adds attachments to parsed results" do
  517. attachment = %{"url" => [%{"href" => "SakuraPM.png"}]}
  518. assert Utils.maybe_add_attachments(
  519. {"test", [], ["tags"]},
  520. [attachment],
  521. true
  522. ) == {
  523. "test<br><a href=\"SakuraPM.png\" class='attachment'>SakuraPM.png</a>",
  524. [],
  525. ["tags"]
  526. }
  527. end
  528. end
  529. end