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"}
40 id: to_string(user.id),
43 display_name: user.name,
45 created_at: "2017-08-15T15:47:06.000Z",
49 note: "<span>valid html</span>. a<br/>b<br/>c<br/>d<br/>f",
51 avatar: "http://localhost:4001/images/avi.png",
52 avatar_static: "http://localhost:4001/images/avi.png",
53 header: "http://localhost:4001/images/banner.png",
54 header_static: "http://localhost:4001/images/banner.png",
57 "static_url" => "/file.png",
59 "shortcode" => "karjalanpiirakka",
60 "visible_in_picker" => false
66 note: "valid html. a\nb\nc\nd\nf",
76 background_image: "https://example.com/images/asuka_hospital.png",
77 confirmation_pending: false,
82 hide_followers: false,
84 hide_followers_count: false,
85 hide_follows_count: false,
87 skip_thread_containment: false
91 assert expected == AccountView.render("show.json", %{user: user})
94 test "Represent the user account for the account owner" do
97 notification_settings = %Pleroma.User.NotificationSetting{}
98 privacy = user.default_scope
101 pleroma: %{notification_settings: ^notification_settings, allow_following_move: true},
102 source: %{privacy: ^privacy}
103 } = AccountView.render("show.json", %{user: user, for: user})
106 test "Represent a Service(bot) account" do
111 actor_type: "Service",
112 nickname: "shp@shitposter.club",
113 inserted_at: ~N[2017-08-15 15:47:06.597036]
117 id: to_string(user.id),
120 display_name: user.name,
122 created_at: "2017-08-15T15:47:06.000Z",
128 avatar: "http://localhost:4001/images/avi.png",
129 avatar_static: "http://localhost:4001/images/avi.png",
130 header: "http://localhost:4001/images/banner.png",
131 header_static: "http://localhost:4001/images/banner.png",
139 actor_type: "Service",
146 background_image: nil,
147 confirmation_pending: false,
151 hide_favorites: true,
152 hide_followers: false,
154 hide_followers_count: false,
155 hide_follows_count: false,
157 skip_thread_containment: false
161 assert expected == AccountView.render("show.json", %{user: user})
164 test "Represent a Funkwhale channel" do
166 User.get_or_fetch_by_ap_id(
167 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
170 assert represented = AccountView.render("show.json", %{user: user})
171 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
172 assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
175 test "Represent a deactivated user for an admin" do
176 admin = insert(:user, is_admin: true)
177 deactivated_user = insert(:user, deactivated: true)
178 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
179 assert represented[:pleroma][:deactivated] == true
182 test "Represent a smaller mention" do
186 id: to_string(user.id),
188 username: user.nickname,
192 assert expected == AccountView.render("mention.json", %{user: user})
195 describe "relationship" do
196 defp test_relationship_rendering(user, other_user, expected_result) do
197 opts = %{user: user, target: other_user, relationships: nil}
198 assert expected_result == AccountView.render("relationship.json", opts)
200 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
201 opts = Map.put(opts, :relationships, relationships_opt)
202 assert expected_result == AccountView.render("relationship.json", opts)
204 assert [expected_result] ==
205 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
214 muting_notifications: false,
217 domain_blocking: false,
218 showing_reblogs: true,
222 test "represent a relationship for the following and followed user" do
224 other_user = insert(:user)
226 {:ok, user} = User.follow(user, other_user)
227 {:ok, other_user} = User.follow(other_user, user)
228 {:ok, _subscription} = User.subscribe(user, other_user)
229 {:ok, _user_relationships} = User.mute(user, other_user, true)
230 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
239 muting_notifications: true,
241 showing_reblogs: false,
242 id: to_string(other_user.id)
246 test_relationship_rendering(user, other_user, expected)
249 test "represent a relationship for the blocking and blocked user" do
251 other_user = insert(:user)
253 {:ok, user} = User.follow(user, other_user)
254 {:ok, _subscription} = User.subscribe(user, other_user)
255 {:ok, _user_relationship} = User.block(user, other_user)
256 {:ok, _user_relationship} = User.block(other_user, user)
261 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
264 test_relationship_rendering(user, other_user, expected)
267 test "represent a relationship for the user blocking a domain" do
269 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
271 {:ok, user} = User.block_domain(user, "bad.site")
276 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
279 test_relationship_rendering(user, other_user, expected)
282 test "represent a relationship for the user with a pending follow request" do
284 other_user = insert(:user, locked: true)
286 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
287 user = User.get_cached_by_id(user.id)
288 other_user = User.get_cached_by_id(other_user.id)
293 %{requested: true, following: false, id: to_string(other_user.id)}
296 test_relationship_rendering(user, other_user, expected)
300 test "represent an embedded relationship" do
305 actor_type: "Service",
306 nickname: "shp@shitposter.club",
307 inserted_at: ~N[2017-08-15 15:47:06.597036]
310 other_user = insert(:user)
311 {:ok, other_user} = User.follow(other_user, user)
312 {:ok, _user_relationship} = User.block(other_user, user)
313 {:ok, _} = User.follow(insert(:user), user)
316 id: to_string(user.id),
319 display_name: user.name,
321 created_at: "2017-08-15T15:47:06.000Z",
327 avatar: "http://localhost:4001/images/avi.png",
328 avatar_static: "http://localhost:4001/images/avi.png",
329 header: "http://localhost:4001/images/banner.png",
330 header_static: "http://localhost:4001/images/banner.png",
338 actor_type: "Service",
345 background_image: nil,
346 confirmation_pending: false,
350 hide_favorites: true,
351 hide_followers: false,
353 hide_followers_count: false,
354 hide_follows_count: false,
356 id: to_string(user.id),
363 muting_notifications: false,
365 domain_blocking: false,
366 showing_reblogs: true,
369 skip_thread_containment: false
374 AccountView.render("show.json", %{user: refresh_record(user), for: other_user})
377 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
378 user = insert(:user, pleroma_settings_store: %{fe: "test"})
381 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
383 assert result.pleroma.settings_store == %{:fe => "test"}
385 result = AccountView.render("show.json", %{user: user, with_pleroma_settings: true})
386 assert result.pleroma[:settings_store] == nil
388 result = AccountView.render("show.json", %{user: user, for: user})
389 assert result.pleroma[:settings_store] == nil
392 test "doesn't sanitize display names" do
393 user = insert(:user, name: "<marquee> username </marquee>")
394 result = AccountView.render("show.json", %{user: user})
395 assert result.display_name == "<marquee> username </marquee>"
398 test "never display nil user follow counts" do
399 user = insert(:user, following_count: 0, follower_count: 0)
400 result = AccountView.render("show.json", %{user: user})
402 assert result.following_count == 0
403 assert result.followers_count == 0
406 describe "hiding follows/following" do
407 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
410 hide_followers: true,
411 hide_followers_count: true,
413 hide_follows_count: true
416 other_user = insert(:user)
417 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
418 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
423 pleroma: %{hide_follows_count: true, hide_followers_count: true}
424 } = AccountView.render("show.json", %{user: user})
427 test "shows when follows/followers are hidden" do
428 user = insert(:user, hide_followers: true, hide_follows: true)
429 other_user = insert(:user)
430 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
431 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
436 pleroma: %{hide_follows: true, hide_followers: true}
437 } = AccountView.render("show.json", %{user: user})
440 test "shows actual follower/following count to the account owner" do
441 user = insert(:user, hide_followers: true, hide_follows: true)
442 other_user = insert(:user)
443 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
444 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
449 } = AccountView.render("show.json", %{user: user, for: user})
452 test "shows unread_conversation_count only to the account owner" do
454 other_user = insert(:user)
457 CommonAPI.post(other_user, %{
458 "status" => "Hey @#{user.nickname}.",
459 "visibility" => "direct"
462 user = User.get_cached_by_ap_id(user.ap_id)
464 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
465 :unread_conversation_count
468 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
469 :unread_conversation_count
474 describe "follow requests counter" do
475 test "shows zero when no follow requests are pending" do
478 assert %{follow_requests_count: 0} =
479 AccountView.render("show.json", %{user: user, for: user})
481 other_user = insert(:user)
482 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
484 assert %{follow_requests_count: 0} =
485 AccountView.render("show.json", %{user: user, for: user})
488 test "shows non-zero when follow requests are pending" do
489 user = insert(:user, locked: true)
491 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
493 other_user = insert(:user)
494 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
496 assert %{locked: true, follow_requests_count: 1} =
497 AccountView.render("show.json", %{user: user, for: user})
500 test "decreases when accepting a follow request" do
501 user = insert(:user, locked: true)
503 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
505 other_user = insert(:user)
506 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
508 assert %{locked: true, follow_requests_count: 1} =
509 AccountView.render("show.json", %{user: user, for: user})
511 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
513 assert %{locked: true, follow_requests_count: 0} =
514 AccountView.render("show.json", %{user: user, for: user})
517 test "decreases when rejecting a follow request" do
518 user = insert(:user, locked: true)
520 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
522 other_user = insert(:user)
523 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
525 assert %{locked: true, follow_requests_count: 1} =
526 AccountView.render("show.json", %{user: user, for: user})
528 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
530 assert %{locked: true, follow_requests_count: 0} =
531 AccountView.render("show.json", %{user: user, for: user})
534 test "shows non-zero when historical unapproved requests are present" do
535 user = insert(:user, locked: true)
537 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
539 other_user = insert(:user)
540 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
542 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
544 assert %{locked: false, follow_requests_count: 1} =
545 AccountView.render("show.json", %{user: user, for: user})