Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into chat-federation...
[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})
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})
112
113 Config.put([:instances_favicons, :enabled], false)
114
115 assert %{pleroma: %{favicon: nil}} = AccountView.render("show.json", %{user: user})
116 end
117
118 test "Represent the user account for the account owner" do
119 user = insert(:user)
120
121 notification_settings = %{
122 followers: true,
123 follows: true,
124 non_followers: true,
125 non_follows: true,
126 privacy_option: 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: false
172 },
173 fields: []
174 },
175 pleroma: %{
176 ap_id: user.ap_id,
177 background_image: nil,
178 favicon:
179 "https://shitposter.club/plugins/Qvitter/img/gnusocial-favicons/favicon-16x16.png",
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})
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 = AccountView.render("show.json", %{user: user})
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 describe "relationship" do
230 defp test_relationship_rendering(user, other_user, expected_result) do
231 opts = %{user: user, target: other_user, relationships: nil}
232 assert expected_result == AccountView.render("relationship.json", opts)
233
234 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
235 opts = Map.put(opts, :relationships, relationships_opt)
236 assert expected_result == AccountView.render("relationship.json", opts)
237
238 assert [expected_result] ==
239 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
240 end
241
242 @blank_response %{
243 following: false,
244 followed_by: false,
245 blocking: false,
246 blocked_by: false,
247 muting: false,
248 muting_notifications: false,
249 subscribing: false,
250 requested: false,
251 domain_blocking: false,
252 showing_reblogs: true,
253 endorsed: false
254 }
255
256 test "represent a relationship for the following and followed user" do
257 user = insert(:user)
258 other_user = insert(:user)
259
260 {:ok, user} = User.follow(user, other_user)
261 {:ok, other_user} = User.follow(other_user, user)
262 {:ok, _subscription} = User.subscribe(user, other_user)
263 {:ok, _user_relationships} = User.mute(user, other_user, true)
264 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
265
266 expected =
267 Map.merge(
268 @blank_response,
269 %{
270 following: true,
271 followed_by: true,
272 muting: true,
273 muting_notifications: true,
274 subscribing: true,
275 showing_reblogs: false,
276 id: to_string(other_user.id)
277 }
278 )
279
280 test_relationship_rendering(user, other_user, expected)
281 end
282
283 test "represent a relationship for the blocking and blocked user" do
284 user = insert(:user)
285 other_user = insert(:user)
286
287 {:ok, user} = User.follow(user, other_user)
288 {:ok, _subscription} = User.subscribe(user, other_user)
289 {:ok, _user_relationship} = User.block(user, other_user)
290 {:ok, _user_relationship} = User.block(other_user, user)
291
292 expected =
293 Map.merge(
294 @blank_response,
295 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
296 )
297
298 test_relationship_rendering(user, other_user, expected)
299 end
300
301 test "represent a relationship for the user blocking a domain" do
302 user = insert(:user)
303 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
304
305 {:ok, user} = User.block_domain(user, "bad.site")
306
307 expected =
308 Map.merge(
309 @blank_response,
310 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
311 )
312
313 test_relationship_rendering(user, other_user, expected)
314 end
315
316 test "represent a relationship for the user with a pending follow request" do
317 user = insert(:user)
318 other_user = insert(:user, locked: true)
319
320 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
321 user = User.get_cached_by_id(user.id)
322 other_user = User.get_cached_by_id(other_user.id)
323
324 expected =
325 Map.merge(
326 @blank_response,
327 %{requested: true, following: false, id: to_string(other_user.id)}
328 )
329
330 test_relationship_rendering(user, other_user, expected)
331 end
332 end
333
334 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
335 user = insert(:user, pleroma_settings_store: %{fe: "test"})
336
337 result =
338 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
339
340 assert result.pleroma.settings_store == %{:fe => "test"}
341
342 result = AccountView.render("show.json", %{user: user, with_pleroma_settings: true})
343 assert result.pleroma[:settings_store] == nil
344
345 result = AccountView.render("show.json", %{user: user, for: user})
346 assert result.pleroma[:settings_store] == nil
347 end
348
349 test "doesn't sanitize display names" do
350 user = insert(:user, name: "<marquee> username </marquee>")
351 result = AccountView.render("show.json", %{user: user})
352 assert result.display_name == "<marquee> username </marquee>"
353 end
354
355 test "never display nil user follow counts" do
356 user = insert(:user, following_count: 0, follower_count: 0)
357 result = AccountView.render("show.json", %{user: user})
358
359 assert result.following_count == 0
360 assert result.followers_count == 0
361 end
362
363 describe "hiding follows/following" do
364 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
365 user =
366 insert(:user, %{
367 hide_followers: true,
368 hide_followers_count: true,
369 hide_follows: true,
370 hide_follows_count: true
371 })
372
373 other_user = insert(:user)
374 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
375 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
376
377 assert %{
378 followers_count: 0,
379 following_count: 0,
380 pleroma: %{hide_follows_count: true, hide_followers_count: true}
381 } = AccountView.render("show.json", %{user: user})
382 end
383
384 test "shows when follows/followers are hidden" do
385 user = insert(:user, hide_followers: true, hide_follows: true)
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: 1,
392 following_count: 1,
393 pleroma: %{hide_follows: true, hide_followers: true}
394 } = AccountView.render("show.json", %{user: user})
395 end
396
397 test "shows actual follower/following count to the account owner" 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
402 assert User.following?(user, other_user)
403 assert Pleroma.FollowingRelationship.follower_count(other_user) == 1
404 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
405
406 assert %{
407 followers_count: 1,
408 following_count: 1
409 } = AccountView.render("show.json", %{user: user, for: user})
410 end
411
412 test "shows unread_conversation_count only to the account owner" do
413 user = insert(:user)
414 other_user = insert(:user)
415
416 {:ok, _activity} =
417 CommonAPI.post(other_user, %{
418 status: "Hey @#{user.nickname}.",
419 visibility: "direct"
420 })
421
422 user = User.get_cached_by_ap_id(user.ap_id)
423
424 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
425 :unread_conversation_count
426 ] == nil
427
428 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
429 :unread_conversation_count
430 ] == 1
431 end
432
433 test "shows unread_count only to the account owner" do
434 user = insert(:user)
435 insert_list(7, :notification, user: user)
436 other_user = insert(:user)
437
438 user = User.get_cached_by_ap_id(user.ap_id)
439
440 assert AccountView.render(
441 "show.json",
442 %{user: user, for: other_user}
443 )[:pleroma][:unread_notifications_count] == nil
444
445 assert AccountView.render(
446 "show.json",
447 %{user: user, for: user}
448 )[:pleroma][:unread_notifications_count] == 7
449 end
450 end
451
452 describe "follow requests counter" do
453 test "shows zero when no follow requests are pending" do
454 user = insert(:user)
455
456 assert %{follow_requests_count: 0} =
457 AccountView.render("show.json", %{user: user, for: user})
458
459 other_user = insert(:user)
460 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
461
462 assert %{follow_requests_count: 0} =
463 AccountView.render("show.json", %{user: user, for: user})
464 end
465
466 test "shows non-zero when follow requests are pending" do
467 user = insert(:user, locked: true)
468
469 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
470
471 other_user = insert(:user)
472 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
473
474 assert %{locked: true, follow_requests_count: 1} =
475 AccountView.render("show.json", %{user: user, for: user})
476 end
477
478 test "decreases when accepting a follow request" do
479 user = insert(:user, locked: true)
480
481 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
482
483 other_user = insert(:user)
484 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
485
486 assert %{locked: true, follow_requests_count: 1} =
487 AccountView.render("show.json", %{user: user, for: user})
488
489 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
490
491 assert %{locked: true, follow_requests_count: 0} =
492 AccountView.render("show.json", %{user: user, for: user})
493 end
494
495 test "decreases when rejecting 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.reject_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 "shows non-zero when historical unapproved requests are present" 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 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
521
522 assert %{locked: false, follow_requests_count: 1} =
523 AccountView.render("show.json", %{user: user, for: user})
524 end
525 end
526
527 test "uses mediaproxy urls when it's enabled" do
528 clear_config([:media_proxy, :enabled], true)
529
530 user =
531 insert(:user,
532 avatar: %{"url" => [%{"href" => "https://evil.website/avatar.png"}]},
533 banner: %{"url" => [%{"href" => "https://evil.website/banner.png"}]},
534 emoji: %{"joker_smile" => "https://evil.website/society.png"}
535 )
536
537 AccountView.render("show.json", %{user: user})
538 |> Enum.all?(fn
539 {key, url} when key in [:avatar, :avatar_static, :header, :header_static] ->
540 String.starts_with?(url, Pleroma.Web.base_url())
541
542 {:emojis, emojis} ->
543 Enum.all?(emojis, fn %{url: url, static_url: static_url} ->
544 String.starts_with?(url, Pleroma.Web.base_url()) &&
545 String.starts_with?(static_url, Pleroma.Web.base_url())
546 end)
547
548 _ ->
549 true
550 end)
551 |> assert()
552 end
553 end