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 = %Pleroma.User.NotificationSetting{}
97 privacy = user.default_scope
100 pleroma: %{notification_settings: ^notification_settings, allow_following_move: true},
101 source: %{privacy: ^privacy}
102 } = AccountView.render("show.json", %{user: user, for: user})
105 test "Represent a Service(bot) account" do
110 actor_type: "Service",
111 nickname: "shp@shitposter.club",
112 inserted_at: ~N[2017-08-15 15:47:06.597036]
116 id: to_string(user.id),
119 display_name: user.name,
121 created_at: "2017-08-15T15:47:06.000Z",
127 avatar: "http://localhost:4001/images/avi.png",
128 avatar_static: "http://localhost:4001/images/avi.png",
129 header: "http://localhost:4001/images/banner.png",
130 header_static: "http://localhost:4001/images/banner.png",
138 actor_type: "Service",
144 background_image: nil,
145 confirmation_pending: false,
149 hide_favorites: true,
150 hide_followers: false,
152 hide_followers_count: false,
153 hide_follows_count: false,
155 skip_thread_containment: false
159 assert expected == AccountView.render("show.json", %{user: user})
162 test "Represent a Funkwhale channel" do
164 User.get_or_fetch_by_ap_id(
165 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
168 assert represented = AccountView.render("show.json", %{user: user})
169 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
170 assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
173 test "Represent a deactivated user for an admin" do
174 admin = insert(:user, is_admin: true)
175 deactivated_user = insert(:user, deactivated: true)
176 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
177 assert represented[:pleroma][:deactivated] == true
180 test "Represent a smaller mention" do
184 id: to_string(user.id),
186 username: user.nickname,
190 assert expected == AccountView.render("mention.json", %{user: user})
193 describe "relationship" do
194 defp test_relationship_rendering(user, other_user, expected_result) do
195 opts = %{user: user, target: other_user, relationships: nil}
196 assert expected_result == AccountView.render("relationship.json", opts)
198 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
199 opts = Map.put(opts, :relationships, relationships_opt)
200 assert expected_result == AccountView.render("relationship.json", opts)
202 assert [expected_result] ==
203 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
212 muting_notifications: false,
215 domain_blocking: false,
216 showing_reblogs: true,
220 test "represent a relationship for the following and followed user" do
222 other_user = insert(:user)
224 {:ok, user} = User.follow(user, other_user)
225 {:ok, other_user} = User.follow(other_user, user)
226 {:ok, _subscription} = User.subscribe(user, other_user)
227 {:ok, _user_relationships} = User.mute(user, other_user, true)
228 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
237 muting_notifications: true,
239 showing_reblogs: false,
240 id: to_string(other_user.id)
244 test_relationship_rendering(user, other_user, expected)
247 test "represent a relationship for the blocking and blocked user" do
249 other_user = insert(:user)
251 {:ok, user} = User.follow(user, other_user)
252 {:ok, _subscription} = User.subscribe(user, other_user)
253 {:ok, _user_relationship} = User.block(user, other_user)
254 {:ok, _user_relationship} = User.block(other_user, user)
259 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
262 test_relationship_rendering(user, other_user, expected)
265 test "represent a relationship for the user blocking a domain" do
267 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
269 {:ok, user} = User.block_domain(user, "bad.site")
274 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
277 test_relationship_rendering(user, other_user, expected)
280 test "represent a relationship for the user with a pending follow request" do
282 other_user = insert(:user, locked: true)
284 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
285 user = User.get_cached_by_id(user.id)
286 other_user = User.get_cached_by_id(other_user.id)
291 %{requested: true, following: false, id: to_string(other_user.id)}
294 test_relationship_rendering(user, other_user, expected)
298 test "represent an embedded relationship" do
303 actor_type: "Service",
304 nickname: "shp@shitposter.club",
305 inserted_at: ~N[2017-08-15 15:47:06.597036]
308 other_user = insert(:user)
309 {:ok, other_user} = User.follow(other_user, user)
310 {:ok, _user_relationship} = User.block(other_user, user)
311 {:ok, _} = User.follow(insert(:user), user)
314 id: to_string(user.id),
317 display_name: user.name,
319 created_at: "2017-08-15T15:47:06.000Z",
325 avatar: "http://localhost:4001/images/avi.png",
326 avatar_static: "http://localhost:4001/images/avi.png",
327 header: "http://localhost:4001/images/banner.png",
328 header_static: "http://localhost:4001/images/banner.png",
336 actor_type: "Service",
342 background_image: nil,
343 confirmation_pending: false,
347 hide_favorites: true,
348 hide_followers: false,
350 hide_followers_count: false,
351 hide_follows_count: false,
353 id: to_string(user.id),
360 muting_notifications: false,
362 domain_blocking: false,
363 showing_reblogs: true,
366 skip_thread_containment: false
371 AccountView.render("show.json", %{user: refresh_record(user), for: other_user})
374 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
375 user = insert(:user, pleroma_settings_store: %{fe: "test"})
378 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
380 assert result.pleroma.settings_store == %{:fe => "test"}
382 result = AccountView.render("show.json", %{user: user, with_pleroma_settings: true})
383 assert result.pleroma[:settings_store] == nil
385 result = AccountView.render("show.json", %{user: user, for: user})
386 assert result.pleroma[:settings_store] == nil
389 test "doesn't sanitize display names" do
390 user = insert(:user, name: "<marquee> username </marquee>")
391 result = AccountView.render("show.json", %{user: user})
392 assert result.display_name == "<marquee> username </marquee>"
395 test "never display nil user follow counts" do
396 user = insert(:user, following_count: 0, follower_count: 0)
397 result = AccountView.render("show.json", %{user: user})
399 assert result.following_count == 0
400 assert result.followers_count == 0
403 describe "hiding follows/following" do
404 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
407 hide_followers: true,
408 hide_followers_count: true,
410 hide_follows_count: true
413 other_user = insert(:user)
414 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
415 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
420 pleroma: %{hide_follows_count: true, hide_followers_count: true}
421 } = AccountView.render("show.json", %{user: user})
424 test "shows when follows/followers are hidden" do
425 user = insert(:user, hide_followers: true, hide_follows: true)
426 other_user = insert(:user)
427 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
428 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
433 pleroma: %{hide_follows: true, hide_followers: true}
434 } = AccountView.render("show.json", %{user: user})
437 test "shows actual follower/following count to the account owner" do
438 user = insert(:user, hide_followers: true, hide_follows: true)
439 other_user = insert(:user)
440 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
441 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
446 } = AccountView.render("show.json", %{user: user, for: user})
449 test "shows unread_conversation_count only to the account owner" do
451 other_user = insert(:user)
454 CommonAPI.post(other_user, %{
455 "status" => "Hey @#{user.nickname}.",
456 "visibility" => "direct"
459 user = User.get_cached_by_ap_id(user.ap_id)
461 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
462 :unread_conversation_count
465 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
466 :unread_conversation_count
471 describe "follow requests counter" do
472 test "shows zero when no follow requests are pending" do
475 assert %{follow_requests_count: 0} =
476 AccountView.render("show.json", %{user: user, for: user})
478 other_user = insert(:user)
479 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
481 assert %{follow_requests_count: 0} =
482 AccountView.render("show.json", %{user: user, for: user})
485 test "shows non-zero when follow requests are pending" do
486 user = insert(:user, locked: true)
488 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
490 other_user = insert(:user)
491 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
493 assert %{locked: true, follow_requests_count: 1} =
494 AccountView.render("show.json", %{user: user, for: user})
497 test "decreases when accepting a follow request" do
498 user = insert(:user, locked: true)
500 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
502 other_user = insert(:user)
503 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
505 assert %{locked: true, follow_requests_count: 1} =
506 AccountView.render("show.json", %{user: user, for: user})
508 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
510 assert %{locked: true, follow_requests_count: 0} =
511 AccountView.render("show.json", %{user: user, for: user})
514 test "decreases when rejecting a follow request" do
515 user = insert(:user, locked: true)
517 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
519 other_user = insert(:user)
520 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
522 assert %{locked: true, follow_requests_count: 1} =
523 AccountView.render("show.json", %{user: user, for: user})
525 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
527 assert %{locked: true, follow_requests_count: 0} =
528 AccountView.render("show.json", %{user: user, for: user})
531 test "shows non-zero when historical unapproved requests are present" do
532 user = insert(:user, locked: true)
534 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
536 other_user = insert(:user)
537 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
539 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
541 assert %{locked: false, follow_requests_count: 1} =
542 AccountView.render("show.json", %{user: user, for: user})