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