Merge branch 'mix-tasks-improvement' 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.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 }
95 }
96
97 assert expected == AccountView.render("show.json", %{user: user})
98 end
99
100 test "Favicon is nil when :instances_favicons is disabled" do
101 user = insert(:user)
102
103 Config.put([: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})
111
112 Config.put([:instances_favicons, :enabled], false)
113
114 assert %{pleroma: %{favicon: nil}} = AccountView.render("show.json", %{user: user})
115 end
116
117 test "Represent the user account for the account owner" do
118 user = insert(:user)
119
120 notification_settings = %{
121 followers: true,
122 follows: true,
123 non_followers: true,
124 non_follows: true,
125 privacy_option: false
126 }
127
128 privacy = user.default_scope
129
130 assert %{
131 pleroma: %{notification_settings: ^notification_settings, allow_following_move: true},
132 source: %{privacy: ^privacy}
133 } = AccountView.render("show.json", %{user: user, for: user})
134 end
135
136 test "Represent a Service(bot) account" do
137 user =
138 insert(:user, %{
139 follower_count: 3,
140 note_count: 5,
141 actor_type: "Service",
142 nickname: "shp@shitposter.club",
143 inserted_at: ~N[2017-08-15 15:47:06.597036]
144 })
145
146 expected = %{
147 id: to_string(user.id),
148 username: "shp",
149 acct: user.nickname,
150 display_name: user.name,
151 locked: false,
152 created_at: "2017-08-15T15:47:06.000Z",
153 followers_count: 3,
154 following_count: 0,
155 statuses_count: 5,
156 note: user.bio,
157 url: user.ap_id,
158 avatar: "http://localhost:4001/images/avi.png",
159 avatar_static: "http://localhost:4001/images/avi.png",
160 header: "http://localhost:4001/images/banner.png",
161 header_static: "http://localhost:4001/images/banner.png",
162 emojis: [],
163 fields: [],
164 bot: true,
165 source: %{
166 note: user.bio,
167 sensitive: false,
168 pleroma: %{
169 actor_type: "Service",
170 discoverable: false
171 },
172 fields: []
173 },
174 pleroma: %{
175 ap_id: user.ap_id,
176 background_image: nil,
177 favicon:
178 "https://shitposter.club/plugins/Qvitter/img/gnusocial-favicons/favicon-16x16.png",
179 confirmation_pending: false,
180 tags: [],
181 is_admin: false,
182 is_moderator: false,
183 hide_favorites: true,
184 hide_followers: false,
185 hide_follows: false,
186 hide_followers_count: false,
187 hide_follows_count: false,
188 relationship: %{},
189 skip_thread_containment: false
190 }
191 }
192
193 assert expected == AccountView.render("show.json", %{user: user})
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})
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 describe "relationship" do
228 defp test_relationship_rendering(user, other_user, expected_result) do
229 opts = %{user: user, target: other_user, relationships: nil}
230 assert expected_result == AccountView.render("relationship.json", opts)
231
232 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
233 opts = Map.put(opts, :relationships, relationships_opt)
234 assert expected_result == AccountView.render("relationship.json", opts)
235
236 assert [expected_result] ==
237 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
238 end
239
240 @blank_response %{
241 following: false,
242 followed_by: false,
243 blocking: false,
244 blocked_by: false,
245 muting: false,
246 muting_notifications: false,
247 subscribing: false,
248 requested: false,
249 domain_blocking: false,
250 showing_reblogs: true,
251 endorsed: false
252 }
253
254 test "represent a relationship for the following and followed user" do
255 user = insert(:user)
256 other_user = insert(:user)
257
258 {:ok, user} = User.follow(user, other_user)
259 {:ok, other_user} = User.follow(other_user, user)
260 {:ok, _subscription} = User.subscribe(user, other_user)
261 {:ok, _user_relationships} = User.mute(user, other_user, true)
262 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
263
264 expected =
265 Map.merge(
266 @blank_response,
267 %{
268 following: true,
269 followed_by: true,
270 muting: true,
271 muting_notifications: true,
272 subscribing: true,
273 showing_reblogs: false,
274 id: to_string(other_user.id)
275 }
276 )
277
278 test_relationship_rendering(user, other_user, expected)
279 end
280
281 test "represent a relationship for the blocking and blocked user" do
282 user = insert(:user)
283 other_user = insert(:user)
284
285 {:ok, user} = User.follow(user, other_user)
286 {:ok, _subscription} = User.subscribe(user, other_user)
287 {:ok, _user_relationship} = User.block(user, other_user)
288 {:ok, _user_relationship} = User.block(other_user, user)
289
290 expected =
291 Map.merge(
292 @blank_response,
293 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
294 )
295
296 test_relationship_rendering(user, other_user, expected)
297 end
298
299 test "represent a relationship for the user blocking a domain" do
300 user = insert(:user)
301 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
302
303 {:ok, user} = User.block_domain(user, "bad.site")
304
305 expected =
306 Map.merge(
307 @blank_response,
308 %{domain_blocking: true, blocking: false, 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 with a pending follow request" do
315 user = insert(:user)
316 other_user = insert(:user, locked: true)
317
318 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
319 user = User.get_cached_by_id(user.id)
320 other_user = User.get_cached_by_id(other_user.id)
321
322 expected =
323 Map.merge(
324 @blank_response,
325 %{requested: true, following: false, id: to_string(other_user.id)}
326 )
327
328 test_relationship_rendering(user, other_user, expected)
329 end
330 end
331
332 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
333 user = insert(:user, pleroma_settings_store: %{fe: "test"})
334
335 result =
336 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
337
338 assert result.pleroma.settings_store == %{:fe => "test"}
339
340 result = AccountView.render("show.json", %{user: user, with_pleroma_settings: true})
341 assert result.pleroma[:settings_store] == nil
342
343 result = AccountView.render("show.json", %{user: user, for: user})
344 assert result.pleroma[:settings_store] == nil
345 end
346
347 test "doesn't sanitize display names" do
348 user = insert(:user, name: "<marquee> username </marquee>")
349 result = AccountView.render("show.json", %{user: user})
350 assert result.display_name == "<marquee> username </marquee>"
351 end
352
353 test "never display nil user follow counts" do
354 user = insert(:user, following_count: 0, follower_count: 0)
355 result = AccountView.render("show.json", %{user: user})
356
357 assert result.following_count == 0
358 assert result.followers_count == 0
359 end
360
361 describe "hiding follows/following" do
362 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
363 user =
364 insert(:user, %{
365 hide_followers: true,
366 hide_followers_count: true,
367 hide_follows: true,
368 hide_follows_count: true
369 })
370
371 other_user = insert(:user)
372 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
373 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
374
375 assert %{
376 followers_count: 0,
377 following_count: 0,
378 pleroma: %{hide_follows_count: true, hide_followers_count: true}
379 } = AccountView.render("show.json", %{user: user})
380 end
381
382 test "shows when follows/followers are hidden" do
383 user = insert(:user, hide_followers: true, hide_follows: true)
384 other_user = insert(:user)
385 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
386 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
387
388 assert %{
389 followers_count: 1,
390 following_count: 1,
391 pleroma: %{hide_follows: true, hide_followers: true}
392 } = AccountView.render("show.json", %{user: user})
393 end
394
395 test "shows actual follower/following count to the account owner" do
396 user = insert(:user, hide_followers: true, hide_follows: true)
397 other_user = insert(:user)
398 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
399
400 assert User.following?(user, other_user)
401 assert Pleroma.FollowingRelationship.follower_count(other_user) == 1
402 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
403
404 assert %{
405 followers_count: 1,
406 following_count: 1
407 } = AccountView.render("show.json", %{user: user, for: user})
408 end
409
410 test "shows unread_conversation_count only to the account owner" do
411 user = insert(:user)
412 other_user = insert(:user)
413
414 {:ok, _activity} =
415 CommonAPI.post(other_user, %{
416 status: "Hey @#{user.nickname}.",
417 visibility: "direct"
418 })
419
420 user = User.get_cached_by_ap_id(user.ap_id)
421
422 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
423 :unread_conversation_count
424 ] == nil
425
426 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
427 :unread_conversation_count
428 ] == 1
429 end
430
431 test "shows unread_count only to the account owner" do
432 user = insert(:user)
433 insert_list(7, :notification, user: user)
434 other_user = insert(:user)
435
436 user = User.get_cached_by_ap_id(user.ap_id)
437
438 assert AccountView.render(
439 "show.json",
440 %{user: user, for: other_user}
441 )[:pleroma][:unread_notifications_count] == nil
442
443 assert AccountView.render(
444 "show.json",
445 %{user: user, for: user}
446 )[:pleroma][:unread_notifications_count] == 7
447 end
448 end
449
450 describe "follow requests counter" do
451 test "shows zero when no follow requests are pending" do
452 user = insert(:user)
453
454 assert %{follow_requests_count: 0} =
455 AccountView.render("show.json", %{user: user, for: user})
456
457 other_user = insert(:user)
458 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
459
460 assert %{follow_requests_count: 0} =
461 AccountView.render("show.json", %{user: user, for: user})
462 end
463
464 test "shows non-zero when follow requests are pending" do
465 user = insert(:user, locked: true)
466
467 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
468
469 other_user = insert(:user)
470 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
471
472 assert %{locked: true, follow_requests_count: 1} =
473 AccountView.render("show.json", %{user: user, for: user})
474 end
475
476 test "decreases when accepting a follow request" do
477 user = insert(:user, locked: true)
478
479 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
480
481 other_user = insert(:user)
482 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
483
484 assert %{locked: true, follow_requests_count: 1} =
485 AccountView.render("show.json", %{user: user, for: user})
486
487 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
488
489 assert %{locked: true, follow_requests_count: 0} =
490 AccountView.render("show.json", %{user: user, for: user})
491 end
492
493 test "decreases when rejecting a follow request" do
494 user = insert(:user, locked: true)
495
496 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
497
498 other_user = insert(:user)
499 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
500
501 assert %{locked: true, follow_requests_count: 1} =
502 AccountView.render("show.json", %{user: user, for: user})
503
504 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
505
506 assert %{locked: true, follow_requests_count: 0} =
507 AccountView.render("show.json", %{user: user, for: user})
508 end
509
510 test "shows non-zero when historical unapproved requests are present" do
511 user = insert(:user, locked: true)
512
513 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
514
515 other_user = insert(:user)
516 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
517
518 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
519
520 assert %{locked: false, follow_requests_count: 1} =
521 AccountView.render("show.json", %{user: user, for: user})
522 end
523 end
524
525 test "uses mediaproxy urls when it's enabled" do
526 clear_config([:media_proxy, :enabled], true)
527
528 user =
529 insert(:user,
530 avatar: %{"url" => [%{"href" => "https://evil.website/avatar.png"}]},
531 banner: %{"url" => [%{"href" => "https://evil.website/banner.png"}]},
532 emoji: %{"joker_smile" => "https://evil.website/society.png"}
533 )
534
535 AccountView.render("show.json", %{user: user})
536 |> Enum.all?(fn
537 {key, url} when key in [:avatar, :avatar_static, :header, :header_static] ->
538 String.starts_with?(url, Pleroma.Web.base_url())
539
540 {:emojis, emojis} ->
541 Enum.all?(emojis, fn %{url: url, static_url: static_url} ->
542 String.starts_with?(url, Pleroma.Web.base_url()) &&
543 String.starts_with?(static_url, Pleroma.Web.base_url())
544 end)
545
546 _ ->
547 true
548 end)
549 |> assert()
550 end
551 end