docs: Remove quarantine section
[akkoma] / test / pleroma / web / mastodon_api / views / account_view_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
6 use Pleroma.DataCase, async: false
7
8 alias Pleroma.User
9 alias Pleroma.UserRelationship
10 alias Pleroma.Web.CommonAPI
11 alias Pleroma.Web.MastodonAPI.AccountView
12
13 import Pleroma.Factory
14 import Tesla.Mock
15 import Mock
16
17 setup do
18 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
19 :ok
20 end
21
22 test "Represent a user account" do
23 background_image = %{
24 "url" => [%{"href" => "https://example.com/images/asuka_hospital.png"}]
25 }
26
27 user =
28 insert(:user, %{
29 ap_id: "https://example.com/users/chikichikibanban",
30 follower_count: 3,
31 note_count: 5,
32 background: background_image,
33 nickname: "shp@shitposter.club",
34 name: ":karjalanpiirakka: shp",
35 bio:
36 "<script src=\"invalid-html\"></script><span>valid html</span>. a<br>b<br/>c<br >d<br />f '&<>\"",
37 inserted_at: ~N[2017-08-15 15:47:06.597036],
38 emoji: %{"karjalanpiirakka" => "/file.png"},
39 raw_bio: "valid html. a\nb\nc\nd\nf '&<>\"",
40 also_known_as: ["https://shitposter.zone/users/shp"]
41 })
42
43 insert(:instance, %{host: "example.com", nodeinfo: %{version: "2.1"}})
44
45 expected = %{
46 id: to_string(user.id),
47 username: "shp",
48 acct: user.nickname,
49 display_name: user.name,
50 locked: false,
51 created_at: "2017-08-15T15:47:06.000Z",
52 followers_count: 3,
53 following_count: 0,
54 statuses_count: 5,
55 note: "<span>valid html</span>. a<br/>b<br/>c<br/>d<br/>f &#39;&amp;&lt;&gt;&quot;",
56 url: user.ap_id,
57 akkoma: %{
58 instance: %{
59 name: "example.com",
60 nodeinfo: %{
61 "version" => "2.1"
62 },
63 favicon: nil
64 }
65 },
66 avatar: "http://localhost:4001/images/avi.png",
67 avatar_static: "http://localhost:4001/images/avi.png",
68 header: "http://localhost:4001/images/banner.png",
69 header_static: "http://localhost:4001/images/banner.png",
70 emojis: [
71 %{
72 static_url: "/file.png",
73 url: "/file.png",
74 shortcode: "karjalanpiirakka",
75 visible_in_picker: false
76 }
77 ],
78 fields: [],
79 bot: false,
80 source: %{
81 note: "valid html. a\nb\nc\nd\nf '&<>\"",
82 sensitive: false,
83 pleroma: %{
84 actor_type: "Person",
85 discoverable: true
86 },
87 fields: []
88 },
89 fqn: "shp@shitposter.club",
90 last_status_at: nil,
91 pleroma: %{
92 ap_id: user.ap_id,
93 also_known_as: ["https://shitposter.zone/users/shp"],
94 background_image: "https://example.com/images/asuka_hospital.png",
95 favicon: nil,
96 is_confirmed: true,
97 tags: [],
98 is_admin: false,
99 is_moderator: false,
100 is_suggested: false,
101 hide_favorites: true,
102 hide_followers: false,
103 hide_follows: false,
104 hide_followers_count: false,
105 hide_follows_count: false,
106 relationship: %{},
107 skip_thread_containment: false
108 }
109 }
110
111 assert expected == AccountView.render("show.json", %{user: user, skip_visibility_check: true})
112 end
113
114 describe "nodeinfo" do
115 setup do
116 [
117 user: insert(:user, ap_id: "https://somewhere.example.com/users/chikichikibanban"),
118 instance:
119 insert(:instance, %{
120 host: "somewhere.example.com",
121 favicon: "https://example.com/favicon.ico"
122 })
123 ]
124 end
125
126 test "is embedded in the account view", %{user: user} do
127 assert %{
128 akkoma: %{
129 instance: %{
130 name: "somewhere.example.com",
131 nodeinfo: %{
132 "version" => "2.0"
133 },
134 favicon: "https://example.com/favicon.ico"
135 }
136 }
137 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
138 end
139
140 test "uses local nodeinfo for local users" do
141 user = insert(:user)
142
143 assert %{
144 akkoma: %{
145 instance: %{
146 name: "localhost",
147 nodeinfo: %{
148 software: %{
149 name: "akkoma"
150 }
151 }
152 }
153 }
154 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
155 end
156 end
157
158 describe "favicon" do
159 setup do
160 [
161 user: insert(:user, ap_id: "https://example.com/users/chikichikibanban"),
162 instance:
163 insert(:instance, %{host: "example.com", favicon: "https://example.com/favicon.ico"})
164 ]
165 end
166
167 test "is parsed when :instance_favicons is enabled", %{user: user} do
168 clear_config([:instances_favicons, :enabled], true)
169
170 assert %{
171 pleroma: %{
172 favicon: "https://example.com/favicon.ico"
173 }
174 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
175 end
176
177 test "is nil when we have no instance", %{user: user} do
178 user = %{user | ap_id: "https://wowee.example.com/users/2"}
179
180 assert %{pleroma: %{favicon: nil}} =
181 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
182 end
183 end
184
185 test "Represent the user account for the account owner" do
186 user = insert(:user)
187
188 notification_settings = %{
189 block_from_strangers: false,
190 hide_notification_contents: false
191 }
192
193 privacy = user.default_scope
194
195 assert %{
196 pleroma: %{notification_settings: ^notification_settings, allow_following_move: true},
197 source: %{privacy: ^privacy}
198 } = AccountView.render("show.json", %{user: user, for: user})
199 end
200
201 test "Represent a Service(bot) account" do
202 user =
203 insert(:user, %{
204 follower_count: 3,
205 note_count: 5,
206 actor_type: "Service",
207 nickname: "shp@shitposter.club",
208 inserted_at: ~N[2017-08-15 15:47:06.597036]
209 })
210
211 expected = %{
212 id: to_string(user.id),
213 username: "shp",
214 acct: user.nickname,
215 display_name: user.name,
216 locked: false,
217 created_at: "2017-08-15T15:47:06.000Z",
218 followers_count: 3,
219 following_count: 0,
220 statuses_count: 5,
221 note: user.bio,
222 url: user.ap_id,
223 avatar: "http://localhost:4001/images/avi.png",
224 avatar_static: "http://localhost:4001/images/avi.png",
225 header: "http://localhost:4001/images/banner.png",
226 header_static: "http://localhost:4001/images/banner.png",
227 emojis: [],
228 fields: [],
229 bot: true,
230 source: %{
231 note: user.bio,
232 sensitive: false,
233 pleroma: %{
234 actor_type: "Service",
235 discoverable: true
236 },
237 fields: []
238 },
239 fqn: "shp@shitposter.club",
240 last_status_at: nil,
241 akkoma: %{
242 instance: %{
243 name: "localhost",
244 favicon: "http://localhost:4001/favicon.png",
245 nodeinfo: %{version: "2.0"}
246 }
247 },
248 pleroma: %{
249 ap_id: user.ap_id,
250 also_known_as: [],
251 background_image: nil,
252 favicon: "http://localhost:4001/favicon.png",
253 is_confirmed: true,
254 tags: [],
255 is_admin: false,
256 is_moderator: false,
257 is_suggested: false,
258 hide_favorites: true,
259 hide_followers: false,
260 hide_follows: false,
261 hide_followers_count: false,
262 hide_follows_count: false,
263 relationship: %{},
264 skip_thread_containment: false
265 }
266 }
267
268 with_mock(
269 Pleroma.Web.Nodeinfo.NodeinfoController,
270 raw_nodeinfo: fn -> %{version: "2.0"} end
271 ) do
272 assert expected ==
273 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
274 end
275 end
276
277 test "Represent a Funkwhale channel" do
278 {:ok, user} =
279 User.get_or_fetch_by_ap_id(
280 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
281 )
282
283 assert represented =
284 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
285
286 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
287 assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
288 end
289
290 test "Represent a deactivated user for an admin" do
291 admin = insert(:user, is_admin: true)
292 deactivated_user = insert(:user, is_active: false)
293 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
294 assert represented[:pleroma][:deactivated] == true
295 end
296
297 test "Represent a smaller mention" do
298 user = insert(:user)
299
300 expected = %{
301 id: to_string(user.id),
302 acct: user.nickname,
303 username: user.nickname,
304 url: user.ap_id
305 }
306
307 assert expected == AccountView.render("mention.json", %{user: user})
308 end
309
310 test "demands :for or :skip_visibility_check option for account rendering" do
311 clear_config([:restrict_unauthenticated, :profiles, :local], false)
312
313 user = insert(:user)
314 user_id = user.id
315
316 assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: nil})
317 assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: user})
318
319 assert %{id: ^user_id} =
320 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
321
322 assert_raise RuntimeError, ~r/:skip_visibility_check or :for option is required/, fn ->
323 AccountView.render("show.json", %{user: user})
324 end
325 end
326
327 describe "relationship" do
328 defp test_relationship_rendering(user, other_user, expected_result) do
329 opts = %{user: user, target: other_user, relationships: nil}
330 assert expected_result == AccountView.render("relationship.json", opts)
331
332 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
333 opts = Map.put(opts, :relationships, relationships_opt)
334 assert expected_result == AccountView.render("relationship.json", opts)
335
336 assert [expected_result] ==
337 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
338 end
339
340 @blank_response %{
341 following: false,
342 followed_by: false,
343 blocking: false,
344 blocked_by: false,
345 muting: false,
346 muting_notifications: false,
347 subscribing: false,
348 notifying: false,
349 requested: false,
350 requested_by: false,
351 domain_blocking: false,
352 showing_reblogs: true,
353 endorsed: false,
354 note: ""
355 }
356
357 test "represent a relationship for the following and followed user" do
358 user = insert(:user)
359 other_user = insert(:user)
360
361 {:ok, user, other_user} = User.follow(user, other_user)
362 {:ok, other_user, user} = User.follow(other_user, user)
363 {:ok, _subscription} = User.subscribe(user, other_user)
364 {:ok, _user_relationships} = User.mute(user, other_user, %{notifications: true})
365 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
366
367 expected =
368 Map.merge(
369 @blank_response,
370 %{
371 following: true,
372 followed_by: true,
373 muting: true,
374 muting_notifications: true,
375 subscribing: true,
376 notifying: true,
377 showing_reblogs: false,
378 id: to_string(other_user.id)
379 }
380 )
381
382 test_relationship_rendering(user, other_user, expected)
383 end
384
385 test "represent a relationship for the blocking and blocked user" do
386 user = insert(:user)
387 other_user = insert(:user)
388
389 {:ok, user, other_user} = User.follow(user, other_user)
390 {:ok, _subscription} = User.subscribe(user, other_user)
391 {:ok, _user_relationship} = User.block(user, other_user)
392 {:ok, _user_relationship} = User.block(other_user, user)
393
394 expected =
395 Map.merge(
396 @blank_response,
397 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
398 )
399
400 test_relationship_rendering(user, other_user, expected)
401 end
402
403 test "represent a relationship for the user blocking a domain" do
404 user = insert(:user)
405 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
406
407 {:ok, user} = User.block_domain(user, "bad.site")
408
409 expected =
410 Map.merge(
411 @blank_response,
412 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
413 )
414
415 test_relationship_rendering(user, other_user, expected)
416 end
417
418 test "represent a relationship for the user with a pending follow request" do
419 user = insert(:user)
420 other_user = insert(:user, is_locked: true)
421
422 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
423 user = User.get_cached_by_id(user.id)
424 other_user = User.get_cached_by_id(other_user.id)
425
426 expected =
427 Map.merge(
428 @blank_response,
429 %{requested: true, following: false, id: to_string(other_user.id)}
430 )
431
432 test_relationship_rendering(user, other_user, expected)
433 end
434 end
435
436 test "represent a relationship for a user with an inbound pending follow request" do
437 follower = insert(:user)
438 followed = insert(:user, is_locked: true)
439
440 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
441
442 follower = User.get_cached_by_id(follower.id)
443 followed = User.get_cached_by_id(followed.id)
444
445 expected =
446 Map.merge(
447 @blank_response,
448 %{requested_by: true, followed_by: false, id: to_string(follower.id)}
449 )
450
451 test_relationship_rendering(followed, follower, expected)
452 end
453
454 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
455 user = insert(:user, pleroma_settings_store: %{fe: "test"})
456
457 result =
458 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
459
460 assert result.pleroma.settings_store == %{:fe => "test"}
461
462 result = AccountView.render("show.json", %{user: user, for: nil, with_pleroma_settings: true})
463 assert result.pleroma[:settings_store] == nil
464
465 result = AccountView.render("show.json", %{user: user, for: user})
466 assert result.pleroma[:settings_store] == nil
467 end
468
469 test "doesn't sanitize display names" do
470 user = insert(:user, name: "<marquee> username </marquee>")
471 result = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
472 assert result.display_name == "<marquee> username </marquee>"
473 end
474
475 test "never display nil user follow counts" do
476 user = insert(:user, following_count: 0, follower_count: 0)
477 result = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
478
479 assert result.following_count == 0
480 assert result.followers_count == 0
481 end
482
483 describe "hiding follows/following" do
484 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
485 user =
486 insert(:user, %{
487 hide_followers: true,
488 hide_followers_count: true,
489 hide_follows: true,
490 hide_follows_count: true
491 })
492
493 other_user = insert(:user)
494 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
495 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
496
497 assert %{
498 followers_count: 0,
499 following_count: 0,
500 pleroma: %{hide_follows_count: true, hide_followers_count: true}
501 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
502 end
503
504 test "shows when follows/followers are hidden" do
505 user = insert(:user, hide_followers: true, hide_follows: true)
506 other_user = insert(:user)
507 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
508 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
509
510 assert %{
511 followers_count: 1,
512 following_count: 1,
513 pleroma: %{hide_follows: true, hide_followers: true}
514 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
515 end
516
517 test "shows actual follower/following count to the account owner" do
518 user = insert(:user, hide_followers: true, hide_follows: true)
519 other_user = insert(:user)
520 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
521
522 assert User.following?(user, other_user)
523 assert Pleroma.FollowingRelationship.follower_count(other_user) == 1
524 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
525
526 assert %{
527 followers_count: 1,
528 following_count: 1
529 } = AccountView.render("show.json", %{user: user, for: user})
530 end
531
532 test "shows unread_conversation_count only to the account owner" do
533 user = insert(:user)
534 other_user = insert(:user)
535
536 {:ok, _activity} =
537 CommonAPI.post(other_user, %{
538 status: "Hey @#{user.nickname}.",
539 visibility: "direct"
540 })
541
542 user = User.get_cached_by_ap_id(user.ap_id)
543
544 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
545 :unread_conversation_count
546 ] == nil
547
548 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
549 :unread_conversation_count
550 ] == 1
551 end
552
553 test "shows unread_count only to the account owner" do
554 user = insert(:user)
555 insert_list(7, :notification, user: user, activity: insert(:note_activity))
556 other_user = insert(:user)
557
558 user = User.get_cached_by_ap_id(user.ap_id)
559
560 assert AccountView.render(
561 "show.json",
562 %{user: user, for: other_user}
563 )[:pleroma][:unread_notifications_count] == nil
564
565 assert AccountView.render(
566 "show.json",
567 %{user: user, for: user}
568 )[:pleroma][:unread_notifications_count] == 7
569 end
570
571 test "shows email only to the account owner" do
572 user = insert(:user)
573 other_user = insert(:user)
574
575 user = User.get_cached_by_ap_id(user.ap_id)
576
577 assert AccountView.render(
578 "show.json",
579 %{user: user, for: other_user}
580 )[:pleroma][:email] == nil
581
582 assert AccountView.render(
583 "show.json",
584 %{user: user, for: user}
585 )[:pleroma][:email] == user.email
586 end
587 end
588
589 describe "follow requests counter" do
590 test "shows zero when no follow requests are pending" do
591 user = insert(:user)
592
593 assert %{follow_requests_count: 0} =
594 AccountView.render("show.json", %{user: user, for: user})
595
596 other_user = insert(:user)
597 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
598
599 assert %{follow_requests_count: 0} =
600 AccountView.render("show.json", %{user: user, for: user})
601 end
602
603 test "shows non-zero when follow requests are pending" do
604 user = insert(:user, is_locked: true)
605
606 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
607
608 other_user = insert(:user)
609 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
610
611 assert %{locked: true, follow_requests_count: 1} =
612 AccountView.render("show.json", %{user: user, for: user})
613 end
614
615 test "decreases when accepting a follow request" do
616 user = insert(:user, is_locked: true)
617
618 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
619
620 other_user = insert(:user)
621 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
622
623 assert %{locked: true, follow_requests_count: 1} =
624 AccountView.render("show.json", %{user: user, for: user})
625
626 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
627
628 assert %{locked: true, follow_requests_count: 0} =
629 AccountView.render("show.json", %{user: user, for: user})
630 end
631
632 test "decreases when rejecting a follow request" do
633 user = insert(:user, is_locked: true)
634
635 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
636
637 other_user = insert(:user)
638 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
639
640 assert %{locked: true, follow_requests_count: 1} =
641 AccountView.render("show.json", %{user: user, for: user})
642
643 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
644
645 assert %{locked: true, follow_requests_count: 0} =
646 AccountView.render("show.json", %{user: user, for: user})
647 end
648
649 test "shows non-zero when historical unapproved requests are present" do
650 user = insert(:user, is_locked: true)
651
652 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
653
654 other_user = insert(:user)
655 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
656
657 {:ok, user} = User.update_and_set_cache(user, %{is_locked: false})
658
659 assert %{locked: false, follow_requests_count: 1} =
660 AccountView.render("show.json", %{user: user, for: user})
661 end
662 end
663
664 test "uses mediaproxy urls when it's enabled (regardless of media preview proxy state)" do
665 clear_config([:media_proxy, :enabled], true)
666 clear_config([:media_preview_proxy, :enabled])
667
668 user =
669 insert(:user,
670 avatar: %{"url" => [%{"href" => "https://evil.website/avatar.png"}]},
671 banner: %{"url" => [%{"href" => "https://evil.website/banner.png"}]},
672 emoji: %{"joker_smile" => "https://evil.website/society.png"}
673 )
674
675 insert(:instance, %{host: "localhost", favicon: "https://evil.website/favicon.png"})
676
677 with media_preview_enabled <- [false, true] do
678 clear_config([:media_preview_proxy, :enabled], media_preview_enabled)
679
680 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
681 |> Enum.all?(fn
682 {key, url} when key in [:avatar, :avatar_static, :header, :header_static] ->
683 String.starts_with?(url, Pleroma.Web.Endpoint.url())
684
685 {:akkoma, %{instance: %{favicon: favicon_url}}} ->
686 String.starts_with?(favicon_url, Pleroma.Web.Endpoint.url())
687
688 {:emojis, emojis} ->
689 Enum.all?(emojis, fn %{url: url, static_url: static_url} ->
690 String.starts_with?(url, Pleroma.Web.Endpoint.url()) &&
691 String.starts_with?(static_url, Pleroma.Web.Endpoint.url())
692 end)
693
694 _ ->
695 true
696 end)
697 |> assert()
698 end
699 end
700
701 test "returns nil in the instance field when no instance is held locally" do
702 user = insert(:user, ap_id: "https://example.com/users/1")
703 view = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
704 assert view[:akkoma][:instance] == nil
705 end
706 end