MastoAPI: Add user notes on accounts
[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 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 fqn: "shp@shitposter.club",
177 pleroma: %{
178 ap_id: user.ap_id,
179 also_known_as: [],
180 background_image: nil,
181 favicon: nil,
182 is_confirmed: true,
183 tags: [],
184 is_admin: false,
185 is_moderator: false,
186 hide_favorites: true,
187 hide_followers: false,
188 hide_follows: false,
189 hide_followers_count: false,
190 hide_follows_count: false,
191 relationship: %{},
192 skip_thread_containment: false,
193 accepts_chat_messages: nil
194 }
195 }
196
197 assert expected == AccountView.render("show.json", %{user: user, skip_visibility_check: true})
198 end
199
200 test "Represent a Funkwhale channel" do
201 {:ok, user} =
202 User.get_or_fetch_by_ap_id(
203 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
204 )
205
206 assert represented =
207 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
208
209 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
210 assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
211 end
212
213 test "Represent a deactivated user for an admin" do
214 admin = insert(:user, is_admin: true)
215 deactivated_user = insert(:user, is_active: false)
216 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
217 assert represented[:pleroma][:deactivated] == true
218 end
219
220 test "Represent a smaller mention" do
221 user = insert(:user)
222
223 expected = %{
224 id: to_string(user.id),
225 acct: user.nickname,
226 username: user.nickname,
227 url: user.ap_id
228 }
229
230 assert expected == AccountView.render("mention.json", %{user: user})
231 end
232
233 test "demands :for or :skip_visibility_check option for account rendering" do
234 clear_config([:restrict_unauthenticated, :profiles, :local], false)
235
236 user = insert(:user)
237 user_id = user.id
238
239 assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: nil})
240 assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: user})
241
242 assert %{id: ^user_id} =
243 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
244
245 assert_raise RuntimeError, ~r/:skip_visibility_check or :for option is required/, fn ->
246 AccountView.render("show.json", %{user: user})
247 end
248 end
249
250 describe "relationship" do
251 defp test_relationship_rendering(user, other_user, expected_result) do
252 opts = %{user: user, target: other_user, relationships: nil}
253 assert expected_result == AccountView.render("relationship.json", opts)
254
255 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
256 opts = Map.put(opts, :relationships, relationships_opt)
257 assert expected_result == AccountView.render("relationship.json", opts)
258
259 assert [expected_result] ==
260 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
261 end
262
263 @blank_response %{
264 following: false,
265 followed_by: false,
266 blocking: false,
267 blocked_by: false,
268 muting: false,
269 muting_notifications: false,
270 subscribing: false,
271 requested: false,
272 domain_blocking: false,
273 showing_reblogs: true,
274 endorsed: false,
275 note: ""
276 }
277
278 test "represent a relationship for the following and followed user" do
279 user = insert(:user)
280 other_user = insert(:user)
281
282 {:ok, user, other_user} = User.follow(user, other_user)
283 {:ok, other_user, user} = User.follow(other_user, user)
284 {:ok, _subscription} = User.subscribe(user, other_user)
285 {:ok, _user_relationships} = User.mute(user, other_user, %{notifications: true})
286 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
287
288 expected =
289 Map.merge(
290 @blank_response,
291 %{
292 following: true,
293 followed_by: true,
294 muting: true,
295 muting_notifications: true,
296 subscribing: true,
297 showing_reblogs: false,
298 id: to_string(other_user.id)
299 }
300 )
301
302 test_relationship_rendering(user, other_user, expected)
303 end
304
305 test "represent a relationship for the blocking and blocked user" do
306 user = insert(:user)
307 other_user = insert(:user)
308
309 {:ok, user, other_user} = User.follow(user, other_user)
310 {:ok, _subscription} = User.subscribe(user, other_user)
311 {:ok, _user_relationship} = User.block(user, other_user)
312 {:ok, _user_relationship} = User.block(other_user, user)
313
314 expected =
315 Map.merge(
316 @blank_response,
317 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
318 )
319
320 test_relationship_rendering(user, other_user, expected)
321 end
322
323 test "represent a relationship for the user blocking a domain" do
324 user = insert(:user)
325 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
326
327 {:ok, user} = User.block_domain(user, "bad.site")
328
329 expected =
330 Map.merge(
331 @blank_response,
332 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
333 )
334
335 test_relationship_rendering(user, other_user, expected)
336 end
337
338 test "represent a relationship for the user with a pending follow request" do
339 user = insert(:user)
340 other_user = insert(:user, is_locked: true)
341
342 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
343 user = User.get_cached_by_id(user.id)
344 other_user = User.get_cached_by_id(other_user.id)
345
346 expected =
347 Map.merge(
348 @blank_response,
349 %{requested: true, following: false, id: to_string(other_user.id)}
350 )
351
352 test_relationship_rendering(user, other_user, expected)
353 end
354 end
355
356 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
357 user = insert(:user, pleroma_settings_store: %{fe: "test"})
358
359 result =
360 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
361
362 assert result.pleroma.settings_store == %{:fe => "test"}
363
364 result = AccountView.render("show.json", %{user: user, for: nil, with_pleroma_settings: true})
365 assert result.pleroma[:settings_store] == nil
366
367 result = AccountView.render("show.json", %{user: user, for: user})
368 assert result.pleroma[:settings_store] == nil
369 end
370
371 test "doesn't sanitize display names" do
372 user = insert(:user, name: "<marquee> username </marquee>")
373 result = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
374 assert result.display_name == "<marquee> username </marquee>"
375 end
376
377 test "never display nil user follow counts" do
378 user = insert(:user, following_count: 0, follower_count: 0)
379 result = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
380
381 assert result.following_count == 0
382 assert result.followers_count == 0
383 end
384
385 describe "hiding follows/following" do
386 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
387 user =
388 insert(:user, %{
389 hide_followers: true,
390 hide_followers_count: true,
391 hide_follows: true,
392 hide_follows_count: true
393 })
394
395 other_user = insert(:user)
396 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
397 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
398
399 assert %{
400 followers_count: 0,
401 following_count: 0,
402 pleroma: %{hide_follows_count: true, hide_followers_count: true}
403 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
404 end
405
406 test "shows when follows/followers are hidden" do
407 user = insert(:user, hide_followers: true, hide_follows: true)
408 other_user = insert(:user)
409 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
410 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
411
412 assert %{
413 followers_count: 1,
414 following_count: 1,
415 pleroma: %{hide_follows: true, hide_followers: true}
416 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
417 end
418
419 test "shows actual follower/following count to the account owner" do
420 user = insert(:user, hide_followers: true, hide_follows: true)
421 other_user = insert(:user)
422 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
423
424 assert User.following?(user, other_user)
425 assert Pleroma.FollowingRelationship.follower_count(other_user) == 1
426 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
427
428 assert %{
429 followers_count: 1,
430 following_count: 1
431 } = AccountView.render("show.json", %{user: user, for: user})
432 end
433
434 test "shows unread_conversation_count only to the account owner" do
435 user = insert(:user)
436 other_user = insert(:user)
437
438 {:ok, _activity} =
439 CommonAPI.post(other_user, %{
440 status: "Hey @#{user.nickname}.",
441 visibility: "direct"
442 })
443
444 user = User.get_cached_by_ap_id(user.ap_id)
445
446 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
447 :unread_conversation_count
448 ] == nil
449
450 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
451 :unread_conversation_count
452 ] == 1
453 end
454
455 test "shows unread_count only to the account owner" do
456 user = insert(:user)
457 insert_list(7, :notification, user: user, activity: insert(:note_activity))
458 other_user = insert(:user)
459
460 user = User.get_cached_by_ap_id(user.ap_id)
461
462 assert AccountView.render(
463 "show.json",
464 %{user: user, for: other_user}
465 )[:pleroma][:unread_notifications_count] == nil
466
467 assert AccountView.render(
468 "show.json",
469 %{user: user, for: user}
470 )[:pleroma][:unread_notifications_count] == 7
471 end
472
473 test "shows email only to the account owner" do
474 user = insert(:user)
475 other_user = insert(:user)
476
477 user = User.get_cached_by_ap_id(user.ap_id)
478
479 assert AccountView.render(
480 "show.json",
481 %{user: user, for: other_user}
482 )[:pleroma][:email] == nil
483
484 assert AccountView.render(
485 "show.json",
486 %{user: user, for: user}
487 )[:pleroma][:email] == user.email
488 end
489 end
490
491 describe "follow requests counter" do
492 test "shows zero when no follow requests are pending" do
493 user = insert(:user)
494
495 assert %{follow_requests_count: 0} =
496 AccountView.render("show.json", %{user: user, for: user})
497
498 other_user = insert(:user)
499 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
500
501 assert %{follow_requests_count: 0} =
502 AccountView.render("show.json", %{user: user, for: user})
503 end
504
505 test "shows non-zero when follow requests are pending" do
506 user = insert(:user, is_locked: true)
507
508 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
509
510 other_user = insert(:user)
511 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
512
513 assert %{locked: true, follow_requests_count: 1} =
514 AccountView.render("show.json", %{user: user, for: user})
515 end
516
517 test "decreases when accepting a follow request" do
518 user = insert(:user, is_locked: true)
519
520 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
521
522 other_user = insert(:user)
523 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
524
525 assert %{locked: true, follow_requests_count: 1} =
526 AccountView.render("show.json", %{user: user, for: user})
527
528 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
529
530 assert %{locked: true, follow_requests_count: 0} =
531 AccountView.render("show.json", %{user: user, for: user})
532 end
533
534 test "decreases when rejecting a follow request" do
535 user = insert(:user, is_locked: true)
536
537 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
538
539 other_user = insert(:user)
540 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
541
542 assert %{locked: true, follow_requests_count: 1} =
543 AccountView.render("show.json", %{user: user, for: user})
544
545 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
546
547 assert %{locked: true, follow_requests_count: 0} =
548 AccountView.render("show.json", %{user: user, for: user})
549 end
550
551 test "shows non-zero when historical unapproved requests are present" do
552 user = insert(:user, is_locked: true)
553
554 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
555
556 other_user = insert(:user)
557 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
558
559 {:ok, user} = User.update_and_set_cache(user, %{is_locked: false})
560
561 assert %{locked: false, follow_requests_count: 1} =
562 AccountView.render("show.json", %{user: user, for: user})
563 end
564 end
565
566 test "uses mediaproxy urls when it's enabled (regardless of media preview proxy state)" do
567 clear_config([:media_proxy, :enabled], true)
568 clear_config([:media_preview_proxy, :enabled])
569
570 user =
571 insert(:user,
572 avatar: %{"url" => [%{"href" => "https://evil.website/avatar.png"}]},
573 banner: %{"url" => [%{"href" => "https://evil.website/banner.png"}]},
574 emoji: %{"joker_smile" => "https://evil.website/society.png"}
575 )
576
577 with media_preview_enabled <- [false, true] do
578 clear_config([:media_preview_proxy, :enabled], media_preview_enabled)
579
580 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
581 |> Enum.all?(fn
582 {key, url} when key in [:avatar, :avatar_static, :header, :header_static] ->
583 String.starts_with?(url, Pleroma.Web.Endpoint.url())
584
585 {:emojis, emojis} ->
586 Enum.all?(emojis, fn %{url: url, static_url: static_url} ->
587 String.starts_with?(url, Pleroma.Web.Endpoint.url()) &&
588 String.starts_with?(static_url, Pleroma.Web.Endpoint.url())
589 end)
590
591 _ ->
592 true
593 end)
594 |> assert()
595 end
596 end
597 end