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
74 background_image: "https://example.com/images/asuka_hospital.png",
75 confirmation_pending: false,
80 hide_followers: false,
82 hide_followers_count: false,
83 hide_follows_count: false,
85 skip_thread_containment: false
89 assert expected == AccountView.render("show.json", %{user: user})
92 test "Represent the user account for the account owner" do
95 notification_settings = %{
98 "non_follows" => true,
99 "non_followers" => true
102 privacy = user.default_scope
105 pleroma: %{notification_settings: ^notification_settings},
106 source: %{privacy: ^privacy}
107 } = AccountView.render("show.json", %{user: user, for: user})
110 test "Represent a Service(bot) account" do
115 source_data: %{"type" => "Service"},
116 nickname: "shp@shitposter.club",
117 inserted_at: ~N[2017-08-15 15:47:06.597036]
121 id: to_string(user.id),
124 display_name: user.name,
126 created_at: "2017-08-15T15:47:06.000Z",
132 avatar: "http://localhost:4001/images/avi.png",
133 avatar_static: "http://localhost:4001/images/avi.png",
134 header: "http://localhost:4001/images/banner.png",
135 header_static: "http://localhost:4001/images/banner.png",
148 background_image: nil,
149 confirmation_pending: false,
153 hide_favorites: true,
154 hide_followers: false,
156 hide_followers_count: false,
157 hide_follows_count: false,
159 skip_thread_containment: false
163 assert expected == AccountView.render("show.json", %{user: user})
166 test "Represent a deactivated user for an admin" do
167 admin = insert(:user, is_admin: true)
168 deactivated_user = insert(:user, deactivated: true)
169 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
170 assert represented[:pleroma][:deactivated] == true
173 test "Represent a smaller mention" do
177 id: to_string(user.id),
179 username: user.nickname,
183 assert expected == AccountView.render("mention.json", %{user: user})
186 describe "relationship" do
187 test "represent a relationship for the following and followed user" do
189 other_user = insert(:user)
191 {:ok, user} = User.follow(user, other_user)
192 {:ok, other_user} = User.follow(other_user, user)
193 {:ok, _subscription} = User.subscribe(user, other_user)
194 {:ok, _user_relationships} = User.mute(user, other_user, true)
195 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
198 id: to_string(other_user.id),
204 muting_notifications: true,
207 domain_blocking: false,
208 showing_reblogs: false,
213 AccountView.render("relationship.json", %{user: user, target: other_user})
216 test "represent a relationship for the blocking and blocked user" do
218 other_user = insert(:user)
220 {:ok, user} = User.follow(user, other_user)
221 {:ok, _subscription} = User.subscribe(user, other_user)
222 {:ok, _user_relationship} = User.block(user, other_user)
223 {:ok, _user_relationship} = User.block(other_user, user)
226 id: to_string(other_user.id),
232 muting_notifications: false,
235 domain_blocking: false,
236 showing_reblogs: true,
241 AccountView.render("relationship.json", %{user: user, target: other_user})
244 test "represent a relationship for the user blocking a domain" do
246 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
248 {:ok, user} = User.block_domain(user, "bad.site")
250 assert %{domain_blocking: true, blocking: false} =
251 AccountView.render("relationship.json", %{user: user, target: other_user})
254 test "represent a relationship for the user with a pending follow request" do
256 other_user = insert(:user, locked: true)
258 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
259 user = User.get_cached_by_id(user.id)
260 other_user = User.get_cached_by_id(other_user.id)
263 id: to_string(other_user.id),
269 muting_notifications: false,
272 domain_blocking: false,
273 showing_reblogs: true,
278 AccountView.render("relationship.json", %{user: user, target: other_user})
282 test "represent an embedded relationship" do
287 source_data: %{"type" => "Service"},
288 nickname: "shp@shitposter.club",
289 inserted_at: ~N[2017-08-15 15:47:06.597036]
292 other_user = insert(:user)
293 {:ok, other_user} = User.follow(other_user, user)
294 {:ok, _user_relationship} = User.block(other_user, user)
295 {:ok, _} = User.follow(insert(:user), user)
298 id: to_string(user.id),
301 display_name: user.name,
303 created_at: "2017-08-15T15:47:06.000Z",
309 avatar: "http://localhost:4001/images/avi.png",
310 avatar_static: "http://localhost:4001/images/avi.png",
311 header: "http://localhost:4001/images/banner.png",
312 header_static: "http://localhost:4001/images/banner.png",
325 background_image: nil,
326 confirmation_pending: false,
330 hide_favorites: true,
331 hide_followers: false,
333 hide_followers_count: false,
334 hide_follows_count: false,
336 id: to_string(user.id),
343 muting_notifications: false,
345 domain_blocking: false,
346 showing_reblogs: true,
349 skip_thread_containment: false
353 assert expected == AccountView.render("show.json", %{user: 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 "sanitizes display names" do
372 user = insert(:user, name: "<marquee> username </marquee>")
373 result = AccountView.render("show.json", %{user: user})
374 refute result.display_name == "<marquee> username </marquee>"
377 describe "hiding follows/following" do
378 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
381 hide_followers: true,
382 hide_followers_count: true,
384 hide_follows_count: true
387 other_user = insert(:user)
388 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
389 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
394 pleroma: %{hide_follows_count: true, hide_followers_count: true}
395 } = AccountView.render("show.json", %{user: user})
398 test "shows when follows/followers are hidden" do
399 user = insert(:user, hide_followers: true, hide_follows: true)
400 other_user = insert(:user)
401 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
402 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
407 pleroma: %{hide_follows: true, hide_followers: true}
408 } = AccountView.render("show.json", %{user: user})
411 test "shows actual follower/following count to the account owner" do
412 user = insert(:user, hide_followers: true, hide_follows: true)
413 other_user = insert(:user)
414 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
415 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
420 } = AccountView.render("show.json", %{user: user, for: user})
423 test "shows unread_conversation_count only to the account owner" do
425 other_user = insert(:user)
428 CommonAPI.post(other_user, %{
429 "status" => "Hey @#{user.nickname}.",
430 "visibility" => "direct"
433 user = User.get_cached_by_ap_id(user.ap_id)
435 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
436 :unread_conversation_count
439 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
440 :unread_conversation_count
445 describe "follow requests counter" do
446 test "shows zero when no follow requests are pending" do
449 assert %{follow_requests_count: 0} =
450 AccountView.render("show.json", %{user: user, for: user})
452 other_user = insert(:user)
453 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
455 assert %{follow_requests_count: 0} =
456 AccountView.render("show.json", %{user: user, for: user})
459 test "shows non-zero when follow requests are pending" do
460 user = insert(:user, locked: true)
462 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
464 other_user = insert(:user)
465 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
467 assert %{locked: true, follow_requests_count: 1} =
468 AccountView.render("show.json", %{user: user, for: user})
471 test "decreases when accepting a follow request" do
472 user = insert(:user, locked: true)
474 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
476 other_user = insert(:user)
477 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
479 assert %{locked: true, follow_requests_count: 1} =
480 AccountView.render("show.json", %{user: user, for: user})
482 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
484 assert %{locked: true, follow_requests_count: 0} =
485 AccountView.render("show.json", %{user: user, for: user})
488 test "decreases when rejecting a follow request" do
489 user = insert(:user, locked: true)
491 assert %{locked: true} = 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 %{locked: true, follow_requests_count: 1} =
497 AccountView.render("show.json", %{user: user, for: user})
499 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
501 assert %{locked: true, follow_requests_count: 0} =
502 AccountView.render("show.json", %{user: user, for: user})
505 test "shows non-zero when historical unapproved requests are present" do
506 user = insert(:user, locked: true)
508 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
510 other_user = insert(:user)
511 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
513 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
515 assert %{locked: false, follow_requests_count: 1} =
516 AccountView.render("show.json", %{user: user, for: user})