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
26 "icon" => %{"url" => "/file.png"},
27 "name" => ":karjalanpiirakka:"
33 "url" => [%{"href" => "https://example.com/images/asuka_hospital.png"}]
40 source_data: source_data,
41 background: background_image,
42 nickname: "shp@shitposter.club",
43 name: ":karjalanpiirakka: shp",
45 "<script src=\"invalid-html\"></script><span>valid html</span>. a<br>b<br/>c<br >d<br />f",
46 inserted_at: ~N[2017-08-15 15:47:06.597036]
50 id: to_string(user.id),
53 display_name: user.name,
55 created_at: "2017-08-15T15:47:06.000Z",
59 note: "<span>valid html</span>. a<br/>b<br/>c<br/>d<br/>f",
61 avatar: "http://localhost:4001/images/avi.png",
62 avatar_static: "http://localhost:4001/images/avi.png",
63 header: "http://localhost:4001/images/banner.png",
64 header_static: "http://localhost:4001/images/banner.png",
67 "static_url" => "/file.png",
69 "shortcode" => "karjalanpiirakka",
70 "visible_in_picker" => false
76 note: "valid html. a\nb\nc\nd\nf",
85 background_image: "https://example.com/images/asuka_hospital.png",
86 confirmation_pending: false,
91 hide_followers: false,
93 hide_followers_count: false,
94 hide_follows_count: false,
96 skip_thread_containment: false
100 assert expected == AccountView.render("show.json", %{user: user})
103 test "Represent the user account for the account owner" do
106 notification_settings = %Pleroma.User.NotificationSetting{}
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
121 actor_type: "Service",
122 nickname: "shp@shitposter.club",
123 inserted_at: ~N[2017-08-15 15:47:06.597036]
127 id: to_string(user.id),
130 display_name: user.name,
132 created_at: "2017-08-15T15:47:06.000Z",
138 avatar: "http://localhost:4001/images/avi.png",
139 avatar_static: "http://localhost:4001/images/avi.png",
140 header: "http://localhost:4001/images/banner.png",
141 header_static: "http://localhost:4001/images/banner.png",
149 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
170 assert expected == AccountView.render("show.json", %{user: user})
173 test "Represent a Funkwhale channel" do
175 User.get_or_fetch_by_ap_id(
176 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
179 assert represented = AccountView.render("show.json", %{user: user})
180 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
181 # assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
182 assert represented.url ==
183 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
186 test "Represent a deactivated user for an admin" do
187 admin = insert(:user, is_admin: true)
188 deactivated_user = insert(:user, deactivated: true)
189 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
190 assert represented[:pleroma][:deactivated] == true
193 test "Represent a smaller mention" do
197 id: to_string(user.id),
199 username: user.nickname,
203 assert expected == AccountView.render("mention.json", %{user: user})
206 describe "relationship" do
207 defp test_relationship_rendering(user, other_user, expected_result) do
208 opts = %{user: user, target: other_user, relationships: nil}
209 assert expected_result == AccountView.render("relationship.json", opts)
211 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
212 opts = Map.put(opts, :relationships, relationships_opt)
213 assert expected_result == AccountView.render("relationship.json", opts)
222 muting_notifications: false,
225 domain_blocking: false,
226 showing_reblogs: true,
230 test "represent a relationship for the following and followed user" do
232 other_user = insert(:user)
234 {:ok, user} = User.follow(user, other_user)
235 {:ok, other_user} = User.follow(other_user, user)
236 {:ok, _subscription} = User.subscribe(user, other_user)
237 {:ok, _user_relationships} = User.mute(user, other_user, true)
238 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
247 muting_notifications: true,
249 showing_reblogs: false,
250 id: to_string(other_user.id)
254 test_relationship_rendering(user, other_user, expected)
257 test "represent a relationship for the blocking and blocked user" do
259 other_user = insert(:user)
261 {:ok, user} = User.follow(user, other_user)
262 {:ok, _subscription} = User.subscribe(user, other_user)
263 {:ok, _user_relationship} = User.block(user, other_user)
264 {:ok, _user_relationship} = User.block(other_user, user)
269 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
272 test_relationship_rendering(user, other_user, expected)
275 test "represent a relationship for the user blocking a domain" do
277 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
279 {:ok, user} = User.block_domain(user, "bad.site")
284 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
287 test_relationship_rendering(user, other_user, expected)
290 test "represent a relationship for the user with a pending follow request" do
292 other_user = insert(:user, locked: true)
294 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
295 user = User.get_cached_by_id(user.id)
296 other_user = User.get_cached_by_id(other_user.id)
301 %{requested: true, following: false, id: to_string(other_user.id)}
304 test_relationship_rendering(user, other_user, expected)
308 test "represent an embedded relationship" do
314 actor_type: "Service",
315 nickname: "shp@shitposter.club",
316 inserted_at: ~N[2017-08-15 15:47:06.597036]
319 other_user = insert(:user)
320 {:ok, other_user} = User.follow(other_user, user)
321 {:ok, _user_relationship} = User.block(other_user, user)
322 {:ok, _} = User.follow(insert(:user), user)
325 id: to_string(user.id),
328 display_name: user.name,
330 created_at: "2017-08-15T15:47:06.000Z",
336 avatar: "http://localhost:4001/images/avi.png",
337 avatar_static: "http://localhost:4001/images/avi.png",
338 header: "http://localhost:4001/images/banner.png",
339 header_static: "http://localhost:4001/images/banner.png",
347 actor_type: "Service",
353 background_image: nil,
354 confirmation_pending: false,
358 hide_favorites: true,
359 hide_followers: false,
361 hide_followers_count: false,
362 hide_follows_count: false,
364 id: to_string(user.id),
371 muting_notifications: false,
373 domain_blocking: false,
374 showing_reblogs: true,
377 skip_thread_containment: false
382 AccountView.render("show.json", %{user: refresh_record(user), for: other_user})
385 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
386 user = insert(:user, pleroma_settings_store: %{fe: "test"})
389 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
391 assert result.pleroma.settings_store == %{:fe => "test"}
393 result = AccountView.render("show.json", %{user: user, with_pleroma_settings: true})
394 assert result.pleroma[:settings_store] == nil
396 result = AccountView.render("show.json", %{user: user, for: user})
397 assert result.pleroma[:settings_store] == nil
400 test "doesn't sanitize display names" do
401 user = insert(:user, name: "<marquee> username </marquee>")
402 result = AccountView.render("show.json", %{user: user})
403 assert result.display_name == "<marquee> username </marquee>"
406 test "never display nil user follow counts" do
407 user = insert(:user, following_count: 0, follower_count: 0)
408 result = AccountView.render("show.json", %{user: user})
410 assert result.following_count == 0
411 assert result.followers_count == 0
414 describe "hiding follows/following" do
415 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
418 hide_followers: true,
419 hide_followers_count: true,
421 hide_follows_count: true
424 other_user = insert(:user)
425 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
426 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
431 pleroma: %{hide_follows_count: true, hide_followers_count: true}
432 } = AccountView.render("show.json", %{user: user})
435 test "shows when follows/followers are hidden" do
436 user = insert(:user, hide_followers: true, hide_follows: true)
437 other_user = insert(:user)
438 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
439 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
444 pleroma: %{hide_follows: true, hide_followers: true}
445 } = AccountView.render("show.json", %{user: user})
448 test "shows actual follower/following count to the account owner" do
449 user = insert(:user, hide_followers: true, hide_follows: true)
450 other_user = insert(:user)
451 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
452 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
457 } = AccountView.render("show.json", %{user: user, for: user})
460 test "shows unread_conversation_count only to the account owner" do
462 other_user = insert(:user)
465 CommonAPI.post(other_user, %{
466 "status" => "Hey @#{user.nickname}.",
467 "visibility" => "direct"
470 user = User.get_cached_by_ap_id(user.ap_id)
472 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
473 :unread_conversation_count
476 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
477 :unread_conversation_count
482 describe "follow requests counter" do
483 test "shows zero when no follow requests are pending" do
486 assert %{follow_requests_count: 0} =
487 AccountView.render("show.json", %{user: user, for: user})
489 other_user = insert(:user)
490 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
492 assert %{follow_requests_count: 0} =
493 AccountView.render("show.json", %{user: user, for: user})
496 test "shows non-zero when follow requests are pending" do
497 user = insert(:user, locked: true)
499 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
501 other_user = insert(:user)
502 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
504 assert %{locked: true, follow_requests_count: 1} =
505 AccountView.render("show.json", %{user: user, for: user})
508 test "decreases when accepting 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.accept_follow_request(other_user, user)
521 assert %{locked: true, follow_requests_count: 0} =
522 AccountView.render("show.json", %{user: user, for: user})
525 test "decreases when rejecting a follow request" 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 assert %{locked: true, follow_requests_count: 1} =
534 AccountView.render("show.json", %{user: user, for: user})
536 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
538 assert %{locked: true, follow_requests_count: 0} =
539 AccountView.render("show.json", %{user: user, for: user})
542 test "shows non-zero when historical unapproved requests are present" do
543 user = insert(:user, locked: true)
545 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
547 other_user = insert(:user)
548 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
550 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
552 assert %{locked: false, follow_requests_count: 1} =
553 AccountView.render("show.json", %{user: user, for: user})