Merge branch 'develop' into '2435-list-multiple-users'
[akkoma] / test / pleroma / web / mastodon_api / views / account_view_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
6 use Pleroma.DataCase
7
8 alias Pleroma.Config
9 alias Pleroma.User
10 alias Pleroma.UserRelationship
11 alias Pleroma.Web.CommonAPI
12 alias Pleroma.Web.MastodonAPI.AccountView
13
14 import Pleroma.Factory
15 import Tesla.Mock
16
17 setup do
18 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
19 :ok
20 end
21
22 test "Represent a user account" do
23 background_image = %{
24 "url" => [%{"href" => "https://example.com/images/asuka_hospital.png"}]
25 }
26
27 user =
28 insert(:user, %{
29 follower_count: 3,
30 note_count: 5,
31 background: background_image,
32 nickname: "shp@shitposter.club",
33 name: ":karjalanpiirakka: shp",
34 bio:
35 "<script src=\"invalid-html\"></script><span>valid html</span>. a<br>b<br/>c<br >d<br />f '&<>\"",
36 inserted_at: ~N[2017-08-15 15:47:06.597036],
37 emoji: %{"karjalanpiirakka" => "/file.png"},
38 raw_bio: "valid html. a\nb\nc\nd\nf '&<>\"",
39 also_known_as: ["https://shitposter.zone/users/shp"]
40 })
41
42 expected = %{
43 id: to_string(user.id),
44 username: "shp",
45 acct: user.nickname,
46 display_name: user.name,
47 locked: false,
48 created_at: "2017-08-15T15:47:06.000Z",
49 followers_count: 3,
50 following_count: 0,
51 statuses_count: 5,
52 note: "<span>valid html</span>. a<br/>b<br/>c<br/>d<br/>f &#39;&amp;&lt;&gt;&quot;",
53 url: user.ap_id,
54 avatar: "http://localhost:4001/images/avi.png",
55 avatar_static: "http://localhost:4001/images/avi.png",
56 header: "http://localhost:4001/images/banner.png",
57 header_static: "http://localhost:4001/images/banner.png",
58 emojis: [
59 %{
60 static_url: "/file.png",
61 url: "/file.png",
62 shortcode: "karjalanpiirakka",
63 visible_in_picker: false
64 }
65 ],
66 fields: [],
67 bot: false,
68 source: %{
69 note: "valid html. a\nb\nc\nd\nf '&<>\"",
70 sensitive: false,
71 pleroma: %{
72 actor_type: "Person",
73 discoverable: true
74 },
75 fields: []
76 },
77 pleroma: %{
78 ap_id: user.ap_id,
79 also_known_as: ["https://shitposter.zone/users/shp"],
80 background_image: "https://example.com/images/asuka_hospital.png",
81 favicon: nil,
82 is_confirmed: true,
83 tags: [],
84 is_admin: false,
85 is_moderator: false,
86 hide_favorites: true,
87 hide_followers: false,
88 hide_follows: false,
89 hide_followers_count: false,
90 hide_follows_count: false,
91 relationship: %{},
92 skip_thread_containment: false,
93 accepts_chat_messages: nil
94 }
95 }
96
97 assert expected == AccountView.render("show.json", %{user: user, skip_visibility_check: true})
98 end
99
100 describe "favicon" do
101 setup do
102 [user: insert(:user)]
103 end
104
105 test "is parsed when :instance_favicons is enabled", %{user: user} do
106 clear_config([:instances_favicons, :enabled], true)
107
108 assert %{
109 pleroma: %{
110 favicon:
111 "https://shitposter.club/plugins/Qvitter/img/gnusocial-favicons/favicon-16x16.png"
112 }
113 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
114 end
115
116 test "is nil when :instances_favicons is disabled", %{user: user} do
117 assert %{pleroma: %{favicon: nil}} =
118 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
119 end
120 end
121
122 test "Represent the user account for the account owner" do
123 user = insert(:user)
124
125 notification_settings = %{
126 block_from_strangers: false,
127 hide_notification_contents: false
128 }
129
130 privacy = user.default_scope
131
132 assert %{
133 pleroma: %{notification_settings: ^notification_settings, allow_following_move: true},
134 source: %{privacy: ^privacy}
135 } = AccountView.render("show.json", %{user: user, for: user})
136 end
137
138 test "Represent a Service(bot) account" do
139 user =
140 insert(:user, %{
141 follower_count: 3,
142 note_count: 5,
143 actor_type: "Service",
144 nickname: "shp@shitposter.club",
145 inserted_at: ~N[2017-08-15 15:47:06.597036]
146 })
147
148 expected = %{
149 id: to_string(user.id),
150 username: "shp",
151 acct: user.nickname,
152 display_name: user.name,
153 locked: false,
154 created_at: "2017-08-15T15:47:06.000Z",
155 followers_count: 3,
156 following_count: 0,
157 statuses_count: 5,
158 note: user.bio,
159 url: user.ap_id,
160 avatar: "http://localhost:4001/images/avi.png",
161 avatar_static: "http://localhost:4001/images/avi.png",
162 header: "http://localhost:4001/images/banner.png",
163 header_static: "http://localhost:4001/images/banner.png",
164 emojis: [],
165 fields: [],
166 bot: true,
167 source: %{
168 note: user.bio,
169 sensitive: false,
170 pleroma: %{
171 actor_type: "Service",
172 discoverable: true
173 },
174 fields: []
175 },
176 pleroma: %{
177 ap_id: user.ap_id,
178 also_known_as: [],
179 background_image: nil,
180 favicon: nil,
181 is_confirmed: true,
182 tags: [],
183 is_admin: false,
184 is_moderator: false,
185 hide_favorites: true,
186 hide_followers: false,
187 hide_follows: false,
188 hide_followers_count: false,
189 hide_follows_count: false,
190 relationship: %{},
191 skip_thread_containment: false,
192 accepts_chat_messages: nil
193 }
194 }
195
196 assert expected == AccountView.render("show.json", %{user: user, skip_visibility_check: true})
197 end
198
199 test "Represent a Funkwhale channel" do
200 {:ok, user} =
201 User.get_or_fetch_by_ap_id(
202 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
203 )
204
205 assert represented =
206 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
207
208 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
209 assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
210 end
211
212 test "Represent a deactivated user for an admin" do
213 admin = insert(:user, is_admin: true)
214 deactivated_user = insert(:user, is_active: false)
215 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
216 assert represented[:pleroma][:deactivated] == true
217 end
218
219 test "Represent a smaller mention" do
220 user = insert(:user)
221
222 expected = %{
223 id: to_string(user.id),
224 acct: user.nickname,
225 username: user.nickname,
226 url: user.ap_id
227 }
228
229 assert expected == AccountView.render("mention.json", %{user: user})
230 end
231
232 test "demands :for or :skip_visibility_check option for account rendering" do
233 clear_config([:restrict_unauthenticated, :profiles, :local], false)
234
235 user = insert(:user)
236 user_id = user.id
237
238 assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: nil})
239 assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: user})
240
241 assert %{id: ^user_id} =
242 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
243
244 assert_raise RuntimeError, ~r/:skip_visibility_check or :for option is required/, fn ->
245 AccountView.render("show.json", %{user: user})
246 end
247 end
248
249 describe "relationship" do
250 defp test_relationship_rendering(user, other_user, expected_result) do
251 opts = %{user: user, target: other_user, relationships: nil}
252 assert expected_result == AccountView.render("relationship.json", opts)
253
254 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
255 opts = Map.put(opts, :relationships, relationships_opt)
256 assert expected_result == AccountView.render("relationship.json", opts)
257
258 assert [expected_result] ==
259 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
260 end
261
262 @blank_response %{
263 following: false,
264 followed_by: false,
265 blocking: false,
266 blocked_by: false,
267 muting: false,
268 muting_notifications: false,
269 subscribing: false,
270 requested: false,
271 domain_blocking: false,
272 showing_reblogs: true,
273 endorsed: false
274 }
275
276 test "represent a relationship for the following and followed user" do
277 user = insert(:user)
278 other_user = insert(:user)
279
280 {:ok, user, other_user} = User.follow(user, other_user)
281 {:ok, other_user, user} = User.follow(other_user, user)
282 {:ok, _subscription} = User.subscribe(user, other_user)
283 {:ok, _user_relationships} = User.mute(user, other_user, %{notifications: true})
284 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
285
286 expected =
287 Map.merge(
288 @blank_response,
289 %{
290 following: true,
291 followed_by: true,
292 muting: true,
293 muting_notifications: true,
294 subscribing: true,
295 showing_reblogs: false,
296 id: to_string(other_user.id)
297 }
298 )
299
300 test_relationship_rendering(user, other_user, expected)
301 end
302
303 test "represent a relationship for the blocking and blocked user" do
304 user = insert(:user)
305 other_user = insert(:user)
306
307 {:ok, user, other_user} = User.follow(user, other_user)
308 {:ok, _subscription} = User.subscribe(user, other_user)
309 {:ok, _user_relationship} = User.block(user, other_user)
310 {:ok, _user_relationship} = User.block(other_user, user)
311
312 expected =
313 Map.merge(
314 @blank_response,
315 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
316 )
317
318 test_relationship_rendering(user, other_user, expected)
319 end
320
321 test "represent a relationship for the user blocking a domain" do
322 user = insert(:user)
323 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
324
325 {:ok, user} = User.block_domain(user, "bad.site")
326
327 expected =
328 Map.merge(
329 @blank_response,
330 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
331 )
332
333 test_relationship_rendering(user, other_user, expected)
334 end
335
336 test "represent a relationship for the user with a pending follow request" do
337 user = insert(:user)
338 other_user = insert(:user, is_locked: true)
339
340 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
341 user = User.get_cached_by_id(user.id)
342 other_user = User.get_cached_by_id(other_user.id)
343
344 expected =
345 Map.merge(
346 @blank_response,
347 %{requested: true, following: false, id: to_string(other_user.id)}
348 )
349
350 test_relationship_rendering(user, other_user, expected)
351 end
352 end
353
354 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
355 user = insert(:user, pleroma_settings_store: %{fe: "test"})
356
357 result =
358 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
359
360 assert result.pleroma.settings_store == %{:fe => "test"}
361
362 result = AccountView.render("show.json", %{user: user, for: nil, with_pleroma_settings: true})
363 assert result.pleroma[:settings_store] == nil
364
365 result = AccountView.render("show.json", %{user: user, for: user})
366 assert result.pleroma[:settings_store] == nil
367 end
368
369 test "doesn't sanitize display names" do
370 user = insert(:user, name: "<marquee> username </marquee>")
371 result = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
372 assert result.display_name == "<marquee> username </marquee>"
373 end
374
375 test "never display nil user follow counts" do
376 user = insert(:user, following_count: 0, follower_count: 0)
377 result = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
378
379 assert result.following_count == 0
380 assert result.followers_count == 0
381 end
382
383 describe "hiding follows/following" do
384 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
385 user =
386 insert(:user, %{
387 hide_followers: true,
388 hide_followers_count: true,
389 hide_follows: true,
390 hide_follows_count: true
391 })
392
393 other_user = insert(:user)
394 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
395 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
396
397 assert %{
398 followers_count: 0,
399 following_count: 0,
400 pleroma: %{hide_follows_count: true, hide_followers_count: true}
401 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
402 end
403
404 test "shows when follows/followers are hidden" do
405 user = insert(:user, hide_followers: true, hide_follows: true)
406 other_user = insert(:user)
407 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
408 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
409
410 assert %{
411 followers_count: 1,
412 following_count: 1,
413 pleroma: %{hide_follows: true, hide_followers: true}
414 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
415 end
416
417 test "shows actual follower/following count to the account owner" do
418 user = insert(:user, hide_followers: true, hide_follows: true)
419 other_user = insert(:user)
420 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
421
422 assert User.following?(user, other_user)
423 assert Pleroma.FollowingRelationship.follower_count(other_user) == 1
424 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
425
426 assert %{
427 followers_count: 1,
428 following_count: 1
429 } = AccountView.render("show.json", %{user: user, for: user})
430 end
431
432 test "shows unread_conversation_count only to the account owner" do
433 user = insert(:user)
434 other_user = insert(:user)
435
436 {:ok, _activity} =
437 CommonAPI.post(other_user, %{
438 status: "Hey @#{user.nickname}.",
439 visibility: "direct"
440 })
441
442 user = User.get_cached_by_ap_id(user.ap_id)
443
444 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
445 :unread_conversation_count
446 ] == nil
447
448 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
449 :unread_conversation_count
450 ] == 1
451 end
452
453 test "shows unread_count only to the account owner" do
454 user = insert(:user)
455 insert_list(7, :notification, user: user, activity: insert(:note_activity))
456 other_user = insert(:user)
457
458 user = User.get_cached_by_ap_id(user.ap_id)
459
460 assert AccountView.render(
461 "show.json",
462 %{user: user, for: other_user}
463 )[:pleroma][:unread_notifications_count] == nil
464
465 assert AccountView.render(
466 "show.json",
467 %{user: user, for: user}
468 )[:pleroma][:unread_notifications_count] == 7
469 end
470 end
471
472 describe "follow requests counter" do
473 test "shows zero when no follow requests are pending" do
474 user = insert(:user)
475
476 assert %{follow_requests_count: 0} =
477 AccountView.render("show.json", %{user: user, for: user})
478
479 other_user = insert(:user)
480 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
481
482 assert %{follow_requests_count: 0} =
483 AccountView.render("show.json", %{user: user, for: user})
484 end
485
486 test "shows non-zero when follow requests are pending" do
487 user = insert(:user, is_locked: true)
488
489 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
490
491 other_user = insert(:user)
492 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
493
494 assert %{locked: true, follow_requests_count: 1} =
495 AccountView.render("show.json", %{user: user, for: user})
496 end
497
498 test "decreases when accepting a follow request" do
499 user = insert(:user, is_locked: true)
500
501 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
502
503 other_user = insert(:user)
504 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
505
506 assert %{locked: true, follow_requests_count: 1} =
507 AccountView.render("show.json", %{user: user, for: user})
508
509 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
510
511 assert %{locked: true, follow_requests_count: 0} =
512 AccountView.render("show.json", %{user: user, for: user})
513 end
514
515 test "decreases when rejecting a follow request" do
516 user = insert(:user, is_locked: true)
517
518 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
519
520 other_user = insert(:user)
521 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
522
523 assert %{locked: true, follow_requests_count: 1} =
524 AccountView.render("show.json", %{user: user, for: user})
525
526 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
527
528 assert %{locked: true, follow_requests_count: 0} =
529 AccountView.render("show.json", %{user: user, for: user})
530 end
531
532 test "shows non-zero when historical unapproved requests are present" do
533 user = insert(:user, is_locked: true)
534
535 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
536
537 other_user = insert(:user)
538 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
539
540 {:ok, user} = User.update_and_set_cache(user, %{is_locked: false})
541
542 assert %{locked: false, follow_requests_count: 1} =
543 AccountView.render("show.json", %{user: user, for: user})
544 end
545 end
546
547 test "uses mediaproxy urls when it's enabled (regardless of media preview proxy state)" do
548 clear_config([:media_proxy, :enabled], true)
549 clear_config([:media_preview_proxy, :enabled])
550
551 user =
552 insert(:user,
553 avatar: %{"url" => [%{"href" => "https://evil.website/avatar.png"}]},
554 banner: %{"url" => [%{"href" => "https://evil.website/banner.png"}]},
555 emoji: %{"joker_smile" => "https://evil.website/society.png"}
556 )
557
558 with media_preview_enabled <- [false, true] do
559 Config.put([:media_preview_proxy, :enabled], media_preview_enabled)
560
561 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
562 |> Enum.all?(fn
563 {key, url} when key in [:avatar, :avatar_static, :header, :header_static] ->
564 String.starts_with?(url, Pleroma.Web.base_url())
565
566 {:emojis, emojis} ->
567 Enum.all?(emojis, fn %{url: url, static_url: static_url} ->
568 String.starts_with?(url, Pleroma.Web.base_url()) &&
569 String.starts_with?(static_url, Pleroma.Web.base_url())
570 end)
571
572 _ ->
573 true
574 end)
575 |> assert()
576 end
577 end
578 end