1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
10 alias Pleroma.UserRelationship
11 alias Pleroma.Web.CommonAPI
12 alias Pleroma.Web.MastodonAPI.AccountView
14 import Pleroma.Factory
18 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
22 test "Represent a user account" do
24 "url" => [%{"href" => "https://example.com/images/asuka_hospital.png"}]
31 background: background_image,
32 nickname: "shp@shitposter.club",
33 name: ":karjalanpiirakka: shp",
35 "<script src=\"invalid-html\"></script><span>valid html</span>. a<br>b<br/>c<br >d<br />f '&<>\"",
36 inserted_at: ~N[2017-08-15 15:47:06.597036],
37 emoji: %{"karjalanpiirakka" => "/file.png"},
38 raw_bio: "valid html. a\nb\nc\nd\nf '&<>\""
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 '&<>\"",
78 background_image: "https://example.com/images/asuka_hospital.png",
80 confirmation_pending: false,
85 hide_followers: false,
87 hide_followers_count: false,
88 hide_follows_count: false,
90 skip_thread_containment: false,
91 accepts_chat_messages: nil
95 assert expected == AccountView.render("show.json", %{user: user, skip_visibility_check: true})
100 [user: insert(:user)]
103 test "is parsed when :instance_favicons is enabled", %{user: user} do
104 clear_config([:instances_favicons, :enabled], true)
109 "https://shitposter.club/plugins/Qvitter/img/gnusocial-favicons/favicon-16x16.png"
111 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
114 test "is nil when :instances_favicons is disabled", %{user: user} do
115 assert %{pleroma: %{favicon: nil}} =
116 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
120 test "Represent the user account for the account owner" do
123 notification_settings = %{
124 block_from_strangers: false,
125 hide_notification_contents: false
128 privacy = user.default_scope
131 pleroma: %{notification_settings: ^notification_settings, allow_following_move: true},
132 source: %{privacy: ^privacy}
133 } = AccountView.render("show.json", %{user: user, for: user})
136 test "Represent a Service(bot) account" do
141 actor_type: "Service",
142 nickname: "shp@shitposter.club",
143 inserted_at: ~N[2017-08-15 15:47:06.597036]
147 id: to_string(user.id),
150 display_name: user.name,
152 created_at: "2017-08-15T15:47:06.000Z",
158 avatar: "http://localhost:4001/images/avi.png",
159 avatar_static: "http://localhost:4001/images/avi.png",
160 header: "http://localhost:4001/images/banner.png",
161 header_static: "http://localhost:4001/images/banner.png",
169 actor_type: "Service",
176 background_image: nil,
178 confirmation_pending: false,
182 hide_favorites: true,
183 hide_followers: false,
185 hide_followers_count: false,
186 hide_follows_count: false,
188 skip_thread_containment: false,
189 accepts_chat_messages: nil
193 assert expected == AccountView.render("show.json", %{user: user, skip_visibility_check: true})
196 test "Represent a Funkwhale channel" do
198 User.get_or_fetch_by_ap_id(
199 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
203 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
205 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
206 assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
209 test "Represent a deactivated user for an admin" do
210 admin = insert(:user, is_admin: true)
211 deactivated_user = insert(:user, deactivated: true)
212 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
213 assert represented[:pleroma][:deactivated] == true
216 test "Represent a smaller mention" do
220 id: to_string(user.id),
222 username: user.nickname,
226 assert expected == AccountView.render("mention.json", %{user: user})
229 test "demands :for or :skip_visibility_check option for account rendering" do
230 clear_config([:restrict_unauthenticated, :profiles, :local], false)
235 assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: nil})
236 assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: user})
238 assert %{id: ^user_id} =
239 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
241 assert_raise RuntimeError, ~r/:skip_visibility_check or :for option is required/, fn ->
242 AccountView.render("show.json", %{user: user})
246 describe "relationship" do
247 defp test_relationship_rendering(user, other_user, expected_result) do
248 opts = %{user: user, target: other_user, relationships: nil}
249 assert expected_result == AccountView.render("relationship.json", opts)
251 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
252 opts = Map.put(opts, :relationships, relationships_opt)
253 assert expected_result == AccountView.render("relationship.json", opts)
255 assert [expected_result] ==
256 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
265 muting_notifications: false,
268 domain_blocking: false,
269 showing_reblogs: true,
273 test "represent a relationship for the following and followed user" do
275 other_user = insert(:user)
277 {:ok, user} = User.follow(user, other_user)
278 {:ok, other_user} = User.follow(other_user, user)
279 {:ok, _subscription} = User.subscribe(user, other_user)
280 {:ok, _user_relationships} = User.mute(user, other_user, %{notifications: true})
281 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
290 muting_notifications: true,
292 showing_reblogs: false,
293 id: to_string(other_user.id)
297 test_relationship_rendering(user, other_user, expected)
300 test "represent a relationship for the blocking and blocked user" do
302 other_user = insert(:user)
304 {:ok, user} = User.follow(user, other_user)
305 {:ok, _subscription} = User.subscribe(user, other_user)
306 {:ok, _user_relationship} = User.block(user, other_user)
307 {:ok, _user_relationship} = User.block(other_user, user)
312 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
315 test_relationship_rendering(user, other_user, expected)
318 test "represent a relationship for the user blocking a domain" do
320 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
322 {:ok, user} = User.block_domain(user, "bad.site")
327 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
330 test_relationship_rendering(user, other_user, expected)
333 test "represent a relationship for the user with a pending follow request" do
335 other_user = insert(:user, is_locked: true)
337 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
338 user = User.get_cached_by_id(user.id)
339 other_user = User.get_cached_by_id(other_user.id)
344 %{requested: true, following: false, id: to_string(other_user.id)}
347 test_relationship_rendering(user, other_user, expected)
351 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
352 user = insert(:user, pleroma_settings_store: %{fe: "test"})
355 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
357 assert result.pleroma.settings_store == %{:fe => "test"}
359 result = AccountView.render("show.json", %{user: user, for: nil, with_pleroma_settings: true})
360 assert result.pleroma[:settings_store] == nil
362 result = AccountView.render("show.json", %{user: user, for: user})
363 assert result.pleroma[:settings_store] == nil
366 test "doesn't sanitize display names" do
367 user = insert(:user, name: "<marquee> username </marquee>")
368 result = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
369 assert result.display_name == "<marquee> username </marquee>"
372 test "never display nil user follow counts" do
373 user = insert(:user, following_count: 0, follower_count: 0)
374 result = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
376 assert result.following_count == 0
377 assert result.followers_count == 0
380 describe "hiding follows/following" do
381 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
384 hide_followers: true,
385 hide_followers_count: true,
387 hide_follows_count: true
390 other_user = insert(:user)
391 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
392 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
397 pleroma: %{hide_follows_count: true, hide_followers_count: true}
398 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
401 test "shows when follows/followers are hidden" do
402 user = insert(:user, hide_followers: true, hide_follows: true)
403 other_user = insert(:user)
404 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
405 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
410 pleroma: %{hide_follows: true, hide_followers: true}
411 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
414 test "shows actual follower/following count to the account owner" do
415 user = insert(:user, hide_followers: true, hide_follows: true)
416 other_user = insert(:user)
417 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
419 assert User.following?(user, other_user)
420 assert Pleroma.FollowingRelationship.follower_count(other_user) == 1
421 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
426 } = AccountView.render("show.json", %{user: user, for: user})
429 test "shows unread_conversation_count only to the account owner" do
431 other_user = insert(:user)
434 CommonAPI.post(other_user, %{
435 status: "Hey @#{user.nickname}.",
439 user = User.get_cached_by_ap_id(user.ap_id)
441 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
442 :unread_conversation_count
445 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
446 :unread_conversation_count
450 test "shows unread_count only to the account owner" do
452 insert_list(7, :notification, user: user, activity: insert(:note_activity))
453 other_user = insert(:user)
455 user = User.get_cached_by_ap_id(user.ap_id)
457 assert AccountView.render(
459 %{user: user, for: other_user}
460 )[:pleroma][:unread_notifications_count] == nil
462 assert AccountView.render(
464 %{user: user, for: user}
465 )[:pleroma][:unread_notifications_count] == 7
469 describe "follow requests counter" do
470 test "shows zero when no follow requests are pending" do
473 assert %{follow_requests_count: 0} =
474 AccountView.render("show.json", %{user: user, for: user})
476 other_user = insert(:user)
477 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
479 assert %{follow_requests_count: 0} =
480 AccountView.render("show.json", %{user: user, for: user})
483 test "shows non-zero when follow requests are pending" do
484 user = insert(:user, is_locked: true)
486 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
488 other_user = insert(:user)
489 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
491 assert %{locked: true, follow_requests_count: 1} =
492 AccountView.render("show.json", %{user: user, for: user})
495 test "decreases when accepting a follow request" do
496 user = insert(:user, is_locked: true)
498 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
500 other_user = insert(:user)
501 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
503 assert %{locked: true, follow_requests_count: 1} =
504 AccountView.render("show.json", %{user: user, for: user})
506 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
508 assert %{locked: true, follow_requests_count: 0} =
509 AccountView.render("show.json", %{user: user, for: user})
512 test "decreases when rejecting a follow request" do
513 user = insert(:user, is_locked: true)
515 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
517 other_user = insert(:user)
518 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
520 assert %{locked: true, follow_requests_count: 1} =
521 AccountView.render("show.json", %{user: user, for: user})
523 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
525 assert %{locked: true, follow_requests_count: 0} =
526 AccountView.render("show.json", %{user: user, for: user})
529 test "shows non-zero when historical unapproved requests are present" do
530 user = insert(:user, is_locked: true)
532 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
534 other_user = insert(:user)
535 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
537 {:ok, user} = User.update_and_set_cache(user, %{is_locked: false})
539 assert %{locked: false, follow_requests_count: 1} =
540 AccountView.render("show.json", %{user: user, for: user})
544 test "uses mediaproxy urls when it's enabled (regardless of media preview proxy state)" do
545 clear_config([:media_proxy, :enabled], true)
546 clear_config([:media_preview_proxy, :enabled])
550 avatar: %{"url" => [%{"href" => "https://evil.website/avatar.png"}]},
551 banner: %{"url" => [%{"href" => "https://evil.website/banner.png"}]},
552 emoji: %{"joker_smile" => "https://evil.website/society.png"}
555 with media_preview_enabled <- [false, true] do
556 Config.put([:media_preview_proxy, :enabled], media_preview_enabled)
558 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
560 {key, url} when key in [:avatar, :avatar_static, :header, :header_static] ->
561 String.starts_with?(url, Pleroma.Web.base_url())
564 Enum.all?(emojis, fn %{url: url, static_url: static_url} ->
565 String.starts_with?(url, Pleroma.Web.base_url()) &&
566 String.starts_with?(static_url, Pleroma.Web.base_url())