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