1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
9 alias Pleroma.Web.CommonAPI
10 alias Pleroma.Web.MastodonAPI.AccountView
12 test "Represent a user account" do
17 "icon" => %{"url" => "/file.png"},
18 "name" => ":karjalanpiirakka:"
24 "url" => [%{"href" => "https://example.com/images/asuka_hospital.png"}]
31 source_data: source_data,
32 background: background_image,
33 nickname: "shp@shitposter.club",
34 name: ":karjalanpiirakka: shp",
35 bio: "<script src=\"invalid-html\"></script><span>valid html</span>",
36 inserted_at: ~N[2017-08-15 15:47:06.597036]
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>",
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
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
111 actor_type: "Service",
112 nickname: "shp@shitposter.club",
113 inserted_at: ~N[2017-08-15 15:47:06.597036]
117 id: to_string(user.id),
120 display_name: user.name,
122 created_at: "2017-08-15T15:47:06.000Z",
128 avatar: "http://localhost:4001/images/avi.png",
129 avatar_static: "http://localhost:4001/images/avi.png",
130 header: "http://localhost:4001/images/banner.png",
131 header_static: "http://localhost:4001/images/banner.png",
139 actor_type: "Service",
145 background_image: nil,
146 confirmation_pending: false,
150 hide_favorites: true,
151 hide_followers: false,
153 hide_followers_count: false,
154 hide_follows_count: false,
156 skip_thread_containment: false
160 assert expected == AccountView.render("show.json", %{user: user})
163 test "Represent a deactivated user for an admin" do
164 admin = insert(:user, is_admin: true)
165 deactivated_user = insert(:user, deactivated: true)
166 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
167 assert represented[:pleroma][:deactivated] == true
170 test "Represent a smaller mention" do
174 id: to_string(user.id),
176 username: user.nickname,
180 assert expected == AccountView.render("mention.json", %{user: user})
183 describe "relationship" do
184 test "represent a relationship for the following and followed user" do
186 other_user = insert(:user)
188 {:ok, user} = User.follow(user, other_user)
189 {:ok, other_user} = User.follow(other_user, user)
190 {:ok, _subscription} = User.subscribe(user, other_user)
191 {:ok, _user_relationships} = User.mute(user, other_user, true)
192 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
195 id: to_string(other_user.id),
201 muting_notifications: true,
204 domain_blocking: false,
205 showing_reblogs: false,
210 AccountView.render("relationship.json", %{user: user, target: other_user})
213 test "represent a relationship for the blocking and blocked user" do
215 other_user = insert(:user)
217 {:ok, user} = User.follow(user, other_user)
218 {:ok, _subscription} = User.subscribe(user, other_user)
219 {:ok, _user_relationship} = User.block(user, other_user)
220 {:ok, _user_relationship} = User.block(other_user, user)
223 id: to_string(other_user.id),
229 muting_notifications: false,
232 domain_blocking: false,
233 showing_reblogs: true,
238 AccountView.render("relationship.json", %{user: user, target: other_user})
241 test "represent a relationship for the user blocking a domain" do
243 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
245 {:ok, user} = User.block_domain(user, "bad.site")
247 assert %{domain_blocking: true, blocking: false} =
248 AccountView.render("relationship.json", %{user: user, target: other_user})
251 test "represent a relationship for the user with a pending follow request" do
253 other_user = insert(:user, locked: true)
255 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
256 user = User.get_cached_by_id(user.id)
257 other_user = User.get_cached_by_id(other_user.id)
260 id: to_string(other_user.id),
266 muting_notifications: false,
269 domain_blocking: false,
270 showing_reblogs: true,
275 AccountView.render("relationship.json", %{user: user, target: other_user})
279 test "represent an embedded relationship" do
285 actor_type: "Service",
286 nickname: "shp@shitposter.club",
287 inserted_at: ~N[2017-08-15 15:47:06.597036]
290 other_user = insert(:user)
291 {:ok, other_user} = User.follow(other_user, user)
292 {:ok, _user_relationship} = User.block(other_user, user)
293 {:ok, _} = User.follow(insert(:user), user)
296 id: to_string(user.id),
299 display_name: user.name,
301 created_at: "2017-08-15T15:47:06.000Z",
307 avatar: "http://localhost:4001/images/avi.png",
308 avatar_static: "http://localhost:4001/images/avi.png",
309 header: "http://localhost:4001/images/banner.png",
310 header_static: "http://localhost:4001/images/banner.png",
318 actor_type: "Service",
324 background_image: nil,
325 confirmation_pending: false,
329 hide_favorites: true,
330 hide_followers: false,
332 hide_followers_count: false,
333 hide_follows_count: false,
335 id: to_string(user.id),
342 muting_notifications: false,
344 domain_blocking: false,
345 showing_reblogs: true,
348 skip_thread_containment: false
353 AccountView.render("show.json", %{user: refresh_record(user), for: other_user})
356 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
357 user = insert(:user, pleroma_settings_store: %{fe: "test"})
360 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
362 assert result.pleroma.settings_store == %{:fe => "test"}
364 result = AccountView.render("show.json", %{user: user, with_pleroma_settings: true})
365 assert result.pleroma[:settings_store] == nil
367 result = AccountView.render("show.json", %{user: user, for: user})
368 assert result.pleroma[:settings_store] == nil
371 test "doesn't sanitize display names" do
372 user = insert(:user, name: "<marquee> username </marquee>")
373 result = AccountView.render("show.json", %{user: user})
374 assert result.display_name == "<marquee> username </marquee>"
377 test "never display nil user follow counts" do
378 user = insert(:user, following_count: 0, follower_count: 0)
379 result = AccountView.render("show.json", %{user: user})
381 assert result.following_count == 0
382 assert result.followers_count == 0
385 describe "hiding follows/following" do
386 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
389 hide_followers: true,
390 hide_followers_count: true,
392 hide_follows_count: true
395 other_user = insert(:user)
396 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
397 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
402 pleroma: %{hide_follows_count: true, hide_followers_count: true}
403 } = AccountView.render("show.json", %{user: user})
406 test "shows when follows/followers are hidden" do
407 user = insert(:user, hide_followers: true, hide_follows: true)
408 other_user = insert(:user)
409 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
410 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
415 pleroma: %{hide_follows: true, hide_followers: true}
416 } = AccountView.render("show.json", %{user: user})
419 test "shows actual follower/following count to the account owner" do
420 user = insert(:user, hide_followers: true, hide_follows: true)
421 other_user = insert(:user)
422 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
423 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
428 } = AccountView.render("show.json", %{user: user, for: user})
431 test "shows unread_conversation_count only to the account owner" do
433 other_user = insert(:user)
436 CommonAPI.post(other_user, %{
437 "status" => "Hey @#{user.nickname}.",
438 "visibility" => "direct"
441 user = User.get_cached_by_ap_id(user.ap_id)
443 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
444 :unread_conversation_count
447 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
448 :unread_conversation_count
453 describe "follow requests counter" do
454 test "shows zero when no follow requests are pending" do
457 assert %{follow_requests_count: 0} =
458 AccountView.render("show.json", %{user: user, for: user})
460 other_user = insert(:user)
461 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
463 assert %{follow_requests_count: 0} =
464 AccountView.render("show.json", %{user: user, for: user})
467 test "shows non-zero when follow requests are pending" do
468 user = insert(:user, locked: true)
470 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
472 other_user = insert(:user)
473 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
475 assert %{locked: true, follow_requests_count: 1} =
476 AccountView.render("show.json", %{user: user, for: user})
479 test "decreases when accepting a follow request" do
480 user = insert(:user, locked: true)
482 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
484 other_user = insert(:user)
485 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
487 assert %{locked: true, follow_requests_count: 1} =
488 AccountView.render("show.json", %{user: user, for: user})
490 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
492 assert %{locked: true, follow_requests_count: 0} =
493 AccountView.render("show.json", %{user: user, for: user})
496 test "decreases when rejecting a follow request" do
497 user = insert(:user, locked: true)
499 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
501 other_user = insert(:user)
502 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
504 assert %{locked: true, follow_requests_count: 1} =
505 AccountView.render("show.json", %{user: user, for: user})
507 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
509 assert %{locked: true, follow_requests_count: 0} =
510 AccountView.render("show.json", %{user: user, for: user})
513 test "shows non-zero when historical unapproved requests are present" 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 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
523 assert %{locked: false, follow_requests_count: 1} =
524 AccountView.render("show.json", %{user: user, for: user})