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