Merge remote-tracking branch 'upstream/develop' into aliases
[akkoma] / test / pleroma / web / common_api / utils_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 use Pleroma.DataCase
11
12 import ExUnit.CaptureLog
13 import Pleroma.Factory
14
15 @public_address "https://www.w3.org/ns/activitystreams#Public"
16
17 describe "add_attachments/2" do
18 setup do
19 name =
20 "Sakura Mana – Turned on by a Senior OL with a Temptating Tight Skirt-s Full Hipline and Panty Shot- Beautiful Thick Thighs- and Erotic Ass- -2015- -- Oppaitime 8-28-2017 6-50-33 PM.png"
21
22 attachment = %{
23 "url" => [%{"href" => URI.encode(name)}]
24 }
25
26 %{name: name, attachment: attachment}
27 end
28
29 test "it adds attachment links to a given text and attachment set", %{
30 name: name,
31 attachment: attachment
32 } do
33 len = 10
34 clear_config([Pleroma.Upload, :filename_display_max_length], len)
35
36 expected =
37 "<br><a href=\"#{URI.encode(name)}\" class='attachment'>#{String.slice(name, 0..len)}…</a>"
38
39 assert Utils.add_attachments("", [attachment]) == expected
40 end
41
42 test "doesn't truncate file name if config for truncate is set to 0", %{
43 name: name,
44 attachment: attachment
45 } do
46 clear_config([Pleroma.Upload, :filename_display_max_length], 0)
47
48 expected = "<br><a href=\"#{URI.encode(name)}\" class='attachment'>#{name}</a>"
49
50 assert Utils.add_attachments("", [attachment]) == expected
51 end
52 end
53
54 describe "it confirms the password given is the current users password" do
55 test "incorrect password given" do
56 {:ok, user} = UserBuilder.insert()
57
58 assert Utils.confirm_current_password(user, "") == {:error, "Invalid password."}
59 end
60
61 test "correct password given" do
62 {:ok, user} = UserBuilder.insert()
63 assert Utils.confirm_current_password(user, "test") == {:ok, user}
64 end
65 end
66
67 describe "format_input/3" do
68 test "works for bare text/plain" do
69 text = "hello world!"
70 expected = "hello world!"
71
72 {output, [], []} = Utils.format_input(text, "text/plain")
73
74 assert output == expected
75
76 text = "hello world!\n\nsecond paragraph!"
77 expected = "hello world!<br><br>second paragraph!"
78
79 {output, [], []} = Utils.format_input(text, "text/plain")
80
81 assert output == expected
82 end
83
84 test "works for bare text/html" do
85 text = "<p>hello world!</p>"
86 expected = "<p>hello world!</p>"
87
88 {output, [], []} = Utils.format_input(text, "text/html")
89
90 assert output == expected
91
92 text = "<p>hello world!</p><br/>\n<p>second paragraph</p>"
93 expected = "<p>hello world!</p><br/>\n<p>second paragraph</p>"
94
95 {output, [], []} = Utils.format_input(text, "text/html")
96
97 assert output == expected
98 end
99
100 test "works for bare text/markdown" do
101 text = "**hello world**"
102 expected = "<p><strong>hello world</strong></p>"
103
104 {output, [], []} = Utils.format_input(text, "text/markdown")
105
106 assert output == expected
107
108 text = "**hello world**\n\n*another paragraph*"
109 expected = "<p><strong>hello world</strong></p><p><em>another paragraph</em></p>"
110
111 {output, [], []} = Utils.format_input(text, "text/markdown")
112
113 assert output == expected
114
115 text = """
116 > cool quote
117
118 by someone
119 """
120
121 expected = "<blockquote><p>cool quote</p></blockquote><p>by someone</p>"
122
123 {output, [], []} = Utils.format_input(text, "text/markdown")
124
125 assert output == expected
126 end
127
128 test "works for bare text/bbcode" do
129 text = "[b]hello world[/b]"
130 expected = "<strong>hello world</strong>"
131
132 {output, [], []} = Utils.format_input(text, "text/bbcode")
133
134 assert output == expected
135
136 text = "[b]hello world![/b]\n\nsecond paragraph!"
137 expected = "<strong>hello world!</strong><br><br>second paragraph!"
138
139 {output, [], []} = Utils.format_input(text, "text/bbcode")
140
141 assert output == expected
142
143 text = "[b]hello world![/b]\n\n<strong>second paragraph!</strong>"
144
145 expected =
146 "<strong>hello world!</strong><br><br>&lt;strong&gt;second paragraph!&lt;/strong&gt;"
147
148 {output, [], []} = Utils.format_input(text, "text/bbcode")
149
150 assert output == expected
151 end
152
153 test "works for text/markdown with mentions" do
154 {:ok, user} =
155 UserBuilder.insert(%{nickname: "user__test", ap_id: "http://foo.com/user__test"})
156
157 text = "**hello world**\n\n*another @user__test and @user__test google.com paragraph*"
158
159 {output, _, _} = Utils.format_input(text, "text/markdown")
160
161 assert output ==
162 ~s(<p><strong>hello world</strong></p><p><em>another <span class="h-card"><a class="u-url mention" data-user="#{
163 user.id
164 }" href="http://foo.com/user__test" rel="ugc">@<span>user__test</span></a></span> and <span class="h-card"><a class="u-url mention" data-user="#{
165 user.id
166 }" href="http://foo.com/user__test" rel="ugc">@<span>user__test</span></a></span> <a href="http://google.com" rel="ugc">google.com</a> paragraph</em></p>)
167 end
168 end
169
170 describe "context_to_conversation_id" do
171 test "creates a mapping object" do
172 conversation_id = Utils.context_to_conversation_id("random context")
173 object = Object.get_by_ap_id("random context")
174
175 assert conversation_id == object.id
176 end
177
178 test "returns an existing mapping for an existing object" do
179 {:ok, object} = Object.context_mapping("random context") |> Repo.insert()
180 conversation_id = Utils.context_to_conversation_id("random context")
181
182 assert conversation_id == object.id
183 end
184 end
185
186 describe "formats date to asctime" do
187 test "when date is in ISO 8601 format" do
188 date = DateTime.utc_now() |> DateTime.to_iso8601()
189
190 expected =
191 date
192 |> DateTime.from_iso8601()
193 |> elem(1)
194 |> Calendar.Strftime.strftime!("%a %b %d %H:%M:%S %z %Y")
195
196 assert Utils.date_to_asctime(date) == expected
197 end
198
199 test "when date is a binary in wrong format" do
200 date = DateTime.utc_now()
201
202 expected = ""
203
204 assert capture_log(fn ->
205 assert Utils.date_to_asctime(date) == expected
206 end) =~ "[warn] Date #{date} in wrong format, must be ISO 8601"
207 end
208
209 test "when date is a Unix timestamp" do
210 date = DateTime.utc_now() |> DateTime.to_unix()
211
212 expected = ""
213
214 assert capture_log(fn ->
215 assert Utils.date_to_asctime(date) == expected
216 end) =~ "[warn] Date #{date} in wrong format, must be ISO 8601"
217 end
218
219 test "when date is nil" do
220 expected = ""
221
222 assert capture_log(fn ->
223 assert Utils.date_to_asctime(nil) == expected
224 end) =~ "[warn] Date in wrong format, must be ISO 8601"
225 end
226
227 test "when date is a random string" do
228 assert capture_log(fn ->
229 assert Utils.date_to_asctime("foo") == ""
230 end) =~ "[warn] Date foo in wrong format, must be ISO 8601"
231 end
232 end
233
234 describe "get_to_and_cc" do
235 test "for public posts, not a reply" do
236 user = insert(:user)
237 mentioned_user = insert(:user)
238 mentions = [mentioned_user.ap_id]
239
240 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "public", nil)
241
242 assert length(to) == 2
243 assert length(cc) == 1
244
245 assert @public_address in to
246 assert mentioned_user.ap_id in to
247 assert user.follower_address in cc
248 end
249
250 test "for public posts, a reply" do
251 user = insert(:user)
252 mentioned_user = insert(:user)
253 third_user = insert(:user)
254 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
255 mentions = [mentioned_user.ap_id]
256
257 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "public", nil)
258
259 assert length(to) == 3
260 assert length(cc) == 1
261
262 assert @public_address in to
263 assert mentioned_user.ap_id in to
264 assert third_user.ap_id in to
265 assert user.follower_address in cc
266 end
267
268 test "for unlisted posts, not a reply" do
269 user = insert(:user)
270 mentioned_user = insert(:user)
271 mentions = [mentioned_user.ap_id]
272
273 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "unlisted", nil)
274
275 assert length(to) == 2
276 assert length(cc) == 1
277
278 assert @public_address in cc
279 assert mentioned_user.ap_id in to
280 assert user.follower_address in to
281 end
282
283 test "for unlisted posts, a reply" do
284 user = insert(:user)
285 mentioned_user = insert(:user)
286 third_user = insert(:user)
287 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
288 mentions = [mentioned_user.ap_id]
289
290 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "unlisted", nil)
291
292 assert length(to) == 3
293 assert length(cc) == 1
294
295 assert @public_address in cc
296 assert mentioned_user.ap_id in to
297 assert third_user.ap_id in to
298 assert user.follower_address in to
299 end
300
301 test "for private posts, not a reply" do
302 user = insert(:user)
303 mentioned_user = insert(:user)
304 mentions = [mentioned_user.ap_id]
305
306 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "private", nil)
307 assert length(to) == 2
308 assert Enum.empty?(cc)
309
310 assert mentioned_user.ap_id in to
311 assert user.follower_address in to
312 end
313
314 test "for private posts, a reply" do
315 user = insert(:user)
316 mentioned_user = insert(:user)
317 third_user = insert(:user)
318 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
319 mentions = [mentioned_user.ap_id]
320
321 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "private", nil)
322
323 assert length(to) == 2
324 assert Enum.empty?(cc)
325
326 assert mentioned_user.ap_id in to
327 assert user.follower_address in to
328 end
329
330 test "for direct posts, not a reply" do
331 user = insert(:user)
332 mentioned_user = insert(:user)
333 mentions = [mentioned_user.ap_id]
334
335 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "direct", nil)
336
337 assert length(to) == 1
338 assert Enum.empty?(cc)
339
340 assert mentioned_user.ap_id in to
341 end
342
343 test "for direct posts, a reply" do
344 user = insert(:user)
345 mentioned_user = insert(:user)
346 third_user = insert(:user)
347 {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
348 mentions = [mentioned_user.ap_id]
349
350 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "direct", nil)
351
352 assert length(to) == 1
353 assert Enum.empty?(cc)
354
355 assert mentioned_user.ap_id in to
356
357 {:ok, direct_activity} = CommonAPI.post(third_user, %{status: "uguu", visibility: "direct"})
358
359 {to, cc} = Utils.get_to_and_cc(user, mentions, direct_activity, "direct", nil)
360
361 assert length(to) == 2
362 assert Enum.empty?(cc)
363
364 assert mentioned_user.ap_id in to
365 assert third_user.ap_id in to
366 end
367 end
368
369 describe "to_master_date/1" do
370 test "removes microseconds from date (NaiveDateTime)" do
371 assert Utils.to_masto_date(~N[2015-01-23 23:50:07.123]) == "2015-01-23T23:50:07.000Z"
372 end
373
374 test "removes microseconds from date (String)" do
375 assert Utils.to_masto_date("2015-01-23T23:50:07.123Z") == "2015-01-23T23:50:07.000Z"
376 end
377
378 test "returns empty string when date invalid" do
379 assert Utils.to_masto_date("2015-01?23T23:50:07.123Z") == ""
380 end
381 end
382
383 describe "conversation_id_to_context/1" do
384 test "returns id" do
385 object = insert(:note)
386 assert Utils.conversation_id_to_context(object.id) == object.data["id"]
387 end
388
389 test "returns error if object not found" do
390 assert Utils.conversation_id_to_context("123") == {:error, "No such conversation"}
391 end
392 end
393
394 describe "maybe_notify_mentioned_recipients/2" do
395 test "returns recipients when activity is not `Create`" do
396 activity = insert(:like_activity)
397 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == ["test"]
398 end
399
400 test "returns recipients from tag" do
401 user = insert(:user)
402
403 object =
404 insert(:note,
405 user: user,
406 data: %{
407 "tag" => [
408 %{"type" => "Hashtag"},
409 "",
410 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
411 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
412 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
413 ]
414 }
415 )
416
417 activity = insert(:note_activity, user: user, note: object)
418
419 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
420 "test",
421 "https://testing.pleroma.lol/users/lain",
422 "https://shitposter.club/user/5381"
423 ]
424 end
425
426 test "returns recipients when object is map" do
427 user = insert(:user)
428 object = insert(:note, user: user)
429
430 activity =
431 insert(:note_activity,
432 user: user,
433 note: object,
434 data_attrs: %{
435 "object" => %{
436 "tag" => [
437 %{"type" => "Hashtag"},
438 "",
439 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
440 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
441 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
442 ]
443 }
444 }
445 )
446
447 Pleroma.Repo.delete(object)
448
449 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
450 "test",
451 "https://testing.pleroma.lol/users/lain",
452 "https://shitposter.club/user/5381"
453 ]
454 end
455
456 test "returns recipients when object not found" do
457 user = insert(:user)
458 object = insert(:note, user: user)
459
460 activity = insert(:note_activity, user: user, note: object)
461 Pleroma.Repo.delete(object)
462
463 obj_url = activity.data["object"]
464
465 Tesla.Mock.mock(fn
466 %{method: :get, url: ^obj_url} ->
467 %Tesla.Env{status: 404, body: ""}
468 end)
469
470 assert Utils.maybe_notify_mentioned_recipients(["test-test"], activity) == [
471 "test-test"
472 ]
473 end
474 end
475
476 describe "attachments_from_ids_descs/2" do
477 test "returns [] when attachment ids is empty" do
478 assert Utils.attachments_from_ids_descs([], "{}") == []
479 end
480
481 test "returns list attachments with desc" do
482 object = insert(:note)
483 desc = Jason.encode!(%{object.id => "test-desc"})
484
485 assert Utils.attachments_from_ids_descs(["#{object.id}", "34"], desc) == [
486 Map.merge(object.data, %{"name" => "test-desc"})
487 ]
488 end
489 end
490
491 describe "attachments_from_ids/1" do
492 test "returns attachments with descs" do
493 object = insert(:note)
494 desc = Jason.encode!(%{object.id => "test-desc"})
495
496 assert Utils.attachments_from_ids(%{
497 media_ids: ["#{object.id}"],
498 descriptions: desc
499 }) == [
500 Map.merge(object.data, %{"name" => "test-desc"})
501 ]
502 end
503
504 test "returns attachments without descs" do
505 object = insert(:note)
506 assert Utils.attachments_from_ids(%{media_ids: ["#{object.id}"]}) == [object.data]
507 end
508
509 test "returns [] when not pass media_ids" do
510 assert Utils.attachments_from_ids(%{}) == []
511 end
512 end
513
514 describe "maybe_add_list_data/3" do
515 test "adds list params when found user list" do
516 user = insert(:user)
517 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
518
519 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
520 %{
521 additional: %{"bcc" => [list.ap_id], "listMessage" => list.ap_id},
522 object: %{"listMessage" => list.ap_id}
523 }
524 end
525
526 test "returns original params when list not found" do
527 user = insert(:user)
528 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", insert(:user))
529
530 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
531 %{additional: %{}, object: %{}}
532 end
533 end
534
535 describe "make_note_data/11" do
536 test "returns note data" do
537 user = insert(:user)
538 note = insert(:note)
539 user2 = insert(:user)
540 user3 = insert(:user)
541
542 assert Utils.make_note_data(
543 user.ap_id,
544 [user2.ap_id],
545 "2hu",
546 "<h1>This is :moominmamma: note</h1>",
547 [],
548 note.id,
549 [name: "jimm"],
550 "test summary",
551 [user3.ap_id],
552 false,
553 %{"custom_tag" => "test"}
554 ) == %{
555 "actor" => user.ap_id,
556 "attachment" => [],
557 "cc" => [user3.ap_id],
558 "content" => "<h1>This is :moominmamma: note</h1>",
559 "context" => "2hu",
560 "sensitive" => false,
561 "summary" => "test summary",
562 "tag" => ["jimm"],
563 "to" => [user2.ap_id],
564 "type" => "Note",
565 "custom_tag" => "test"
566 }
567 end
568 end
569
570 describe "maybe_add_attachments/3" do
571 test "returns parsed results when attachment_links is false" do
572 assert Utils.maybe_add_attachments(
573 {"test", [], ["tags"]},
574 [],
575 false
576 ) == {"test", [], ["tags"]}
577 end
578
579 test "adds attachments to parsed results" do
580 attachment = %{"url" => [%{"href" => "SakuraPM.png"}]}
581
582 assert Utils.maybe_add_attachments(
583 {"test", [], ["tags"]},
584 [attachment],
585 true
586 ) == {
587 "test<br><a href=\"SakuraPM.png\" class='attachment'>SakuraPM.png</a>",
588 [],
589 ["tags"]
590 }
591 end
592 end
593 end