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.Web.CommonAPI
10 alias Pleroma.Web.MastodonAPI.AccountView
12 import Pleroma.Factory
16 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
20 test "Represent a user account" do
25 "icon" => %{"url" => "/file.png"},
26 "name" => ":karjalanpiirakka:"
32 "url" => [%{"href" => "https://example.com/images/asuka_hospital.png"}]
39 source_data: source_data,
40 background: background_image,
41 nickname: "shp@shitposter.club",
42 name: ":karjalanpiirakka: shp",
44 "<script src=\"invalid-html\"></script><span>valid html</span>. a<br>b<br/>c<br >d<br />f",
45 inserted_at: ~N[2017-08-15 15:47:06.597036]
49 id: to_string(user.id),
52 display_name: user.name,
54 created_at: "2017-08-15T15:47:06.000Z",
58 note: "<span>valid html</span>. a<br/>b<br/>c<br/>d<br/>f",
60 avatar: "http://localhost:4001/images/avi.png",
61 avatar_static: "http://localhost:4001/images/avi.png",
62 header: "http://localhost:4001/images/banner.png",
63 header_static: "http://localhost:4001/images/banner.png",
66 "static_url" => "/file.png",
68 "shortcode" => "karjalanpiirakka",
69 "visible_in_picker" => false
75 note: "valid html. a\nb\nc\nd\nf",
84 background_image: "https://example.com/images/asuka_hospital.png",
85 confirmation_pending: false,
90 hide_followers: false,
92 hide_followers_count: false,
93 hide_follows_count: false,
95 skip_thread_containment: false
99 assert expected == AccountView.render("show.json", %{user: user})
102 test "Represent the user account for the account owner" do
105 notification_settings = %Pleroma.User.NotificationSetting{}
106 privacy = user.default_scope
109 pleroma: %{notification_settings: ^notification_settings, allow_following_move: true},
110 source: %{privacy: ^privacy}
111 } = AccountView.render("show.json", %{user: user, for: user})
114 test "Represent a Service(bot) account" do
120 actor_type: "Service",
121 nickname: "shp@shitposter.club",
122 inserted_at: ~N[2017-08-15 15:47:06.597036]
126 id: to_string(user.id),
129 display_name: user.name,
131 created_at: "2017-08-15T15:47:06.000Z",
137 avatar: "http://localhost:4001/images/avi.png",
138 avatar_static: "http://localhost:4001/images/avi.png",
139 header: "http://localhost:4001/images/banner.png",
140 header_static: "http://localhost:4001/images/banner.png",
148 actor_type: "Service",
154 background_image: nil,
155 confirmation_pending: false,
159 hide_favorites: true,
160 hide_followers: false,
162 hide_followers_count: false,
163 hide_follows_count: false,
165 skip_thread_containment: false
169 assert expected == AccountView.render("show.json", %{user: user})
172 test "Represent a Funkwhale channel" do
174 User.get_or_fetch_by_ap_id(
175 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
178 assert represented = AccountView.render("show.json", %{user: user})
179 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
180 assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
183 test "Represent a deactivated user for an admin" do
184 admin = insert(:user, is_admin: true)
185 deactivated_user = insert(:user, deactivated: true)
186 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
187 assert represented[:pleroma][:deactivated] == true
190 test "Represent a smaller mention" do
194 id: to_string(user.id),
196 username: user.nickname,
200 assert expected == AccountView.render("mention.json", %{user: user})
203 describe "relationship" do
204 test "represent a relationship for the following and followed user" do
206 other_user = insert(:user)
208 {:ok, user} = User.follow(user, other_user)
209 {:ok, other_user} = User.follow(other_user, user)
210 {:ok, _subscription} = User.subscribe(user, other_user)
211 {:ok, _user_relationships} = User.mute(user, other_user, true)
212 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
215 id: to_string(other_user.id),
221 muting_notifications: true,
224 domain_blocking: false,
225 showing_reblogs: false,
230 AccountView.render("relationship.json", %{user: user, target: other_user})
233 test "represent a relationship for the blocking and blocked user" do
235 other_user = insert(:user)
237 {:ok, user} = User.follow(user, other_user)
238 {:ok, _subscription} = User.subscribe(user, other_user)
239 {:ok, _user_relationship} = User.block(user, other_user)
240 {:ok, _user_relationship} = User.block(other_user, user)
243 id: to_string(other_user.id),
249 muting_notifications: false,
252 domain_blocking: false,
253 showing_reblogs: true,
258 AccountView.render("relationship.json", %{user: user, target: other_user})
261 test "represent a relationship for the user blocking a domain" do
263 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
265 {:ok, user} = User.block_domain(user, "bad.site")
267 assert %{domain_blocking: true, blocking: false} =
268 AccountView.render("relationship.json", %{user: user, target: other_user})
271 test "represent a relationship for the user with a pending follow request" do
273 other_user = insert(:user, locked: true)
275 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
276 user = User.get_cached_by_id(user.id)
277 other_user = User.get_cached_by_id(other_user.id)
280 id: to_string(other_user.id),
286 muting_notifications: false,
289 domain_blocking: false,
290 showing_reblogs: true,
295 AccountView.render("relationship.json", %{user: user, target: other_user})
299 test "represent an embedded relationship" do
305 actor_type: "Service",
306 nickname: "shp@shitposter.club",
307 inserted_at: ~N[2017-08-15 15:47:06.597036]
310 other_user = insert(:user)
311 {:ok, other_user} = User.follow(other_user, user)
312 {:ok, _user_relationship} = User.block(other_user, user)
313 {:ok, _} = User.follow(insert(:user), user)
316 id: to_string(user.id),
319 display_name: user.name,
321 created_at: "2017-08-15T15:47:06.000Z",
327 avatar: "http://localhost:4001/images/avi.png",
328 avatar_static: "http://localhost:4001/images/avi.png",
329 header: "http://localhost:4001/images/banner.png",
330 header_static: "http://localhost:4001/images/banner.png",
338 actor_type: "Service",
344 background_image: nil,
345 confirmation_pending: false,
349 hide_favorites: true,
350 hide_followers: false,
352 hide_followers_count: false,
353 hide_follows_count: false,
355 id: to_string(user.id),
362 muting_notifications: false,
364 domain_blocking: false,
365 showing_reblogs: true,
368 skip_thread_containment: false
373 AccountView.render("show.json", %{user: refresh_record(user), for: other_user})
376 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
377 user = insert(:user, pleroma_settings_store: %{fe: "test"})
380 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
382 assert result.pleroma.settings_store == %{:fe => "test"}
384 result = AccountView.render("show.json", %{user: user, with_pleroma_settings: true})
385 assert result.pleroma[:settings_store] == nil
387 result = AccountView.render("show.json", %{user: user, for: user})
388 assert result.pleroma[:settings_store] == nil
391 test "doesn't sanitize display names" do
392 user = insert(:user, name: "<marquee> username </marquee>")
393 result = AccountView.render("show.json", %{user: user})
394 assert result.display_name == "<marquee> username </marquee>"
397 test "never display nil user follow counts" do
398 user = insert(:user, following_count: 0, follower_count: 0)
399 result = AccountView.render("show.json", %{user: user})
401 assert result.following_count == 0
402 assert result.followers_count == 0
405 describe "hiding follows/following" do
406 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
409 hide_followers: true,
410 hide_followers_count: true,
412 hide_follows_count: true
415 other_user = insert(:user)
416 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
417 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
422 pleroma: %{hide_follows_count: true, hide_followers_count: true}
423 } = AccountView.render("show.json", %{user: user})
426 test "shows when follows/followers are hidden" do
427 user = insert(:user, hide_followers: true, hide_follows: true)
428 other_user = insert(:user)
429 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
430 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
435 pleroma: %{hide_follows: true, hide_followers: true}
436 } = AccountView.render("show.json", %{user: user})
439 test "shows actual follower/following count to the account owner" do
440 user = insert(:user, hide_followers: true, hide_follows: true)
441 other_user = insert(:user)
442 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
443 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
448 } = AccountView.render("show.json", %{user: user, for: user})
451 test "shows unread_conversation_count only to the account owner" do
453 other_user = insert(:user)
456 CommonAPI.post(other_user, %{
457 "status" => "Hey @#{user.nickname}.",
458 "visibility" => "direct"
461 user = User.get_cached_by_ap_id(user.ap_id)
463 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
464 :unread_conversation_count
467 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
468 :unread_conversation_count
473 describe "follow requests counter" do
474 test "shows zero when no follow requests are pending" do
477 assert %{follow_requests_count: 0} =
478 AccountView.render("show.json", %{user: user, for: user})
480 other_user = insert(:user)
481 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
483 assert %{follow_requests_count: 0} =
484 AccountView.render("show.json", %{user: user, for: user})
487 test "shows non-zero when follow requests are pending" do
488 user = insert(:user, 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})
499 test "decreases when accepting a follow request" do
500 user = insert(:user, locked: true)
502 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
504 other_user = insert(:user)
505 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
507 assert %{locked: true, follow_requests_count: 1} =
508 AccountView.render("show.json", %{user: user, for: user})
510 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
512 assert %{locked: true, follow_requests_count: 0} =
513 AccountView.render("show.json", %{user: user, for: user})
516 test "decreases when rejecting a follow request" do
517 user = insert(:user, locked: true)
519 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
521 other_user = insert(:user)
522 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
524 assert %{locked: true, follow_requests_count: 1} =
525 AccountView.render("show.json", %{user: user, for: user})
527 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
529 assert %{locked: true, follow_requests_count: 0} =
530 AccountView.render("show.json", %{user: user, for: user})
533 test "shows non-zero when historical unapproved requests are present" do
534 user = insert(:user, locked: true)
536 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
538 other_user = insert(:user)
539 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
541 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
543 assert %{locked: false, follow_requests_count: 1} =
544 AccountView.render("show.json", %{user: user, for: user})