Merge branch 'develop' into 'remove-avatar-header'
[akkoma] / test / web / common_api / common_api_utils_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.Web.CommonAPI.UtilsTest do
6 alias Pleroma.Builders.UserBuilder
7 alias Pleroma.Object
8 alias Pleroma.Web.CommonAPI
9 alias Pleroma.Web.CommonAPI.Utils
10 alias Pleroma.Web.Endpoint
11 use Pleroma.DataCase
12
13 import Pleroma.Factory
14
15 @public_address "https://www.w3.org/ns/activitystreams#Public"
16
17 test "it adds attachment links to a given text and attachment set" do
18 name =
19 "Sakura%20Mana%20%E2%80%93%20Turned%20on%20by%20a%20Senior%20OL%20with%20a%20Temptating%20Tight%20Skirt-s%20Full%20Hipline%20and%20Panty%20Shot-%20Beautiful%20Thick%20Thighs-%20and%20Erotic%20Ass-%20-2015-%20--%20Oppaitime%208-28-2017%206-50-33%20PM.png"
20
21 attachment = %{
22 "url" => [%{"href" => name}]
23 }
24
25 res = Utils.add_attachments("", [attachment])
26
27 assert res ==
28 "<br><a href=\"#{name}\" class='attachment'>Sakura Mana – Turned on by a Se…</a>"
29 end
30
31 describe "it confirms the password given is the current users password" do
32 test "incorrect password given" do
33 {:ok, user} = UserBuilder.insert()
34
35 assert Utils.confirm_current_password(user, "") == {:error, "Invalid password."}
36 end
37
38 test "correct password given" do
39 {:ok, user} = UserBuilder.insert()
40 assert Utils.confirm_current_password(user, "test") == {:ok, user}
41 end
42 end
43
44 test "parses emoji from name and bio" do
45 {:ok, user} = UserBuilder.insert(%{name: ":blank:", bio: ":firefox:"})
46
47 expected = [
48 %{
49 "type" => "Emoji",
50 "icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}/emoji/Firefox.gif"},
51 "name" => ":firefox:"
52 },
53 %{
54 "type" => "Emoji",
55 "icon" => %{
56 "type" => "Image",
57 "url" => "#{Endpoint.url()}/emoji/blank.png"
58 },
59 "name" => ":blank:"
60 }
61 ]
62
63 assert expected == Utils.emoji_from_profile(user)
64 end
65
66 describe "format_input/3" do
67 test "works for bare text/plain" do
68 text = "hello world!"
69 expected = "hello world!"
70
71 {output, [], []} = Utils.format_input(text, "text/plain")
72
73 assert output == expected
74
75 text = "hello world!\n\nsecond paragraph!"
76 expected = "hello world!<br><br>second paragraph!"
77
78 {output, [], []} = Utils.format_input(text, "text/plain")
79
80 assert output == expected
81 end
82
83 test "works for bare text/html" do
84 text = "<p>hello world!</p>"
85 expected = "<p>hello world!</p>"
86
87 {output, [], []} = Utils.format_input(text, "text/html")
88
89 assert output == expected
90
91 text = "<p>hello world!</p>\n\n<p>second paragraph</p>"
92 expected = "<p>hello world!</p>\n\n<p>second paragraph</p>"
93
94 {output, [], []} = Utils.format_input(text, "text/html")
95
96 assert output == expected
97 end
98
99 test "works for bare text/markdown" do
100 text = "**hello world**"
101 expected = "<p><strong>hello world</strong></p>\n"
102
103 {output, [], []} = Utils.format_input(text, "text/markdown")
104
105 assert output == expected
106
107 text = "**hello world**\n\n*another paragraph*"
108 expected = "<p><strong>hello world</strong></p>\n<p><em>another paragraph</em></p>\n"
109
110 {output, [], []} = Utils.format_input(text, "text/markdown")
111
112 assert output == expected
113
114 text = """
115 > cool quote
116
117 by someone
118 """
119
120 expected = "<blockquote><p>cool quote</p>\n</blockquote>\n<p>by someone</p>\n"
121
122 {output, [], []} = Utils.format_input(text, "text/markdown")
123
124 assert output == expected
125 end
126
127 test "works for bare text/bbcode" do
128 text = "[b]hello world[/b]"
129 expected = "<strong>hello world</strong>"
130
131 {output, [], []} = Utils.format_input(text, "text/bbcode")
132
133 assert output == expected
134
135 text = "[b]hello world![/b]\n\nsecond paragraph!"
136 expected = "<strong>hello world!</strong><br>\n<br>\nsecond paragraph!"
137
138 {output, [], []} = Utils.format_input(text, "text/bbcode")
139
140 assert output == expected
141
142 text = "[b]hello world![/b]\n\n<strong>second paragraph!</strong>"
143
144 expected =
145 "<strong>hello world!</strong><br>\n<br>\n&lt;strong&gt;second paragraph!&lt;/strong&gt;"
146
147 {output, [], []} = Utils.format_input(text, "text/bbcode")
148
149 assert output == expected
150 end
151
152 test "works for text/markdown with mentions" do
153 {:ok, user} =
154 UserBuilder.insert(%{nickname: "user__test", ap_id: "http://foo.com/user__test"})
155
156 text = "**hello world**\n\n*another @user__test and @user__test google.com paragraph*"
157
158 expected =
159 "<p><strong>hello world</strong></p>\n<p><em>another <span class=\"h-card\"><a data-user=\"#{
160 user.id
161 }\" class=\"u-url mention\" href=\"http://foo.com/user__test\">@<span>user__test</span></a></span> and <span class=\"h-card\"><a data-user=\"#{
162 user.id
163 }\" class=\"u-url mention\" href=\"http://foo.com/user__test\">@<span>user__test</span></a></span> <a href=\"http://google.com\">google.com</a> paragraph</em></p>\n"
164
165 {output, _, _} = Utils.format_input(text, "text/markdown")
166
167 assert output == expected
168 end
169 end
170
171 describe "context_to_conversation_id" do
172 test "creates a mapping object" do
173 conversation_id = Utils.context_to_conversation_id("random context")
174 object = Object.get_by_ap_id("random context")
175
176 assert conversation_id == object.id
177 end
178
179 test "returns an existing mapping for an existing object" do
180 {:ok, object} = Object.context_mapping("random context") |> Repo.insert()
181 conversation_id = Utils.context_to_conversation_id("random context")
182
183 assert conversation_id == object.id
184 end
185 end
186
187 describe "formats date to asctime" do
188 test "when date is in ISO 8601 format" do
189 date = DateTime.utc_now() |> DateTime.to_iso8601()
190
191 expected =
192 date
193 |> DateTime.from_iso8601()
194 |> elem(1)
195 |> Calendar.Strftime.strftime!("%a %b %d %H:%M:%S %z %Y")
196
197 assert Utils.date_to_asctime(date) == expected
198 end
199
200 test "when date is a binary in wrong format" do
201 date = DateTime.utc_now()
202
203 expected = ""
204
205 assert Utils.date_to_asctime(date) == expected
206 end
207
208 test "when date is a Unix timestamp" do
209 date = DateTime.utc_now() |> DateTime.to_unix()
210
211 expected = ""
212
213 assert Utils.date_to_asctime(date) == expected
214 end
215
216 test "when date is nil" do
217 expected = ""
218
219 assert Utils.date_to_asctime(nil) == expected
220 end
221 end
222
223 describe "get_to_and_cc" do
224 test "for public posts, not a reply" do
225 user = insert(:user)
226 mentioned_user = insert(:user)
227 mentions = [mentioned_user.ap_id]
228
229 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "public")
230
231 assert length(to) == 2
232 assert length(cc) == 1
233
234 assert @public_address in to
235 assert mentioned_user.ap_id in to
236 assert user.follower_address in cc
237 end
238
239 test "for public posts, a reply" do
240 user = insert(:user)
241 mentioned_user = insert(:user)
242 third_user = insert(:user)
243 {:ok, activity} = CommonAPI.post(third_user, %{"status" => "uguu"})
244 mentions = [mentioned_user.ap_id]
245
246 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "public")
247
248 assert length(to) == 3
249 assert length(cc) == 1
250
251 assert @public_address in to
252 assert mentioned_user.ap_id in to
253 assert third_user.ap_id in to
254 assert user.follower_address in cc
255 end
256
257 test "for unlisted posts, not a reply" do
258 user = insert(:user)
259 mentioned_user = insert(:user)
260 mentions = [mentioned_user.ap_id]
261
262 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "unlisted")
263
264 assert length(to) == 2
265 assert length(cc) == 1
266
267 assert @public_address in cc
268 assert mentioned_user.ap_id in to
269 assert user.follower_address in to
270 end
271
272 test "for unlisted posts, a reply" do
273 user = insert(:user)
274 mentioned_user = insert(:user)
275 third_user = insert(:user)
276 {:ok, activity} = CommonAPI.post(third_user, %{"status" => "uguu"})
277 mentions = [mentioned_user.ap_id]
278
279 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "unlisted")
280
281 assert length(to) == 3
282 assert length(cc) == 1
283
284 assert @public_address in cc
285 assert mentioned_user.ap_id in to
286 assert third_user.ap_id in to
287 assert user.follower_address in to
288 end
289
290 test "for private posts, not a reply" do
291 user = insert(:user)
292 mentioned_user = insert(:user)
293 mentions = [mentioned_user.ap_id]
294
295 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "private")
296
297 assert length(to) == 2
298 assert length(cc) == 0
299
300 assert mentioned_user.ap_id in to
301 assert user.follower_address in to
302 end
303
304 test "for private posts, a reply" do
305 user = insert(:user)
306 mentioned_user = insert(:user)
307 third_user = insert(:user)
308 {:ok, activity} = CommonAPI.post(third_user, %{"status" => "uguu"})
309 mentions = [mentioned_user.ap_id]
310
311 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "private")
312
313 assert length(to) == 3
314 assert length(cc) == 0
315
316 assert mentioned_user.ap_id in to
317 assert third_user.ap_id in to
318 assert user.follower_address in to
319 end
320
321 test "for direct posts, not a reply" do
322 user = insert(:user)
323 mentioned_user = insert(:user)
324 mentions = [mentioned_user.ap_id]
325
326 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "direct")
327
328 assert length(to) == 1
329 assert length(cc) == 0
330
331 assert mentioned_user.ap_id in to
332 end
333
334 test "for direct posts, a reply" do
335 user = insert(:user)
336 mentioned_user = insert(:user)
337 third_user = insert(:user)
338 {:ok, activity} = CommonAPI.post(third_user, %{"status" => "uguu"})
339 mentions = [mentioned_user.ap_id]
340
341 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "direct")
342
343 assert length(to) == 2
344 assert length(cc) == 0
345
346 assert mentioned_user.ap_id in to
347 assert third_user.ap_id in to
348 end
349 end
350 end