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",
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",
176 fqn: "shp@shitposter.club",
180 background_image: nil,
186 hide_favorites: true,
187 hide_followers: false,
189 hide_followers_count: false,
190 hide_follows_count: false,
192 skip_thread_containment: false,
193 accepts_chat_messages: nil
197 assert expected == AccountView.render("show.json", %{user: user, skip_visibility_check: true})
200 test "Represent a Funkwhale channel" do
202 User.get_or_fetch_by_ap_id(
203 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
207 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
209 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
210 assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
213 test "Represent a deactivated user for an admin" do
214 admin = insert(:user, is_admin: true)
215 deactivated_user = insert(:user, is_active: false)
216 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
217 assert represented[:pleroma][:deactivated] == true
220 test "Represent a smaller mention" do
224 id: to_string(user.id),
226 username: user.nickname,
230 assert expected == AccountView.render("mention.json", %{user: user})
233 test "demands :for or :skip_visibility_check option for account rendering" do
234 clear_config([:restrict_unauthenticated, :profiles, :local], false)
239 assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: nil})
240 assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: user})
242 assert %{id: ^user_id} =
243 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
245 assert_raise RuntimeError, ~r/:skip_visibility_check or :for option is required/, fn ->
246 AccountView.render("show.json", %{user: user})
250 describe "relationship" do
251 defp test_relationship_rendering(user, other_user, expected_result) do
252 opts = %{user: user, target: other_user, relationships: nil}
253 assert expected_result == AccountView.render("relationship.json", opts)
255 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
256 opts = Map.put(opts, :relationships, relationships_opt)
257 assert expected_result == AccountView.render("relationship.json", opts)
259 assert [expected_result] ==
260 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
269 muting_notifications: false,
272 domain_blocking: false,
273 showing_reblogs: true,
277 test "represent a relationship for the following and followed user" do
279 other_user = insert(:user)
281 {:ok, user, other_user} = User.follow(user, other_user)
282 {:ok, other_user, user} = User.follow(other_user, user)
283 {:ok, _subscription} = User.subscribe(user, other_user)
284 {:ok, _user_relationships} = User.mute(user, other_user, %{notifications: true})
285 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
294 muting_notifications: true,
296 showing_reblogs: false,
297 id: to_string(other_user.id)
301 test_relationship_rendering(user, other_user, expected)
304 test "represent a relationship for the blocking and blocked user" do
306 other_user = insert(:user)
308 {:ok, user, other_user} = User.follow(user, other_user)
309 {:ok, _subscription} = User.subscribe(user, other_user)
310 {:ok, _user_relationship} = User.block(user, other_user)
311 {:ok, _user_relationship} = User.block(other_user, user)
316 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
319 test_relationship_rendering(user, other_user, expected)
322 test "represent a relationship for the user blocking a domain" do
324 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
326 {:ok, user} = User.block_domain(user, "bad.site")
331 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
334 test_relationship_rendering(user, other_user, expected)
337 test "represent a relationship for the user with a pending follow request" do
339 other_user = insert(:user, is_locked: true)
341 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
342 user = User.get_cached_by_id(user.id)
343 other_user = User.get_cached_by_id(other_user.id)
348 %{requested: true, following: false, id: to_string(other_user.id)}
351 test_relationship_rendering(user, other_user, expected)
355 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
356 user = insert(:user, pleroma_settings_store: %{fe: "test"})
359 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
361 assert result.pleroma.settings_store == %{:fe => "test"}
363 result = AccountView.render("show.json", %{user: user, for: nil, with_pleroma_settings: true})
364 assert result.pleroma[:settings_store] == nil
366 result = AccountView.render("show.json", %{user: user, for: user})
367 assert result.pleroma[:settings_store] == nil
370 test "doesn't sanitize display names" do
371 user = insert(:user, name: "<marquee> username </marquee>")
372 result = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
373 assert result.display_name == "<marquee> username </marquee>"
376 test "never display nil user follow counts" do
377 user = insert(:user, following_count: 0, follower_count: 0)
378 result = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
380 assert result.following_count == 0
381 assert result.followers_count == 0
384 describe "hiding follows/following" do
385 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
388 hide_followers: true,
389 hide_followers_count: true,
391 hide_follows_count: true
394 other_user = insert(:user)
395 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
396 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
401 pleroma: %{hide_follows_count: true, hide_followers_count: true}
402 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
405 test "shows when follows/followers are hidden" do
406 user = insert(:user, hide_followers: true, hide_follows: true)
407 other_user = insert(:user)
408 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
409 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
414 pleroma: %{hide_follows: true, hide_followers: true}
415 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
418 test "shows actual follower/following count to the account owner" do
419 user = insert(:user, hide_followers: true, hide_follows: true)
420 other_user = insert(:user)
421 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
423 assert User.following?(user, other_user)
424 assert Pleroma.FollowingRelationship.follower_count(other_user) == 1
425 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
430 } = AccountView.render("show.json", %{user: user, for: user})
433 test "shows unread_conversation_count only to the account owner" do
435 other_user = insert(:user)
438 CommonAPI.post(other_user, %{
439 status: "Hey @#{user.nickname}.",
443 user = User.get_cached_by_ap_id(user.ap_id)
445 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
446 :unread_conversation_count
449 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
450 :unread_conversation_count
454 test "shows unread_count only to the account owner" do
456 insert_list(7, :notification, user: user, activity: insert(:note_activity))
457 other_user = insert(:user)
459 user = User.get_cached_by_ap_id(user.ap_id)
461 assert AccountView.render(
463 %{user: user, for: other_user}
464 )[:pleroma][:unread_notifications_count] == nil
466 assert AccountView.render(
468 %{user: user, for: user}
469 )[:pleroma][:unread_notifications_count] == 7
473 describe "follow requests counter" do
474 test "shows zero when no follow requests are pending" do
477 assert %{follow_requests_count: 0} =
478 AccountView.render("show.json", %{user: user, for: user})
480 other_user = insert(:user)
481 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
483 assert %{follow_requests_count: 0} =
484 AccountView.render("show.json", %{user: user, for: user})
487 test "shows non-zero when follow requests are pending" do
488 user = insert(:user, is_locked: true)
490 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
492 other_user = insert(:user)
493 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
495 assert %{locked: true, follow_requests_count: 1} =
496 AccountView.render("show.json", %{user: user, for: user})
499 test "decreases when accepting a follow request" do
500 user = insert(:user, is_locked: true)
502 assert %{locked: true} = 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 %{locked: true, follow_requests_count: 1} =
508 AccountView.render("show.json", %{user: user, for: user})
510 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
512 assert %{locked: true, follow_requests_count: 0} =
513 AccountView.render("show.json", %{user: user, for: user})
516 test "decreases when rejecting a follow request" do
517 user = insert(:user, is_locked: true)
519 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
521 other_user = insert(:user)
522 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
524 assert %{locked: true, follow_requests_count: 1} =
525 AccountView.render("show.json", %{user: user, for: user})
527 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
529 assert %{locked: true, follow_requests_count: 0} =
530 AccountView.render("show.json", %{user: user, for: user})
533 test "shows non-zero when historical unapproved requests are present" do
534 user = insert(:user, is_locked: true)
536 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
538 other_user = insert(:user)
539 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
541 {:ok, user} = User.update_and_set_cache(user, %{is_locked: false})
543 assert %{locked: false, follow_requests_count: 1} =
544 AccountView.render("show.json", %{user: user, for: user})
548 test "uses mediaproxy urls when it's enabled (regardless of media preview proxy state)" do
549 clear_config([:media_proxy, :enabled], true)
550 clear_config([:media_preview_proxy, :enabled])
554 avatar: %{"url" => [%{"href" => "https://evil.website/avatar.png"}]},
555 banner: %{"url" => [%{"href" => "https://evil.website/banner.png"}]},
556 emoji: %{"joker_smile" => "https://evil.website/society.png"}
559 with media_preview_enabled <- [false, true] do
560 clear_config([:media_preview_proxy, :enabled], media_preview_enabled)
562 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
564 {key, url} when key in [:avatar, :avatar_static, :header, :header_static] ->
565 String.starts_with?(url, Pleroma.Web.base_url())
568 Enum.all?(emojis, fn %{url: url, static_url: static_url} ->
569 String.starts_with?(url, Pleroma.Web.base_url()) &&
570 String.starts_with?(static_url, Pleroma.Web.base_url())