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