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 "returns the settings store if the requesting user is the represented user and it's requested specifically" do
308 user = insert(:user, pleroma_settings_store: %{fe: "test"})
311 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
313 assert result.pleroma.settings_store == %{:fe => "test"}
315 result = AccountView.render("show.json", %{user: user, with_pleroma_settings: true})
316 assert result.pleroma[:settings_store] == nil
318 result = AccountView.render("show.json", %{user: user, for: user})
319 assert result.pleroma[:settings_store] == nil
322 test "doesn't sanitize display names" do
323 user = insert(:user, name: "<marquee> username </marquee>")
324 result = AccountView.render("show.json", %{user: user})
325 assert result.display_name == "<marquee> username </marquee>"
328 test "never display nil user follow counts" do
329 user = insert(:user, following_count: 0, follower_count: 0)
330 result = AccountView.render("show.json", %{user: user})
332 assert result.following_count == 0
333 assert result.followers_count == 0
336 describe "hiding follows/following" do
337 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
340 hide_followers: true,
341 hide_followers_count: true,
343 hide_follows_count: true
346 other_user = insert(:user)
347 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
348 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
353 pleroma: %{hide_follows_count: true, hide_followers_count: true}
354 } = AccountView.render("show.json", %{user: user})
357 test "shows when follows/followers are hidden" do
358 user = insert(:user, hide_followers: true, hide_follows: true)
359 other_user = insert(:user)
360 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
361 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
366 pleroma: %{hide_follows: true, hide_followers: true}
367 } = AccountView.render("show.json", %{user: user})
370 test "shows actual follower/following count to the account owner" do
371 user = insert(:user, hide_followers: true, hide_follows: true)
372 other_user = insert(:user)
373 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
374 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
379 } = AccountView.render("show.json", %{user: user, for: user})
382 test "shows unread_conversation_count only to the account owner" do
384 other_user = insert(:user)
387 CommonAPI.post(other_user, %{
388 status: "Hey @#{user.nickname}.",
392 user = User.get_cached_by_ap_id(user.ap_id)
394 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
395 :unread_conversation_count
398 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
399 :unread_conversation_count
403 test "shows unread_count only to the account owner" do
405 insert_list(7, :notification, user: user)
406 other_user = insert(:user)
408 user = User.get_cached_by_ap_id(user.ap_id)
410 assert AccountView.render(
412 %{user: user, for: other_user}
413 )[:pleroma][:unread_notifications_count] == nil
415 assert AccountView.render(
417 %{user: user, for: user}
418 )[:pleroma][:unread_notifications_count] == 7
422 describe "follow requests counter" do
423 test "shows zero when no follow requests are pending" do
426 assert %{follow_requests_count: 0} =
427 AccountView.render("show.json", %{user: user, for: user})
429 other_user = insert(:user)
430 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
432 assert %{follow_requests_count: 0} =
433 AccountView.render("show.json", %{user: user, for: user})
436 test "shows non-zero when follow requests are pending" do
437 user = insert(:user, locked: true)
439 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
441 other_user = insert(:user)
442 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
444 assert %{locked: true, follow_requests_count: 1} =
445 AccountView.render("show.json", %{user: user, for: user})
448 test "decreases when accepting a follow request" do
449 user = insert(:user, locked: true)
451 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
453 other_user = insert(:user)
454 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
456 assert %{locked: true, follow_requests_count: 1} =
457 AccountView.render("show.json", %{user: user, for: user})
459 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
461 assert %{locked: true, follow_requests_count: 0} =
462 AccountView.render("show.json", %{user: user, for: user})
465 test "decreases when rejecting a follow request" do
466 user = insert(:user, locked: true)
468 assert %{locked: true} = 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 %{locked: true, follow_requests_count: 1} =
474 AccountView.render("show.json", %{user: user, for: user})
476 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
478 assert %{locked: true, follow_requests_count: 0} =
479 AccountView.render("show.json", %{user: user, for: user})
482 test "shows non-zero when historical unapproved requests are present" do
483 user = insert(:user, locked: true)
485 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
487 other_user = insert(:user)
488 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
490 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
492 assert %{locked: false, follow_requests_count: 1} =
493 AccountView.render("show.json", %{user: user, for: user})