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 '&<>\"",
75 background_image: "https://example.com/images/asuka_hospital.png",
76 confirmation_pending: false,
81 hide_followers: false,
83 hide_followers_count: false,
84 hide_follows_count: false,
86 skip_thread_containment: false
90 assert expected == AccountView.render("show.json", %{user: user})
93 test "Represent the user account for the account owner" do
96 notification_settings = %{
100 privacy_option: false
103 privacy = user.default_scope
106 pleroma: %{notification_settings: ^notification_settings, allow_following_move: true},
107 source: %{privacy: ^privacy}
108 } = AccountView.render("show.json", %{user: user, for: user})
111 test "Represent a Service(bot) account" do
116 actor_type: "Service",
117 nickname: "shp@shitposter.club",
118 inserted_at: ~N[2017-08-15 15:47:06.597036]
122 id: to_string(user.id),
125 display_name: user.name,
127 created_at: "2017-08-15T15:47:06.000Z",
133 avatar: "http://localhost:4001/images/avi.png",
134 avatar_static: "http://localhost:4001/images/avi.png",
135 header: "http://localhost:4001/images/banner.png",
136 header_static: "http://localhost:4001/images/banner.png",
144 actor_type: "Service",
150 background_image: nil,
151 confirmation_pending: false,
155 hide_favorites: true,
156 hide_followers: false,
158 hide_followers_count: false,
159 hide_follows_count: false,
161 skip_thread_containment: false
165 assert expected == AccountView.render("show.json", %{user: user})
168 test "Represent a Funkwhale channel" do
170 User.get_or_fetch_by_ap_id(
171 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
174 assert represented = AccountView.render("show.json", %{user: user})
175 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
176 assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
179 test "Represent a deactivated user for an admin" do
180 admin = insert(:user, is_admin: true)
181 deactivated_user = insert(:user, deactivated: true)
182 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
183 assert represented[:pleroma][:deactivated] == true
186 test "Represent a smaller mention" do
190 id: to_string(user.id),
192 username: user.nickname,
196 assert expected == AccountView.render("mention.json", %{user: user})
199 describe "relationship" do
200 defp test_relationship_rendering(user, other_user, expected_result) do
201 opts = %{user: user, target: other_user, relationships: nil}
202 assert expected_result == AccountView.render("relationship.json", opts)
204 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
205 opts = Map.put(opts, :relationships, relationships_opt)
206 assert expected_result == AccountView.render("relationship.json", opts)
208 assert [expected_result] ==
209 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
218 muting_notifications: false,
221 domain_blocking: false,
222 showing_reblogs: true,
226 test "represent a relationship for the following and followed user" do
228 other_user = insert(:user)
230 {:ok, user} = User.follow(user, other_user)
231 {:ok, other_user} = User.follow(other_user, user)
232 {:ok, _subscription} = User.subscribe(user, other_user)
233 {:ok, _user_relationships} = User.mute(user, other_user, true)
234 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
243 muting_notifications: true,
245 showing_reblogs: false,
246 id: to_string(other_user.id)
250 test_relationship_rendering(user, other_user, expected)
253 test "represent a relationship for the blocking and blocked user" do
255 other_user = insert(:user)
257 {:ok, user} = User.follow(user, other_user)
258 {:ok, _subscription} = User.subscribe(user, other_user)
259 {:ok, _user_relationship} = User.block(user, other_user)
260 {:ok, _user_relationship} = User.block(other_user, user)
265 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
268 test_relationship_rendering(user, other_user, expected)
271 test "represent a relationship for the user blocking a domain" do
273 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
275 {:ok, user} = User.block_domain(user, "bad.site")
280 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
283 test_relationship_rendering(user, other_user, expected)
286 test "represent a relationship for the user with a pending follow request" do
288 other_user = insert(:user, locked: true)
290 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
291 user = User.get_cached_by_id(user.id)
292 other_user = User.get_cached_by_id(other_user.id)
297 %{requested: true, following: false, id: to_string(other_user.id)}
300 test_relationship_rendering(user, other_user, expected)
304 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
305 user = insert(:user, pleroma_settings_store: %{fe: "test"})
308 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
310 assert result.pleroma.settings_store == %{:fe => "test"}
312 result = AccountView.render("show.json", %{user: user, with_pleroma_settings: true})
313 assert result.pleroma[:settings_store] == nil
315 result = AccountView.render("show.json", %{user: user, for: user})
316 assert result.pleroma[:settings_store] == nil
319 test "doesn't sanitize display names" do
320 user = insert(:user, name: "<marquee> username </marquee>")
321 result = AccountView.render("show.json", %{user: user})
322 assert result.display_name == "<marquee> username </marquee>"
325 test "never display nil user follow counts" do
326 user = insert(:user, following_count: 0, follower_count: 0)
327 result = AccountView.render("show.json", %{user: user})
329 assert result.following_count == 0
330 assert result.followers_count == 0
333 describe "hiding follows/following" do
334 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
337 hide_followers: true,
338 hide_followers_count: true,
340 hide_follows_count: true
343 other_user = insert(:user)
344 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
345 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
350 pleroma: %{hide_follows_count: true, hide_followers_count: true}
351 } = AccountView.render("show.json", %{user: user})
354 test "shows when follows/followers are hidden" do
355 user = insert(:user, hide_followers: true, hide_follows: true)
356 other_user = insert(:user)
357 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
358 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
363 pleroma: %{hide_follows: true, hide_followers: true}
364 } = AccountView.render("show.json", %{user: user})
367 test "shows actual follower/following count to the account owner" do
368 user = insert(:user, hide_followers: true, hide_follows: true)
369 other_user = insert(:user)
370 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
371 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
376 } = AccountView.render("show.json", %{user: user, for: user})
379 test "shows unread_conversation_count only to the account owner" do
381 other_user = insert(:user)
384 CommonAPI.post(other_user, %{
385 status: "Hey @#{user.nickname}.",
389 user = User.get_cached_by_ap_id(user.ap_id)
391 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
392 :unread_conversation_count
395 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
396 :unread_conversation_count
400 test "shows unread_count only to the account owner" do
402 insert_list(7, :notification, user: user)
403 other_user = insert(:user)
405 user = User.get_cached_by_ap_id(user.ap_id)
407 assert AccountView.render(
409 %{user: user, for: other_user}
410 )[:pleroma][:unread_notifications_count] == nil
412 assert AccountView.render(
414 %{user: user, for: user}
415 )[:pleroma][:unread_notifications_count] == 7
419 describe "follow requests counter" do
420 test "shows zero when no follow requests are pending" do
423 assert %{follow_requests_count: 0} =
424 AccountView.render("show.json", %{user: user, for: user})
426 other_user = insert(:user)
427 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
429 assert %{follow_requests_count: 0} =
430 AccountView.render("show.json", %{user: user, for: user})
433 test "shows non-zero when follow requests are pending" do
434 user = insert(:user, locked: true)
436 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
438 other_user = insert(:user)
439 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
441 assert %{locked: true, follow_requests_count: 1} =
442 AccountView.render("show.json", %{user: user, for: user})
445 test "decreases when accepting a follow request" do
446 user = insert(:user, locked: true)
448 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
450 other_user = insert(:user)
451 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
453 assert %{locked: true, follow_requests_count: 1} =
454 AccountView.render("show.json", %{user: user, for: user})
456 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
458 assert %{locked: true, follow_requests_count: 0} =
459 AccountView.render("show.json", %{user: user, for: user})
462 test "decreases when rejecting a follow request" do
463 user = insert(:user, locked: true)
465 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
467 other_user = insert(:user)
468 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
470 assert %{locked: true, follow_requests_count: 1} =
471 AccountView.render("show.json", %{user: user, for: user})
473 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
475 assert %{locked: true, follow_requests_count: 0} =
476 AccountView.render("show.json", %{user: user, for: user})
479 test "shows non-zero when historical unapproved requests are present" do
480 user = insert(:user, locked: true)
482 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
484 other_user = insert(:user)
485 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
487 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
489 assert %{locked: false, follow_requests_count: 1} =
490 AccountView.render("show.json", %{user: user, for: user})