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 '&<>\"",
76 background_image: "https://example.com/images/asuka_hospital.png",
77 confirmation_pending: false,
82 hide_followers: false,
84 hide_followers_count: false,
85 hide_follows_count: false,
87 skip_thread_containment: false
91 assert expected == AccountView.render("show.json", %{user: user})
94 test "Represent the user account for the account owner" do
97 notification_settings = %{
102 privacy_option: false
105 privacy = user.default_scope
108 pleroma: %{notification_settings: ^notification_settings, allow_following_move: true},
109 source: %{privacy: ^privacy}
110 } = AccountView.render("show.json", %{user: user, for: user})
113 test "Represent a Service(bot) account" do
118 actor_type: "Service",
119 nickname: "shp@shitposter.club",
120 inserted_at: ~N[2017-08-15 15:47:06.597036]
124 id: to_string(user.id),
127 display_name: user.name,
129 created_at: "2017-08-15T15:47:06.000Z",
135 avatar: "http://localhost:4001/images/avi.png",
136 avatar_static: "http://localhost:4001/images/avi.png",
137 header: "http://localhost:4001/images/banner.png",
138 header_static: "http://localhost:4001/images/banner.png",
146 actor_type: "Service",
153 background_image: nil,
154 confirmation_pending: false,
158 hide_favorites: true,
159 hide_followers: false,
161 hide_followers_count: false,
162 hide_follows_count: false,
164 skip_thread_containment: false
168 assert expected == AccountView.render("show.json", %{user: user})
171 test "Represent a Funkwhale channel" do
173 User.get_or_fetch_by_ap_id(
174 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
177 assert represented = AccountView.render("show.json", %{user: user})
178 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
179 assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
182 test "Represent a deactivated user for an admin" do
183 admin = insert(:user, is_admin: true)
184 deactivated_user = insert(:user, deactivated: true)
185 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
186 assert represented[:pleroma][:deactivated] == true
189 test "Represent a smaller mention" do
193 id: to_string(user.id),
195 username: user.nickname,
199 assert expected == AccountView.render("mention.json", %{user: user})
202 describe "relationship" do
203 defp test_relationship_rendering(user, other_user, expected_result) do
204 opts = %{user: user, target: other_user, relationships: nil}
205 assert expected_result == AccountView.render("relationship.json", opts)
207 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
208 opts = Map.put(opts, :relationships, relationships_opt)
209 assert expected_result == AccountView.render("relationship.json", opts)
211 assert [expected_result] ==
212 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
221 muting_notifications: false,
224 domain_blocking: false,
225 showing_reblogs: true,
229 test "represent a relationship for the following and followed user" do
231 other_user = insert(:user)
233 {:ok, user} = User.follow(user, other_user)
234 {:ok, other_user} = User.follow(other_user, user)
235 {:ok, _subscription} = User.subscribe(user, other_user)
236 {:ok, _user_relationships} = User.mute(user, other_user, true)
237 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
246 muting_notifications: true,
248 showing_reblogs: false,
249 id: to_string(other_user.id)
253 test_relationship_rendering(user, other_user, expected)
256 test "represent a relationship for the blocking and blocked user" do
258 other_user = insert(:user)
260 {:ok, user} = User.follow(user, other_user)
261 {:ok, _subscription} = User.subscribe(user, other_user)
262 {:ok, _user_relationship} = User.block(user, other_user)
263 {:ok, _user_relationship} = User.block(other_user, user)
268 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
271 test_relationship_rendering(user, other_user, expected)
274 test "represent a relationship for the user blocking a domain" do
276 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
278 {:ok, user} = User.block_domain(user, "bad.site")
283 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
286 test_relationship_rendering(user, other_user, expected)
289 test "represent a relationship for the user with a pending follow request" do
291 other_user = insert(:user, locked: true)
293 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
294 user = User.get_cached_by_id(user.id)
295 other_user = User.get_cached_by_id(other_user.id)
300 %{requested: true, following: false, id: to_string(other_user.id)}
303 test_relationship_rendering(user, other_user, expected)
307 test "represent an embedded relationship" do
312 actor_type: "Service",
313 nickname: "shp@shitposter.club",
314 inserted_at: ~N[2017-08-15 15:47:06.597036]
317 other_user = insert(:user)
318 {:ok, other_user} = User.follow(other_user, user)
319 {:ok, _user_relationship} = User.block(other_user, user)
320 {:ok, _} = User.follow(insert(:user), user)
323 id: to_string(user.id),
326 display_name: user.name,
328 created_at: "2017-08-15T15:47:06.000Z",
334 avatar: "http://localhost:4001/images/avi.png",
335 avatar_static: "http://localhost:4001/images/avi.png",
336 header: "http://localhost:4001/images/banner.png",
337 header_static: "http://localhost:4001/images/banner.png",
345 actor_type: "Service",
352 background_image: nil,
353 confirmation_pending: false,
357 hide_favorites: true,
358 hide_followers: false,
360 hide_followers_count: false,
361 hide_follows_count: false,
363 id: to_string(user.id),
370 muting_notifications: false,
372 domain_blocking: false,
373 showing_reblogs: true,
376 skip_thread_containment: false
381 AccountView.render("show.json", %{user: refresh_record(user), for: other_user})
384 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
385 user = insert(:user, pleroma_settings_store: %{fe: "test"})
388 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
390 assert result.pleroma.settings_store == %{:fe => "test"}
392 result = AccountView.render("show.json", %{user: user, with_pleroma_settings: true})
393 assert result.pleroma[:settings_store] == nil
395 result = AccountView.render("show.json", %{user: user, for: user})
396 assert result.pleroma[:settings_store] == nil
399 test "doesn't sanitize display names" do
400 user = insert(:user, name: "<marquee> username </marquee>")
401 result = AccountView.render("show.json", %{user: user})
402 assert result.display_name == "<marquee> username </marquee>"
405 test "never display nil user follow counts" do
406 user = insert(:user, following_count: 0, follower_count: 0)
407 result = AccountView.render("show.json", %{user: user})
409 assert result.following_count == 0
410 assert result.followers_count == 0
413 describe "hiding follows/following" do
414 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
417 hide_followers: true,
418 hide_followers_count: true,
420 hide_follows_count: true
423 other_user = insert(:user)
424 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
425 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
430 pleroma: %{hide_follows_count: true, hide_followers_count: true}
431 } = AccountView.render("show.json", %{user: user})
434 test "shows when follows/followers are hidden" do
435 user = insert(:user, hide_followers: true, hide_follows: true)
436 other_user = insert(:user)
437 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
438 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
443 pleroma: %{hide_follows: true, hide_followers: true}
444 } = AccountView.render("show.json", %{user: user})
447 test "shows actual follower/following count to the account owner" do
448 user = insert(:user, hide_followers: true, hide_follows: true)
449 other_user = insert(:user)
450 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
451 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
456 } = AccountView.render("show.json", %{user: user, for: user})
459 test "shows unread_conversation_count only to the account owner" do
461 other_user = insert(:user)
464 CommonAPI.post(other_user, %{
465 status: "Hey @#{user.nickname}.",
469 user = User.get_cached_by_ap_id(user.ap_id)
471 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
472 :unread_conversation_count
475 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
476 :unread_conversation_count
480 test "shows unread_count only to the account owner" do
482 insert_list(7, :notification, user: user)
483 other_user = insert(:user)
485 user = User.get_cached_by_ap_id(user.ap_id)
487 assert AccountView.render(
489 %{user: user, for: other_user}
490 )[:pleroma][:unread_notifications_count] == nil
492 assert AccountView.render(
494 %{user: user, for: user}
495 )[:pleroma][:unread_notifications_count] == 7
499 describe "follow requests counter" do
500 test "shows zero when no follow requests are pending" do
503 assert %{follow_requests_count: 0} =
504 AccountView.render("show.json", %{user: user, for: user})
506 other_user = insert(:user)
507 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
509 assert %{follow_requests_count: 0} =
510 AccountView.render("show.json", %{user: user, for: user})
513 test "shows non-zero when follow requests are pending" do
514 user = insert(:user, locked: true)
516 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
518 other_user = insert(:user)
519 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
521 assert %{locked: true, follow_requests_count: 1} =
522 AccountView.render("show.json", %{user: user, for: user})
525 test "decreases when accepting a follow request" do
526 user = insert(:user, locked: true)
528 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
530 other_user = insert(:user)
531 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
533 assert %{locked: true, follow_requests_count: 1} =
534 AccountView.render("show.json", %{user: user, for: user})
536 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
538 assert %{locked: true, follow_requests_count: 0} =
539 AccountView.render("show.json", %{user: user, for: user})
542 test "decreases when rejecting a follow request" do
543 user = insert(:user, locked: true)
545 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
547 other_user = insert(:user)
548 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
550 assert %{locked: true, follow_requests_count: 1} =
551 AccountView.render("show.json", %{user: user, for: user})
553 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
555 assert %{locked: true, follow_requests_count: 0} =
556 AccountView.render("show.json", %{user: user, for: user})
559 test "shows non-zero when historical unapproved requests are present" do
560 user = insert(:user, locked: true)
562 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
564 other_user = insert(:user)
565 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
567 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
569 assert %{locked: false, follow_requests_count: 1} =
570 AccountView.render("show.json", %{user: user, for: user})