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
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 '&<>\"",
39 also_known_as: ["https://shitposter.zone/users/shp"]
43 id: to_string(user.id),
46 display_name: user.name,
48 created_at: "2017-08-15T15:47:06.000Z",
52 note: "<span>valid html</span>. a<br/>b<br/>c<br/>d<br/>f '&<>"",
54 avatar: "http://localhost:4001/images/avi.png",
55 avatar_static: "http://localhost:4001/images/avi.png",
56 header: "http://localhost:4001/images/banner.png",
57 header_static: "http://localhost:4001/images/banner.png",
60 static_url: "/file.png",
62 shortcode: "karjalanpiirakka",
63 visible_in_picker: false
69 note: "valid html. a\nb\nc\nd\nf '&<>\"",
79 also_known_as: ["https://shitposter.zone/users/shp"],
80 background_image: "https://example.com/images/asuka_hospital.png",
87 hide_followers: false,
89 hide_followers_count: false,
90 hide_follows_count: false,
92 skip_thread_containment: false,
93 accepts_chat_messages: nil
97 assert expected == AccountView.render("show.json", %{user: user, skip_visibility_check: true})
100 describe "favicon" do
102 [user: insert(:user)]
105 test "is parsed when :instance_favicons is enabled", %{user: user} do
106 clear_config([:instances_favicons, :enabled], true)
111 "https://shitposter.club/plugins/Qvitter/img/gnusocial-favicons/favicon-16x16.png"
113 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
116 test "is nil when :instances_favicons is disabled", %{user: user} do
117 assert %{pleroma: %{favicon: nil}} =
118 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
122 test "Represent the user account for the account owner" do
125 notification_settings = %{
126 block_from_strangers: false,
127 hide_notification_contents: false
130 privacy = user.default_scope
133 pleroma: %{notification_settings: ^notification_settings, allow_following_move: true},
134 source: %{privacy: ^privacy}
135 } = AccountView.render("show.json", %{user: user, for: user})
138 test "Represent a Service(bot) account" do
143 actor_type: "Service",
144 nickname: "shp@shitposter.club",
145 inserted_at: ~N[2017-08-15 15:47:06.597036]
149 id: to_string(user.id),
152 display_name: user.name,
154 created_at: "2017-08-15T15:47:06.000Z",
160 avatar: "http://localhost:4001/images/avi.png",
161 avatar_static: "http://localhost:4001/images/avi.png",
162 header: "http://localhost:4001/images/banner.png",
163 header_static: "http://localhost:4001/images/banner.png",
171 actor_type: "Service",
179 background_image: nil,
185 hide_favorites: true,
186 hide_followers: false,
188 hide_followers_count: false,
189 hide_follows_count: false,
191 skip_thread_containment: false,
192 accepts_chat_messages: nil
196 assert expected == AccountView.render("show.json", %{user: user, skip_visibility_check: true})
199 test "Represent a Funkwhale channel" do
201 User.get_or_fetch_by_ap_id(
202 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
206 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
208 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
209 assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
212 test "Represent a deactivated user for an admin" do
213 admin = insert(:user, is_admin: true)
214 deactivated_user = insert(:user, deactivated: true)
215 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
216 assert represented[:pleroma][:deactivated] == true
219 test "Represent a smaller mention" do
223 id: to_string(user.id),
225 username: user.nickname,
229 assert expected == AccountView.render("mention.json", %{user: user})
232 test "demands :for or :skip_visibility_check option for account rendering" do
233 clear_config([:restrict_unauthenticated, :profiles, :local], false)
238 assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: nil})
239 assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: user})
241 assert %{id: ^user_id} =
242 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
244 assert_raise RuntimeError, ~r/:skip_visibility_check or :for option is required/, fn ->
245 AccountView.render("show.json", %{user: user})
249 describe "relationship" do
250 defp test_relationship_rendering(user, other_user, expected_result) do
251 opts = %{user: user, target: other_user, relationships: nil}
252 assert expected_result == AccountView.render("relationship.json", opts)
254 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
255 opts = Map.put(opts, :relationships, relationships_opt)
256 assert expected_result == AccountView.render("relationship.json", opts)
258 assert [expected_result] ==
259 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
268 muting_notifications: false,
271 domain_blocking: false,
272 showing_reblogs: true,
276 test "represent a relationship for the following and followed user" do
278 other_user = insert(:user)
280 {:ok, user, other_user} = User.follow(user, other_user)
281 {:ok, other_user, user} = User.follow(other_user, user)
282 {:ok, _subscription} = User.subscribe(user, other_user)
283 {:ok, _user_relationships} = User.mute(user, other_user, %{notifications: true})
284 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
293 muting_notifications: true,
295 showing_reblogs: false,
296 id: to_string(other_user.id)
300 test_relationship_rendering(user, other_user, expected)
303 test "represent a relationship for the blocking and blocked user" do
305 other_user = insert(:user)
307 {:ok, user, other_user} = User.follow(user, other_user)
308 {:ok, _subscription} = User.subscribe(user, other_user)
309 {:ok, _user_relationship} = User.block(user, other_user)
310 {:ok, _user_relationship} = User.block(other_user, user)
315 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
318 test_relationship_rendering(user, other_user, expected)
321 test "represent a relationship for the user blocking a domain" do
323 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
325 {:ok, user} = User.block_domain(user, "bad.site")
330 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
333 test_relationship_rendering(user, other_user, expected)
336 test "represent a relationship for the user with a pending follow request" do
338 other_user = insert(:user, is_locked: true)
340 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
341 user = User.get_cached_by_id(user.id)
342 other_user = User.get_cached_by_id(other_user.id)
347 %{requested: true, following: false, id: to_string(other_user.id)}
350 test_relationship_rendering(user, other_user, expected)
354 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
355 user = insert(:user, pleroma_settings_store: %{fe: "test"})
358 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
360 assert result.pleroma.settings_store == %{:fe => "test"}
362 result = AccountView.render("show.json", %{user: user, for: nil, with_pleroma_settings: true})
363 assert result.pleroma[:settings_store] == nil
365 result = AccountView.render("show.json", %{user: user, for: user})
366 assert result.pleroma[:settings_store] == nil
369 test "doesn't sanitize display names" do
370 user = insert(:user, name: "<marquee> username </marquee>")
371 result = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
372 assert result.display_name == "<marquee> username </marquee>"
375 test "never display nil user follow counts" do
376 user = insert(:user, following_count: 0, follower_count: 0)
377 result = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
379 assert result.following_count == 0
380 assert result.followers_count == 0
383 describe "hiding follows/following" do
384 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
387 hide_followers: true,
388 hide_followers_count: true,
390 hide_follows_count: true
393 other_user = insert(:user)
394 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
395 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
400 pleroma: %{hide_follows_count: true, hide_followers_count: true}
401 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
404 test "shows when follows/followers are hidden" do
405 user = insert(:user, hide_followers: true, hide_follows: true)
406 other_user = insert(:user)
407 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
408 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
413 pleroma: %{hide_follows: true, hide_followers: true}
414 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
417 test "shows actual follower/following count to the account owner" do
418 user = insert(:user, hide_followers: true, hide_follows: true)
419 other_user = insert(:user)
420 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
422 assert User.following?(user, other_user)
423 assert Pleroma.FollowingRelationship.follower_count(other_user) == 1
424 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
429 } = AccountView.render("show.json", %{user: user, for: user})
432 test "shows unread_conversation_count only to the account owner" do
434 other_user = insert(:user)
437 CommonAPI.post(other_user, %{
438 status: "Hey @#{user.nickname}.",
442 user = User.get_cached_by_ap_id(user.ap_id)
444 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
445 :unread_conversation_count
448 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
449 :unread_conversation_count
453 test "shows unread_count only to the account owner" do
455 insert_list(7, :notification, user: user, activity: insert(:note_activity))
456 other_user = insert(:user)
458 user = User.get_cached_by_ap_id(user.ap_id)
460 assert AccountView.render(
462 %{user: user, for: other_user}
463 )[:pleroma][:unread_notifications_count] == nil
465 assert AccountView.render(
467 %{user: user, for: user}
468 )[:pleroma][:unread_notifications_count] == 7
472 describe "follow requests counter" do
473 test "shows zero when no follow requests are pending" do
476 assert %{follow_requests_count: 0} =
477 AccountView.render("show.json", %{user: user, for: user})
479 other_user = insert(:user)
480 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
482 assert %{follow_requests_count: 0} =
483 AccountView.render("show.json", %{user: user, for: user})
486 test "shows non-zero when follow requests are pending" do
487 user = insert(:user, is_locked: true)
489 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
491 other_user = insert(:user)
492 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
494 assert %{locked: true, follow_requests_count: 1} =
495 AccountView.render("show.json", %{user: user, for: user})
498 test "decreases when accepting a follow request" do
499 user = insert(:user, is_locked: true)
501 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
503 other_user = insert(:user)
504 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
506 assert %{locked: true, follow_requests_count: 1} =
507 AccountView.render("show.json", %{user: user, for: user})
509 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
511 assert %{locked: true, follow_requests_count: 0} =
512 AccountView.render("show.json", %{user: user, for: user})
515 test "decreases when rejecting a follow request" do
516 user = insert(:user, is_locked: true)
518 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
520 other_user = insert(:user)
521 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
523 assert %{locked: true, follow_requests_count: 1} =
524 AccountView.render("show.json", %{user: user, for: user})
526 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
528 assert %{locked: true, follow_requests_count: 0} =
529 AccountView.render("show.json", %{user: user, for: user})
532 test "shows non-zero when historical unapproved requests are present" do
533 user = insert(:user, is_locked: true)
535 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
537 other_user = insert(:user)
538 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
540 {:ok, user} = User.update_and_set_cache(user, %{is_locked: false})
542 assert %{locked: false, follow_requests_count: 1} =
543 AccountView.render("show.json", %{user: user, for: user})
547 test "uses mediaproxy urls when it's enabled (regardless of media preview proxy state)" do
548 clear_config([:media_proxy, :enabled], true)
549 clear_config([:media_preview_proxy, :enabled])
553 avatar: %{"url" => [%{"href" => "https://evil.website/avatar.png"}]},
554 banner: %{"url" => [%{"href" => "https://evil.website/banner.png"}]},
555 emoji: %{"joker_smile" => "https://evil.website/society.png"}
558 with media_preview_enabled <- [false, true] do
559 Config.put([:media_preview_proxy, :enabled], media_preview_enabled)
561 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
563 {key, url} when key in [:avatar, :avatar_static, :header, :header_static] ->
564 String.starts_with?(url, Pleroma.Web.base_url())
567 Enum.all?(emojis, fn %{url: url, static_url: static_url} ->
568 String.starts_with?(url, Pleroma.Web.base_url()) &&
569 String.starts_with?(static_url, Pleroma.Web.base_url())