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 setup do: clear_config([:instances_favicons, :enabled])
24 test "Represent a user account" do
26 "url" => [%{"href" => "https://example.com/images/asuka_hospital.png"}]
33 background: background_image,
34 nickname: "shp@shitposter.club",
35 name: ":karjalanpiirakka: shp",
37 "<script src=\"invalid-html\"></script><span>valid html</span>. a<br>b<br/>c<br >d<br />f '&<>\"",
38 inserted_at: ~N[2017-08-15 15:47:06.597036],
39 emoji: %{"karjalanpiirakka" => "/file.png"},
40 raw_bio: "valid html. a\nb\nc\nd\nf '&<>\""
44 id: to_string(user.id),
47 display_name: user.name,
49 created_at: "2017-08-15T15:47:06.000Z",
53 note: "<span>valid html</span>. a<br/>b<br/>c<br/>d<br/>f '&<>"",
55 avatar: "http://localhost:4001/images/avi.png",
56 avatar_static: "http://localhost:4001/images/avi.png",
57 header: "http://localhost:4001/images/banner.png",
58 header_static: "http://localhost:4001/images/banner.png",
61 static_url: "/file.png",
63 shortcode: "karjalanpiirakka",
64 visible_in_picker: false
70 note: "valid html. a\nb\nc\nd\nf '&<>\"",
80 background_image: "https://example.com/images/asuka_hospital.png",
82 "https://shitposter.club/plugins/Qvitter/img/gnusocial-favicons/favicon-16x16.png",
83 confirmation_pending: false,
88 hide_followers: false,
90 hide_followers_count: false,
91 hide_follows_count: false,
93 skip_thread_containment: false,
94 accepts_chat_messages: nil
98 assert expected == AccountView.render("show.json", %{user: user, force: true})
101 test "Favicon is nil when :instances_favicons is disabled" do
104 Config.put([:instances_favicons, :enabled], true)
109 "https://shitposter.club/plugins/Qvitter/img/gnusocial-favicons/favicon-16x16.png"
111 } = AccountView.render("show.json", %{user: user, force: true})
113 Config.put([:instances_favicons, :enabled], false)
115 assert %{pleroma: %{favicon: nil}} =
116 AccountView.render("show.json", %{user: user, force: true})
119 test "Represent the user account for the account owner" do
122 notification_settings = %{
123 block_from_strangers: false,
124 hide_notification_contents: false
127 privacy = user.default_scope
130 pleroma: %{notification_settings: ^notification_settings, allow_following_move: true},
131 source: %{privacy: ^privacy}
132 } = AccountView.render("show.json", %{user: user, for: user})
135 test "Represent a Service(bot) account" do
140 actor_type: "Service",
141 nickname: "shp@shitposter.club",
142 inserted_at: ~N[2017-08-15 15:47:06.597036]
146 id: to_string(user.id),
149 display_name: user.name,
151 created_at: "2017-08-15T15:47:06.000Z",
157 avatar: "http://localhost:4001/images/avi.png",
158 avatar_static: "http://localhost:4001/images/avi.png",
159 header: "http://localhost:4001/images/banner.png",
160 header_static: "http://localhost:4001/images/banner.png",
168 actor_type: "Service",
175 background_image: nil,
177 "https://shitposter.club/plugins/Qvitter/img/gnusocial-favicons/favicon-16x16.png",
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, force: 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"
202 assert represented = AccountView.render("show.json", %{user: user, force: true})
203 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
204 assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
207 test "Represent a deactivated user for an admin" do
208 admin = insert(:user, is_admin: true)
209 deactivated_user = insert(:user, deactivated: true)
210 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
211 assert represented[:pleroma][:deactivated] == true
214 test "Represent a smaller mention" do
218 id: to_string(user.id),
220 username: user.nickname,
224 assert expected == AccountView.render("mention.json", %{user: user})
227 test "demands :for or :force option for account rendering" do
228 clear_config([:restrict_unauthenticated, :profiles, :local], false)
233 assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: nil})
234 assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: user})
235 assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, force: true})
237 assert_raise RuntimeError, ~r/:force or :for option is required/, fn ->
238 AccountView.render("show.json", %{user: user})
242 describe "relationship" do
243 defp test_relationship_rendering(user, other_user, expected_result) do
244 opts = %{user: user, target: other_user, relationships: nil}
245 assert expected_result == AccountView.render("relationship.json", opts)
247 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
248 opts = Map.put(opts, :relationships, relationships_opt)
249 assert expected_result == AccountView.render("relationship.json", opts)
251 assert [expected_result] ==
252 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
261 muting_notifications: false,
264 domain_blocking: false,
265 showing_reblogs: true,
269 test "represent a relationship for the following and followed user" do
271 other_user = insert(:user)
273 {:ok, user} = User.follow(user, other_user)
274 {:ok, other_user} = User.follow(other_user, user)
275 {:ok, _subscription} = User.subscribe(user, other_user)
276 {:ok, _user_relationships} = User.mute(user, other_user, true)
277 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
286 muting_notifications: true,
288 showing_reblogs: false,
289 id: to_string(other_user.id)
293 test_relationship_rendering(user, other_user, expected)
296 test "represent a relationship for the blocking and blocked user" do
298 other_user = insert(:user)
300 {:ok, user} = User.follow(user, other_user)
301 {:ok, _subscription} = User.subscribe(user, other_user)
302 {:ok, _user_relationship} = User.block(user, other_user)
303 {:ok, _user_relationship} = User.block(other_user, user)
308 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
311 test_relationship_rendering(user, other_user, expected)
314 test "represent a relationship for the user blocking a domain" do
316 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
318 {:ok, user} = User.block_domain(user, "bad.site")
323 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
326 test_relationship_rendering(user, other_user, expected)
329 test "represent a relationship for the user with a pending follow request" do
331 other_user = insert(:user, locked: true)
333 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
334 user = User.get_cached_by_id(user.id)
335 other_user = User.get_cached_by_id(other_user.id)
340 %{requested: true, following: false, id: to_string(other_user.id)}
343 test_relationship_rendering(user, other_user, expected)
347 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
348 user = insert(:user, pleroma_settings_store: %{fe: "test"})
351 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
353 assert result.pleroma.settings_store == %{:fe => "test"}
355 result = AccountView.render("show.json", %{user: user, for: nil, with_pleroma_settings: true})
356 assert result.pleroma[:settings_store] == nil
358 result = AccountView.render("show.json", %{user: user, for: user})
359 assert result.pleroma[:settings_store] == nil
362 test "doesn't sanitize display names" do
363 user = insert(:user, name: "<marquee> username </marquee>")
364 result = AccountView.render("show.json", %{user: user, force: true})
365 assert result.display_name == "<marquee> username </marquee>"
368 test "never display nil user follow counts" do
369 user = insert(:user, following_count: 0, follower_count: 0)
370 result = AccountView.render("show.json", %{user: user, force: true})
372 assert result.following_count == 0
373 assert result.followers_count == 0
376 describe "hiding follows/following" do
377 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
380 hide_followers: true,
381 hide_followers_count: true,
383 hide_follows_count: true
386 other_user = insert(:user)
387 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
388 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
393 pleroma: %{hide_follows_count: true, hide_followers_count: true}
394 } = AccountView.render("show.json", %{user: user, force: true})
397 test "shows when follows/followers are hidden" do
398 user = insert(:user, hide_followers: true, hide_follows: true)
399 other_user = insert(:user)
400 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
401 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
406 pleroma: %{hide_follows: true, hide_followers: true}
407 } = AccountView.render("show.json", %{user: user, force: true})
410 test "shows actual follower/following count to the account owner" do
411 user = insert(:user, hide_followers: true, hide_follows: true)
412 other_user = insert(:user)
413 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
415 assert User.following?(user, other_user)
416 assert Pleroma.FollowingRelationship.follower_count(other_user) == 1
417 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
422 } = AccountView.render("show.json", %{user: user, for: user})
425 test "shows unread_conversation_count only to the account owner" do
427 other_user = insert(:user)
430 CommonAPI.post(other_user, %{
431 status: "Hey @#{user.nickname}.",
435 user = User.get_cached_by_ap_id(user.ap_id)
437 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
438 :unread_conversation_count
441 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
442 :unread_conversation_count
446 test "shows unread_count only to the account owner" do
448 insert_list(7, :notification, user: user)
449 other_user = insert(:user)
451 user = User.get_cached_by_ap_id(user.ap_id)
453 assert AccountView.render(
455 %{user: user, for: other_user}
456 )[:pleroma][:unread_notifications_count] == nil
458 assert AccountView.render(
460 %{user: user, for: user}
461 )[:pleroma][:unread_notifications_count] == 7
465 describe "follow requests counter" do
466 test "shows zero when no follow requests are pending" do
469 assert %{follow_requests_count: 0} =
470 AccountView.render("show.json", %{user: user, for: user})
472 other_user = insert(:user)
473 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
475 assert %{follow_requests_count: 0} =
476 AccountView.render("show.json", %{user: user, for: user})
479 test "shows non-zero when follow requests are pending" do
480 user = insert(:user, locked: true)
482 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
484 other_user = insert(:user)
485 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
487 assert %{locked: true, follow_requests_count: 1} =
488 AccountView.render("show.json", %{user: user, for: user})
491 test "decreases when accepting a follow request" do
492 user = insert(:user, locked: true)
494 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
496 other_user = insert(:user)
497 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
499 assert %{locked: true, follow_requests_count: 1} =
500 AccountView.render("show.json", %{user: user, for: user})
502 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
504 assert %{locked: true, follow_requests_count: 0} =
505 AccountView.render("show.json", %{user: user, for: user})
508 test "decreases when rejecting a follow request" do
509 user = insert(:user, locked: true)
511 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
513 other_user = insert(:user)
514 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
516 assert %{locked: true, follow_requests_count: 1} =
517 AccountView.render("show.json", %{user: user, for: user})
519 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
521 assert %{locked: true, follow_requests_count: 0} =
522 AccountView.render("show.json", %{user: user, for: user})
525 test "shows non-zero when historical unapproved requests are present" do
526 user = insert(:user, locked: true)
528 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
530 other_user = insert(:user)
531 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
533 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
535 assert %{locked: false, follow_requests_count: 1} =
536 AccountView.render("show.json", %{user: user, for: user})
540 test "uses mediaproxy urls when it's enabled" do
541 clear_config([:media_proxy, :enabled], true)
545 avatar: %{"url" => [%{"href" => "https://evil.website/avatar.png"}]},
546 banner: %{"url" => [%{"href" => "https://evil.website/banner.png"}]},
547 emoji: %{"joker_smile" => "https://evil.website/society.png"}
550 AccountView.render("show.json", %{user: user, force: true})
552 {key, url} when key in [:avatar, :avatar_static, :header, :header_static] ->
553 String.starts_with?(url, Pleroma.Web.base_url())
556 Enum.all?(emojis, fn %{url: url, static_url: static_url} ->
557 String.starts_with?(url, Pleroma.Web.base_url()) &&
558 String.starts_with?(static_url, Pleroma.Web.base_url())