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"}]
32 source_data: source_data,
33 background: background_image
35 nickname: "shp@shitposter.club",
36 name: ":karjalanpiirakka: shp",
37 bio: "<script src=\"invalid-html\"></script><span>valid html</span>",
38 inserted_at: ~N[2017-08-15 15:47:06.597036]
42 id: to_string(user.id),
45 display_name: user.name,
47 created_at: "2017-08-15T15:47:06.000Z",
51 note: "<span>valid html</span>",
53 avatar: "http://localhost:4001/images/avi.png",
54 avatar_static: "http://localhost:4001/images/avi.png",
55 header: "http://localhost:4001/images/banner.png",
56 header_static: "http://localhost:4001/images/banner.png",
59 "static_url" => "/file.png",
61 "shortcode" => "karjalanpiirakka",
62 "visible_in_picker" => false
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 = %{
100 "non_follows" => true,
101 "non_followers" => true
104 privacy = user.info.default_scope
107 pleroma: %{notification_settings: ^notification_settings},
108 source: %{privacy: ^privacy}
109 } = AccountView.render("show.json", %{user: user, for: user})
112 test "Represent a Service(bot) account" do
115 info: %{note_count: 5, follower_count: 3, 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, %{info: %{is_admin: true}})
168 deactivated_user = insert(:user, %{info: %{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, other_user} = User.subscribe(user, other_user)
194 {:ok, user} = User.mute(user, other_user, true)
195 {:ok, user} = 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, other_user} = User.subscribe(user, other_user)
222 {:ok, user} = User.block(user, other_user)
223 {:ok, other_user} = 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, %{info: %User.Info{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
285 info: %{note_count: 5, follower_count: 0, source_data: %{"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, other_user} = 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",
323 background_image: nil,
324 confirmation_pending: false,
328 hide_favorites: true,
329 hide_followers: false,
331 hide_followers_count: false,
332 hide_follows_count: false,
334 id: to_string(user.id),
341 muting_notifications: false,
343 domain_blocking: false,
344 showing_reblogs: true,
347 skip_thread_containment: false
351 assert expected == AccountView.render("show.json", %{user: user, for: other_user})
354 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
355 user = insert(:user, %{info: %User.Info{pleroma_settings_store: %{fe: "test"}}})
358 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
360 assert result.pleroma.settings_store == %{:fe => "test"}
362 result = AccountView.render("show.json", %{user: user, with_pleroma_settings: true})
363 assert result.pleroma[:settings_store] == nil
365 result = AccountView.render("show.json", %{user: user, for: user})
366 assert result.pleroma[:settings_store] == nil
369 test "sanitizes display names" do
370 user = insert(:user, name: "<marquee> username </marquee>")
371 result = AccountView.render("show.json", %{user: user})
372 refute result.display_name == "<marquee> username </marquee>"
375 describe "hiding follows/following" do
376 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
378 hide_followers: true,
379 hide_followers_count: true,
381 hide_follows_count: true
384 user = insert(:user, info: info)
386 other_user = insert(:user)
387 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
388 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
393 pleroma: %{hide_follows_count: true, hide_followers_count: true}
394 } = AccountView.render("show.json", %{user: user})
397 test "shows when follows/followers are hidden" do
398 user = insert(:user, info: %{hide_followers: true, hide_follows: true})
399 other_user = insert(:user)
400 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
401 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
406 pleroma: %{hide_follows: true, hide_followers: true}
407 } = AccountView.render("show.json", %{user: user})
410 test "shows actual follower/following count to the account owner" do
411 user = insert(:user, info: %{hide_followers: true, hide_follows: true})
412 other_user = insert(:user)
413 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
414 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
419 } = AccountView.render("show.json", %{user: user, for: user})
422 test "shows unread_conversation_count only to the account owner" do
424 other_user = insert(:user)
427 CommonAPI.post(other_user, %{
428 "status" => "Hey @#{user.nickname}.",
429 "visibility" => "direct"
432 user = User.get_cached_by_ap_id(user.ap_id)
434 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
435 :unread_conversation_count
438 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
439 :unread_conversation_count
444 describe "follow requests counter" do
445 test "shows zero when no follow requests are pending" do
448 assert %{follow_requests_count: 0} =
449 AccountView.render("show.json", %{user: user, for: user})
451 other_user = insert(:user)
452 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
454 assert %{follow_requests_count: 0} =
455 AccountView.render("show.json", %{user: user, for: user})
458 test "shows non-zero when follow requests are pending" do
459 user = insert(:user, %{info: %{locked: true}})
461 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
463 other_user = insert(:user)
464 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
466 assert %{locked: true, follow_requests_count: 1} =
467 AccountView.render("show.json", %{user: user, for: user})
470 test "decreases when accepting a follow request" do
471 user = insert(:user, %{info: %{locked: true}})
473 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
475 other_user = insert(:user)
476 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
478 assert %{locked: true, follow_requests_count: 1} =
479 AccountView.render("show.json", %{user: user, for: user})
481 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
483 assert %{locked: true, follow_requests_count: 0} =
484 AccountView.render("show.json", %{user: user, for: user})
487 test "decreases when rejecting a follow request" do
488 user = insert(:user, %{info: %{locked: true}})
490 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
492 other_user = insert(:user)
493 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
495 assert %{locked: true, follow_requests_count: 1} =
496 AccountView.render("show.json", %{user: user, for: user})
498 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
500 assert %{locked: true, follow_requests_count: 0} =
501 AccountView.render("show.json", %{user: user, for: user})
504 test "shows non-zero when historical unapproved requests are present" do
505 user = insert(:user, %{info: %{locked: true}})
507 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
509 other_user = insert(:user)
510 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
512 {:ok, user} = User.update_info(user, &User.Info.user_upgrade(&1, %{locked: false}))
514 assert %{locked: false, follow_requests_count: 1} =
515 AccountView.render("show.json", %{user: user, for: user})