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"
184 test "Represent a deactivated user for an admin" do
185 admin = insert(:user, is_admin: true)
186 deactivated_user = insert(:user, deactivated: true)
187 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
188 assert represented[:pleroma][:deactivated] == true
191 test "Represent a smaller mention" do
195 id: to_string(user.id),
197 username: user.nickname,
201 assert expected == AccountView.render("mention.json", %{user: user})
204 describe "relationship" do
205 defp test_relationship_rendering(user, other_user, expected_result) do
206 opts = %{user: user, target: other_user, relationships: nil}
207 assert expected_result == AccountView.render("relationship.json", opts)
209 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
210 opts = Map.put(opts, :relationships, relationships_opt)
211 assert expected_result == AccountView.render("relationship.json", opts)
220 muting_notifications: false,
223 domain_blocking: false,
224 showing_reblogs: true,
228 test "represent a relationship for the following and followed user" do
230 other_user = insert(:user)
232 {:ok, user} = User.follow(user, other_user)
233 {:ok, other_user} = User.follow(other_user, user)
234 {:ok, _subscription} = User.subscribe(user, other_user)
235 {:ok, _user_relationships} = User.mute(user, other_user, true)
236 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
245 muting_notifications: true,
247 showing_reblogs: false,
248 id: to_string(other_user.id)
252 test_relationship_rendering(user, other_user, expected)
255 test "represent a relationship for the blocking and blocked user" do
257 other_user = insert(:user)
259 {:ok, user} = User.follow(user, other_user)
260 {:ok, _subscription} = User.subscribe(user, other_user)
261 {:ok, _user_relationship} = User.block(user, other_user)
262 {:ok, _user_relationship} = User.block(other_user, user)
267 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
270 test_relationship_rendering(user, other_user, expected)
273 test "represent a relationship for the user blocking a domain" do
275 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
277 {:ok, user} = User.block_domain(user, "bad.site")
282 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
285 test_relationship_rendering(user, other_user, expected)
288 test "represent a relationship for the user with a pending follow request" do
290 other_user = insert(:user, locked: true)
292 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
293 user = User.get_cached_by_id(user.id)
294 other_user = User.get_cached_by_id(other_user.id)
299 %{requested: true, following: false, id: to_string(other_user.id)}
302 test_relationship_rendering(user, other_user, expected)
306 test "represent an embedded relationship" do
312 actor_type: "Service",
313 nickname: "shp@shitposter.club",
314 inserted_at: ~N[2017-08-15 15:47:06.597036]
317 other_user = insert(:user)
318 {:ok, other_user} = User.follow(other_user, user)
319 {:ok, _user_relationship} = User.block(other_user, user)
320 {:ok, _} = User.follow(insert(:user), user)
323 id: to_string(user.id),
326 display_name: user.name,
328 created_at: "2017-08-15T15:47:06.000Z",
334 avatar: "http://localhost:4001/images/avi.png",
335 avatar_static: "http://localhost:4001/images/avi.png",
336 header: "http://localhost:4001/images/banner.png",
337 header_static: "http://localhost:4001/images/banner.png",
345 actor_type: "Service",
351 background_image: nil,
352 confirmation_pending: false,
356 hide_favorites: true,
357 hide_followers: false,
359 hide_followers_count: false,
360 hide_follows_count: false,
362 id: to_string(user.id),
369 muting_notifications: false,
371 domain_blocking: false,
372 showing_reblogs: true,
375 skip_thread_containment: false
380 AccountView.render("show.json", %{user: refresh_record(user), for: other_user})
383 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
384 user = insert(:user, pleroma_settings_store: %{fe: "test"})
387 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
389 assert result.pleroma.settings_store == %{:fe => "test"}
391 result = AccountView.render("show.json", %{user: user, with_pleroma_settings: true})
392 assert result.pleroma[:settings_store] == nil
394 result = AccountView.render("show.json", %{user: user, for: user})
395 assert result.pleroma[:settings_store] == nil
398 test "doesn't sanitize display names" do
399 user = insert(:user, name: "<marquee> username </marquee>")
400 result = AccountView.render("show.json", %{user: user})
401 assert result.display_name == "<marquee> username </marquee>"
404 test "never display nil user follow counts" do
405 user = insert(:user, following_count: 0, follower_count: 0)
406 result = AccountView.render("show.json", %{user: user})
408 assert result.following_count == 0
409 assert result.followers_count == 0
412 describe "hiding follows/following" do
413 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
416 hide_followers: true,
417 hide_followers_count: true,
419 hide_follows_count: true
422 other_user = insert(:user)
423 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
424 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
429 pleroma: %{hide_follows_count: true, hide_followers_count: true}
430 } = AccountView.render("show.json", %{user: user})
433 test "shows when follows/followers are hidden" do
434 user = insert(:user, hide_followers: true, hide_follows: true)
435 other_user = insert(:user)
436 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
437 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
442 pleroma: %{hide_follows: true, hide_followers: true}
443 } = AccountView.render("show.json", %{user: user})
446 test "shows actual follower/following count to the account owner" do
447 user = insert(:user, hide_followers: true, hide_follows: true)
448 other_user = insert(:user)
449 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
450 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
455 } = AccountView.render("show.json", %{user: user, for: user})
458 test "shows unread_conversation_count only to the account owner" do
460 other_user = insert(:user)
463 CommonAPI.post(other_user, %{
464 "status" => "Hey @#{user.nickname}.",
465 "visibility" => "direct"
468 user = User.get_cached_by_ap_id(user.ap_id)
470 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
471 :unread_conversation_count
474 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
475 :unread_conversation_count
480 describe "follow requests counter" do
481 test "shows zero when no follow requests are pending" do
484 assert %{follow_requests_count: 0} =
485 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 %{follow_requests_count: 0} =
491 AccountView.render("show.json", %{user: user, for: user})
494 test "shows non-zero when follow requests are pending" 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})
506 test "decreases when accepting a follow request" do
507 user = insert(:user, locked: true)
509 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
511 other_user = insert(:user)
512 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
514 assert %{locked: true, follow_requests_count: 1} =
515 AccountView.render("show.json", %{user: user, for: user})
517 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
519 assert %{locked: true, follow_requests_count: 0} =
520 AccountView.render("show.json", %{user: user, for: user})
523 test "decreases when rejecting a follow request" do
524 user = insert(:user, locked: true)
526 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
528 other_user = insert(:user)
529 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
531 assert %{locked: true, follow_requests_count: 1} =
532 AccountView.render("show.json", %{user: user, for: user})
534 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
536 assert %{locked: true, follow_requests_count: 0} =
537 AccountView.render("show.json", %{user: user, for: user})
540 test "shows non-zero when historical unapproved requests are present" do
541 user = insert(:user, locked: true)
543 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
545 other_user = insert(:user)
546 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
548 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
550 assert %{locked: false, follow_requests_count: 1} =
551 AccountView.render("show.json", %{user: user, for: user})