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 = %{
122 block_from_strangers: false,
123 hide_notification_contents: false
126 privacy = user.default_scope
129 pleroma: %{notification_settings: ^notification_settings, allow_following_move: true},
130 source: %{privacy: ^privacy}
131 } = AccountView.render("show.json", %{user: user, for: user})
134 test "Represent a Service(bot) account" do
139 actor_type: "Service",
140 nickname: "shp@shitposter.club",
141 inserted_at: ~N[2017-08-15 15:47:06.597036]
145 id: to_string(user.id),
148 display_name: user.name,
150 created_at: "2017-08-15T15:47:06.000Z",
156 avatar: "http://localhost:4001/images/avi.png",
157 avatar_static: "http://localhost:4001/images/avi.png",
158 header: "http://localhost:4001/images/banner.png",
159 header_static: "http://localhost:4001/images/banner.png",
167 actor_type: "Service",
174 background_image: nil,
176 "https://shitposter.club/plugins/Qvitter/img/gnusocial-favicons/favicon-16x16.png",
177 confirmation_pending: false,
181 hide_favorites: true,
182 hide_followers: false,
184 hide_followers_count: false,
185 hide_follows_count: false,
187 skip_thread_containment: false,
188 accepts_chat_messages: nil
192 assert expected == AccountView.render("show.json", %{user: user})
195 test "Represent a Funkwhale channel" do
197 User.get_or_fetch_by_ap_id(
198 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
201 assert represented = AccountView.render("show.json", %{user: user})
202 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
203 assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
206 test "Represent a deactivated user for an admin" do
207 admin = insert(:user, is_admin: true)
208 deactivated_user = insert(:user, deactivated: true)
209 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
210 assert represented[:pleroma][:deactivated] == true
213 test "Represent a smaller mention" do
217 id: to_string(user.id),
219 username: user.nickname,
223 assert expected == AccountView.render("mention.json", %{user: user})
226 describe "relationship" do
227 defp test_relationship_rendering(user, other_user, expected_result) do
228 opts = %{user: user, target: other_user, relationships: nil}
229 assert expected_result == AccountView.render("relationship.json", opts)
231 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
232 opts = Map.put(opts, :relationships, relationships_opt)
233 assert expected_result == AccountView.render("relationship.json", opts)
235 assert [expected_result] ==
236 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
245 muting_notifications: false,
248 domain_blocking: false,
249 showing_reblogs: true,
253 test "represent a relationship for the following and followed user" do
255 other_user = insert(:user)
257 {:ok, user} = User.follow(user, other_user)
258 {:ok, other_user} = User.follow(other_user, user)
259 {:ok, _subscription} = User.subscribe(user, other_user)
260 {:ok, _user_relationships} = User.mute(user, other_user, true)
261 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
270 muting_notifications: true,
272 showing_reblogs: false,
273 id: to_string(other_user.id)
277 test_relationship_rendering(user, other_user, expected)
280 test "represent a relationship for the blocking and blocked user" do
282 other_user = insert(:user)
284 {:ok, user} = User.follow(user, other_user)
285 {:ok, _subscription} = User.subscribe(user, other_user)
286 {:ok, _user_relationship} = User.block(user, other_user)
287 {:ok, _user_relationship} = User.block(other_user, user)
292 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
295 test_relationship_rendering(user, other_user, expected)
298 test "represent a relationship for the user blocking a domain" do
300 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
302 {:ok, user} = User.block_domain(user, "bad.site")
307 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
310 test_relationship_rendering(user, other_user, expected)
313 test "represent a relationship for the user with a pending follow request" do
315 other_user = insert(:user, locked: true)
317 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
318 user = User.get_cached_by_id(user.id)
319 other_user = User.get_cached_by_id(other_user.id)
324 %{requested: true, following: false, id: to_string(other_user.id)}
327 test_relationship_rendering(user, other_user, expected)
331 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
332 user = insert(:user, pleroma_settings_store: %{fe: "test"})
335 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
337 assert result.pleroma.settings_store == %{:fe => "test"}
339 result = AccountView.render("show.json", %{user: user, with_pleroma_settings: true})
340 assert result.pleroma[:settings_store] == nil
342 result = AccountView.render("show.json", %{user: user, for: user})
343 assert result.pleroma[:settings_store] == nil
346 test "doesn't sanitize display names" do
347 user = insert(:user, name: "<marquee> username </marquee>")
348 result = AccountView.render("show.json", %{user: user})
349 assert result.display_name == "<marquee> username </marquee>"
352 test "never display nil user follow counts" do
353 user = insert(:user, following_count: 0, follower_count: 0)
354 result = AccountView.render("show.json", %{user: user})
356 assert result.following_count == 0
357 assert result.followers_count == 0
360 describe "hiding follows/following" do
361 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
364 hide_followers: true,
365 hide_followers_count: true,
367 hide_follows_count: true
370 other_user = insert(:user)
371 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
372 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
377 pleroma: %{hide_follows_count: true, hide_followers_count: true}
378 } = AccountView.render("show.json", %{user: user})
381 test "shows when follows/followers are hidden" do
382 user = insert(:user, hide_followers: true, hide_follows: true)
383 other_user = insert(:user)
384 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
385 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
390 pleroma: %{hide_follows: true, hide_followers: true}
391 } = AccountView.render("show.json", %{user: user})
394 test "shows actual follower/following count to the account owner" do
395 user = insert(:user, hide_followers: true, hide_follows: true)
396 other_user = insert(:user)
397 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
399 assert User.following?(user, other_user)
400 assert Pleroma.FollowingRelationship.follower_count(other_user) == 1
401 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
406 } = AccountView.render("show.json", %{user: user, for: user})
409 test "shows unread_conversation_count only to the account owner" do
411 other_user = insert(:user)
414 CommonAPI.post(other_user, %{
415 status: "Hey @#{user.nickname}.",
419 user = User.get_cached_by_ap_id(user.ap_id)
421 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
422 :unread_conversation_count
425 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
426 :unread_conversation_count
430 test "shows unread_count only to the account owner" do
432 insert_list(7, :notification, user: user)
433 other_user = insert(:user)
435 user = User.get_cached_by_ap_id(user.ap_id)
437 assert AccountView.render(
439 %{user: user, for: other_user}
440 )[:pleroma][:unread_notifications_count] == nil
442 assert AccountView.render(
444 %{user: user, for: user}
445 )[:pleroma][:unread_notifications_count] == 7
449 describe "follow requests counter" do
450 test "shows zero when no follow requests are pending" do
453 assert %{follow_requests_count: 0} =
454 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 %{follow_requests_count: 0} =
460 AccountView.render("show.json", %{user: user, for: user})
463 test "shows non-zero when follow requests are pending" do
464 user = insert(:user, locked: true)
466 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
468 other_user = insert(:user)
469 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
471 assert %{locked: true, follow_requests_count: 1} =
472 AccountView.render("show.json", %{user: user, for: user})
475 test "decreases when accepting a follow request" do
476 user = insert(:user, locked: true)
478 assert %{locked: true} = 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 %{locked: true, follow_requests_count: 1} =
484 AccountView.render("show.json", %{user: user, for: user})
486 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
488 assert %{locked: true, follow_requests_count: 0} =
489 AccountView.render("show.json", %{user: user, for: user})
492 test "decreases when rejecting a follow request" do
493 user = insert(:user, locked: true)
495 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
497 other_user = insert(:user)
498 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
500 assert %{locked: true, follow_requests_count: 1} =
501 AccountView.render("show.json", %{user: user, for: user})
503 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
505 assert %{locked: true, follow_requests_count: 0} =
506 AccountView.render("show.json", %{user: user, for: user})
509 test "shows non-zero when historical unapproved requests are present" do
510 user = insert(:user, locked: true)
512 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
514 other_user = insert(:user)
515 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
517 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
519 assert %{locked: false, follow_requests_count: 1} =
520 AccountView.render("show.json", %{user: user, for: user})
524 test "uses mediaproxy urls when it's enabled" do
525 clear_config([:media_proxy, :enabled], true)
529 avatar: %{"url" => [%{"href" => "https://evil.website/avatar.png"}]},
530 banner: %{"url" => [%{"href" => "https://evil.website/banner.png"}]},
531 emoji: %{"joker_smile" => "https://evil.website/society.png"}
534 AccountView.render("show.json", %{user: user})
536 {key, url} when key in [:avatar, :avatar_static, :header, :header_static] ->
537 String.starts_with?(url, Pleroma.Web.base_url())
540 Enum.all?(emojis, fn %{url: url, static_url: static_url} ->
541 String.starts_with?(url, Pleroma.Web.base_url()) &&
542 String.starts_with?(static_url, Pleroma.Web.base_url())