1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.FormatterTest do
6 alias Pleroma.Formatter
10 import Pleroma.Factory
13 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
17 describe ".add_hashtag_links" do
18 test "turns hashtags into links" do
19 text = "I love #cofe and #2hu"
22 ~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>)
24 assert {^expected_text, [], _tags} = Formatter.linkify(text)
27 test "does not turn html characters to tags" do
28 text = "#fact_3: pleroma does what mastodon't"
31 ~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)
33 assert {^expected_text, [], _tags} = Formatter.linkify(text)
37 describe ".add_links" do
38 test "turning urls into links" do
39 text = "Hey, check out https://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla ."
42 ~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> .)
44 assert {^expected, [], []} = Formatter.linkify(text)
46 text = "https://mastodon.social/@lambadalambda"
49 ~S(<a href="https://mastodon.social/@lambadalambda" rel="ugc">https://mastodon.social/@lambadalambda</a>)
51 assert {^expected, [], []} = Formatter.linkify(text)
53 text = "https://mastodon.social:4000/@lambadalambda"
56 ~S(<a href="https://mastodon.social:4000/@lambadalambda" rel="ugc">https://mastodon.social:4000/@lambadalambda</a>)
58 assert {^expected, [], []} = Formatter.linkify(text)
60 text = "@lambadalambda"
61 expected = "@lambadalambda"
63 assert {^expected, [], []} = Formatter.linkify(text)
65 text = "http://www.cs.vu.nl/~ast/intel/"
68 ~S(<a href="http://www.cs.vu.nl/~ast/intel/" rel="ugc">http://www.cs.vu.nl/~ast/intel/</a>)
70 assert {^expected, [], []} = Formatter.linkify(text)
72 text = "https://forum.zdoom.org/viewtopic.php?f=44&t=57087"
75 "<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>"
77 assert {^expected, [], []} = Formatter.linkify(text)
79 text = "https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul"
82 "<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>"
84 assert {^expected, [], []} = Formatter.linkify(text)
86 text = "https://www.google.co.jp/search?q=Nasim+Aghdam"
89 "<a href=\"https://www.google.co.jp/search?q=Nasim+Aghdam\" rel=\"ugc\">https://www.google.co.jp/search?q=Nasim+Aghdam</a>"
91 assert {^expected, [], []} = Formatter.linkify(text)
93 text = "https://en.wikipedia.org/wiki/Duff's_device"
96 "<a href=\"https://en.wikipedia.org/wiki/Duff's_device\" rel=\"ugc\">https://en.wikipedia.org/wiki/Duff's_device</a>"
98 assert {^expected, [], []} = Formatter.linkify(text)
100 text = "https://pleroma.com https://pleroma.com/sucks"
103 "<a href=\"https://pleroma.com\" rel=\"ugc\">https://pleroma.com</a> <a href=\"https://pleroma.com/sucks\" rel=\"ugc\">https://pleroma.com/sucks</a>"
105 assert {^expected, [], []} = Formatter.linkify(text)
107 text = "xmpp:contact@hacktivis.me"
109 expected = "<a href=\"xmpp:contact@hacktivis.me\" rel=\"ugc\">xmpp:contact@hacktivis.me</a>"
111 assert {^expected, [], []} = Formatter.linkify(text)
114 "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"
116 expected = "<a href=\"#{text}\" rel=\"ugc\">#{text}</a>"
118 assert {^expected, [], []} = Formatter.linkify(text)
122 describe "Formatter.linkify" do
123 test "correctly finds mentions that contain the domain name" do
124 _user = insert(:user, %{nickname: "lain"})
125 _remote_user = insert(:user, %{nickname: "lain@lain.com", local: false})
127 text = "hey @lain@lain.com what's up"
129 {_text, mentions, []} = Formatter.linkify(text)
130 [{username, user}] = mentions
132 assert username == "@lain@lain.com"
133 assert user.nickname == "lain@lain.com"
136 test "gives a replacement for user links, using local nicknames in user links text" do
137 text = "@gsimg According to @archa_eme_, that is @daggsy. Also hello @archaeme@archae.me"
138 gsimg = insert(:user, %{nickname: "gsimg"})
142 nickname: "archa_eme_",
143 uri: "https://archeme/@archa_eme_"
146 archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
148 {text, mentions, []} = Formatter.linkify(text)
150 assert length(mentions) == 3
153 ~s(<span class="h-card"><a class="u-url mention" data-user="#{gsimg.id}" href="#{
155 }" rel="ugc">@<span>gsimg</span></a></span> According to <span class="h-card"><a class="u-url mention" data-user="#{
157 }" 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="#{
159 }" href="#{archaeme_remote.ap_id}" rel="ugc">@<span>archaeme</span></a></span>)
161 assert expected_text == text
164 test "gives a replacement for user links when the user is using Osada" do
165 {:ok, mike} = User.get_or_fetch("mike@osada.macgirvin.com")
167 text = "@mike@osada.macgirvin.com test"
169 {text, mentions, []} = Formatter.linkify(text)
171 assert length(mentions) == 1
174 ~s(<span class="h-card"><a class="u-url mention" data-user="#{mike.id}" href="#{
176 }" rel="ugc">@<span>mike</span></a></span> test)
178 assert expected_text == text
181 test "gives a replacement for single-character local nicknames" do
183 o = insert(:user, %{nickname: "o"})
185 {text, mentions, []} = Formatter.linkify(text)
187 assert length(mentions) == 1
190 ~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)
192 assert expected_text == text
195 test "does not give a replacement for single-character local nicknames who don't exist" do
198 expected_text = "@a hi"
199 assert {^expected_text, [] = _mentions, [] = _tags} = Formatter.linkify(text)
202 test "given the 'safe_mention' option, it will only mention people in the beginning" do
204 other_user = insert(:user)
205 third_user = insert(:user)
206 text = " @#{user.nickname} @#{other_user.nickname} hey dudes i hate @#{third_user.nickname}"
207 {expected_text, mentions, [] = _tags} = Formatter.linkify(text, safe_mention: true)
209 assert mentions == [{"@#{user.nickname}", user}, {"@#{other_user.nickname}", other_user}]
211 assert expected_text ==
212 ~s(<span class="h-card"><a class="u-url mention" data-user="#{user.id}" href="#{
214 }" rel="ugc">@<span>#{user.nickname}</span></a></span> <span class="h-card"><a class="u-url mention" data-user="#{
216 }" 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="#{
218 }" href="#{third_user.ap_id}" rel="ugc">@<span>#{third_user.nickname}</span></a></span>)
221 test "given the 'safe_mention' option, it will still work without any mention" do
222 text = "A post without any mention"
223 {expected_text, mentions, [] = _tags} = Formatter.linkify(text, safe_mention: true)
225 assert mentions == []
226 assert expected_text == text
229 test "given the 'safe_mention' option, it will keep text after newlines" do
231 text = " @#{user.nickname}\n hey dude\n\nhow are you doing?"
233 {expected_text, _, _} = Formatter.linkify(text, safe_mention: true)
235 assert expected_text =~ "how are you doing?"
238 test "it can parse mentions and return the relevant users" do
240 "@@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me and @o and @@@jimm"
242 o = insert(:user, %{nickname: "o"})
243 jimm = insert(:user, %{nickname: "jimm"})
244 gsimg = insert(:user, %{nickname: "gsimg"})
245 archaeme = insert(:user, %{nickname: "archaeme"})
246 archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
248 expected_mentions = [
249 {"@archaeme", archaeme},
250 {"@archaeme@archae.me", archaeme_remote},
256 assert {_text, ^expected_mentions, []} = Formatter.linkify(text)
259 test "it parses URL containing local mention" do
260 _user = insert(:user, %{nickname: "lain"})
262 text = "https://example.com/@lain"
264 expected = ~S(<a href="https://example.com/@lain" rel="ugc">https://example.com/@lain</a>)
266 assert {^expected, [], []} = Formatter.linkify(text)
269 test "it correctly parses angry face D:< with mention" do
272 nickname: "lain@lain.com",
273 ap_id: "https://lain.com/users/lain",
274 id: "9qrWmR0cKniB0YU0TA"
277 text = "@lain@lain.com D:<"
280 ~S(<span class="h-card"><a class="u-url mention" data-user="9qrWmR0cKniB0YU0TA" href="https://lain.com/users/lain" rel="ugc">@<span>lain</span></a></span> D:<)
282 expected_mentions = [
283 {"@lain@lain.com", lain}
286 assert {^expected_text, ^expected_mentions, []} = Formatter.linkify(text)
290 describe ".parse_tags" do
291 test "parses tags in the text" do
292 text = "Here's a #Test. Maybe these are #working or not. What about #漢字? And #は。"
296 {"#working", "working"},
301 assert {_text, [], ^expected_tags} = Formatter.linkify(text)
305 test "it escapes HTML in plain text" do
306 text = "hello & world google.com/?a=b&c=d \n http://test.com/?a=b&c=d 1"
307 expected = "hello & world google.com/?a=b&c=d \n http://test.com/?a=b&c=d 1"
309 assert Formatter.html_escape(text, "text/plain") == expected