Merge branch 'fix/upload-filter-exiftool-webp' 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: false
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 "Favicon when :instance_favicons is enabled" do
120 end
121
122 test "Represent the user account for the account owner" do
123 user = insert(:user)
124
125 notification_settings = %{
126 block_from_strangers: false,
127 hide_notification_contents: false
128 }
129
130 privacy = user.default_scope
131
132 assert %{
133 pleroma: %{notification_settings: ^notification_settings, allow_following_move: true},
134 source: %{privacy: ^privacy}
135 } = AccountView.render("show.json", %{user: user, for: user})
136 end
137
138 test "Represent a Service(bot) account" do
139 user =
140 insert(:user, %{
141 follower_count: 3,
142 note_count: 5,
143 actor_type: "Service",
144 nickname: "shp@shitposter.club",
145 inserted_at: ~N[2017-08-15 15:47:06.597036]
146 })
147
148 expected = %{
149 id: to_string(user.id),
150 username: "shp",
151 acct: user.nickname,
152 display_name: user.name,
153 locked: false,
154 created_at: "2017-08-15T15:47:06.000Z",
155 followers_count: 3,
156 following_count: 0,
157 statuses_count: 5,
158 note: user.bio,
159 url: user.ap_id,
160 avatar: "http://localhost:4001/images/avi.png",
161 avatar_static: "http://localhost:4001/images/avi.png",
162 header: "http://localhost:4001/images/banner.png",
163 header_static: "http://localhost:4001/images/banner.png",
164 emojis: [],
165 fields: [],
166 bot: true,
167 source: %{
168 note: user.bio,
169 sensitive: false,
170 pleroma: %{
171 actor_type: "Service",
172 discoverable: false
173 },
174 fields: []
175 },
176 pleroma: %{
177 ap_id: user.ap_id,
178 background_image: nil,
179 favicon: nil,
180 confirmation_pending: false,
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, deactivated: true)
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} = User.follow(user, other_user)
280 {:ok, other_user} = User.follow(other_user, user)
281 {:ok, _subscription} = User.subscribe(user, other_user)
282 {:ok, _user_relationships} = User.mute(user, other_user, 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} = 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, 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)
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, 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, 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, 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, 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, %{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" do
547 clear_config([:media_proxy, :enabled], true)
548
549 user =
550 insert(:user,
551 avatar: %{"url" => [%{"href" => "https://evil.website/avatar.png"}]},
552 banner: %{"url" => [%{"href" => "https://evil.website/banner.png"}]},
553 emoji: %{"joker_smile" => "https://evil.website/society.png"}
554 )
555
556 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
557 |> Enum.all?(fn
558 {key, url} when key in [:avatar, :avatar_static, :header, :header_static] ->
559 String.starts_with?(url, Pleroma.Web.base_url())
560
561 {:emojis, emojis} ->
562 Enum.all?(emojis, fn %{url: url, static_url: static_url} ->
563 String.starts_with?(url, Pleroma.Web.base_url()) &&
564 String.starts_with?(static_url, Pleroma.Web.base_url())
565 end)
566
567 _ ->
568 true
569 end)
570 |> assert()
571 end
572 end