Add 'notifying' to relationship for compatibility with Mastodon
[akkoma] / test / pleroma / web / mastodon_api / views / account_view_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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 also_known_as: ["https://shitposter.zone/users/shp"]
39 })
40
41 expected = %{
42 id: to_string(user.id),
43 username: "shp",
44 acct: user.nickname,
45 display_name: user.name,
46 locked: false,
47 created_at: "2017-08-15T15:47:06.000Z",
48 followers_count: 3,
49 following_count: 0,
50 statuses_count: 5,
51 note: "<span>valid html</span>. a<br/>b<br/>c<br/>d<br/>f &#39;&amp;&lt;&gt;&quot;",
52 url: user.ap_id,
53 avatar: "http://localhost:4001/images/avi.png",
54 avatar_static: "http://localhost:4001/images/avi.png",
55 header: "http://localhost:4001/images/banner.png",
56 header_static: "http://localhost:4001/images/banner.png",
57 emojis: [
58 %{
59 static_url: "/file.png",
60 url: "/file.png",
61 shortcode: "karjalanpiirakka",
62 visible_in_picker: false
63 }
64 ],
65 fields: [],
66 bot: false,
67 source: %{
68 note: "valid html. a\nb\nc\nd\nf '&<>\"",
69 sensitive: false,
70 pleroma: %{
71 actor_type: "Person",
72 discoverable: true
73 },
74 fields: []
75 },
76 fqn: "shp@shitposter.club",
77 pleroma: %{
78 ap_id: user.ap_id,
79 also_known_as: ["https://shitposter.zone/users/shp"],
80 background_image: "https://example.com/images/asuka_hospital.png",
81 favicon: nil,
82 is_confirmed: true,
83 tags: [],
84 is_admin: false,
85 is_moderator: false,
86 hide_favorites: true,
87 hide_followers: false,
88 hide_follows: false,
89 hide_followers_count: false,
90 hide_follows_count: false,
91 relationship: %{},
92 skip_thread_containment: false,
93 accepts_chat_messages: nil
94 }
95 }
96
97 assert expected == AccountView.render("show.json", %{user: user, skip_visibility_check: true})
98 end
99
100 describe "favicon" do
101 setup do
102 [user: insert(:user)]
103 end
104
105 test "is parsed when :instance_favicons is enabled", %{user: user} do
106 clear_config([:instances_favicons, :enabled], true)
107
108 assert %{
109 pleroma: %{
110 favicon:
111 "https://shitposter.club/plugins/Qvitter/img/gnusocial-favicons/favicon-16x16.png"
112 }
113 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
114 end
115
116 test "is nil when :instances_favicons is disabled", %{user: user} do
117 assert %{pleroma: %{favicon: nil}} =
118 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
119 end
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: true
173 },
174 fields: []
175 },
176 fqn: "shp@shitposter.club",
177 pleroma: %{
178 ap_id: user.ap_id,
179 also_known_as: [],
180 background_image: nil,
181 favicon: nil,
182 is_confirmed: true,
183 tags: [],
184 is_admin: false,
185 is_moderator: false,
186 hide_favorites: true,
187 hide_followers: false,
188 hide_follows: false,
189 hide_followers_count: false,
190 hide_follows_count: false,
191 relationship: %{},
192 skip_thread_containment: false,
193 accepts_chat_messages: nil
194 }
195 }
196
197 assert expected == AccountView.render("show.json", %{user: user, skip_visibility_check: true})
198 end
199
200 test "Represent a Funkwhale channel" do
201 {:ok, user} =
202 User.get_or_fetch_by_ap_id(
203 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
204 )
205
206 assert represented =
207 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
208
209 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
210 assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
211 end
212
213 test "Represent a deactivated user for an admin" do
214 admin = insert(:user, is_admin: true)
215 deactivated_user = insert(:user, is_active: false)
216 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
217 assert represented[:pleroma][:deactivated] == true
218 end
219
220 test "Represent a smaller mention" do
221 user = insert(:user)
222
223 expected = %{
224 id: to_string(user.id),
225 acct: user.nickname,
226 username: user.nickname,
227 url: user.ap_id
228 }
229
230 assert expected == AccountView.render("mention.json", %{user: user})
231 end
232
233 test "demands :for or :skip_visibility_check option for account rendering" do
234 clear_config([:restrict_unauthenticated, :profiles, :local], false)
235
236 user = insert(:user)
237 user_id = user.id
238
239 assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: nil})
240 assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: user})
241
242 assert %{id: ^user_id} =
243 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
244
245 assert_raise RuntimeError, ~r/:skip_visibility_check or :for option is required/, fn ->
246 AccountView.render("show.json", %{user: user})
247 end
248 end
249
250 describe "relationship" do
251 defp test_relationship_rendering(user, other_user, expected_result) do
252 opts = %{user: user, target: other_user, relationships: nil}
253 assert expected_result == AccountView.render("relationship.json", opts)
254
255 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
256 opts = Map.put(opts, :relationships, relationships_opt)
257 assert expected_result == AccountView.render("relationship.json", opts)
258
259 assert [expected_result] ==
260 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
261 end
262
263 @blank_response %{
264 following: false,
265 followed_by: false,
266 blocking: false,
267 blocked_by: false,
268 muting: false,
269 muting_notifications: false,
270 subscribing: false,
271 notifying: false,
272 requested: false,
273 domain_blocking: false,
274 showing_reblogs: true,
275 endorsed: false
276 }
277
278 test "represent a relationship for the following and followed user" do
279 user = insert(:user)
280 other_user = insert(:user)
281
282 {:ok, user, other_user} = User.follow(user, other_user)
283 {:ok, other_user, user} = User.follow(other_user, user)
284 {:ok, _subscription} = User.subscribe(user, other_user)
285 {:ok, _user_relationships} = User.mute(user, other_user, %{notifications: true})
286 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
287
288 expected =
289 Map.merge(
290 @blank_response,
291 %{
292 following: true,
293 followed_by: true,
294 muting: true,
295 muting_notifications: true,
296 subscribing: true,
297 notifying: true,
298 showing_reblogs: false,
299 id: to_string(other_user.id)
300 }
301 )
302
303 test_relationship_rendering(user, other_user, expected)
304 end
305
306 test "represent a relationship for the blocking and blocked user" do
307 user = insert(:user)
308 other_user = insert(:user)
309
310 {:ok, user, other_user} = User.follow(user, other_user)
311 {:ok, _subscription} = User.subscribe(user, other_user)
312 {:ok, _user_relationship} = User.block(user, other_user)
313 {:ok, _user_relationship} = User.block(other_user, user)
314
315 expected =
316 Map.merge(
317 @blank_response,
318 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
319 )
320
321 test_relationship_rendering(user, other_user, expected)
322 end
323
324 test "represent a relationship for the user blocking a domain" do
325 user = insert(:user)
326 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
327
328 {:ok, user} = User.block_domain(user, "bad.site")
329
330 expected =
331 Map.merge(
332 @blank_response,
333 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
334 )
335
336 test_relationship_rendering(user, other_user, expected)
337 end
338
339 test "represent a relationship for the user with a pending follow request" do
340 user = insert(:user)
341 other_user = insert(:user, is_locked: true)
342
343 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
344 user = User.get_cached_by_id(user.id)
345 other_user = User.get_cached_by_id(other_user.id)
346
347 expected =
348 Map.merge(
349 @blank_response,
350 %{requested: true, following: false, id: to_string(other_user.id)}
351 )
352
353 test_relationship_rendering(user, other_user, expected)
354 end
355 end
356
357 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
358 user = insert(:user, pleroma_settings_store: %{fe: "test"})
359
360 result =
361 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
362
363 assert result.pleroma.settings_store == %{:fe => "test"}
364
365 result = AccountView.render("show.json", %{user: user, for: nil, with_pleroma_settings: true})
366 assert result.pleroma[:settings_store] == nil
367
368 result = AccountView.render("show.json", %{user: user, for: user})
369 assert result.pleroma[:settings_store] == nil
370 end
371
372 test "doesn't sanitize display names" do
373 user = insert(:user, name: "<marquee> username </marquee>")
374 result = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
375 assert result.display_name == "<marquee> username </marquee>"
376 end
377
378 test "never display nil user follow counts" do
379 user = insert(:user, following_count: 0, follower_count: 0)
380 result = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
381
382 assert result.following_count == 0
383 assert result.followers_count == 0
384 end
385
386 describe "hiding follows/following" do
387 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
388 user =
389 insert(:user, %{
390 hide_followers: true,
391 hide_followers_count: true,
392 hide_follows: true,
393 hide_follows_count: true
394 })
395
396 other_user = insert(:user)
397 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
398 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
399
400 assert %{
401 followers_count: 0,
402 following_count: 0,
403 pleroma: %{hide_follows_count: true, hide_followers_count: true}
404 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
405 end
406
407 test "shows when follows/followers are hidden" do
408 user = insert(:user, hide_followers: true, hide_follows: true)
409 other_user = insert(:user)
410 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
411 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
412
413 assert %{
414 followers_count: 1,
415 following_count: 1,
416 pleroma: %{hide_follows: true, hide_followers: true}
417 } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
418 end
419
420 test "shows actual follower/following count to the account owner" do
421 user = insert(:user, hide_followers: true, hide_follows: true)
422 other_user = insert(:user)
423 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
424
425 assert User.following?(user, other_user)
426 assert Pleroma.FollowingRelationship.follower_count(other_user) == 1
427 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
428
429 assert %{
430 followers_count: 1,
431 following_count: 1
432 } = AccountView.render("show.json", %{user: user, for: user})
433 end
434
435 test "shows unread_conversation_count only to the account owner" do
436 user = insert(:user)
437 other_user = insert(:user)
438
439 {:ok, _activity} =
440 CommonAPI.post(other_user, %{
441 status: "Hey @#{user.nickname}.",
442 visibility: "direct"
443 })
444
445 user = User.get_cached_by_ap_id(user.ap_id)
446
447 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
448 :unread_conversation_count
449 ] == nil
450
451 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
452 :unread_conversation_count
453 ] == 1
454 end
455
456 test "shows unread_count only to the account owner" do
457 user = insert(:user)
458 insert_list(7, :notification, user: user, activity: insert(:note_activity))
459 other_user = insert(:user)
460
461 user = User.get_cached_by_ap_id(user.ap_id)
462
463 assert AccountView.render(
464 "show.json",
465 %{user: user, for: other_user}
466 )[:pleroma][:unread_notifications_count] == nil
467
468 assert AccountView.render(
469 "show.json",
470 %{user: user, for: user}
471 )[:pleroma][:unread_notifications_count] == 7
472 end
473
474 test "shows email only to the account owner" do
475 user = insert(:user)
476 other_user = insert(:user)
477
478 user = User.get_cached_by_ap_id(user.ap_id)
479
480 assert AccountView.render(
481 "show.json",
482 %{user: user, for: other_user}
483 )[:pleroma][:email] == nil
484
485 assert AccountView.render(
486 "show.json",
487 %{user: user, for: user}
488 )[:pleroma][:email] == user.email
489 end
490 end
491
492 describe "follow requests counter" do
493 test "shows zero when no follow requests are pending" do
494 user = insert(:user)
495
496 assert %{follow_requests_count: 0} =
497 AccountView.render("show.json", %{user: user, for: user})
498
499 other_user = insert(:user)
500 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
501
502 assert %{follow_requests_count: 0} =
503 AccountView.render("show.json", %{user: user, for: user})
504 end
505
506 test "shows non-zero when follow requests are pending" do
507 user = insert(:user, is_locked: true)
508
509 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
510
511 other_user = insert(:user)
512 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
513
514 assert %{locked: true, follow_requests_count: 1} =
515 AccountView.render("show.json", %{user: user, for: user})
516 end
517
518 test "decreases when accepting a follow request" do
519 user = insert(:user, is_locked: true)
520
521 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
522
523 other_user = insert(:user)
524 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
525
526 assert %{locked: true, follow_requests_count: 1} =
527 AccountView.render("show.json", %{user: user, for: user})
528
529 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
530
531 assert %{locked: true, follow_requests_count: 0} =
532 AccountView.render("show.json", %{user: user, for: user})
533 end
534
535 test "decreases when rejecting a follow request" do
536 user = insert(:user, is_locked: true)
537
538 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
539
540 other_user = insert(:user)
541 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
542
543 assert %{locked: true, follow_requests_count: 1} =
544 AccountView.render("show.json", %{user: user, for: user})
545
546 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
547
548 assert %{locked: true, follow_requests_count: 0} =
549 AccountView.render("show.json", %{user: user, for: user})
550 end
551
552 test "shows non-zero when historical unapproved requests are present" do
553 user = insert(:user, is_locked: true)
554
555 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
556
557 other_user = insert(:user)
558 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
559
560 {:ok, user} = User.update_and_set_cache(user, %{is_locked: false})
561
562 assert %{locked: false, follow_requests_count: 1} =
563 AccountView.render("show.json", %{user: user, for: user})
564 end
565 end
566
567 test "uses mediaproxy urls when it's enabled (regardless of media preview proxy state)" do
568 clear_config([:media_proxy, :enabled], true)
569 clear_config([:media_preview_proxy, :enabled])
570
571 user =
572 insert(:user,
573 avatar: %{"url" => [%{"href" => "https://evil.website/avatar.png"}]},
574 banner: %{"url" => [%{"href" => "https://evil.website/banner.png"}]},
575 emoji: %{"joker_smile" => "https://evil.website/society.png"}
576 )
577
578 with media_preview_enabled <- [false, true] do
579 clear_config([:media_preview_proxy, :enabled], media_preview_enabled)
580
581 AccountView.render("show.json", %{user: user, skip_visibility_check: true})
582 |> Enum.all?(fn
583 {key, url} when key in [:avatar, :avatar_static, :header, :header_static] ->
584 String.starts_with?(url, Pleroma.Web.Endpoint.url())
585
586 {:emojis, emojis} ->
587 Enum.all?(emojis, fn %{url: url, static_url: static_url} ->
588 String.starts_with?(url, Pleroma.Web.Endpoint.url()) &&
589 String.starts_with?(static_url, Pleroma.Web.Endpoint.url())
590 end)
591
592 _ ->
593 true
594 end)
595 |> assert()
596 end
597 end
598 end