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