[#2332] Misc. improvements per code change requests.
[akkoma] / test / web / common_api / 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 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><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 data-user="#{
163 user.id
164 }" class="u-url mention" href="http://foo.com/user__test" rel="ugc">@<span>user__test</span></a></span> and <span class="h-card"><a data-user="#{
165 user.id
166 }" class="u-url mention" 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) == 3
324 assert Enum.empty?(cc)
325
326 assert mentioned_user.ap_id in to
327 assert third_user.ap_id in to
328 assert user.follower_address in to
329 end
330
331 test "for direct posts, not a reply" do
332 user = insert(:user)
333 mentioned_user = insert(:user)
334 mentions = [mentioned_user.ap_id]
335
336 {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "direct", nil)
337
338 assert length(to) == 1
339 assert Enum.empty?(cc)
340
341 assert mentioned_user.ap_id in to
342 end
343
344 test "for direct posts, a reply" do
345 user = insert(:user)
346 mentioned_user = insert(:user)
347 third_user = insert(:user)
348 {:ok, activity} = CommonAPI.post(third_user, %{"status" => "uguu"})
349 mentions = [mentioned_user.ap_id]
350
351 {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "direct", nil)
352
353 assert length(to) == 2
354 assert Enum.empty?(cc)
355
356 assert mentioned_user.ap_id in to
357 assert third_user.ap_id in to
358 end
359 end
360
361 describe "get_by_id_or_ap_id/1" do
362 test "get activity by id" do
363 activity = insert(:note_activity)
364 %Pleroma.Activity{} = note = Utils.get_by_id_or_ap_id(activity.id)
365 assert note.id == activity.id
366 end
367
368 test "get activity by ap_id" do
369 activity = insert(:note_activity)
370 %Pleroma.Activity{} = note = Utils.get_by_id_or_ap_id(activity.data["object"])
371 assert note.id == activity.id
372 end
373
374 test "get activity by object when type isn't `Create` " do
375 activity = insert(:like_activity)
376 %Pleroma.Activity{} = like = Utils.get_by_id_or_ap_id(activity.id)
377 assert like.data["object"] == activity.data["object"]
378 end
379 end
380
381 describe "to_master_date/1" do
382 test "removes microseconds from date (NaiveDateTime)" do
383 assert Utils.to_masto_date(~N[2015-01-23 23:50:07.123]) == "2015-01-23T23:50:07.000Z"
384 end
385
386 test "removes microseconds from date (String)" do
387 assert Utils.to_masto_date("2015-01-23T23:50:07.123Z") == "2015-01-23T23:50:07.000Z"
388 end
389
390 test "returns empty string when date invalid" do
391 assert Utils.to_masto_date("2015-01?23T23:50:07.123Z") == ""
392 end
393 end
394
395 describe "conversation_id_to_context/1" do
396 test "returns id" do
397 object = insert(:note)
398 assert Utils.conversation_id_to_context(object.id) == object.data["id"]
399 end
400
401 test "returns error if object not found" do
402 assert Utils.conversation_id_to_context("123") == {:error, "No such conversation"}
403 end
404 end
405
406 describe "maybe_notify_mentioned_recipients/2" do
407 test "returns recipients when activity is not `Create`" do
408 activity = insert(:like_activity)
409 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == ["test"]
410 end
411
412 test "returns recipients from tag" do
413 user = insert(:user)
414
415 object =
416 insert(:note,
417 user: user,
418 data: %{
419 "tag" => [
420 %{"type" => "Hashtag"},
421 "",
422 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
423 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
424 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
425 ]
426 }
427 )
428
429 activity = insert(:note_activity, user: user, note: object)
430
431 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
432 "test",
433 "https://testing.pleroma.lol/users/lain",
434 "https://shitposter.club/user/5381"
435 ]
436 end
437
438 test "returns recipients when object is map" do
439 user = insert(:user)
440 object = insert(:note, user: user)
441
442 activity =
443 insert(:note_activity,
444 user: user,
445 note: object,
446 data_attrs: %{
447 "object" => %{
448 "tag" => [
449 %{"type" => "Hashtag"},
450 "",
451 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
452 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
453 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
454 ]
455 }
456 }
457 )
458
459 Pleroma.Repo.delete(object)
460
461 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
462 "test",
463 "https://testing.pleroma.lol/users/lain",
464 "https://shitposter.club/user/5381"
465 ]
466 end
467
468 test "returns recipients when object not found" do
469 user = insert(:user)
470 object = insert(:note, user: user)
471
472 activity = insert(:note_activity, user: user, note: object)
473 Pleroma.Repo.delete(object)
474
475 assert Utils.maybe_notify_mentioned_recipients(["test-test"], activity) == [
476 "test-test"
477 ]
478 end
479 end
480
481 describe "attachments_from_ids_descs/2" do
482 test "returns [] when attachment ids is empty" do
483 assert Utils.attachments_from_ids_descs([], "{}") == []
484 end
485
486 test "returns list attachments with desc" do
487 object = insert(:note)
488 desc = Jason.encode!(%{object.id => "test-desc"})
489
490 assert Utils.attachments_from_ids_descs(["#{object.id}", "34"], desc) == [
491 Map.merge(object.data, %{"name" => "test-desc"})
492 ]
493 end
494 end
495
496 describe "attachments_from_ids/1" do
497 test "returns attachments with descs" do
498 object = insert(:note)
499 desc = Jason.encode!(%{object.id => "test-desc"})
500
501 assert Utils.attachments_from_ids(%{
502 "media_ids" => ["#{object.id}"],
503 "descriptions" => desc
504 }) == [
505 Map.merge(object.data, %{"name" => "test-desc"})
506 ]
507 end
508
509 test "returns attachments without descs" do
510 object = insert(:note)
511 assert Utils.attachments_from_ids(%{"media_ids" => ["#{object.id}"]}) == [object.data]
512 end
513
514 test "returns [] when not pass media_ids" do
515 assert Utils.attachments_from_ids(%{}) == []
516 end
517 end
518
519 describe "maybe_add_list_data/3" do
520 test "adds list params when found user list" do
521 user = insert(:user)
522 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
523
524 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
525 %{
526 additional: %{"bcc" => [list.ap_id], "listMessage" => list.ap_id},
527 object: %{"listMessage" => list.ap_id}
528 }
529 end
530
531 test "returns original params when list not found" do
532 user = insert(:user)
533 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", insert(:user))
534
535 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
536 %{additional: %{}, object: %{}}
537 end
538 end
539
540 describe "make_note_data/11" do
541 test "returns note data" do
542 user = insert(:user)
543 note = insert(:note)
544 user2 = insert(:user)
545 user3 = insert(:user)
546
547 assert Utils.make_note_data(
548 user.ap_id,
549 [user2.ap_id],
550 "2hu",
551 "<h1>This is :moominmamma: note</h1>",
552 [],
553 note.id,
554 [name: "jimm"],
555 "test summary",
556 [user3.ap_id],
557 false,
558 %{"custom_tag" => "test"}
559 ) == %{
560 "actor" => user.ap_id,
561 "attachment" => [],
562 "cc" => [user3.ap_id],
563 "content" => "<h1>This is :moominmamma: note</h1>",
564 "context" => "2hu",
565 "sensitive" => false,
566 "summary" => "test summary",
567 "tag" => ["jimm"],
568 "to" => [user2.ap_id],
569 "type" => "Note",
570 "custom_tag" => "test"
571 }
572 end
573 end
574
575 describe "maybe_add_attachments/3" do
576 test "returns parsed results when attachment_links is false" do
577 assert Utils.maybe_add_attachments(
578 {"test", [], ["tags"]},
579 [],
580 false
581 ) == {"test", [], ["tags"]}
582 end
583
584 test "adds attachments to parsed results" do
585 attachment = %{"url" => [%{"href" => "SakuraPM.png"}]}
586
587 assert Utils.maybe_add_attachments(
588 {"test", [], ["tags"]},
589 [attachment],
590 true
591 ) == {
592 "test<br><a href=\"SakuraPM.png\" class='attachment'>SakuraPM.png</a>",
593 [],
594 ["tags"]
595 }
596 end
597 end
598 end