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 "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 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, skip_visibility_check: true})
195 test "Represent a Funkwhale channel" do
197 User.get_or_fetch_by_ap_id(
198 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
202 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
204 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
205 assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
208 test "Represent a deactivated user for an admin" do
209 admin = insert(:user, is_admin: true)
210 deactivated_user = insert(:user, deactivated: true)
211 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
212 assert represented[:pleroma][:deactivated] == true
215 test "Represent a smaller mention" do
219 id: to_string(user.id),
221 username: user.nickname,
225 assert expected == AccountView.render("mention.json", %{user: user})
228 test "demands :for or :skip_visibility_check option for account rendering" do
229 clear_config([:restrict_unauthenticated, :profiles, :local], false)
234 assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: nil})
235 assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: user})
237 assert %{id: ^user_id} =
238 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
240 assert_raise RuntimeError, ~r/:skip_visibility_check or :for option is required/, fn ->
241 AccountView.render("show.json", %{user: user})
245 describe "relationship" do
246 defp test_relationship_rendering(user, other_user, expected_result) do
247 opts = %{user: user, target: other_user, relationships: nil}
248 assert expected_result == AccountView.render("relationship.json", opts)
250 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
251 opts = Map.put(opts, :relationships, relationships_opt)
252 assert expected_result == AccountView.render("relationship.json", opts)
254 assert [expected_result] ==
255 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
264 muting_notifications: false,
267 domain_blocking: false,
268 showing_reblogs: true,
272 test "represent a relationship for the following and followed user" do
274 other_user = insert(:user)
276 {:ok, user} = User.follow(user, other_user)
277 {:ok, other_user} = User.follow(other_user, user)
278 {:ok, _subscription} = User.subscribe(user, other_user)
279 {:ok, _user_relationships} = User.mute(user, other_user, true)
280 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
289 muting_notifications: true,
291 showing_reblogs: false,
292 id: to_string(other_user.id)
296 test_relationship_rendering(user, other_user, expected)
299 test "represent a relationship for the blocking and blocked user" do
301 other_user = insert(:user)
303 {:ok, user} = User.follow(user, other_user)
304 {:ok, _subscription} = User.subscribe(user, other_user)
305 {:ok, _user_relationship} = User.block(user, other_user)
306 {:ok, _user_relationship} = User.block(other_user, user)
311 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
314 test_relationship_rendering(user, other_user, expected)
317 test "represent a relationship for the user blocking a domain" do
319 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
321 {:ok, user} = User.block_domain(user, "bad.site")
326 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
329 test_relationship_rendering(user, other_user, expected)
332 test "represent a relationship for the user with a pending follow request" do
334 other_user = insert(:user, locked: true)
336 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
337 user = User.get_cached_by_id(user.id)
338 other_user = User.get_cached_by_id(other_user.id)
343 %{requested: true, following: false, id: to_string(other_user.id)}
346 test_relationship_rendering(user, other_user, expected)
350 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
351 user = insert(:user, pleroma_settings_store: %{fe: "test"})
354 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
356 assert result.pleroma.settings_store == %{:fe => "test"}
358 result = AccountView.render("show.json", %{user: user, for: nil, with_pleroma_settings: true})
359 assert result.pleroma[:settings_store] == nil
361 result = AccountView.render("show.json", %{user: user, for: user})
362 assert result.pleroma[:settings_store] == nil
365 test "doesn't sanitize display names" do
366 user = insert(:user, name: "<marquee> username </marquee>")
367 result = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
368 assert result.display_name == "<marquee> username </marquee>"
371 test "never display nil user follow counts" do
372 user = insert(:user, following_count: 0, follower_count: 0)
373 result = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
375 assert result.following_count == 0
376 assert result.followers_count == 0
379 describe "hiding follows/following" do
380 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
383 hide_followers: true,
384 hide_followers_count: true,
386 hide_follows_count: true
389 other_user = insert(:user)
390 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
391 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
396 pleroma: %{hide_follows_count: true, hide_followers_count: true}
397 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
400 test "shows when follows/followers are hidden" do
401 user = insert(:user, hide_followers: true, hide_follows: true)
402 other_user = insert(:user)
403 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
404 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
409 pleroma: %{hide_follows: true, hide_followers: true}
410 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
413 test "shows actual follower/following count to the account owner" do
414 user = insert(:user, hide_followers: true, hide_follows: true)
415 other_user = insert(:user)
416 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
418 assert User.following?(user, other_user)
419 assert Pleroma.FollowingRelationship.follower_count(other_user) == 1
420 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
425 } = AccountView.render("show.json", %{user: user, for: user})
428 test "shows unread_conversation_count only to the account owner" do
430 other_user = insert(:user)
433 CommonAPI.post(other_user, %{
434 status: "Hey @#{user.nickname}.",
438 user = User.get_cached_by_ap_id(user.ap_id)
440 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
441 :unread_conversation_count
444 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
445 :unread_conversation_count
449 test "shows unread_count only to the account owner" do
451 insert_list(7, :notification, user: user, activity: insert(:note_activity))
452 other_user = insert(:user)
454 user = User.get_cached_by_ap_id(user.ap_id)
456 assert AccountView.render(
458 %{user: user, for: other_user}
459 )[:pleroma][:unread_notifications_count] == nil
461 assert AccountView.render(
463 %{user: user, for: user}
464 )[:pleroma][:unread_notifications_count] == 7
468 describe "follow requests counter" do
469 test "shows zero when no follow requests are pending" do
472 assert %{follow_requests_count: 0} =
473 AccountView.render("show.json", %{user: user, for: user})
475 other_user = insert(:user)
476 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
478 assert %{follow_requests_count: 0} =
479 AccountView.render("show.json", %{user: user, for: user})
482 test "shows non-zero when follow requests are pending" do
483 user = insert(:user, locked: true)
485 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
487 other_user = insert(:user)
488 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
490 assert %{locked: true, follow_requests_count: 1} =
491 AccountView.render("show.json", %{user: user, for: user})
494 test "decreases when accepting a follow request" do
495 user = insert(:user, locked: true)
497 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
499 other_user = insert(:user)
500 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
502 assert %{locked: true, follow_requests_count: 1} =
503 AccountView.render("show.json", %{user: user, for: user})
505 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
507 assert %{locked: true, follow_requests_count: 0} =
508 AccountView.render("show.json", %{user: user, for: user})
511 test "decreases when rejecting a follow request" do
512 user = insert(:user, locked: true)
514 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
516 other_user = insert(:user)
517 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
519 assert %{locked: true, follow_requests_count: 1} =
520 AccountView.render("show.json", %{user: user, for: user})
522 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
524 assert %{locked: true, follow_requests_count: 0} =
525 AccountView.render("show.json", %{user: user, for: user})
528 test "shows non-zero when historical unapproved requests are present" do
529 user = insert(:user, locked: true)
531 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
533 other_user = insert(:user)
534 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
536 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
538 assert %{locked: false, follow_requests_count: 1} =
539 AccountView.render("show.json", %{user: user, for: user})
543 test "uses mediaproxy urls when it's enabled" do
544 clear_config([:media_proxy, :enabled], true)
548 avatar: %{"url" => [%{"href" => "https://evil.website/avatar.png"}]},
549 banner: %{"url" => [%{"href" => "https://evil.website/banner.png"}]},
550 emoji: %{"joker_smile" => "https://evil.website/society.png"}
553 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
555 {key, url} when key in [:avatar, :avatar_static, :header, :header_static] ->
556 String.starts_with?(url, Pleroma.Web.base_url())
559 Enum.all?(emojis, fn %{url: url, static_url: static_url} ->
560 String.starts_with?(url, Pleroma.Web.base_url()) &&
561 String.starts_with?(static_url, Pleroma.Web.base_url())