1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
9 alias Pleroma.UserRelationship
10 alias Pleroma.Web.CommonAPI
11 alias Pleroma.Web.MastodonAPI.AccountView
13 import Pleroma.Factory
17 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
21 test "Represent a user account" do
23 "url" => [%{"href" => "https://example.com/images/asuka_hospital.png"}]
30 background: background_image,
31 nickname: "shp@shitposter.club",
32 name: ":karjalanpiirakka: shp",
34 "<script src=\"invalid-html\"></script><span>valid html</span>. a<br>b<br/>c<br >d<br />f '&<>\"",
35 inserted_at: ~N[2017-08-15 15:47:06.597036],
36 emoji: %{"karjalanpiirakka" => "/file.png"},
37 raw_bio: "valid html. a\nb\nc\nd\nf '&<>\"",
38 also_known_as: ["https://shitposter.zone/users/shp"]
42 id: to_string(user.id),
45 display_name: user.name,
47 created_at: "2017-08-15T15:47:06.000Z",
51 note: "<span>valid html</span>. a<br/>b<br/>c<br/>d<br/>f '&<>"",
53 avatar: "http://localhost:4001/images/avi.png",
54 avatar_static: "http://localhost:4001/images/avi.png",
55 header: "http://localhost:4001/images/banner.png",
56 header_static: "http://localhost:4001/images/banner.png",
59 static_url: "/file.png",
61 shortcode: "karjalanpiirakka",
62 visible_in_picker: false
68 note: "valid html. a\nb\nc\nd\nf '&<>\"",
76 fqn: "shp@shitposter.club",
79 also_known_as: ["https://shitposter.zone/users/shp"],
80 background_image: "https://example.com/images/asuka_hospital.png",
88 hide_followers: false,
90 hide_followers_count: false,
91 hide_follows_count: false,
93 skip_thread_containment: false,
94 accepts_chat_messages: nil
98 assert expected == AccountView.render("show.json", %{user: user, skip_visibility_check: true})
101 describe "favicon" do
103 [user: insert(:user)]
106 test "is parsed when :instance_favicons is enabled", %{user: user} do
107 clear_config([:instances_favicons, :enabled], true)
112 "https://shitposter.club/plugins/Qvitter/img/gnusocial-favicons/favicon-16x16.png"
114 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
117 test "is nil when :instances_favicons is disabled", %{user: user} do
118 assert %{pleroma: %{favicon: nil}} =
119 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
123 test "Represent the user account for the account owner" do
126 notification_settings = %{
127 block_from_strangers: false,
128 hide_notification_contents: false
131 privacy = user.default_scope
134 pleroma: %{notification_settings: ^notification_settings, allow_following_move: true},
135 source: %{privacy: ^privacy}
136 } = AccountView.render("show.json", %{user: user, for: user})
139 test "Represent a Service(bot) account" do
144 actor_type: "Service",
145 nickname: "shp@shitposter.club",
146 inserted_at: ~N[2017-08-15 15:47:06.597036]
150 id: to_string(user.id),
153 display_name: user.name,
155 created_at: "2017-08-15T15:47:06.000Z",
161 avatar: "http://localhost:4001/images/avi.png",
162 avatar_static: "http://localhost:4001/images/avi.png",
163 header: "http://localhost:4001/images/banner.png",
164 header_static: "http://localhost:4001/images/banner.png",
172 actor_type: "Service",
177 fqn: "shp@shitposter.club",
181 background_image: nil,
188 hide_favorites: true,
189 hide_followers: false,
191 hide_followers_count: false,
192 hide_follows_count: false,
194 skip_thread_containment: false,
195 accepts_chat_messages: nil
199 assert expected == AccountView.render("show.json", %{user: user, skip_visibility_check: true})
202 test "Represent a Funkwhale channel" do
204 User.get_or_fetch_by_ap_id(
205 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
209 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
211 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
212 assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
215 test "Represent a deactivated user for an admin" do
216 admin = insert(:user, is_admin: true)
217 deactivated_user = insert(:user, is_active: false)
218 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
219 assert represented[:pleroma][:deactivated] == true
222 test "Represent a smaller mention" do
226 id: to_string(user.id),
228 username: user.nickname,
232 assert expected == AccountView.render("mention.json", %{user: user})
235 test "demands :for or :skip_visibility_check option for account rendering" do
236 clear_config([:restrict_unauthenticated, :profiles, :local], false)
241 assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: nil})
242 assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: user})
244 assert %{id: ^user_id} =
245 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
247 assert_raise RuntimeError, ~r/:skip_visibility_check or :for option is required/, fn ->
248 AccountView.render("show.json", %{user: user})
252 describe "relationship" do
253 defp test_relationship_rendering(user, other_user, expected_result) do
254 opts = %{user: user, target: other_user, relationships: nil}
255 assert expected_result == AccountView.render("relationship.json", opts)
257 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
258 opts = Map.put(opts, :relationships, relationships_opt)
259 assert expected_result == AccountView.render("relationship.json", opts)
261 assert [expected_result] ==
262 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
271 muting_notifications: false,
275 domain_blocking: false,
276 showing_reblogs: true,
280 test "represent a relationship for the following and followed user" do
282 other_user = insert(:user)
284 {:ok, user, other_user} = User.follow(user, other_user)
285 {:ok, other_user, user} = User.follow(other_user, user)
286 {:ok, _subscription} = User.subscribe(user, other_user)
287 {:ok, _user_relationships} = User.mute(user, other_user, %{notifications: true})
288 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
297 muting_notifications: true,
300 showing_reblogs: false,
301 id: to_string(other_user.id)
305 test_relationship_rendering(user, other_user, expected)
308 test "represent a relationship for the blocking and blocked user" do
310 other_user = insert(:user)
312 {:ok, user, other_user} = User.follow(user, other_user)
313 {:ok, _subscription} = User.subscribe(user, other_user)
314 {:ok, _user_relationship} = User.block(user, other_user)
315 {:ok, _user_relationship} = User.block(other_user, user)
320 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
323 test_relationship_rendering(user, other_user, expected)
326 test "represent a relationship for the user blocking a domain" do
328 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
330 {:ok, user} = User.block_domain(user, "bad.site")
335 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
338 test_relationship_rendering(user, other_user, expected)
341 test "represent a relationship for the user with a pending follow request" do
343 other_user = insert(:user, is_locked: true)
345 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
346 user = User.get_cached_by_id(user.id)
347 other_user = User.get_cached_by_id(other_user.id)
352 %{requested: true, following: false, id: to_string(other_user.id)}
355 test_relationship_rendering(user, other_user, expected)
359 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
360 user = insert(:user, pleroma_settings_store: %{fe: "test"})
363 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
365 assert result.pleroma.settings_store == %{:fe => "test"}
367 result = AccountView.render("show.json", %{user: user, for: nil, with_pleroma_settings: true})
368 assert result.pleroma[:settings_store] == nil
370 result = AccountView.render("show.json", %{user: user, for: user})
371 assert result.pleroma[:settings_store] == nil
374 test "doesn't sanitize display names" do
375 user = insert(:user, name: "<marquee> username </marquee>")
376 result = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
377 assert result.display_name == "<marquee> username </marquee>"
380 test "never display nil user follow counts" do
381 user = insert(:user, following_count: 0, follower_count: 0)
382 result = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
384 assert result.following_count == 0
385 assert result.followers_count == 0
388 describe "hiding follows/following" do
389 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
392 hide_followers: true,
393 hide_followers_count: true,
395 hide_follows_count: true
398 other_user = insert(:user)
399 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
400 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
405 pleroma: %{hide_follows_count: true, hide_followers_count: true}
406 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
409 test "shows when follows/followers are hidden" do
410 user = insert(:user, hide_followers: true, hide_follows: true)
411 other_user = insert(:user)
412 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
413 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
418 pleroma: %{hide_follows: true, hide_followers: true}
419 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
422 test "shows actual follower/following count to the account owner" do
423 user = insert(:user, hide_followers: true, hide_follows: true)
424 other_user = insert(:user)
425 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
427 assert User.following?(user, other_user)
428 assert Pleroma.FollowingRelationship.follower_count(other_user) == 1
429 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
434 } = AccountView.render("show.json", %{user: user, for: user})
437 test "shows unread_conversation_count only to the account owner" do
439 other_user = insert(:user)
442 CommonAPI.post(other_user, %{
443 status: "Hey @#{user.nickname}.",
447 user = User.get_cached_by_ap_id(user.ap_id)
449 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
450 :unread_conversation_count
453 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
454 :unread_conversation_count
458 test "shows unread_count only to the account owner" do
460 insert_list(7, :notification, user: user, activity: insert(:note_activity))
461 other_user = insert(:user)
463 user = User.get_cached_by_ap_id(user.ap_id)
465 assert AccountView.render(
467 %{user: user, for: other_user}
468 )[:pleroma][:unread_notifications_count] == nil
470 assert AccountView.render(
472 %{user: user, for: user}
473 )[:pleroma][:unread_notifications_count] == 7
476 test "shows email only to the account owner" do
478 other_user = insert(:user)
480 user = User.get_cached_by_ap_id(user.ap_id)
482 assert AccountView.render(
484 %{user: user, for: other_user}
485 )[:pleroma][:email] == nil
487 assert AccountView.render(
489 %{user: user, for: user}
490 )[:pleroma][:email] == user.email
494 describe "follow requests counter" do
495 test "shows zero when no follow requests are pending" do
498 assert %{follow_requests_count: 0} =
499 AccountView.render("show.json", %{user: user, for: user})
501 other_user = insert(:user)
502 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
504 assert %{follow_requests_count: 0} =
505 AccountView.render("show.json", %{user: user, for: user})
508 test "shows non-zero when follow requests are pending" do
509 user = insert(:user, is_locked: true)
511 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
513 other_user = insert(:user)
514 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
516 assert %{locked: true, follow_requests_count: 1} =
517 AccountView.render("show.json", %{user: user, for: user})
520 test "decreases when accepting a follow request" do
521 user = insert(:user, is_locked: true)
523 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
525 other_user = insert(:user)
526 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
528 assert %{locked: true, follow_requests_count: 1} =
529 AccountView.render("show.json", %{user: user, for: user})
531 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
533 assert %{locked: true, follow_requests_count: 0} =
534 AccountView.render("show.json", %{user: user, for: user})
537 test "decreases when rejecting a follow request" do
538 user = insert(:user, is_locked: true)
540 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
542 other_user = insert(:user)
543 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
545 assert %{locked: true, follow_requests_count: 1} =
546 AccountView.render("show.json", %{user: user, for: user})
548 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
550 assert %{locked: true, follow_requests_count: 0} =
551 AccountView.render("show.json", %{user: user, for: user})
554 test "shows non-zero when historical unapproved requests are present" do
555 user = insert(:user, is_locked: true)
557 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
559 other_user = insert(:user)
560 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
562 {:ok, user} = User.update_and_set_cache(user, %{is_locked: false})
564 assert %{locked: false, follow_requests_count: 1} =
565 AccountView.render("show.json", %{user: user, for: user})
569 test "uses mediaproxy urls when it's enabled (regardless of media preview proxy state)" do
570 clear_config([:media_proxy, :enabled], true)
571 clear_config([:media_preview_proxy, :enabled])
575 avatar: %{"url" => [%{"href" => "https://evil.website/avatar.png"}]},
576 banner: %{"url" => [%{"href" => "https://evil.website/banner.png"}]},
577 emoji: %{"joker_smile" => "https://evil.website/society.png"}
580 with media_preview_enabled <- [false, true] do
581 clear_config([:media_preview_proxy, :enabled], media_preview_enabled)
583 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
585 {key, url} when key in [:avatar, :avatar_static, :header, :header_static] ->
586 String.starts_with?(url, Pleroma.Web.Endpoint.url())
589 Enum.all?(emojis, fn %{url: url, static_url: static_url} ->
590 String.starts_with?(url, Pleroma.Web.Endpoint.url()) &&
591 String.starts_with?(static_url, Pleroma.Web.Endpoint.url())