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",
78 confirmation_pending: false,
83 hide_followers: false,
85 hide_followers_count: false,
86 hide_follows_count: false,
88 skip_thread_containment: false,
89 accepts_chat_messages: nil
93 assert expected == AccountView.render("show.json", %{user: user})
96 test "Represent the user account for the account owner" do
99 notification_settings = %{
104 privacy_option: false
107 privacy = user.default_scope
110 pleroma: %{notification_settings: ^notification_settings, allow_following_move: true},
111 source: %{privacy: ^privacy}
112 } = AccountView.render("show.json", %{user: user, for: user})
115 test "Represent a Service(bot) account" do
120 actor_type: "Service",
121 nickname: "shp@shitposter.club",
122 inserted_at: ~N[2017-08-15 15:47:06.597036]
126 id: to_string(user.id),
129 display_name: user.name,
131 created_at: "2017-08-15T15:47:06.000Z",
137 avatar: "http://localhost:4001/images/avi.png",
138 avatar_static: "http://localhost:4001/images/avi.png",
139 header: "http://localhost:4001/images/banner.png",
140 header_static: "http://localhost:4001/images/banner.png",
148 actor_type: "Service",
155 background_image: nil,
156 confirmation_pending: false,
160 hide_favorites: true,
161 hide_followers: false,
163 hide_followers_count: false,
164 hide_follows_count: false,
166 skip_thread_containment: false,
167 accepts_chat_messages: nil
171 assert expected == AccountView.render("show.json", %{user: user})
174 test "Represent a Funkwhale channel" do
176 User.get_or_fetch_by_ap_id(
177 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
180 assert represented = AccountView.render("show.json", %{user: user})
181 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
182 assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
185 test "Represent a deactivated user for an admin" do
186 admin = insert(:user, is_admin: true)
187 deactivated_user = insert(:user, deactivated: true)
188 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
189 assert represented[:pleroma][:deactivated] == true
192 test "Represent a smaller mention" do
196 id: to_string(user.id),
198 username: user.nickname,
202 assert expected == AccountView.render("mention.json", %{user: user})
205 describe "relationship" do
206 defp test_relationship_rendering(user, other_user, expected_result) do
207 opts = %{user: user, target: other_user, relationships: nil}
208 assert expected_result == AccountView.render("relationship.json", opts)
210 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
211 opts = Map.put(opts, :relationships, relationships_opt)
212 assert expected_result == AccountView.render("relationship.json", opts)
214 assert [expected_result] ==
215 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
224 muting_notifications: false,
227 domain_blocking: false,
228 showing_reblogs: true,
232 test "represent a relationship for the following and followed user" do
234 other_user = insert(:user)
236 {:ok, user} = User.follow(user, other_user)
237 {:ok, other_user} = User.follow(other_user, user)
238 {:ok, _subscription} = User.subscribe(user, other_user)
239 {:ok, _user_relationships} = User.mute(user, other_user, true)
240 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
249 muting_notifications: true,
251 showing_reblogs: false,
252 id: to_string(other_user.id)
256 test_relationship_rendering(user, other_user, expected)
259 test "represent a relationship for the blocking and blocked user" do
261 other_user = insert(:user)
263 {:ok, user} = User.follow(user, other_user)
264 {:ok, _subscription} = User.subscribe(user, other_user)
265 {:ok, _user_relationship} = User.block(user, other_user)
266 {:ok, _user_relationship} = User.block(other_user, user)
271 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
274 test_relationship_rendering(user, other_user, expected)
277 test "represent a relationship for the user blocking a domain" do
279 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
281 {:ok, user} = User.block_domain(user, "bad.site")
286 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
289 test_relationship_rendering(user, other_user, expected)
292 test "represent a relationship for the user with a pending follow request" do
294 other_user = insert(:user, locked: true)
296 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
297 user = User.get_cached_by_id(user.id)
298 other_user = User.get_cached_by_id(other_user.id)
303 %{requested: true, following: false, id: to_string(other_user.id)}
306 test_relationship_rendering(user, other_user, expected)
310 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
311 user = insert(:user, pleroma_settings_store: %{fe: "test"})
314 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
316 assert result.pleroma.settings_store == %{:fe => "test"}
318 result = AccountView.render("show.json", %{user: user, with_pleroma_settings: true})
319 assert result.pleroma[:settings_store] == nil
321 result = AccountView.render("show.json", %{user: user, for: user})
322 assert result.pleroma[:settings_store] == nil
325 test "doesn't sanitize display names" do
326 user = insert(:user, name: "<marquee> username </marquee>")
327 result = AccountView.render("show.json", %{user: user})
328 assert result.display_name == "<marquee> username </marquee>"
331 test "never display nil user follow counts" do
332 user = insert(:user, following_count: 0, follower_count: 0)
333 result = AccountView.render("show.json", %{user: user})
335 assert result.following_count == 0
336 assert result.followers_count == 0
339 describe "hiding follows/following" do
340 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
343 hide_followers: true,
344 hide_followers_count: true,
346 hide_follows_count: true
349 other_user = insert(:user)
350 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
351 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
356 pleroma: %{hide_follows_count: true, hide_followers_count: true}
357 } = AccountView.render("show.json", %{user: user})
360 test "shows when follows/followers are hidden" do
361 user = insert(:user, hide_followers: true, hide_follows: true)
362 other_user = insert(:user)
363 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
364 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
369 pleroma: %{hide_follows: true, hide_followers: true}
370 } = AccountView.render("show.json", %{user: user})
373 test "shows actual follower/following count to the account owner" do
374 user = insert(:user, hide_followers: true, hide_follows: true)
375 other_user = insert(:user)
376 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
377 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
382 } = AccountView.render("show.json", %{user: user, for: user})
385 test "shows unread_conversation_count only to the account owner" do
387 other_user = insert(:user)
390 CommonAPI.post(other_user, %{
391 status: "Hey @#{user.nickname}.",
395 user = User.get_cached_by_ap_id(user.ap_id)
397 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
398 :unread_conversation_count
401 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
402 :unread_conversation_count
406 test "shows unread_count only to the account owner" do
408 insert_list(7, :notification, user: user)
409 other_user = insert(:user)
411 user = User.get_cached_by_ap_id(user.ap_id)
413 assert AccountView.render(
415 %{user: user, for: other_user}
416 )[:pleroma][:unread_notifications_count] == nil
418 assert AccountView.render(
420 %{user: user, for: user}
421 )[:pleroma][:unread_notifications_count] == 7
425 describe "follow requests counter" do
426 test "shows zero when no follow requests are pending" do
429 assert %{follow_requests_count: 0} =
430 AccountView.render("show.json", %{user: user, for: user})
432 other_user = insert(:user)
433 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
435 assert %{follow_requests_count: 0} =
436 AccountView.render("show.json", %{user: user, for: user})
439 test "shows non-zero when follow requests are pending" do
440 user = insert(:user, locked: true)
442 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
444 other_user = insert(:user)
445 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
447 assert %{locked: true, follow_requests_count: 1} =
448 AccountView.render("show.json", %{user: user, for: user})
451 test "decreases when accepting a follow request" do
452 user = insert(:user, locked: true)
454 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
456 other_user = insert(:user)
457 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
459 assert %{locked: true, follow_requests_count: 1} =
460 AccountView.render("show.json", %{user: user, for: user})
462 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
464 assert %{locked: true, follow_requests_count: 0} =
465 AccountView.render("show.json", %{user: user, for: user})
468 test "decreases when rejecting a follow request" do
469 user = insert(:user, locked: true)
471 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
473 other_user = insert(:user)
474 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
476 assert %{locked: true, follow_requests_count: 1} =
477 AccountView.render("show.json", %{user: user, for: user})
479 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
481 assert %{locked: true, follow_requests_count: 0} =
482 AccountView.render("show.json", %{user: user, for: user})
485 test "shows non-zero when historical unapproved requests are present" 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 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
495 assert %{locked: false, follow_requests_count: 1} =
496 AccountView.render("show.json", %{user: user, for: user})
500 test "uses mediaproxy urls when it's enabled" do
501 clear_config([:media_proxy, :enabled], true)
505 avatar: %{"url" => [%{"href" => "https://evil.website/avatar.png"}]},
506 banner: %{"url" => [%{"href" => "https://evil.website/banner.png"}]},
507 emoji: %{"joker_smile" => "https://evil.website/society.png"}
510 AccountView.render("show.json", %{user: user})
512 {key, url} when key in [:avatar, :avatar_static, :header, :header_static] ->
513 String.starts_with?(url, Pleroma.Web.base_url())
516 Enum.all?(emojis, fn %{url: url, static_url: static_url} ->
517 String.starts_with?(url, Pleroma.Web.base_url()) &&
518 String.starts_with?(static_url, Pleroma.Web.base_url())