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",
80 also_known_as: ["https://shitposter.zone/users/shp"],
81 background_image: "https://example.com/images/asuka_hospital.png",
89 hide_followers: false,
91 hide_followers_count: false,
92 hide_follows_count: false,
94 skip_thread_containment: false,
95 accepts_chat_messages: nil
99 assert expected == AccountView.render("show.json", %{user: user, skip_visibility_check: true})
102 describe "favicon" do
104 [user: insert(:user)]
107 test "is parsed when :instance_favicons is enabled", %{user: user} do
108 clear_config([:instances_favicons, :enabled], true)
113 "https://shitposter.club/plugins/Qvitter/img/gnusocial-favicons/favicon-16x16.png"
115 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
118 test "is nil when :instances_favicons is disabled", %{user: user} do
119 assert %{pleroma: %{favicon: nil}} =
120 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
124 test "Represent the user account for the account owner" do
127 notification_settings = %{
128 block_from_strangers: false,
129 hide_notification_contents: false
132 privacy = user.default_scope
135 pleroma: %{notification_settings: ^notification_settings, allow_following_move: true},
136 source: %{privacy: ^privacy}
137 } = AccountView.render("show.json", %{user: user, for: user})
140 test "Represent a Service(bot) account" do
145 actor_type: "Service",
146 nickname: "shp@shitposter.club",
147 inserted_at: ~N[2017-08-15 15:47:06.597036]
151 id: to_string(user.id),
154 display_name: user.name,
156 created_at: "2017-08-15T15:47:06.000Z",
162 avatar: "http://localhost:4001/images/avi.png",
163 avatar_static: "http://localhost:4001/images/avi.png",
164 header: "http://localhost:4001/images/banner.png",
165 header_static: "http://localhost:4001/images/banner.png",
173 actor_type: "Service",
178 fqn: "shp@shitposter.club",
183 background_image: nil,
190 hide_favorites: true,
191 hide_followers: false,
193 hide_followers_count: false,
194 hide_follows_count: false,
196 skip_thread_containment: false,
197 accepts_chat_messages: nil
201 assert expected == AccountView.render("show.json", %{user: user, skip_visibility_check: true})
204 test "Represent a Funkwhale channel" do
206 User.get_or_fetch_by_ap_id(
207 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
211 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
213 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
214 assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
217 test "Represent a deactivated user for an admin" do
218 admin = insert(:user, is_admin: true)
219 deactivated_user = insert(:user, is_active: false)
220 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
221 assert represented[:pleroma][:deactivated] == true
224 test "Represent a smaller mention" do
228 id: to_string(user.id),
230 username: user.nickname,
234 assert expected == AccountView.render("mention.json", %{user: user})
237 test "demands :for or :skip_visibility_check option for account rendering" do
238 clear_config([:restrict_unauthenticated, :profiles, :local], false)
243 assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: nil})
244 assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: user})
246 assert %{id: ^user_id} =
247 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
249 assert_raise RuntimeError, ~r/:skip_visibility_check or :for option is required/, fn ->
250 AccountView.render("show.json", %{user: user})
254 describe "relationship" do
255 defp test_relationship_rendering(user, other_user, expected_result) do
256 opts = %{user: user, target: other_user, relationships: nil}
257 assert expected_result == AccountView.render("relationship.json", opts)
259 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
260 opts = Map.put(opts, :relationships, relationships_opt)
261 assert expected_result == AccountView.render("relationship.json", opts)
263 assert [expected_result] ==
264 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
273 muting_notifications: false,
277 domain_blocking: false,
278 showing_reblogs: true,
283 test "represent a relationship for the following and followed user" do
285 other_user = insert(:user)
287 {:ok, user, other_user} = User.follow(user, other_user)
288 {:ok, other_user, user} = User.follow(other_user, user)
289 {:ok, _subscription} = User.subscribe(user, other_user)
290 {:ok, _user_relationships} = User.mute(user, other_user, %{notifications: true})
291 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
300 muting_notifications: true,
303 showing_reblogs: false,
304 id: to_string(other_user.id)
308 test_relationship_rendering(user, other_user, expected)
311 test "represent a relationship for the blocking and blocked user" do
313 other_user = insert(:user)
315 {:ok, user, other_user} = User.follow(user, other_user)
316 {:ok, _subscription} = User.subscribe(user, other_user)
317 {:ok, _user_relationship} = User.block(user, other_user)
318 {:ok, _user_relationship} = User.block(other_user, user)
323 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
326 test_relationship_rendering(user, other_user, expected)
329 test "represent a relationship for the user blocking a domain" do
331 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
333 {:ok, user} = User.block_domain(user, "bad.site")
338 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
341 test_relationship_rendering(user, other_user, expected)
344 test "represent a relationship for the user with a pending follow request" do
346 other_user = insert(:user, is_locked: true)
348 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
349 user = User.get_cached_by_id(user.id)
350 other_user = User.get_cached_by_id(other_user.id)
355 %{requested: true, following: false, id: to_string(other_user.id)}
358 test_relationship_rendering(user, other_user, expected)
362 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
363 user = insert(:user, pleroma_settings_store: %{fe: "test"})
366 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
368 assert result.pleroma.settings_store == %{:fe => "test"}
370 result = AccountView.render("show.json", %{user: user, for: nil, with_pleroma_settings: true})
371 assert result.pleroma[:settings_store] == nil
373 result = AccountView.render("show.json", %{user: user, for: user})
374 assert result.pleroma[:settings_store] == nil
377 test "doesn't sanitize display names" do
378 user = insert(:user, name: "<marquee> username </marquee>")
379 result = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
380 assert result.display_name == "<marquee> username </marquee>"
383 test "never display nil user follow counts" do
384 user = insert(:user, following_count: 0, follower_count: 0)
385 result = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
387 assert result.following_count == 0
388 assert result.followers_count == 0
391 describe "hiding follows/following" do
392 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
395 hide_followers: true,
396 hide_followers_count: true,
398 hide_follows_count: true
401 other_user = insert(:user)
402 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
403 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
408 pleroma: %{hide_follows_count: true, hide_followers_count: true}
409 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
412 test "shows when follows/followers are hidden" do
413 user = insert(:user, hide_followers: true, hide_follows: true)
414 other_user = insert(:user)
415 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
416 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
421 pleroma: %{hide_follows: true, hide_followers: true}
422 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
425 test "shows actual follower/following count to the account owner" do
426 user = insert(:user, hide_followers: true, hide_follows: true)
427 other_user = insert(:user)
428 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
430 assert User.following?(user, other_user)
431 assert Pleroma.FollowingRelationship.follower_count(other_user) == 1
432 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
437 } = AccountView.render("show.json", %{user: user, for: user})
440 test "shows unread_conversation_count only to the account owner" do
442 other_user = insert(:user)
445 CommonAPI.post(other_user, %{
446 status: "Hey @#{user.nickname}.",
450 user = User.get_cached_by_ap_id(user.ap_id)
452 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
453 :unread_conversation_count
456 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
457 :unread_conversation_count
461 test "shows unread_count only to the account owner" do
463 insert_list(7, :notification, user: user, activity: insert(:note_activity))
464 other_user = insert(:user)
466 user = User.get_cached_by_ap_id(user.ap_id)
468 assert AccountView.render(
470 %{user: user, for: other_user}
471 )[:pleroma][:unread_notifications_count] == nil
473 assert AccountView.render(
475 %{user: user, for: user}
476 )[:pleroma][:unread_notifications_count] == 7
479 test "shows email only to the account owner" do
481 other_user = insert(:user)
483 user = User.get_cached_by_ap_id(user.ap_id)
485 assert AccountView.render(
487 %{user: user, for: other_user}
488 )[:pleroma][:email] == nil
490 assert AccountView.render(
492 %{user: user, for: user}
493 )[:pleroma][:email] == user.email
497 describe "follow requests counter" do
498 test "shows zero when no follow requests are pending" do
501 assert %{follow_requests_count: 0} =
502 AccountView.render("show.json", %{user: user, for: user})
504 other_user = insert(:user)
505 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
507 assert %{follow_requests_count: 0} =
508 AccountView.render("show.json", %{user: user, for: user})
511 test "shows non-zero when follow requests are pending" do
512 user = insert(:user, is_locked: true)
514 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
516 other_user = insert(:user)
517 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
519 assert %{locked: true, follow_requests_count: 1} =
520 AccountView.render("show.json", %{user: user, for: user})
523 test "decreases when accepting a follow request" do
524 user = insert(:user, is_locked: true)
526 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
528 other_user = insert(:user)
529 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
531 assert %{locked: true, follow_requests_count: 1} =
532 AccountView.render("show.json", %{user: user, for: user})
534 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
536 assert %{locked: true, follow_requests_count: 0} =
537 AccountView.render("show.json", %{user: user, for: user})
540 test "decreases when rejecting a follow request" do
541 user = insert(:user, is_locked: true)
543 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
545 other_user = insert(:user)
546 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
548 assert %{locked: true, follow_requests_count: 1} =
549 AccountView.render("show.json", %{user: user, for: user})
551 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
553 assert %{locked: true, follow_requests_count: 0} =
554 AccountView.render("show.json", %{user: user, for: user})
557 test "shows non-zero when historical unapproved requests are present" do
558 user = insert(:user, is_locked: true)
560 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
562 other_user = insert(:user)
563 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
565 {:ok, user} = User.update_and_set_cache(user, %{is_locked: false})
567 assert %{locked: false, follow_requests_count: 1} =
568 AccountView.render("show.json", %{user: user, for: user})
572 test "uses mediaproxy urls when it's enabled (regardless of media preview proxy state)" do
573 clear_config([:media_proxy, :enabled], true)
574 clear_config([:media_preview_proxy, :enabled])
578 avatar: %{"url" => [%{"href" => "https://evil.website/avatar.png"}]},
579 banner: %{"url" => [%{"href" => "https://evil.website/banner.png"}]},
580 emoji: %{"joker_smile" => "https://evil.website/society.png"}
583 with media_preview_enabled <- [false, true] do
584 clear_config([:media_preview_proxy, :enabled], media_preview_enabled)
586 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
588 {key, url} when key in [:avatar, :avatar_static, :header, :header_static] ->
589 String.starts_with?(url, Pleroma.Web.Endpoint.url())
592 Enum.all?(emojis, fn %{url: url, static_url: static_url} ->
593 String.starts_with?(url, Pleroma.Web.Endpoint.url()) &&
594 String.starts_with?(static_url, Pleroma.Web.Endpoint.url())