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
11 alias Pleroma.UserRelationship
12 alias Pleroma.Web.CommonAPI
13 alias Pleroma.Web.MastodonAPI.AccountView
15 test "Represent a user account" do
20 "icon" => %{"url" => "/file.png"},
21 "name" => ":karjalanpiirakka:"
27 "url" => [%{"href" => "https://example.com/images/asuka_hospital.png"}]
34 source_data: source_data,
35 background: background_image,
36 nickname: "shp@shitposter.club",
37 name: ":karjalanpiirakka: shp",
39 "<script src=\"invalid-html\"></script><span>valid html</span>. a<br>b<br/>c<br >d<br />f",
40 inserted_at: ~N[2017-08-15 15:47:06.597036]
44 id: to_string(user.id),
47 display_name: user.name,
49 created_at: "2017-08-15T15:47:06.000Z",
53 note: "<span>valid html</span>. a<br/>b<br/>c<br/>d<br/>f",
55 avatar: "http://localhost:4001/images/avi.png",
56 avatar_static: "http://localhost:4001/images/avi.png",
57 header: "http://localhost:4001/images/banner.png",
58 header_static: "http://localhost:4001/images/banner.png",
61 "static_url" => "/file.png",
63 "shortcode" => "karjalanpiirakka",
64 "visible_in_picker" => false
70 note: "valid html. a\nb\nc\nd\nf",
79 background_image: "https://example.com/images/asuka_hospital.png",
80 confirmation_pending: false,
85 hide_followers: false,
87 hide_followers_count: false,
88 hide_follows_count: false,
90 skip_thread_containment: false
94 assert expected == AccountView.render("show.json", %{user: user})
97 test "Represent the user account for the account owner" do
100 notification_settings = %Pleroma.User.NotificationSetting{}
101 privacy = user.default_scope
104 pleroma: %{notification_settings: ^notification_settings, allow_following_move: true},
105 source: %{privacy: ^privacy}
106 } = AccountView.render("show.json", %{user: user, for: user})
109 test "Represent a Service(bot) account" do
115 actor_type: "Service",
116 nickname: "shp@shitposter.club",
117 inserted_at: ~N[2017-08-15 15:47:06.597036]
121 id: to_string(user.id),
124 display_name: user.name,
126 created_at: "2017-08-15T15:47:06.000Z",
132 avatar: "http://localhost:4001/images/avi.png",
133 avatar_static: "http://localhost:4001/images/avi.png",
134 header: "http://localhost:4001/images/banner.png",
135 header_static: "http://localhost:4001/images/banner.png",
143 actor_type: "Service",
149 background_image: nil,
150 confirmation_pending: false,
154 hide_favorites: true,
155 hide_followers: false,
157 hide_followers_count: false,
158 hide_follows_count: false,
160 skip_thread_containment: false
164 assert expected == AccountView.render("show.json", %{user: user})
167 test "Represent a deactivated user for an admin" do
168 admin = insert(:user, is_admin: true)
169 deactivated_user = insert(:user, deactivated: true)
170 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
171 assert represented[:pleroma][:deactivated] == true
174 test "Represent a smaller mention" do
178 id: to_string(user.id),
180 username: user.nickname,
184 assert expected == AccountView.render("mention.json", %{user: user})
187 describe "relationship" do
188 defp test_relationship_rendering(user, other_user, expected_result) do
189 opts = %{user: user, target: other_user, relationships: nil}
190 assert expected_result == AccountView.render("relationship.json", opts)
192 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
193 opts = Map.put(opts, :relationships, relationships_opt)
194 assert expected_result == AccountView.render("relationship.json", opts)
203 muting_notifications: false,
206 domain_blocking: false,
207 showing_reblogs: true,
211 test "represent a relationship for the following and followed user" do
213 other_user = insert(:user)
215 {:ok, user} = User.follow(user, other_user)
216 {:ok, other_user} = User.follow(other_user, user)
217 {:ok, _subscription} = User.subscribe(user, other_user)
218 {:ok, _user_relationships} = User.mute(user, other_user, true)
219 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
228 muting_notifications: true,
230 showing_reblogs: false,
231 id: to_string(other_user.id)
235 test_relationship_rendering(user, other_user, expected)
238 test "represent a relationship for the blocking and blocked user" do
240 other_user = insert(:user)
242 {:ok, user} = User.follow(user, other_user)
243 {:ok, _subscription} = User.subscribe(user, other_user)
244 {:ok, _user_relationship} = User.block(user, other_user)
245 {:ok, _user_relationship} = User.block(other_user, user)
250 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
253 test_relationship_rendering(user, other_user, expected)
256 test "represent a relationship for the user blocking a domain" do
258 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
260 {:ok, user} = User.block_domain(user, "bad.site")
265 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
268 test_relationship_rendering(user, other_user, expected)
271 test "represent a relationship for the user with a pending follow request" do
273 other_user = insert(:user, locked: true)
275 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
276 user = User.get_cached_by_id(user.id)
277 other_user = User.get_cached_by_id(other_user.id)
282 %{requested: true, following: false, id: to_string(other_user.id)}
285 test_relationship_rendering(user, other_user, expected)
289 test "represent an embedded relationship" do
295 actor_type: "Service",
296 nickname: "shp@shitposter.club",
297 inserted_at: ~N[2017-08-15 15:47:06.597036]
300 other_user = insert(:user)
301 {:ok, other_user} = User.follow(other_user, user)
302 {:ok, _user_relationship} = User.block(other_user, user)
303 {:ok, _} = User.follow(insert(:user), user)
306 id: to_string(user.id),
309 display_name: user.name,
311 created_at: "2017-08-15T15:47:06.000Z",
317 avatar: "http://localhost:4001/images/avi.png",
318 avatar_static: "http://localhost:4001/images/avi.png",
319 header: "http://localhost:4001/images/banner.png",
320 header_static: "http://localhost:4001/images/banner.png",
328 actor_type: "Service",
334 background_image: nil,
335 confirmation_pending: false,
339 hide_favorites: true,
340 hide_followers: false,
342 hide_followers_count: false,
343 hide_follows_count: false,
345 id: to_string(user.id),
352 muting_notifications: false,
354 domain_blocking: false,
355 showing_reblogs: true,
358 skip_thread_containment: false
363 AccountView.render("show.json", %{user: refresh_record(user), for: other_user})
366 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
367 user = insert(:user, pleroma_settings_store: %{fe: "test"})
370 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
372 assert result.pleroma.settings_store == %{:fe => "test"}
374 result = AccountView.render("show.json", %{user: user, with_pleroma_settings: true})
375 assert result.pleroma[:settings_store] == nil
377 result = AccountView.render("show.json", %{user: user, for: user})
378 assert result.pleroma[:settings_store] == nil
381 test "doesn't sanitize display names" do
382 user = insert(:user, name: "<marquee> username </marquee>")
383 result = AccountView.render("show.json", %{user: user})
384 assert result.display_name == "<marquee> username </marquee>"
387 test "never display nil user follow counts" do
388 user = insert(:user, following_count: 0, follower_count: 0)
389 result = AccountView.render("show.json", %{user: user})
391 assert result.following_count == 0
392 assert result.followers_count == 0
395 describe "hiding follows/following" do
396 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
399 hide_followers: true,
400 hide_followers_count: true,
402 hide_follows_count: true
405 other_user = insert(:user)
406 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
407 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
412 pleroma: %{hide_follows_count: true, hide_followers_count: true}
413 } = AccountView.render("show.json", %{user: user})
416 test "shows when follows/followers are hidden" do
417 user = insert(:user, hide_followers: true, hide_follows: true)
418 other_user = insert(:user)
419 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
420 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
425 pleroma: %{hide_follows: true, hide_followers: true}
426 } = AccountView.render("show.json", %{user: user})
429 test "shows actual follower/following count to the account owner" do
430 user = insert(:user, hide_followers: true, hide_follows: true)
431 other_user = insert(:user)
432 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
433 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
438 } = AccountView.render("show.json", %{user: user, for: user})
441 test "shows unread_conversation_count only to the account owner" do
443 other_user = insert(:user)
446 CommonAPI.post(other_user, %{
447 "status" => "Hey @#{user.nickname}.",
448 "visibility" => "direct"
451 user = User.get_cached_by_ap_id(user.ap_id)
453 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
454 :unread_conversation_count
457 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
458 :unread_conversation_count
463 describe "follow requests counter" do
464 test "shows zero when no follow requests are pending" do
467 assert %{follow_requests_count: 0} =
468 AccountView.render("show.json", %{user: user, for: user})
470 other_user = insert(:user)
471 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
473 assert %{follow_requests_count: 0} =
474 AccountView.render("show.json", %{user: user, for: user})
477 test "shows non-zero when follow requests are pending" do
478 user = insert(:user, locked: true)
480 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
482 other_user = insert(:user)
483 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
485 assert %{locked: true, follow_requests_count: 1} =
486 AccountView.render("show.json", %{user: user, for: user})
489 test "decreases when accepting a follow request" do
490 user = insert(:user, locked: true)
492 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
494 other_user = insert(:user)
495 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
497 assert %{locked: true, follow_requests_count: 1} =
498 AccountView.render("show.json", %{user: user, for: user})
500 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
502 assert %{locked: true, follow_requests_count: 0} =
503 AccountView.render("show.json", %{user: user, for: user})
506 test "decreases when rejecting 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.reject_follow_request(other_user, user)
519 assert %{locked: true, follow_requests_count: 0} =
520 AccountView.render("show.json", %{user: user, for: user})
523 test "shows non-zero when historical unapproved requests are present" 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 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
533 assert %{locked: false, follow_requests_count: 1} =
534 AccountView.render("show.json", %{user: user, for: user})