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"},
37 raw_bio: "valid html. a\nb\nc\nd\nf '&<>\""
41 id: to_string(user.id),
44 display_name: user.name,
46 created_at: "2017-08-15T15:47:06.000Z",
50 note: "<span>valid html</span>. a<br/>b<br/>c<br/>d<br/>f '&<>"",
52 avatar: "http://localhost:4001/images/avi.png",
53 avatar_static: "http://localhost:4001/images/avi.png",
54 header: "http://localhost:4001/images/banner.png",
55 header_static: "http://localhost:4001/images/banner.png",
58 static_url: "/file.png",
60 shortcode: "karjalanpiirakka",
61 visible_in_picker: false
67 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",
152 background_image: nil,
153 confirmation_pending: false,
157 hide_favorites: true,
158 hide_followers: false,
160 hide_followers_count: false,
161 hide_follows_count: false,
163 skip_thread_containment: false
167 assert expected == AccountView.render("show.json", %{user: user})
170 test "Represent a Funkwhale channel" do
172 User.get_or_fetch_by_ap_id(
173 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
176 assert represented = AccountView.render("show.json", %{user: user})
177 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
178 assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
181 test "Represent a deactivated user for an admin" do
182 admin = insert(:user, is_admin: true)
183 deactivated_user = insert(:user, deactivated: true)
184 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
185 assert represented[:pleroma][:deactivated] == true
188 test "Represent a smaller mention" do
192 id: to_string(user.id),
194 username: user.nickname,
198 assert expected == AccountView.render("mention.json", %{user: user})
201 describe "relationship" do
202 defp test_relationship_rendering(user, other_user, expected_result) do
203 opts = %{user: user, target: other_user, relationships: nil}
204 assert expected_result == AccountView.render("relationship.json", opts)
206 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
207 opts = Map.put(opts, :relationships, relationships_opt)
208 assert expected_result == AccountView.render("relationship.json", opts)
210 assert [expected_result] ==
211 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
220 muting_notifications: false,
223 domain_blocking: false,
224 showing_reblogs: true,
228 test "represent a relationship for the following and followed user" do
230 other_user = insert(:user)
232 {:ok, user} = User.follow(user, other_user)
233 {:ok, other_user} = User.follow(other_user, user)
234 {:ok, _subscription} = User.subscribe(user, other_user)
235 {:ok, _user_relationships} = User.mute(user, other_user, true)
236 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
245 muting_notifications: true,
247 showing_reblogs: false,
248 id: to_string(other_user.id)
252 test_relationship_rendering(user, other_user, expected)
255 test "represent a relationship for the blocking and blocked user" do
257 other_user = insert(:user)
259 {:ok, user} = User.follow(user, other_user)
260 {:ok, _subscription} = User.subscribe(user, other_user)
261 {:ok, _user_relationship} = User.block(user, other_user)
262 {:ok, _user_relationship} = User.block(other_user, user)
267 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
270 test_relationship_rendering(user, other_user, expected)
273 test "represent a relationship for the user blocking a domain" do
275 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
277 {:ok, user} = User.block_domain(user, "bad.site")
282 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
285 test_relationship_rendering(user, other_user, expected)
288 test "represent a relationship for the user with a pending follow request" do
290 other_user = insert(:user, locked: true)
292 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
293 user = User.get_cached_by_id(user.id)
294 other_user = User.get_cached_by_id(other_user.id)
299 %{requested: true, following: false, id: to_string(other_user.id)}
302 test_relationship_rendering(user, other_user, expected)
306 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
307 user = insert(:user, pleroma_settings_store: %{fe: "test"})
310 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
312 assert result.pleroma.settings_store == %{:fe => "test"}
314 result = AccountView.render("show.json", %{user: user, with_pleroma_settings: true})
315 assert result.pleroma[:settings_store] == nil
317 result = AccountView.render("show.json", %{user: user, for: user})
318 assert result.pleroma[:settings_store] == nil
321 test "doesn't sanitize display names" do
322 user = insert(:user, name: "<marquee> username </marquee>")
323 result = AccountView.render("show.json", %{user: user})
324 assert result.display_name == "<marquee> username </marquee>"
327 test "never display nil user follow counts" do
328 user = insert(:user, following_count: 0, follower_count: 0)
329 result = AccountView.render("show.json", %{user: user})
331 assert result.following_count == 0
332 assert result.followers_count == 0
335 describe "hiding follows/following" do
336 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
339 hide_followers: true,
340 hide_followers_count: true,
342 hide_follows_count: true
345 other_user = insert(:user)
346 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
347 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
352 pleroma: %{hide_follows_count: true, hide_followers_count: true}
353 } = AccountView.render("show.json", %{user: user})
356 test "shows when follows/followers are hidden" do
357 user = insert(:user, hide_followers: true, hide_follows: true)
358 other_user = insert(:user)
359 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
360 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
365 pleroma: %{hide_follows: true, hide_followers: true}
366 } = AccountView.render("show.json", %{user: user})
369 test "shows actual follower/following count to the account owner" do
370 user = insert(:user, hide_followers: true, hide_follows: true)
371 other_user = insert(:user)
372 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
373 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
378 } = AccountView.render("show.json", %{user: user, for: user})
381 test "shows unread_conversation_count only to the account owner" do
383 other_user = insert(:user)
386 CommonAPI.post(other_user, %{
387 status: "Hey @#{user.nickname}.",
391 user = User.get_cached_by_ap_id(user.ap_id)
393 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
394 :unread_conversation_count
397 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
398 :unread_conversation_count
402 test "shows unread_count only to the account owner" do
404 insert_list(7, :notification, user: user)
405 other_user = insert(:user)
407 user = User.get_cached_by_ap_id(user.ap_id)
409 assert AccountView.render(
411 %{user: user, for: other_user}
412 )[:pleroma][:unread_notifications_count] == nil
414 assert AccountView.render(
416 %{user: user, for: user}
417 )[:pleroma][:unread_notifications_count] == 7
421 describe "follow requests counter" do
422 test "shows zero when no follow requests are pending" do
425 assert %{follow_requests_count: 0} =
426 AccountView.render("show.json", %{user: user, for: user})
428 other_user = insert(:user)
429 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
431 assert %{follow_requests_count: 0} =
432 AccountView.render("show.json", %{user: user, for: user})
435 test "shows non-zero when follow requests are pending" do
436 user = insert(:user, locked: true)
438 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
440 other_user = insert(:user)
441 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
443 assert %{locked: true, follow_requests_count: 1} =
444 AccountView.render("show.json", %{user: user, for: user})
447 test "decreases when accepting a follow request" do
448 user = insert(:user, locked: true)
450 assert %{locked: true} = 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 %{locked: true, follow_requests_count: 1} =
456 AccountView.render("show.json", %{user: user, for: user})
458 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
460 assert %{locked: true, follow_requests_count: 0} =
461 AccountView.render("show.json", %{user: user, for: user})
464 test "decreases when rejecting a follow request" do
465 user = insert(:user, locked: true)
467 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
469 other_user = insert(:user)
470 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
472 assert %{locked: true, follow_requests_count: 1} =
473 AccountView.render("show.json", %{user: user, for: user})
475 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
477 assert %{locked: true, follow_requests_count: 0} =
478 AccountView.render("show.json", %{user: user, for: user})
481 test "shows non-zero when historical unapproved requests are present" do
482 user = insert(:user, locked: true)
484 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
486 other_user = insert(:user)
487 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
489 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
491 assert %{locked: false, follow_requests_count: 1} =
492 AccountView.render("show.json", %{user: user, for: user})
496 test "uses mediaproxy urls when it's enabled" do
497 clear_config([:media_proxy, :enabled], true)
501 avatar: %{"url" => [%{"href" => "https://evil.website/avatar.png"}]},
502 banner: %{"url" => [%{"href" => "https://evil.website/banner.png"}]},
503 emoji: %{"joker_smile" => "https://evil.website/society.png"}
506 AccountView.render("show.json", %{user: user})
508 {key, url} when key in [:avatar, :avatar_static, :header, :header_static] ->
509 String.starts_with?(url, Pleroma.Web.base_url())
512 Enum.all?(emojis, fn %{url: url, static_url: static_url} ->
513 String.starts_with?(url, Pleroma.Web.base_url()) &&
514 String.starts_with?(static_url, Pleroma.Web.base_url())