Merge branch 'develop' into 'remove-twitter-api'
[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 })
38
39 expected = %{
40 id: to_string(user.id),
41 username: "shp",
42 acct: user.nickname,
43 display_name: user.name,
44 locked: false,
45 created_at: "2017-08-15T15:47:06.000Z",
46 followers_count: 3,
47 following_count: 0,
48 statuses_count: 5,
49 note: "<span>valid html</span>. a<br/>b<br/>c<br/>d<br/>f &#39;&amp;&lt;&gt;&quot;",
50 url: user.ap_id,
51 avatar: "http://localhost:4001/images/avi.png",
52 avatar_static: "http://localhost:4001/images/avi.png",
53 header: "http://localhost:4001/images/banner.png",
54 header_static: "http://localhost:4001/images/banner.png",
55 emojis: [
56 %{
57 "static_url" => "/file.png",
58 "url" => "/file.png",
59 "shortcode" => "karjalanpiirakka",
60 "visible_in_picker" => false
61 }
62 ],
63 fields: [],
64 bot: false,
65 source: %{
66 note: "valid html. a\nb\nc\nd\nf '&<>\"",
67 sensitive: false,
68 pleroma: %{
69 actor_type: "Person",
70 discoverable: false
71 },
72 fields: []
73 },
74 pleroma: %{
75 background_image: "https://example.com/images/asuka_hospital.png",
76 confirmation_pending: false,
77 tags: [],
78 is_admin: false,
79 is_moderator: false,
80 hide_favorites: true,
81 hide_followers: false,
82 hide_follows: false,
83 hide_followers_count: false,
84 hide_follows_count: false,
85 relationship: %{},
86 skip_thread_containment: false
87 }
88 }
89
90 assert expected == AccountView.render("show.json", %{user: user})
91 end
92
93 test "Represent the user account for the account owner" do
94 user = insert(:user)
95
96 notification_settings = %{
97 followers: true,
98 follows: true,
99 non_followers: true,
100 non_follows: true,
101 privacy_option: false
102 }
103
104 privacy = user.default_scope
105
106 assert %{
107 pleroma: %{notification_settings: ^notification_settings, allow_following_move: true},
108 source: %{privacy: ^privacy}
109 } = AccountView.render("show.json", %{user: user, for: user})
110 end
111
112 test "Represent a Service(bot) account" do
113 user =
114 insert(:user, %{
115 follower_count: 3,
116 note_count: 5,
117 actor_type: "Service",
118 nickname: "shp@shitposter.club",
119 inserted_at: ~N[2017-08-15 15:47:06.597036]
120 })
121
122 expected = %{
123 id: to_string(user.id),
124 username: "shp",
125 acct: user.nickname,
126 display_name: user.name,
127 locked: false,
128 created_at: "2017-08-15T15:47:06.000Z",
129 followers_count: 3,
130 following_count: 0,
131 statuses_count: 5,
132 note: user.bio,
133 url: user.ap_id,
134 avatar: "http://localhost:4001/images/avi.png",
135 avatar_static: "http://localhost:4001/images/avi.png",
136 header: "http://localhost:4001/images/banner.png",
137 header_static: "http://localhost:4001/images/banner.png",
138 emojis: [],
139 fields: [],
140 bot: true,
141 source: %{
142 note: user.bio,
143 sensitive: false,
144 pleroma: %{
145 actor_type: "Service",
146 discoverable: false
147 },
148 fields: []
149 },
150 pleroma: %{
151 background_image: nil,
152 confirmation_pending: false,
153 tags: [],
154 is_admin: false,
155 is_moderator: false,
156 hide_favorites: true,
157 hide_followers: false,
158 hide_follows: false,
159 hide_followers_count: false,
160 hide_follows_count: false,
161 relationship: %{},
162 skip_thread_containment: false
163 }
164 }
165
166 assert expected == AccountView.render("show.json", %{user: user})
167 end
168
169 test "Represent a Funkwhale channel" do
170 {:ok, user} =
171 User.get_or_fetch_by_ap_id(
172 "https://channels.tests.funkwhale.audio/federation/actors/compositions"
173 )
174
175 assert represented = AccountView.render("show.json", %{user: user})
176 assert represented.acct == "compositions@channels.tests.funkwhale.audio"
177 assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"
178 end
179
180 test "Represent a deactivated user for an admin" do
181 admin = insert(:user, is_admin: true)
182 deactivated_user = insert(:user, deactivated: true)
183 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
184 assert represented[:pleroma][:deactivated] == true
185 end
186
187 test "Represent a smaller mention" do
188 user = insert(:user)
189
190 expected = %{
191 id: to_string(user.id),
192 acct: user.nickname,
193 username: user.nickname,
194 url: user.ap_id
195 }
196
197 assert expected == AccountView.render("mention.json", %{user: user})
198 end
199
200 describe "relationship" do
201 defp test_relationship_rendering(user, other_user, expected_result) do
202 opts = %{user: user, target: other_user, relationships: nil}
203 assert expected_result == AccountView.render("relationship.json", opts)
204
205 relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
206 opts = Map.put(opts, :relationships, relationships_opt)
207 assert expected_result == AccountView.render("relationship.json", opts)
208
209 assert [expected_result] ==
210 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
211 end
212
213 @blank_response %{
214 following: false,
215 followed_by: false,
216 blocking: false,
217 blocked_by: false,
218 muting: false,
219 muting_notifications: false,
220 subscribing: false,
221 requested: false,
222 domain_blocking: false,
223 showing_reblogs: true,
224 endorsed: false
225 }
226
227 test "represent a relationship for the following and followed user" do
228 user = insert(:user)
229 other_user = insert(:user)
230
231 {:ok, user} = User.follow(user, other_user)
232 {:ok, other_user} = User.follow(other_user, user)
233 {:ok, _subscription} = User.subscribe(user, other_user)
234 {:ok, _user_relationships} = User.mute(user, other_user, true)
235 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
236
237 expected =
238 Map.merge(
239 @blank_response,
240 %{
241 following: true,
242 followed_by: true,
243 muting: true,
244 muting_notifications: true,
245 subscribing: true,
246 showing_reblogs: false,
247 id: to_string(other_user.id)
248 }
249 )
250
251 test_relationship_rendering(user, other_user, expected)
252 end
253
254 test "represent a relationship for the blocking and blocked user" do
255 user = insert(:user)
256 other_user = insert(:user)
257
258 {:ok, user} = User.follow(user, other_user)
259 {:ok, _subscription} = User.subscribe(user, other_user)
260 {:ok, _user_relationship} = User.block(user, other_user)
261 {:ok, _user_relationship} = User.block(other_user, user)
262
263 expected =
264 Map.merge(
265 @blank_response,
266 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
267 )
268
269 test_relationship_rendering(user, other_user, expected)
270 end
271
272 test "represent a relationship for the user blocking a domain" do
273 user = insert(:user)
274 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
275
276 {:ok, user} = User.block_domain(user, "bad.site")
277
278 expected =
279 Map.merge(
280 @blank_response,
281 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
282 )
283
284 test_relationship_rendering(user, other_user, expected)
285 end
286
287 test "represent a relationship for the user with a pending follow request" do
288 user = insert(:user)
289 other_user = insert(:user, locked: true)
290
291 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
292 user = User.get_cached_by_id(user.id)
293 other_user = User.get_cached_by_id(other_user.id)
294
295 expected =
296 Map.merge(
297 @blank_response,
298 %{requested: true, following: false, id: to_string(other_user.id)}
299 )
300
301 test_relationship_rendering(user, other_user, expected)
302 end
303 end
304
305 test "represent an embedded relationship" do
306 user =
307 insert(:user, %{
308 follower_count: 0,
309 note_count: 5,
310 actor_type: "Service",
311 nickname: "shp@shitposter.club",
312 inserted_at: ~N[2017-08-15 15:47:06.597036]
313 })
314
315 other_user = insert(:user)
316 {:ok, other_user} = User.follow(other_user, user)
317 {:ok, _user_relationship} = User.block(other_user, user)
318 {:ok, _} = User.follow(insert(:user), user)
319
320 expected = %{
321 id: to_string(user.id),
322 username: "shp",
323 acct: user.nickname,
324 display_name: user.name,
325 locked: false,
326 created_at: "2017-08-15T15:47:06.000Z",
327 followers_count: 1,
328 following_count: 0,
329 statuses_count: 5,
330 note: user.bio,
331 url: user.ap_id,
332 avatar: "http://localhost:4001/images/avi.png",
333 avatar_static: "http://localhost:4001/images/avi.png",
334 header: "http://localhost:4001/images/banner.png",
335 header_static: "http://localhost:4001/images/banner.png",
336 emojis: [],
337 fields: [],
338 bot: true,
339 source: %{
340 note: user.bio,
341 sensitive: false,
342 pleroma: %{
343 actor_type: "Service",
344 discoverable: false
345 },
346 fields: []
347 },
348 pleroma: %{
349 background_image: nil,
350 confirmation_pending: false,
351 tags: [],
352 is_admin: false,
353 is_moderator: false,
354 hide_favorites: true,
355 hide_followers: false,
356 hide_follows: false,
357 hide_followers_count: false,
358 hide_follows_count: false,
359 relationship: %{
360 id: to_string(user.id),
361 following: false,
362 followed_by: false,
363 blocking: true,
364 blocked_by: false,
365 subscribing: false,
366 muting: false,
367 muting_notifications: false,
368 requested: false,
369 domain_blocking: false,
370 showing_reblogs: true,
371 endorsed: false
372 },
373 skip_thread_containment: false
374 }
375 }
376
377 assert expected ==
378 AccountView.render("show.json", %{user: refresh_record(user), for: other_user})
379 end
380
381 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
382 user = insert(:user, pleroma_settings_store: %{fe: "test"})
383
384 result =
385 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
386
387 assert result.pleroma.settings_store == %{:fe => "test"}
388
389 result = AccountView.render("show.json", %{user: user, with_pleroma_settings: true})
390 assert result.pleroma[:settings_store] == nil
391
392 result = AccountView.render("show.json", %{user: user, for: user})
393 assert result.pleroma[:settings_store] == nil
394 end
395
396 test "doesn't sanitize display names" do
397 user = insert(:user, name: "<marquee> username </marquee>")
398 result = AccountView.render("show.json", %{user: user})
399 assert result.display_name == "<marquee> username </marquee>"
400 end
401
402 test "never display nil user follow counts" do
403 user = insert(:user, following_count: 0, follower_count: 0)
404 result = AccountView.render("show.json", %{user: user})
405
406 assert result.following_count == 0
407 assert result.followers_count == 0
408 end
409
410 describe "hiding follows/following" do
411 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
412 user =
413 insert(:user, %{
414 hide_followers: true,
415 hide_followers_count: true,
416 hide_follows: true,
417 hide_follows_count: true
418 })
419
420 other_user = insert(:user)
421 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
422 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
423
424 assert %{
425 followers_count: 0,
426 following_count: 0,
427 pleroma: %{hide_follows_count: true, hide_followers_count: true}
428 } = AccountView.render("show.json", %{user: user})
429 end
430
431 test "shows when follows/followers are hidden" do
432 user = insert(:user, hide_followers: true, hide_follows: true)
433 other_user = insert(:user)
434 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
435 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
436
437 assert %{
438 followers_count: 1,
439 following_count: 1,
440 pleroma: %{hide_follows: true, hide_followers: true}
441 } = AccountView.render("show.json", %{user: user})
442 end
443
444 test "shows actual follower/following count to the account owner" do
445 user = insert(:user, hide_followers: true, hide_follows: true)
446 other_user = insert(:user)
447 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
448 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
449
450 assert %{
451 followers_count: 1,
452 following_count: 1
453 } = AccountView.render("show.json", %{user: user, for: user})
454 end
455
456 test "shows unread_conversation_count only to the account owner" do
457 user = insert(:user)
458 other_user = insert(:user)
459
460 {:ok, _activity} =
461 CommonAPI.post(other_user, %{
462 status: "Hey @#{user.nickname}.",
463 visibility: "direct"
464 })
465
466 user = User.get_cached_by_ap_id(user.ap_id)
467
468 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
469 :unread_conversation_count
470 ] == nil
471
472 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
473 :unread_conversation_count
474 ] == 1
475 end
476
477 test "shows unread_count only to the account owner" do
478 user = insert(:user)
479 insert_list(7, :notification, user: user)
480 other_user = insert(:user)
481
482 user = User.get_cached_by_ap_id(user.ap_id)
483
484 assert AccountView.render(
485 "show.json",
486 %{user: user, for: other_user}
487 )[:pleroma][:unread_notifications_count] == nil
488
489 assert AccountView.render(
490 "show.json",
491 %{user: user, for: user}
492 )[:pleroma][:unread_notifications_count] == 7
493 end
494 end
495
496 describe "follow requests counter" do
497 test "shows zero when no follow requests are pending" do
498 user = insert(:user)
499
500 assert %{follow_requests_count: 0} =
501 AccountView.render("show.json", %{user: user, for: user})
502
503 other_user = insert(:user)
504 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
505
506 assert %{follow_requests_count: 0} =
507 AccountView.render("show.json", %{user: user, for: user})
508 end
509
510 test "shows non-zero when follow requests are pending" 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 assert %{locked: true, follow_requests_count: 1} =
519 AccountView.render("show.json", %{user: user, for: user})
520 end
521
522 test "decreases when accepting a follow request" do
523 user = insert(:user, locked: true)
524
525 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
526
527 other_user = insert(:user)
528 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
529
530 assert %{locked: true, follow_requests_count: 1} =
531 AccountView.render("show.json", %{user: user, for: user})
532
533 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
534
535 assert %{locked: true, follow_requests_count: 0} =
536 AccountView.render("show.json", %{user: user, for: user})
537 end
538
539 test "decreases when rejecting a follow request" do
540 user = insert(:user, locked: true)
541
542 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
543
544 other_user = insert(:user)
545 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
546
547 assert %{locked: true, follow_requests_count: 1} =
548 AccountView.render("show.json", %{user: user, for: user})
549
550 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
551
552 assert %{locked: true, follow_requests_count: 0} =
553 AccountView.render("show.json", %{user: user, for: user})
554 end
555
556 test "shows non-zero when historical unapproved requests are present" do
557 user = insert(:user, locked: true)
558
559 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
560
561 other_user = insert(:user)
562 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
563
564 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
565
566 assert %{locked: false, follow_requests_count: 1} =
567 AccountView.render("show.json", %{user: user, for: user})
568 end
569 end
570 end