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