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 "returns the settings store if the requesting user is the represented user and it's requested specifically" do
299 user = insert(:user, pleroma_settings_store: %{fe: "test"})
302 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
304 assert result.pleroma.settings_store == %{:fe => "test"}
306 result = AccountView.render("show.json", %{user: user, with_pleroma_settings: true})
307 assert result.pleroma[:settings_store] == nil
309 result = AccountView.render("show.json", %{user: user, for: user})
310 assert result.pleroma[:settings_store] == nil
313 test "doesn't sanitize display names" do
314 user = insert(:user, name: "<marquee> username </marquee>")
315 result = AccountView.render("show.json", %{user: user})
316 assert result.display_name == "<marquee> username </marquee>"
319 test "never display nil user follow counts" do
320 user = insert(:user, following_count: 0, follower_count: 0)
321 result = AccountView.render("show.json", %{user: user})
323 assert result.following_count == 0
324 assert result.followers_count == 0
327 describe "hiding follows/following" do
328 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
331 hide_followers: true,
332 hide_followers_count: true,
334 hide_follows_count: true
337 other_user = insert(:user)
338 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
339 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
344 pleroma: %{hide_follows_count: true, hide_followers_count: true}
345 } = AccountView.render("show.json", %{user: user})
348 test "shows when follows/followers are hidden" do
349 user = insert(:user, hide_followers: true, hide_follows: true)
350 other_user = insert(:user)
351 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
352 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
357 pleroma: %{hide_follows: true, hide_followers: true}
358 } = AccountView.render("show.json", %{user: user})
361 test "shows actual follower/following count to the account owner" do
362 user = insert(:user, hide_followers: true, hide_follows: true)
363 other_user = insert(:user)
364 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
365 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
370 } = AccountView.render("show.json", %{user: user, for: user})
373 test "shows unread_conversation_count only to the account owner" do
375 other_user = insert(:user)
378 CommonAPI.post(other_user, %{
379 "status" => "Hey @#{user.nickname}.",
380 "visibility" => "direct"
383 user = User.get_cached_by_ap_id(user.ap_id)
385 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
386 :unread_conversation_count
389 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
390 :unread_conversation_count
394 test "shows unread_count only to the account owner" do
396 insert_list(7, :notification, user: user)
397 other_user = insert(:user)
399 user = User.get_cached_by_ap_id(user.ap_id)
401 assert AccountView.render(
403 %{user: user, for: other_user}
404 )[:pleroma][:unread_notifications_count] == nil
406 assert AccountView.render(
408 %{user: user, for: user}
409 )[:pleroma][:unread_notifications_count] == 7
413 describe "follow requests counter" do
414 test "shows zero when no follow requests are pending" do
417 assert %{follow_requests_count: 0} =
418 AccountView.render("show.json", %{user: user, for: user})
420 other_user = insert(:user)
421 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
423 assert %{follow_requests_count: 0} =
424 AccountView.render("show.json", %{user: user, for: user})
427 test "shows non-zero when follow requests are pending" do
428 user = insert(:user, locked: true)
430 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
432 other_user = insert(:user)
433 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
435 assert %{locked: true, follow_requests_count: 1} =
436 AccountView.render("show.json", %{user: user, for: user})
439 test "decreases when accepting a follow request" do
440 user = insert(:user, locked: true)
442 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
444 other_user = insert(:user)
445 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
447 assert %{locked: true, follow_requests_count: 1} =
448 AccountView.render("show.json", %{user: user, for: user})
450 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
452 assert %{locked: true, follow_requests_count: 0} =
453 AccountView.render("show.json", %{user: user, for: user})
456 test "decreases when rejecting a follow request" do
457 user = insert(:user, locked: true)
459 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
461 other_user = insert(:user)
462 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
464 assert %{locked: true, follow_requests_count: 1} =
465 AccountView.render("show.json", %{user: user, for: user})
467 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
469 assert %{locked: true, follow_requests_count: 0} =
470 AccountView.render("show.json", %{user: user, for: user})
473 test "shows non-zero when historical unapproved requests are present" do
474 user = insert(:user, locked: true)
476 assert %{locked: true} = 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 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
483 assert %{locked: false, follow_requests_count: 1} =
484 AccountView.render("show.json", %{user: user, for: user})