Merge branch 'length-limit-bio' into 'develop'
[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 ExUnit.CaptureLog
14 import Pleroma.Factory
15
16 @public_address "https://www.w3.org/ns/activitystreams#Public"
17
18 test "it adds attachment links to a given text and attachment set" do
19 name =
20 "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"
21
22 attachment = %{
23 "url" => [%{"href" => name}]
24 }
25
26 res = Utils.add_attachments("", [attachment])
27
28 assert res ==
29 "<br><a href=\"#{name}\" class='attachment'>Sakura Mana – Turned on by a Se…</a>"
30 end
31
32 describe "it confirms the password given is the current users password" do
33 test "incorrect password given" do
34 {:ok, user} = UserBuilder.insert()
35
36 assert Utils.confirm_current_password(user, "") == {:error, "Invalid password."}
37 end
38
39 test "correct password given" do
40 {:ok, user} = UserBuilder.insert()
41 assert Utils.confirm_current_password(user, "test") == {:ok, user}
42 end
43 end
44
45 test "parses emoji from name and bio" do
46 {:ok, user} = UserBuilder.insert(%{name: ":blank:", bio: ":firefox:"})
47
48 expected = [
49 %{
50 "type" => "Emoji",
51 "icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}/emoji/Firefox.gif"},
52 "name" => ":firefox:"
53 },
54 %{
55 "type" => "Emoji",
56 "icon" => %{
57 "type" => "Image",
58 "url" => "#{Endpoint.url()}/emoji/blank.png"
59 },
60 "name" => ":blank:"
61 }
62 ]
63
64 assert expected == Utils.emoji_from_profile(user)
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>\n\n<p>second paragraph</p>"
93 expected = "<p>hello world!</p>\n\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>\n"
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>\n<p><em>another paragraph</em></p>\n"
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>\n</blockquote>\n<p>by someone</p>\n"
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>\n<br>\nsecond 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>\n<br>\n&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 expected =
160 "<p><strong>hello world</strong></p>\n<p><em>another <span class=\"h-card\"><a data-user=\"#{
161 user.id
162 }\" class=\"u-url mention\" href=\"http://foo.com/user__test\">@<span>user__test</span></a></span> and <span class=\"h-card\"><a data-user=\"#{
163 user.id
164 }\" 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"
165
166 {output, _, _} = Utils.format_input(text, "text/markdown")
167
168 assert output == expected
169 end
170 end
171
172 describe "context_to_conversation_id" do
173 test "creates a mapping object" do
174 conversation_id = Utils.context_to_conversation_id("random context")
175 object = Object.get_by_ap_id("random context")
176
177 assert conversation_id == object.id
178 end
179
180 test "returns an existing mapping for an existing object" do
181 {:ok, object} = Object.context_mapping("random context") |> Repo.insert()
182 conversation_id = Utils.context_to_conversation_id("random context")
183
184 assert conversation_id == object.id
185 end
186 end
187
188 describe "formats date to asctime" do
189 test "when date is in ISO 8601 format" do
190 date = DateTime.utc_now() |> DateTime.to_iso8601()
191
192 expected =
193 date
194 |> DateTime.from_iso8601()
195 |> elem(1)
196 |> Calendar.Strftime.strftime!("%a %b %d %H:%M:%S %z %Y")
197
198 assert Utils.date_to_asctime(date) == expected
199 end
200
201 test "when date is a binary in wrong format" do
202 date = DateTime.utc_now()
203
204 expected = ""
205
206 assert capture_log(fn ->
207 assert Utils.date_to_asctime(date) == expected
208 end) =~ "[warn] Date #{date} in wrong format, must be ISO 8601"
209 end
210
211 test "when date is a Unix timestamp" do
212 date = DateTime.utc_now() |> DateTime.to_unix()
213
214 expected = ""
215
216 assert capture_log(fn ->
217 assert Utils.date_to_asctime(date) == expected
218 end) =~ "[warn] Date #{date} in wrong format, must be ISO 8601"
219 end
220
221 test "when date is nil" do
222 expected = ""
223
224 assert capture_log(fn ->
225 assert Utils.date_to_asctime(nil) == expected
226 end) =~ "[warn] Date in wrong format, must be ISO 8601"
227 end
228
229 test "when date is a random string" do
230 assert capture_log(fn ->
231 assert Utils.date_to_asctime("foo") == ""
232 end) =~ "[warn] Date foo in wrong format, must be ISO 8601"
233 end
234 end
235
236 describe "get_to_and_cc" do
237 test "for public posts, not a reply" do
238 user = insert(:user)
239 mentioned_user = insert(:user)
240 mentions = [mentioned_user.ap_id]
241
242 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "public")
243
244 assert length(to) == 2
245 assert length(cc) == 1
246
247 assert @public_address in to
248 assert mentioned_user.ap_id in to
249 assert user.follower_address in cc
250 end
251
252 test "for public posts, a reply" do
253 user = insert(:user)
254 mentioned_user = insert(:user)
255 third_user = insert(:user)
256 {:ok, activity} = CommonAPI.post(third_user, %{"status" => "uguu"})
257 mentions = [mentioned_user.ap_id]
258
259 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "public")
260
261 assert length(to) == 3
262 assert length(cc) == 1
263
264 assert @public_address in to
265 assert mentioned_user.ap_id in to
266 assert third_user.ap_id in to
267 assert user.follower_address in cc
268 end
269
270 test "for unlisted posts, not a reply" do
271 user = insert(:user)
272 mentioned_user = insert(:user)
273 mentions = [mentioned_user.ap_id]
274
275 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "unlisted")
276
277 assert length(to) == 2
278 assert length(cc) == 1
279
280 assert @public_address in cc
281 assert mentioned_user.ap_id in to
282 assert user.follower_address in to
283 end
284
285 test "for unlisted posts, a reply" do
286 user = insert(:user)
287 mentioned_user = insert(:user)
288 third_user = insert(:user)
289 {:ok, activity} = CommonAPI.post(third_user, %{"status" => "uguu"})
290 mentions = [mentioned_user.ap_id]
291
292 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "unlisted")
293
294 assert length(to) == 3
295 assert length(cc) == 1
296
297 assert @public_address in cc
298 assert mentioned_user.ap_id in to
299 assert third_user.ap_id in to
300 assert user.follower_address in to
301 end
302
303 test "for private posts, not a reply" do
304 user = insert(:user)
305 mentioned_user = insert(:user)
306 mentions = [mentioned_user.ap_id]
307
308 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "private")
309 assert length(to) == 2
310 assert length(cc) == 0
311
312 assert mentioned_user.ap_id in to
313 assert user.follower_address in to
314 end
315
316 test "for private posts, a reply" do
317 user = insert(:user)
318 mentioned_user = insert(:user)
319 third_user = insert(:user)
320 {:ok, activity} = CommonAPI.post(third_user, %{"status" => "uguu"})
321 mentions = [mentioned_user.ap_id]
322
323 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "private")
324
325 assert length(to) == 3
326 assert length(cc) == 0
327
328 assert mentioned_user.ap_id in to
329 assert third_user.ap_id in to
330 assert user.follower_address in to
331 end
332
333 test "for direct posts, not a reply" do
334 user = insert(:user)
335 mentioned_user = insert(:user)
336 mentions = [mentioned_user.ap_id]
337
338 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "direct")
339
340 assert length(to) == 1
341 assert length(cc) == 0
342
343 assert mentioned_user.ap_id in to
344 end
345
346 test "for direct posts, a reply" do
347 user = insert(:user)
348 mentioned_user = insert(:user)
349 third_user = insert(:user)
350 {:ok, activity} = CommonAPI.post(third_user, %{"status" => "uguu"})
351 mentions = [mentioned_user.ap_id]
352
353 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "direct")
354
355 assert length(to) == 2
356 assert length(cc) == 0
357
358 assert mentioned_user.ap_id in to
359 assert third_user.ap_id in to
360 end
361 end
362
363 describe "get_by_id_or_ap_id/1" do
364 test "get activity by id" do
365 activity = insert(:note_activity)
366 %Pleroma.Activity{} = note = Utils.get_by_id_or_ap_id(activity.id)
367 assert note.id == activity.id
368 end
369
370 test "get activity by ap_id" do
371 activity = insert(:note_activity)
372 %Pleroma.Activity{} = note = Utils.get_by_id_or_ap_id(activity.data["object"])
373 assert note.id == activity.id
374 end
375
376 test "get activity by object when type isn't `Create` " do
377 activity = insert(:like_activity)
378 %Pleroma.Activity{} = like = Utils.get_by_id_or_ap_id(activity.id)
379 assert like.data["object"] == activity.data["object"]
380 end
381 end
382
383 describe "to_master_date/1" do
384 test "removes microseconds from date (NaiveDateTime)" do
385 assert Utils.to_masto_date(~N[2015-01-23 23:50:07.123]) == "2015-01-23T23:50:07.000Z"
386 end
387
388 test "removes microseconds from date (String)" do
389 assert Utils.to_masto_date("2015-01-23T23:50:07.123Z") == "2015-01-23T23:50:07.000Z"
390 end
391
392 test "returns empty string when date invalid" do
393 assert Utils.to_masto_date("2015-01?23T23:50:07.123Z") == ""
394 end
395 end
396
397 describe "conversation_id_to_context/1" do
398 test "returns id" do
399 object = insert(:note)
400 assert Utils.conversation_id_to_context(object.id) == object.data["id"]
401 end
402
403 test "returns error if object not found" do
404 assert Utils.conversation_id_to_context("123") == {:error, "No such conversation"}
405 end
406 end
407
408 describe "maybe_notify_mentioned_recipients/2" do
409 test "returns recipients when activity is not `Create`" do
410 activity = insert(:like_activity)
411 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == ["test"]
412 end
413
414 test "returns recipients from tag" do
415 user = insert(:user)
416
417 object =
418 insert(:note,
419 user: user,
420 data: %{
421 "tag" => [
422 %{"type" => "Hashtag"},
423 "",
424 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
425 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
426 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
427 ]
428 }
429 )
430
431 activity = insert(:note_activity, user: user, note: object)
432
433 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
434 "test",
435 "https://testing.pleroma.lol/users/lain",
436 "https://shitposter.club/user/5381"
437 ]
438 end
439
440 test "returns recipients when object is map" do
441 user = insert(:user)
442 object = insert(:note, user: user)
443
444 activity =
445 insert(:note_activity,
446 user: user,
447 note: object,
448 data_attrs: %{
449 "object" => %{
450 "tag" => [
451 %{"type" => "Hashtag"},
452 "",
453 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
454 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
455 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
456 ]
457 }
458 }
459 )
460
461 Pleroma.Repo.delete(object)
462
463 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
464 "test",
465 "https://testing.pleroma.lol/users/lain",
466 "https://shitposter.club/user/5381"
467 ]
468 end
469
470 test "returns recipients when object not found" do
471 user = insert(:user)
472 object = insert(:note, user: user)
473
474 activity = insert(:note_activity, user: user, note: object)
475 Pleroma.Repo.delete(object)
476
477 assert Utils.maybe_notify_mentioned_recipients(["test-test"], activity) == [
478 "test-test"
479 ]
480 end
481 end
482
483 describe "attachments_from_ids_descs/2" do
484 test "returns [] when attachment ids is empty" do
485 assert Utils.attachments_from_ids_descs([], "{}") == []
486 end
487
488 test "returns list attachments with desc" do
489 object = insert(:note)
490 desc = Jason.encode!(%{object.id => "test-desc"})
491
492 assert Utils.attachments_from_ids_descs(["#{object.id}", "34"], desc) == [
493 Map.merge(object.data, %{"name" => "test-desc"})
494 ]
495 end
496 end
497
498 describe "attachments_from_ids/1" do
499 test "returns attachments with descs" do
500 object = insert(:note)
501 desc = Jason.encode!(%{object.id => "test-desc"})
502
503 assert Utils.attachments_from_ids(%{
504 "media_ids" => ["#{object.id}"],
505 "descriptions" => desc
506 }) == [
507 Map.merge(object.data, %{"name" => "test-desc"})
508 ]
509 end
510
511 test "returns attachments without descs" do
512 object = insert(:note)
513 assert Utils.attachments_from_ids(%{"media_ids" => ["#{object.id}"]}) == [object.data]
514 end
515
516 test "returns [] when not pass media_ids" do
517 assert Utils.attachments_from_ids(%{}) == []
518 end
519 end
520
521 describe "maybe_add_list_data/3" do
522 test "adds list params when found user list" do
523 user = insert(:user)
524 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
525
526 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
527 %{
528 additional: %{"bcc" => [list.ap_id], "listMessage" => list.ap_id},
529 object: %{"listMessage" => list.ap_id}
530 }
531 end
532
533 test "returns original params when list not found" do
534 user = insert(:user)
535 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", insert(:user))
536
537 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
538 %{additional: %{}, object: %{}}
539 end
540 end
541
542 describe "make_note_data/11" do
543 test "returns note data" do
544 user = insert(:user)
545 note = insert(:note)
546 user2 = insert(:user)
547 user3 = insert(:user)
548
549 assert Utils.make_note_data(
550 user.ap_id,
551 [user2.ap_id],
552 "2hu",
553 "<h1>This is :moominmamma: note</h1>",
554 [],
555 note.id,
556 [name: "jimm"],
557 "test summary",
558 [user3.ap_id],
559 false,
560 %{"custom_tag" => "test"}
561 ) == %{
562 "actor" => user.ap_id,
563 "attachment" => [],
564 "cc" => [user3.ap_id],
565 "content" => "<h1>This is :moominmamma: note</h1>",
566 "context" => "2hu",
567 "sensitive" => false,
568 "summary" => "test summary",
569 "tag" => ["jimm"],
570 "to" => [user2.ap_id],
571 "type" => "Note",
572 "custom_tag" => "test"
573 }
574 end
575 end
576
577 describe "maybe_add_attachments/3" do
578 test "returns parsed results when no_links is true" do
579 assert Utils.maybe_add_attachments(
580 {"test", [], ["tags"]},
581 [],
582 true
583 ) == {"test", [], ["tags"]}
584 end
585
586 test "adds attachments to parsed results" do
587 attachment = %{"url" => [%{"href" => "SakuraPM.png"}]}
588
589 assert Utils.maybe_add_attachments(
590 {"test", [], ["tags"]},
591 [attachment],
592 false
593 ) == {
594 "test<br><a href=\"SakuraPM.png\" class='attachment'>SakuraPM.png</a>",
595 [],
596 ["tags"]
597 }
598 end
599 end
600 end