Merge branch 'develop' into 'remove-twitter-api'
[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 "to_master_date/1" do
339 test "removes microseconds from date (NaiveDateTime)" do
340 assert Utils.to_masto_date(~N[2015-01-23 23:50:07.123]) == "2015-01-23T23:50:07.000Z"
341 end
342
343 test "removes microseconds from date (String)" do
344 assert Utils.to_masto_date("2015-01-23T23:50:07.123Z") == "2015-01-23T23:50:07.000Z"
345 end
346
347 test "returns empty string when date invalid" do
348 assert Utils.to_masto_date("2015-01?23T23:50:07.123Z") == ""
349 end
350 end
351
352 describe "conversation_id_to_context/1" do
353 test "returns id" do
354 object = insert(:note)
355 assert Utils.conversation_id_to_context(object.id) == object.data["id"]
356 end
357
358 test "returns error if object not found" do
359 assert Utils.conversation_id_to_context("123") == {:error, "No such conversation"}
360 end
361 end
362
363 describe "maybe_notify_mentioned_recipients/2" do
364 test "returns recipients when activity is not `Create`" do
365 activity = insert(:like_activity)
366 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == ["test"]
367 end
368
369 test "returns recipients from tag" do
370 user = insert(:user)
371
372 object =
373 insert(:note,
374 user: user,
375 data: %{
376 "tag" => [
377 %{"type" => "Hashtag"},
378 "",
379 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
380 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
381 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
382 ]
383 }
384 )
385
386 activity = insert(:note_activity, user: user, note: object)
387
388 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
389 "test",
390 "https://testing.pleroma.lol/users/lain",
391 "https://shitposter.club/user/5381"
392 ]
393 end
394
395 test "returns recipients when object is map" do
396 user = insert(:user)
397 object = insert(:note, user: user)
398
399 activity =
400 insert(:note_activity,
401 user: user,
402 note: object,
403 data_attrs: %{
404 "object" => %{
405 "tag" => [
406 %{"type" => "Hashtag"},
407 "",
408 %{"type" => "Mention", "href" => "https://testing.pleroma.lol/users/lain"},
409 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"},
410 %{"type" => "Mention", "href" => "https://shitposter.club/user/5381"}
411 ]
412 }
413 }
414 )
415
416 Pleroma.Repo.delete(object)
417
418 assert Utils.maybe_notify_mentioned_recipients(["test"], activity) == [
419 "test",
420 "https://testing.pleroma.lol/users/lain",
421 "https://shitposter.club/user/5381"
422 ]
423 end
424
425 test "returns recipients when object not found" do
426 user = insert(:user)
427 object = insert(:note, user: user)
428
429 activity = insert(:note_activity, user: user, note: object)
430 Pleroma.Repo.delete(object)
431
432 obj_url = activity.data["object"]
433
434 Tesla.Mock.mock(fn
435 %{method: :get, url: ^obj_url} ->
436 %Tesla.Env{status: 404, body: ""}
437 end)
438
439 assert Utils.maybe_notify_mentioned_recipients(["test-test"], activity) == [
440 "test-test"
441 ]
442 end
443 end
444
445 describe "attachments_from_ids_descs/2" do
446 test "returns [] when attachment ids is empty" do
447 assert Utils.attachments_from_ids_descs([], "{}") == []
448 end
449
450 test "returns list attachments with desc" do
451 object = insert(:note)
452 desc = Jason.encode!(%{object.id => "test-desc"})
453
454 assert Utils.attachments_from_ids_descs(["#{object.id}", "34"], desc) == [
455 Map.merge(object.data, %{"name" => "test-desc"})
456 ]
457 end
458 end
459
460 describe "attachments_from_ids/1" do
461 test "returns attachments with descs" do
462 object = insert(:note)
463 desc = Jason.encode!(%{object.id => "test-desc"})
464
465 assert Utils.attachments_from_ids(%{
466 media_ids: ["#{object.id}"],
467 descriptions: desc
468 }) == [
469 Map.merge(object.data, %{"name" => "test-desc"})
470 ]
471 end
472
473 test "returns attachments without descs" do
474 object = insert(:note)
475 assert Utils.attachments_from_ids(%{media_ids: ["#{object.id}"]}) == [object.data]
476 end
477
478 test "returns [] when not pass media_ids" do
479 assert Utils.attachments_from_ids(%{}) == []
480 end
481 end
482
483 describe "maybe_add_list_data/3" do
484 test "adds list params when found user list" do
485 user = insert(:user)
486 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
487
488 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
489 %{
490 additional: %{"bcc" => [list.ap_id], "listMessage" => list.ap_id},
491 object: %{"listMessage" => list.ap_id}
492 }
493 end
494
495 test "returns original params when list not found" do
496 user = insert(:user)
497 {:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", insert(:user))
498
499 assert Utils.maybe_add_list_data(%{additional: %{}, object: %{}}, user, {:list, list.id}) ==
500 %{additional: %{}, object: %{}}
501 end
502 end
503
504 describe "make_note_data/11" do
505 test "returns note data" do
506 user = insert(:user)
507 note = insert(:note)
508 user2 = insert(:user)
509 user3 = insert(:user)
510
511 assert Utils.make_note_data(
512 user.ap_id,
513 [user2.ap_id],
514 "2hu",
515 "<h1>This is :moominmamma: note</h1>",
516 [],
517 note.id,
518 [name: "jimm"],
519 "test summary",
520 [user3.ap_id],
521 false,
522 %{"custom_tag" => "test"}
523 ) == %{
524 "actor" => user.ap_id,
525 "attachment" => [],
526 "cc" => [user3.ap_id],
527 "content" => "<h1>This is :moominmamma: note</h1>",
528 "context" => "2hu",
529 "sensitive" => false,
530 "summary" => "test summary",
531 "tag" => ["jimm"],
532 "to" => [user2.ap_id],
533 "type" => "Note",
534 "custom_tag" => "test"
535 }
536 end
537 end
538
539 describe "maybe_add_attachments/3" do
540 test "returns parsed results when attachment_links is false" do
541 assert Utils.maybe_add_attachments(
542 {"test", [], ["tags"]},
543 [],
544 false
545 ) == {"test", [], ["tags"]}
546 end
547
548 test "adds attachments to parsed results" do
549 attachment = %{"url" => [%{"href" => "SakuraPM.png"}]}
550
551 assert Utils.maybe_add_attachments(
552 {"test", [], ["tags"]},
553 [attachment],
554 true
555 ) == {
556 "test<br><a href=\"SakuraPM.png\" class='attachment'>SakuraPM.png</a>",
557 [],
558 ["tags"]
559 }
560 end
561 end
562 end