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.

282 lines
10KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 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. ~s(I love <a class="hashtag" data-tag="cofe" href="http://localhost:4001/tag/cofe" rel="tag ugc">#cofe</a> and <a class="hashtag" data-tag="2hu" href="http://localhost:4001/tag/2hu" rel="tag ugc">#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. ~s(<a class="hashtag" data-tag="fact_3" href="http://localhost:4001/tag/fact_3" rel="tag ugc">#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. ~S(Hey, check out <a href="https://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla" rel="ugc">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. ~S(<a href="https://mastodon.social/@lambadalambda" rel="ugc">https://mastodon.social/@lambadalambda</a>)
  36. assert {^expected, [], []} = Formatter.linkify(text)
  37. text = "https://mastodon.social:4000/@lambadalambda"
  38. expected =
  39. ~S(<a href="https://mastodon.social:4000/@lambadalambda" rel="ugc">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 =
  46. ~S(<a href="http://www.cs.vu.nl/~ast/intel/" rel="ugc">http://www.cs.vu.nl/~ast/intel/</a>)
  47. assert {^expected, [], []} = Formatter.linkify(text)
  48. text = "https://forum.zdoom.org/viewtopic.php?f=44&t=57087"
  49. expected =
  50. "<a href=\"https://forum.zdoom.org/viewtopic.php?f=44&t=57087\" rel=\"ugc\">https://forum.zdoom.org/viewtopic.php?f=44&t=57087</a>"
  51. assert {^expected, [], []} = Formatter.linkify(text)
  52. text = "https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul"
  53. expected =
  54. "<a href=\"https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul\" rel=\"ugc\">https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul</a>"
  55. assert {^expected, [], []} = Formatter.linkify(text)
  56. text = "https://www.google.co.jp/search?q=Nasim+Aghdam"
  57. expected =
  58. "<a href=\"https://www.google.co.jp/search?q=Nasim+Aghdam\" rel=\"ugc\">https://www.google.co.jp/search?q=Nasim+Aghdam</a>"
  59. assert {^expected, [], []} = Formatter.linkify(text)
  60. text = "https://en.wikipedia.org/wiki/Duff's_device"
  61. expected =
  62. "<a href=\"https://en.wikipedia.org/wiki/Duff's_device\" rel=\"ugc\">https://en.wikipedia.org/wiki/Duff's_device</a>"
  63. assert {^expected, [], []} = Formatter.linkify(text)
  64. text = "https://pleroma.com https://pleroma.com/sucks"
  65. expected =
  66. "<a href=\"https://pleroma.com\" rel=\"ugc\">https://pleroma.com</a> <a href=\"https://pleroma.com/sucks\" rel=\"ugc\">https://pleroma.com/sucks</a>"
  67. assert {^expected, [], []} = Formatter.linkify(text)
  68. text = "xmpp:contact@hacktivis.me"
  69. expected = "<a href=\"xmpp:contact@hacktivis.me\" rel=\"ugc\">xmpp:contact@hacktivis.me</a>"
  70. assert {^expected, [], []} = Formatter.linkify(text)
  71. text =
  72. "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"
  73. expected = "<a href=\"#{text}\" rel=\"ugc\">#{text}</a>"
  74. assert {^expected, [], []} = Formatter.linkify(text)
  75. end
  76. end
  77. describe "Formatter.linkify" do
  78. test "correctly finds mentions that contain the domain name" do
  79. _user = insert(:user, %{nickname: "lain"})
  80. _remote_user = insert(:user, %{nickname: "lain@lain.com", local: false})
  81. text = "hey @lain@lain.com what's up"
  82. {_text, mentions, []} = Formatter.linkify(text)
  83. [{username, user}] = mentions
  84. assert username == "@lain@lain.com"
  85. assert user.nickname == "lain@lain.com"
  86. end
  87. test "gives a replacement for user links, using local nicknames in user links text" do
  88. text = "@gsimg According to @archa_eme_, that is @daggsy. Also hello @archaeme@archae.me"
  89. gsimg = insert(:user, %{nickname: "gsimg"})
  90. archaeme =
  91. insert(:user,
  92. nickname: "archa_eme_",
  93. uri: "https://archeme/@archa_eme_"
  94. )
  95. archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
  96. {text, mentions, []} = Formatter.linkify(text)
  97. assert length(mentions) == 3
  98. expected_text =
  99. ~s(<span class="h-card"><a class="u-url mention" data-user="#{gsimg.id}" href="#{
  100. gsimg.ap_id
  101. }" rel="ugc">@<span>gsimg</span></a></span> According to <span class="h-card"><a class="u-url mention" data-user="#{
  102. archaeme.id
  103. }" href="#{"https://archeme/@archa_eme_"}" rel="ugc">@<span>archa_eme_</span></a></span>, that is @daggsy. Also hello <span class="h-card"><a class="u-url mention" data-user="#{
  104. archaeme_remote.id
  105. }" href="#{archaeme_remote.ap_id}" rel="ugc">@<span>archaeme</span></a></span>)
  106. assert expected_text == text
  107. end
  108. test "gives a replacement for user links when the user is using Osada" do
  109. {:ok, mike} = User.get_or_fetch("mike@osada.macgirvin.com")
  110. text = "@mike@osada.macgirvin.com test"
  111. {text, mentions, []} = Formatter.linkify(text)
  112. assert length(mentions) == 1
  113. expected_text =
  114. ~s(<span class="h-card"><a class="u-url mention" data-user="#{mike.id}" href="#{
  115. mike.ap_id
  116. }" rel="ugc">@<span>mike</span></a></span> test)
  117. assert expected_text == text
  118. end
  119. test "gives a replacement for single-character local nicknames" do
  120. text = "@o hi"
  121. o = insert(:user, %{nickname: "o"})
  122. {text, mentions, []} = Formatter.linkify(text)
  123. assert length(mentions) == 1
  124. expected_text =
  125. ~s(<span class="h-card"><a class="u-url mention" data-user="#{o.id}" href="#{o.ap_id}" rel="ugc">@<span>o</span></a></span> hi)
  126. assert expected_text == text
  127. end
  128. test "does not give a replacement for single-character local nicknames who don't exist" do
  129. text = "@a hi"
  130. expected_text = "@a hi"
  131. assert {^expected_text, [] = _mentions, [] = _tags} = Formatter.linkify(text)
  132. end
  133. test "given the 'safe_mention' option, it will only mention people in the beginning" do
  134. user = insert(:user)
  135. other_user = insert(:user)
  136. third_user = insert(:user)
  137. text = " @#{user.nickname} @#{other_user.nickname} hey dudes i hate @#{third_user.nickname}"
  138. {expected_text, mentions, [] = _tags} = Formatter.linkify(text, safe_mention: true)
  139. assert mentions == [{"@#{user.nickname}", user}, {"@#{other_user.nickname}", other_user}]
  140. assert expected_text ==
  141. ~s(<span class="h-card"><a class="u-url mention" data-user="#{user.id}" href="#{
  142. user.ap_id
  143. }" rel="ugc">@<span>#{user.nickname}</span></a></span> <span class="h-card"><a class="u-url mention" data-user="#{
  144. other_user.id
  145. }" href="#{other_user.ap_id}" rel="ugc">@<span>#{other_user.nickname}</span></a></span> hey dudes i hate <span class="h-card"><a class="u-url mention" data-user="#{
  146. third_user.id
  147. }" href="#{third_user.ap_id}" rel="ugc">@<span>#{third_user.nickname}</span></a></span>)
  148. end
  149. test "given the 'safe_mention' option, it will still work without any mention" do
  150. text = "A post without any mention"
  151. {expected_text, mentions, [] = _tags} = Formatter.linkify(text, safe_mention: true)
  152. assert mentions == []
  153. assert expected_text == text
  154. end
  155. test "given the 'safe_mention' option, it will keep text after newlines" do
  156. user = insert(:user)
  157. text = " @#{user.nickname}\n hey dude\n\nhow are you doing?"
  158. {expected_text, _, _} = Formatter.linkify(text, safe_mention: true)
  159. assert expected_text =~ "how are you doing?"
  160. end
  161. test "it can parse mentions and return the relevant users" do
  162. text =
  163. "@@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me and @o and @@@jimm"
  164. o = insert(:user, %{nickname: "o"})
  165. jimm = insert(:user, %{nickname: "jimm"})
  166. gsimg = insert(:user, %{nickname: "gsimg"})
  167. archaeme = insert(:user, %{nickname: "archaeme"})
  168. archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
  169. expected_mentions = [
  170. {"@archaeme", archaeme},
  171. {"@archaeme@archae.me", archaeme_remote},
  172. {"@gsimg", gsimg},
  173. {"@jimm", jimm},
  174. {"@o", o}
  175. ]
  176. assert {_text, ^expected_mentions, []} = Formatter.linkify(text)
  177. end
  178. end
  179. describe ".parse_tags" do
  180. test "parses tags in the text" do
  181. text = "Here's a #Test. Maybe these are #working or not. What about #漢字? And #は。"
  182. expected_tags = [
  183. {"#Test", "test"},
  184. {"#working", "working"},
  185. {"#は", "は"},
  186. {"#漢字", "漢字"}
  187. ]
  188. assert {_text, [], ^expected_tags} = Formatter.linkify(text)
  189. end
  190. end
  191. test "it escapes HTML in plain text" do
  192. text = "hello & world google.com/?a=b&c=d \n http://test.com/?a=b&c=d 1"
  193. expected = "hello &amp; world google.com/?a=b&c=d \n http://test.com/?a=b&c=d 1"
  194. assert Formatter.html_escape(text, "text/plain") == expected
  195. end
  196. end