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