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