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