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
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 '&<>\""
41 id: to_string(user.id),
44 display_name: user.name,
46 created_at: "2017-08-15T15:47:06.000Z",
50 note: "<span>valid html</span>. a<br/>b<br/>c<br/>d<br/>f '&<>"",
52 avatar: "http://localhost:4001/images/avi.png",
53 avatar_static: "http://localhost:4001/images/avi.png",
54 header: "http://localhost:4001/images/banner.png",
55 header_static: "http://localhost:4001/images/banner.png",
58 static_url: "/file.png",
60 shortcode: "karjalanpiirakka",
61 visible_in_picker: false
67 note: "valid html. a\nb\nc\nd\nf '&<>\"",
77 background_image: "https://example.com/images/asuka_hospital.png",
79 confirmation_pending: false,
84 hide_followers: false,
86 hide_followers_count: false,
87 hide_follows_count: false,
89 skip_thread_containment: false,
90 accepts_chat_messages: nil
94 assert expected == AccountView.render("show.json", %{user: user, skip_visibility_check: true})
102 test "is parsed when :instance_favicons is enabled", %{user: user} do
103 clear_config([:instances_favicons, :enabled], true)
108 "https://shitposter.club/plugins/Qvitter/img/gnusocial-favicons/favicon-16x16.png"
110 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
113 test "is nil when :instances_favicons is disabled", %{user: user} do
114 assert %{pleroma: %{favicon: nil}} =
115 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
119 test "Favicon when :instance_favicons is enabled" do
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",
178 background_image: nil,
180 confirmation_pending: false,
184 hide_favorites: true,
185 hide_followers: false,
187 hide_followers_count: false,
188 hide_follows_count: false,
190 skip_thread_containment: false,
191 accepts_chat_messages: nil
195 assert expected == AccountView.render("show.json", %{user: user, skip_visibility_check: true})
198 test "Represent a Funkwhale channel" do
200 User.get_or_fetch_by_ap_id(
201 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
205 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
207 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
208 assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
211 test "Represent a deactivated user for an admin" do
212 admin = insert(:user, is_admin: true)
213 deactivated_user = insert(:user, deactivated: true)
214 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
215 assert represented[:pleroma][:deactivated] == true
218 test "Represent a smaller mention" do
222 id: to_string(user.id),
224 username: user.nickname,
228 assert expected == AccountView.render("mention.json", %{user: user})
231 test "demands :for or :skip_visibility_check option for account rendering" do
232 clear_config([:restrict_unauthenticated, :profiles, :local], false)
237 assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: nil})
238 assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: user})
240 assert %{id: ^user_id} =
241 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
243 assert_raise RuntimeError, ~r/:skip_visibility_check or :for option is required/, fn ->
244 AccountView.render("show.json", %{user: user})
248 describe "relationship" do
249 defp test_relationship_rendering(user, other_user, expected_result) do
250 opts = %{user: user, target: other_user, relationships: nil}
251 assert expected_result == AccountView.render("relationship.json", opts)
253 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
254 opts = Map.put(opts, :relationships, relationships_opt)
255 assert expected_result == AccountView.render("relationship.json", opts)
257 assert [expected_result] ==
258 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
267 muting_notifications: false,
270 domain_blocking: false,
271 showing_reblogs: true,
275 test "represent a relationship for the following and followed user" do
277 other_user = insert(:user)
279 {:ok, user} = User.follow(user, other_user)
280 {:ok, other_user} = User.follow(other_user, user)
281 {:ok, _subscription} = User.subscribe(user, other_user)
282 {:ok, _user_relationships} = User.mute(user, other_user, true)
283 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
292 muting_notifications: true,
294 showing_reblogs: false,
295 id: to_string(other_user.id)
299 test_relationship_rendering(user, other_user, expected)
302 test "represent a relationship for the blocking and blocked user" do
304 other_user = insert(:user)
306 {:ok, user} = User.follow(user, other_user)
307 {:ok, _subscription} = User.subscribe(user, other_user)
308 {:ok, _user_relationship} = User.block(user, other_user)
309 {:ok, _user_relationship} = User.block(other_user, user)
314 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
317 test_relationship_rendering(user, other_user, expected)
320 test "represent a relationship for the user blocking a domain" do
322 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
324 {:ok, user} = User.block_domain(user, "bad.site")
329 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
332 test_relationship_rendering(user, other_user, expected)
335 test "represent a relationship for the user with a pending follow request" do
337 other_user = insert(:user, locked: true)
339 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
340 user = User.get_cached_by_id(user.id)
341 other_user = User.get_cached_by_id(other_user.id)
346 %{requested: true, following: false, id: to_string(other_user.id)}
349 test_relationship_rendering(user, other_user, expected)
353 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
354 user = insert(:user, pleroma_settings_store: %{fe: "test"})
357 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
359 assert result.pleroma.settings_store == %{:fe => "test"}
361 result = AccountView.render("show.json", %{user: user, for: nil, with_pleroma_settings: true})
362 assert result.pleroma[:settings_store] == nil
364 result = AccountView.render("show.json", %{user: user, for: user})
365 assert result.pleroma[:settings_store] == nil
368 test "doesn't sanitize display names" do
369 user = insert(:user, name: "<marquee> username </marquee>")
370 result = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
371 assert result.display_name == "<marquee> username </marquee>"
374 test "never display nil user follow counts" do
375 user = insert(:user, following_count: 0, follower_count: 0)
376 result = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
378 assert result.following_count == 0
379 assert result.followers_count == 0
382 describe "hiding follows/following" do
383 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
386 hide_followers: true,
387 hide_followers_count: true,
389 hide_follows_count: true
392 other_user = insert(:user)
393 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
394 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
399 pleroma: %{hide_follows_count: true, hide_followers_count: true}
400 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
403 test "shows when follows/followers are hidden" do
404 user = insert(:user, hide_followers: true, hide_follows: true)
405 other_user = insert(:user)
406 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
407 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
412 pleroma: %{hide_follows: true, hide_followers: true}
413 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
416 test "shows actual follower/following count to the account owner" do
417 user = insert(:user, hide_followers: true, hide_follows: true)
418 other_user = insert(:user)
419 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
421 assert User.following?(user, other_user)
422 assert Pleroma.FollowingRelationship.follower_count(other_user) == 1
423 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
428 } = AccountView.render("show.json", %{user: user, for: user})
431 test "shows unread_conversation_count only to the account owner" do
433 other_user = insert(:user)
436 CommonAPI.post(other_user, %{
437 status: "Hey @#{user.nickname}.",
441 user = User.get_cached_by_ap_id(user.ap_id)
443 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
444 :unread_conversation_count
447 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
448 :unread_conversation_count
452 test "shows unread_count only to the account owner" do
454 insert_list(7, :notification, user: user)
455 other_user = insert(:user)
457 user = User.get_cached_by_ap_id(user.ap_id)
459 assert AccountView.render(
461 %{user: user, for: other_user}
462 )[:pleroma][:unread_notifications_count] == nil
464 assert AccountView.render(
466 %{user: user, for: user}
467 )[:pleroma][:unread_notifications_count] == 7
471 describe "follow requests counter" do
472 test "shows zero when no follow requests are pending" do
475 assert %{follow_requests_count: 0} =
476 AccountView.render("show.json", %{user: user, for: user})
478 other_user = insert(:user)
479 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
481 assert %{follow_requests_count: 0} =
482 AccountView.render("show.json", %{user: user, for: user})
485 test "shows non-zero when follow requests are pending" do
486 user = insert(:user, locked: true)
488 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
490 other_user = insert(:user)
491 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
493 assert %{locked: true, follow_requests_count: 1} =
494 AccountView.render("show.json", %{user: user, for: user})
497 test "decreases when accepting a follow request" do
498 user = insert(:user, locked: true)
500 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
502 other_user = insert(:user)
503 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
505 assert %{locked: true, follow_requests_count: 1} =
506 AccountView.render("show.json", %{user: user, for: user})
508 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
510 assert %{locked: true, follow_requests_count: 0} =
511 AccountView.render("show.json", %{user: user, for: user})
514 test "decreases when rejecting a follow request" do
515 user = insert(:user, locked: true)
517 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
519 other_user = insert(:user)
520 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
522 assert %{locked: true, follow_requests_count: 1} =
523 AccountView.render("show.json", %{user: user, for: user})
525 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
527 assert %{locked: true, follow_requests_count: 0} =
528 AccountView.render("show.json", %{user: user, for: user})
531 test "shows non-zero when historical unapproved requests are present" do
532 user = insert(:user, locked: true)
534 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
536 other_user = insert(:user)
537 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
539 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
541 assert %{locked: false, follow_requests_count: 1} =
542 AccountView.render("show.json", %{user: user, for: user})
546 test "uses mediaproxy urls when it's enabled" do
547 clear_config([:media_proxy, :enabled], true)
551 avatar: %{"url" => [%{"href" => "https://evil.website/avatar.png"}]},
552 banner: %{"url" => [%{"href" => "https://evil.website/banner.png"}]},
553 emoji: %{"joker_smile" => "https://evil.website/society.png"}
556 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
558 {key, url} when key in [:avatar, :avatar_static, :header, :header_static] ->
559 String.starts_with?(url, Pleroma.Web.base_url())
562 Enum.all?(emojis, fn %{url: url, static_url: static_url} ->
563 String.starts_with?(url, Pleroma.Web.base_url()) &&
564 String.starts_with?(static_url, Pleroma.Web.base_url())