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