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})
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})
113 Config.put([:instances_favicons, :enabled], false)
115 assert %{pleroma: %{favicon: nil}} = AccountView.render("show.json", %{user: user})
118 test "Represent the user account for the account owner" do
121 notification_settings = %{
126 privacy_option: false
129 privacy = user.default_scope
132 pleroma: %{notification_settings: ^notification_settings, allow_following_move: true},
133 source: %{privacy: ^privacy}
134 } = AccountView.render("show.json", %{user: user, for: user})
137 test "Represent a Service(bot) account" do
142 actor_type: "Service",
143 nickname: "shp@shitposter.club",
144 inserted_at: ~N[2017-08-15 15:47:06.597036]
148 id: to_string(user.id),
151 display_name: user.name,
153 created_at: "2017-08-15T15:47:06.000Z",
159 avatar: "http://localhost:4001/images/avi.png",
160 avatar_static: "http://localhost:4001/images/avi.png",
161 header: "http://localhost:4001/images/banner.png",
162 header_static: "http://localhost:4001/images/banner.png",
170 actor_type: "Service",
177 background_image: nil,
179 "https://shitposter.club/plugins/Qvitter/img/gnusocial-favicons/favicon-16x16.png",
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})
198 test "Represent a Funkwhale channel" do
200 User.get_or_fetch_by_ap_id(
201 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
204 assert represented = AccountView.render("show.json", %{user: user})
205 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
206 assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
209 test "Represent a deactivated user for an admin" do
210 admin = insert(:user, is_admin: true)
211 deactivated_user = insert(:user, deactivated: true)
212 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
213 assert represented[:pleroma][:deactivated] == true
216 test "Represent a smaller mention" do
220 id: to_string(user.id),
222 username: user.nickname,
226 assert expected == AccountView.render("mention.json", %{user: user})
229 describe "relationship" do
230 defp test_relationship_rendering(user, other_user, expected_result) do
231 opts = %{user: user, target: other_user, relationships: nil}
232 assert expected_result == AccountView.render("relationship.json", opts)
234 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
235 opts = Map.put(opts, :relationships, relationships_opt)
236 assert expected_result == AccountView.render("relationship.json", opts)
238 assert [expected_result] ==
239 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
248 muting_notifications: false,
251 domain_blocking: false,
252 showing_reblogs: true,
256 test "represent a relationship for the following and followed user" do
258 other_user = insert(:user)
260 {:ok, user} = User.follow(user, other_user)
261 {:ok, other_user} = User.follow(other_user, user)
262 {:ok, _subscription} = User.subscribe(user, other_user)
263 {:ok, _user_relationships} = User.mute(user, other_user, true)
264 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
273 muting_notifications: true,
275 showing_reblogs: false,
276 id: to_string(other_user.id)
280 test_relationship_rendering(user, other_user, expected)
283 test "represent a relationship for the blocking and blocked user" do
285 other_user = insert(:user)
287 {:ok, user} = User.follow(user, other_user)
288 {:ok, _subscription} = User.subscribe(user, other_user)
289 {:ok, _user_relationship} = User.block(user, other_user)
290 {:ok, _user_relationship} = User.block(other_user, user)
295 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
298 test_relationship_rendering(user, other_user, expected)
301 test "represent a relationship for the user blocking a domain" do
303 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
305 {:ok, user} = User.block_domain(user, "bad.site")
310 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
313 test_relationship_rendering(user, other_user, expected)
316 test "represent a relationship for the user with a pending follow request" do
318 other_user = insert(:user, locked: true)
320 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
321 user = User.get_cached_by_id(user.id)
322 other_user = User.get_cached_by_id(other_user.id)
327 %{requested: true, following: false, id: to_string(other_user.id)}
330 test_relationship_rendering(user, other_user, expected)
334 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
335 user = insert(:user, pleroma_settings_store: %{fe: "test"})
338 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
340 assert result.pleroma.settings_store == %{:fe => "test"}
342 result = AccountView.render("show.json", %{user: user, with_pleroma_settings: true})
343 assert result.pleroma[:settings_store] == nil
345 result = AccountView.render("show.json", %{user: user, for: user})
346 assert result.pleroma[:settings_store] == nil
349 test "doesn't sanitize display names" do
350 user = insert(:user, name: "<marquee> username </marquee>")
351 result = AccountView.render("show.json", %{user: user})
352 assert result.display_name == "<marquee> username </marquee>"
355 test "never display nil user follow counts" do
356 user = insert(:user, following_count: 0, follower_count: 0)
357 result = AccountView.render("show.json", %{user: user})
359 assert result.following_count == 0
360 assert result.followers_count == 0
363 describe "hiding follows/following" do
364 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
367 hide_followers: true,
368 hide_followers_count: true,
370 hide_follows_count: true
373 other_user = insert(:user)
374 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
375 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
380 pleroma: %{hide_follows_count: true, hide_followers_count: true}
381 } = AccountView.render("show.json", %{user: user})
384 test "shows when follows/followers are hidden" do
385 user = insert(:user, hide_followers: true, hide_follows: 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: true, hide_followers: true}
394 } = AccountView.render("show.json", %{user: user})
397 test "shows actual follower/following count to the account owner" 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)
402 assert User.following?(user, other_user)
403 assert Pleroma.FollowingRelationship.follower_count(other_user) == 1
404 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
409 } = AccountView.render("show.json", %{user: user, for: user})
412 test "shows unread_conversation_count only to the account owner" do
414 other_user = insert(:user)
417 CommonAPI.post(other_user, %{
418 status: "Hey @#{user.nickname}.",
422 user = User.get_cached_by_ap_id(user.ap_id)
424 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
425 :unread_conversation_count
428 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
429 :unread_conversation_count
433 test "shows unread_count only to the account owner" do
435 insert_list(7, :notification, user: user)
436 other_user = insert(:user)
438 user = User.get_cached_by_ap_id(user.ap_id)
440 assert AccountView.render(
442 %{user: user, for: other_user}
443 )[:pleroma][:unread_notifications_count] == nil
445 assert AccountView.render(
447 %{user: user, for: user}
448 )[:pleroma][:unread_notifications_count] == 7
452 describe "follow requests counter" do
453 test "shows zero when no follow requests are pending" do
456 assert %{follow_requests_count: 0} =
457 AccountView.render("show.json", %{user: user, for: user})
459 other_user = insert(:user)
460 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
462 assert %{follow_requests_count: 0} =
463 AccountView.render("show.json", %{user: user, for: user})
466 test "shows non-zero when follow requests are pending" do
467 user = insert(:user, locked: true)
469 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
471 other_user = insert(:user)
472 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
474 assert %{locked: true, follow_requests_count: 1} =
475 AccountView.render("show.json", %{user: user, for: user})
478 test "decreases when accepting a follow request" do
479 user = insert(:user, locked: true)
481 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
483 other_user = insert(:user)
484 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
486 assert %{locked: true, follow_requests_count: 1} =
487 AccountView.render("show.json", %{user: user, for: user})
489 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
491 assert %{locked: true, follow_requests_count: 0} =
492 AccountView.render("show.json", %{user: user, for: user})
495 test "decreases when rejecting a follow request" do
496 user = insert(:user, locked: true)
498 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
500 other_user = insert(:user)
501 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
503 assert %{locked: true, follow_requests_count: 1} =
504 AccountView.render("show.json", %{user: user, for: user})
506 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
508 assert %{locked: true, follow_requests_count: 0} =
509 AccountView.render("show.json", %{user: user, for: user})
512 test "shows non-zero when historical unapproved requests are present" do
513 user = insert(:user, locked: true)
515 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
517 other_user = insert(:user)
518 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
520 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
522 assert %{locked: false, follow_requests_count: 1} =
523 AccountView.render("show.json", %{user: user, for: user})
527 test "uses mediaproxy urls when it's enabled" do
528 clear_config([:media_proxy, :enabled], true)
532 avatar: %{"url" => [%{"href" => "https://evil.website/avatar.png"}]},
533 banner: %{"url" => [%{"href" => "https://evil.website/banner.png"}]},
534 emoji: %{"joker_smile" => "https://evil.website/society.png"}
537 AccountView.render("show.json", %{user: user})
539 {key, url} when key in [:avatar, :avatar_static, :header, :header_static] ->
540 String.starts_with?(url, Pleroma.Web.base_url())
543 Enum.all?(emojis, fn %{url: url, static_url: static_url} ->
544 String.starts_with?(url, Pleroma.Web.base_url()) &&
545 String.starts_with?(static_url, Pleroma.Web.base_url())