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