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 = %{
101 privacy_option: false
104 privacy = user.default_scope
107 pleroma: %{notification_settings: ^notification_settings, allow_following_move: true},
108 source: %{privacy: ^privacy}
109 } = AccountView.render("show.json", %{user: user, for: user})
112 test "Represent a Service(bot) account" do
117 actor_type: "Service",
118 nickname: "shp@shitposter.club",
119 inserted_at: ~N[2017-08-15 15:47:06.597036]
123 id: to_string(user.id),
126 display_name: user.name,
128 created_at: "2017-08-15T15:47:06.000Z",
134 avatar: "http://localhost:4001/images/avi.png",
135 avatar_static: "http://localhost:4001/images/avi.png",
136 header: "http://localhost:4001/images/banner.png",
137 header_static: "http://localhost:4001/images/banner.png",
145 actor_type: "Service",
151 background_image: nil,
152 confirmation_pending: false,
156 hide_favorites: true,
157 hide_followers: false,
159 hide_followers_count: false,
160 hide_follows_count: false,
162 skip_thread_containment: false
166 assert expected == AccountView.render("show.json", %{user: user})
169 test "Represent a Funkwhale channel" do
171 User.get_or_fetch_by_ap_id(
172 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
175 assert represented = AccountView.render("show.json", %{user: user})
176 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
177 assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
180 test "Represent a deactivated user for an admin" do
181 admin = insert(:user, is_admin: true)
182 deactivated_user = insert(:user, deactivated: true)
183 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
184 assert represented[:pleroma][:deactivated] == true
187 test "Represent a smaller mention" do
191 id: to_string(user.id),
193 username: user.nickname,
197 assert expected == AccountView.render("mention.json", %{user: user})
200 describe "relationship" do
201 defp test_relationship_rendering(user, other_user, expected_result) do
202 opts = %{user: user, target: other_user, relationships: nil}
203 assert expected_result == AccountView.render("relationship.json", opts)
205 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
206 opts = Map.put(opts, :relationships, relationships_opt)
207 assert expected_result == AccountView.render("relationship.json", opts)
209 assert [expected_result] ==
210 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
219 muting_notifications: false,
222 domain_blocking: false,
223 showing_reblogs: true,
227 test "represent a relationship for the following and followed user" do
229 other_user = insert(:user)
231 {:ok, user} = User.follow(user, other_user)
232 {:ok, other_user} = User.follow(other_user, user)
233 {:ok, _subscription} = User.subscribe(user, other_user)
234 {:ok, _user_relationships} = User.mute(user, other_user, true)
235 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
244 muting_notifications: true,
246 showing_reblogs: false,
247 id: to_string(other_user.id)
251 test_relationship_rendering(user, other_user, expected)
254 test "represent a relationship for the blocking and blocked user" do
256 other_user = insert(:user)
258 {:ok, user} = User.follow(user, other_user)
259 {:ok, _subscription} = User.subscribe(user, other_user)
260 {:ok, _user_relationship} = User.block(user, other_user)
261 {:ok, _user_relationship} = User.block(other_user, user)
266 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
269 test_relationship_rendering(user, other_user, expected)
272 test "represent a relationship for the user blocking a domain" do
274 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
276 {:ok, user} = User.block_domain(user, "bad.site")
281 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
284 test_relationship_rendering(user, other_user, expected)
287 test "represent a relationship for the user with a pending follow request" do
289 other_user = insert(:user, locked: true)
291 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
292 user = User.get_cached_by_id(user.id)
293 other_user = User.get_cached_by_id(other_user.id)
298 %{requested: true, following: false, id: to_string(other_user.id)}
301 test_relationship_rendering(user, other_user, expected)
305 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
306 user = insert(:user, pleroma_settings_store: %{fe: "test"})
309 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
311 assert result.pleroma.settings_store == %{:fe => "test"}
313 result = AccountView.render("show.json", %{user: user, with_pleroma_settings: true})
314 assert result.pleroma[:settings_store] == nil
316 result = AccountView.render("show.json", %{user: user, for: user})
317 assert result.pleroma[:settings_store] == nil
320 test "doesn't sanitize display names" do
321 user = insert(:user, name: "<marquee> username </marquee>")
322 result = AccountView.render("show.json", %{user: user})
323 assert result.display_name == "<marquee> username </marquee>"
326 test "never display nil user follow counts" do
327 user = insert(:user, following_count: 0, follower_count: 0)
328 result = AccountView.render("show.json", %{user: user})
330 assert result.following_count == 0
331 assert result.followers_count == 0
334 describe "hiding follows/following" do
335 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
338 hide_followers: true,
339 hide_followers_count: true,
341 hide_follows_count: true
344 other_user = insert(:user)
345 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
346 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
351 pleroma: %{hide_follows_count: true, hide_followers_count: true}
352 } = AccountView.render("show.json", %{user: user})
355 test "shows when follows/followers are hidden" do
356 user = insert(:user, hide_followers: true, hide_follows: true)
357 other_user = insert(:user)
358 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
359 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
364 pleroma: %{hide_follows: true, hide_followers: true}
365 } = AccountView.render("show.json", %{user: user})
368 test "shows actual follower/following count to the account owner" do
369 user = insert(:user, hide_followers: true, hide_follows: true)
370 other_user = insert(:user)
371 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
372 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
377 } = AccountView.render("show.json", %{user: user, for: user})
380 test "shows unread_conversation_count only to the account owner" do
382 other_user = insert(:user)
385 CommonAPI.post(other_user, %{
386 status: "Hey @#{user.nickname}.",
390 user = User.get_cached_by_ap_id(user.ap_id)
392 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
393 :unread_conversation_count
396 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
397 :unread_conversation_count
401 test "shows unread_count only to the account owner" do
403 insert_list(7, :notification, user: user)
404 other_user = insert(:user)
406 user = User.get_cached_by_ap_id(user.ap_id)
408 assert AccountView.render(
410 %{user: user, for: other_user}
411 )[:pleroma][:unread_notifications_count] == nil
413 assert AccountView.render(
415 %{user: user, for: user}
416 )[:pleroma][:unread_notifications_count] == 7
420 describe "follow requests counter" do
421 test "shows zero when no follow requests are pending" do
424 assert %{follow_requests_count: 0} =
425 AccountView.render("show.json", %{user: user, for: user})
427 other_user = insert(:user)
428 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
430 assert %{follow_requests_count: 0} =
431 AccountView.render("show.json", %{user: user, for: user})
434 test "shows non-zero when follow requests are pending" do
435 user = insert(:user, locked: true)
437 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
439 other_user = insert(:user)
440 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
442 assert %{locked: true, follow_requests_count: 1} =
443 AccountView.render("show.json", %{user: user, for: user})
446 test "decreases when accepting a follow request" do
447 user = insert(:user, locked: true)
449 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
451 other_user = insert(:user)
452 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
454 assert %{locked: true, follow_requests_count: 1} =
455 AccountView.render("show.json", %{user: user, for: user})
457 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
459 assert %{locked: true, follow_requests_count: 0} =
460 AccountView.render("show.json", %{user: user, for: user})
463 test "decreases when rejecting a follow request" do
464 user = insert(:user, locked: true)
466 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
468 other_user = insert(:user)
469 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
471 assert %{locked: true, follow_requests_count: 1} =
472 AccountView.render("show.json", %{user: user, for: user})
474 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
476 assert %{locked: true, follow_requests_count: 0} =
477 AccountView.render("show.json", %{user: user, for: user})
480 test "shows non-zero when historical unapproved requests are present" do
481 user = insert(:user, locked: true)
483 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
485 other_user = insert(:user)
486 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
488 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
490 assert %{locked: false, follow_requests_count: 1} =
491 AccountView.render("show.json", %{user: user, for: user})