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