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",
86 background_image: "https://example.com/images/asuka_hospital.png",
87 confirmation_pending: false,
92 hide_followers: false,
94 hide_followers_count: false,
95 hide_follows_count: false,
97 skip_thread_containment: false
101 assert expected == AccountView.render("show.json", %{user: user})
104 test "Represent the user account for the account owner" do
107 notification_settings = %Pleroma.User.NotificationSetting{}
108 privacy = user.default_scope
111 pleroma: %{notification_settings: ^notification_settings, allow_following_move: true},
112 source: %{privacy: ^privacy}
113 } = AccountView.render("show.json", %{user: user, for: user})
116 test "Represent a Service(bot) account" do
122 actor_type: "Service",
123 nickname: "shp@shitposter.club",
124 inserted_at: ~N[2017-08-15 15:47:06.597036]
128 id: to_string(user.id),
131 display_name: user.name,
133 created_at: "2017-08-15T15:47:06.000Z",
139 avatar: "http://localhost:4001/images/avi.png",
140 avatar_static: "http://localhost:4001/images/avi.png",
141 header: "http://localhost:4001/images/banner.png",
142 header_static: "http://localhost:4001/images/banner.png",
150 actor_type: "Service",
157 background_image: nil,
158 confirmation_pending: false,
162 hide_favorites: true,
163 hide_followers: false,
165 hide_followers_count: false,
166 hide_follows_count: false,
168 skip_thread_containment: false
172 assert expected == AccountView.render("show.json", %{user: user})
175 test "Represent a Funkwhale channel" do
177 User.get_or_fetch_by_ap_id(
178 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
181 assert represented = AccountView.render("show.json", %{user: user})
182 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
183 assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
186 test "Represent a deactivated user for an admin" do
187 admin = insert(:user, is_admin: true)
188 deactivated_user = insert(:user, deactivated: true)
189 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
190 assert represented[:pleroma][:deactivated] == true
193 test "Represent a smaller mention" do
197 id: to_string(user.id),
199 username: user.nickname,
203 assert expected == AccountView.render("mention.json", %{user: user})
206 describe "relationship" do
207 defp test_relationship_rendering(user, other_user, expected_result) do
208 opts = %{user: user, target: other_user, relationships: nil}
209 assert expected_result == AccountView.render("relationship.json", opts)
211 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
212 opts = Map.put(opts, :relationships, relationships_opt)
213 assert expected_result == AccountView.render("relationship.json", opts)
215 assert [expected_result] ==
216 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
225 muting_notifications: false,
228 domain_blocking: false,
229 showing_reblogs: true,
233 test "represent a relationship for the following and followed user" do
235 other_user = insert(:user)
237 {:ok, user} = User.follow(user, other_user)
238 {:ok, other_user} = User.follow(other_user, user)
239 {:ok, _subscription} = User.subscribe(user, other_user)
240 {:ok, _user_relationships} = User.mute(user, other_user, true)
241 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
250 muting_notifications: true,
252 showing_reblogs: false,
253 id: to_string(other_user.id)
257 test_relationship_rendering(user, other_user, expected)
260 test "represent a relationship for the blocking and blocked user" do
262 other_user = insert(:user)
264 {:ok, user} = User.follow(user, other_user)
265 {:ok, _subscription} = User.subscribe(user, other_user)
266 {:ok, _user_relationship} = User.block(user, other_user)
267 {:ok, _user_relationship} = User.block(other_user, user)
272 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
275 test_relationship_rendering(user, other_user, expected)
278 test "represent a relationship for the user blocking a domain" do
280 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
282 {:ok, user} = User.block_domain(user, "bad.site")
287 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
290 test_relationship_rendering(user, other_user, expected)
293 test "represent a relationship for the user with a pending follow request" do
295 other_user = insert(:user, locked: true)
297 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
298 user = User.get_cached_by_id(user.id)
299 other_user = User.get_cached_by_id(other_user.id)
304 %{requested: true, following: false, id: to_string(other_user.id)}
307 test_relationship_rendering(user, other_user, expected)
311 test "represent an embedded relationship" do
317 actor_type: "Service",
318 nickname: "shp@shitposter.club",
319 inserted_at: ~N[2017-08-15 15:47:06.597036]
322 other_user = insert(:user)
323 {:ok, other_user} = User.follow(other_user, user)
324 {:ok, _user_relationship} = User.block(other_user, user)
325 {:ok, _} = User.follow(insert(:user), user)
328 id: to_string(user.id),
331 display_name: user.name,
333 created_at: "2017-08-15T15:47:06.000Z",
339 avatar: "http://localhost:4001/images/avi.png",
340 avatar_static: "http://localhost:4001/images/avi.png",
341 header: "http://localhost:4001/images/banner.png",
342 header_static: "http://localhost:4001/images/banner.png",
350 actor_type: "Service",
357 background_image: nil,
358 confirmation_pending: false,
362 hide_favorites: true,
363 hide_followers: false,
365 hide_followers_count: false,
366 hide_follows_count: false,
368 id: to_string(user.id),
375 muting_notifications: false,
377 domain_blocking: false,
378 showing_reblogs: true,
381 skip_thread_containment: false
386 AccountView.render("show.json", %{user: refresh_record(user), for: other_user})
389 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
390 user = insert(:user, pleroma_settings_store: %{fe: "test"})
393 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
395 assert result.pleroma.settings_store == %{:fe => "test"}
397 result = AccountView.render("show.json", %{user: user, with_pleroma_settings: true})
398 assert result.pleroma[:settings_store] == nil
400 result = AccountView.render("show.json", %{user: user, for: user})
401 assert result.pleroma[:settings_store] == nil
404 test "doesn't sanitize display names" do
405 user = insert(:user, name: "<marquee> username </marquee>")
406 result = AccountView.render("show.json", %{user: user})
407 assert result.display_name == "<marquee> username </marquee>"
410 test "never display nil user follow counts" do
411 user = insert(:user, following_count: 0, follower_count: 0)
412 result = AccountView.render("show.json", %{user: user})
414 assert result.following_count == 0
415 assert result.followers_count == 0
418 describe "hiding follows/following" do
419 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
422 hide_followers: true,
423 hide_followers_count: true,
425 hide_follows_count: true
428 other_user = insert(:user)
429 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
430 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
435 pleroma: %{hide_follows_count: true, hide_followers_count: true}
436 } = AccountView.render("show.json", %{user: user})
439 test "shows when follows/followers are hidden" do
440 user = insert(:user, hide_followers: true, hide_follows: true)
441 other_user = insert(:user)
442 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
443 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
448 pleroma: %{hide_follows: true, hide_followers: true}
449 } = AccountView.render("show.json", %{user: user})
452 test "shows actual follower/following count to the account owner" do
453 user = insert(:user, hide_followers: true, hide_follows: true)
454 other_user = insert(:user)
455 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
456 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
461 } = AccountView.render("show.json", %{user: user, for: user})
464 test "shows unread_conversation_count only to the account owner" do
466 other_user = insert(:user)
469 CommonAPI.post(other_user, %{
470 "status" => "Hey @#{user.nickname}.",
471 "visibility" => "direct"
474 user = User.get_cached_by_ap_id(user.ap_id)
476 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
477 :unread_conversation_count
480 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
481 :unread_conversation_count
486 describe "follow requests counter" do
487 test "shows zero when no follow requests are pending" do
490 assert %{follow_requests_count: 0} =
491 AccountView.render("show.json", %{user: user, for: user})
493 other_user = insert(:user)
494 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
496 assert %{follow_requests_count: 0} =
497 AccountView.render("show.json", %{user: user, for: user})
500 test "shows non-zero when follow requests are pending" do
501 user = insert(:user, locked: true)
503 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
505 other_user = insert(:user)
506 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
508 assert %{locked: true, follow_requests_count: 1} =
509 AccountView.render("show.json", %{user: user, for: user})
512 test "decreases when accepting a follow request" do
513 user = insert(:user, locked: true)
515 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
517 other_user = insert(:user)
518 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
520 assert %{locked: true, follow_requests_count: 1} =
521 AccountView.render("show.json", %{user: user, for: user})
523 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
525 assert %{locked: true, follow_requests_count: 0} =
526 AccountView.render("show.json", %{user: user, for: user})
529 test "decreases when rejecting a follow request" do
530 user = insert(:user, locked: true)
532 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
534 other_user = insert(:user)
535 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
537 assert %{locked: true, follow_requests_count: 1} =
538 AccountView.render("show.json", %{user: user, for: user})
540 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
542 assert %{locked: true, follow_requests_count: 0} =
543 AccountView.render("show.json", %{user: user, for: user})
546 test "shows non-zero when historical unapproved requests are present" do
547 user = insert(:user, locked: true)
549 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
551 other_user = insert(:user)
552 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
554 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
556 assert %{locked: false, follow_requests_count: 1} =
557 AccountView.render("show.json", %{user: user, for: user})