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