Merge branch 'fix/1658-invite-send' into 'develop'
[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
213 assert [expected_result] ==
214 AccountView.render("relationships.json", %{user: user, targets: [other_user]})
215 end
216
217 @blank_response %{
218 following: false,
219 followed_by: false,
220 blocking: false,
221 blocked_by: false,
222 muting: false,
223 muting_notifications: false,
224 subscribing: false,
225 requested: false,
226 domain_blocking: false,
227 showing_reblogs: true,
228 endorsed: false
229 }
230
231 test "represent a relationship for the following and followed user" do
232 user = insert(:user)
233 other_user = insert(:user)
234
235 {:ok, user} = User.follow(user, other_user)
236 {:ok, other_user} = User.follow(other_user, user)
237 {:ok, _subscription} = User.subscribe(user, other_user)
238 {:ok, _user_relationships} = User.mute(user, other_user, true)
239 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user)
240
241 expected =
242 Map.merge(
243 @blank_response,
244 %{
245 following: true,
246 followed_by: true,
247 muting: true,
248 muting_notifications: true,
249 subscribing: true,
250 showing_reblogs: false,
251 id: to_string(other_user.id)
252 }
253 )
254
255 test_relationship_rendering(user, other_user, expected)
256 end
257
258 test "represent a relationship for the blocking and blocked user" do
259 user = insert(:user)
260 other_user = insert(:user)
261
262 {:ok, user} = User.follow(user, other_user)
263 {:ok, _subscription} = User.subscribe(user, other_user)
264 {:ok, _user_relationship} = User.block(user, other_user)
265 {:ok, _user_relationship} = User.block(other_user, user)
266
267 expected =
268 Map.merge(
269 @blank_response,
270 %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)}
271 )
272
273 test_relationship_rendering(user, other_user, expected)
274 end
275
276 test "represent a relationship for the user blocking a domain" do
277 user = insert(:user)
278 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
279
280 {:ok, user} = User.block_domain(user, "bad.site")
281
282 expected =
283 Map.merge(
284 @blank_response,
285 %{domain_blocking: true, blocking: false, id: to_string(other_user.id)}
286 )
287
288 test_relationship_rendering(user, other_user, expected)
289 end
290
291 test "represent a relationship for the user with a pending follow request" do
292 user = insert(:user)
293 other_user = insert(:user, locked: true)
294
295 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
296 user = User.get_cached_by_id(user.id)
297 other_user = User.get_cached_by_id(other_user.id)
298
299 expected =
300 Map.merge(
301 @blank_response,
302 %{requested: true, following: false, id: to_string(other_user.id)}
303 )
304
305 test_relationship_rendering(user, other_user, expected)
306 end
307 end
308
309 test "represent an embedded relationship" do
310 user =
311 insert(:user, %{
312 follower_count: 0,
313 note_count: 5,
314 source_data: %{},
315 actor_type: "Service",
316 nickname: "shp@shitposter.club",
317 inserted_at: ~N[2017-08-15 15:47:06.597036]
318 })
319
320 other_user = insert(:user)
321 {:ok, other_user} = User.follow(other_user, user)
322 {:ok, _user_relationship} = User.block(other_user, user)
323 {:ok, _} = User.follow(insert(:user), user)
324
325 expected = %{
326 id: to_string(user.id),
327 username: "shp",
328 acct: user.nickname,
329 display_name: user.name,
330 locked: false,
331 created_at: "2017-08-15T15:47:06.000Z",
332 followers_count: 1,
333 following_count: 0,
334 statuses_count: 5,
335 note: user.bio,
336 url: user.ap_id,
337 avatar: "http://localhost:4001/images/avi.png",
338 avatar_static: "http://localhost:4001/images/avi.png",
339 header: "http://localhost:4001/images/banner.png",
340 header_static: "http://localhost:4001/images/banner.png",
341 emojis: [],
342 fields: [],
343 bot: true,
344 source: %{
345 note: user.bio,
346 sensitive: false,
347 pleroma: %{
348 actor_type: "Service",
349 discoverable: false
350 },
351 fields: []
352 },
353 pleroma: %{
354 background_image: nil,
355 confirmation_pending: false,
356 tags: [],
357 is_admin: false,
358 is_moderator: false,
359 hide_favorites: true,
360 hide_followers: false,
361 hide_follows: false,
362 hide_followers_count: false,
363 hide_follows_count: false,
364 relationship: %{
365 id: to_string(user.id),
366 following: false,
367 followed_by: false,
368 blocking: true,
369 blocked_by: false,
370 subscribing: false,
371 muting: false,
372 muting_notifications: false,
373 requested: false,
374 domain_blocking: false,
375 showing_reblogs: true,
376 endorsed: false
377 },
378 skip_thread_containment: false
379 }
380 }
381
382 assert expected ==
383 AccountView.render("show.json", %{user: refresh_record(user), for: other_user})
384 end
385
386 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
387 user = insert(:user, pleroma_settings_store: %{fe: "test"})
388
389 result =
390 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
391
392 assert result.pleroma.settings_store == %{:fe => "test"}
393
394 result = AccountView.render("show.json", %{user: user, with_pleroma_settings: true})
395 assert result.pleroma[:settings_store] == nil
396
397 result = AccountView.render("show.json", %{user: user, for: user})
398 assert result.pleroma[:settings_store] == nil
399 end
400
401 test "doesn't sanitize display names" do
402 user = insert(:user, name: "<marquee> username </marquee>")
403 result = AccountView.render("show.json", %{user: user})
404 assert result.display_name == "<marquee> username </marquee>"
405 end
406
407 test "never display nil user follow counts" do
408 user = insert(:user, following_count: 0, follower_count: 0)
409 result = AccountView.render("show.json", %{user: user})
410
411 assert result.following_count == 0
412 assert result.followers_count == 0
413 end
414
415 describe "hiding follows/following" do
416 test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
417 user =
418 insert(:user, %{
419 hide_followers: true,
420 hide_followers_count: true,
421 hide_follows: true,
422 hide_follows_count: true
423 })
424
425 other_user = insert(:user)
426 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
427 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
428
429 assert %{
430 followers_count: 0,
431 following_count: 0,
432 pleroma: %{hide_follows_count: true, hide_followers_count: true}
433 } = AccountView.render("show.json", %{user: user})
434 end
435
436 test "shows when follows/followers are hidden" do
437 user = insert(:user, hide_followers: true, hide_follows: true)
438 other_user = insert(:user)
439 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
440 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
441
442 assert %{
443 followers_count: 1,
444 following_count: 1,
445 pleroma: %{hide_follows: true, hide_followers: true}
446 } = AccountView.render("show.json", %{user: user})
447 end
448
449 test "shows actual follower/following count to the account owner" do
450 user = insert(:user, hide_followers: true, hide_follows: true)
451 other_user = insert(:user)
452 {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
453 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
454
455 assert %{
456 followers_count: 1,
457 following_count: 1
458 } = AccountView.render("show.json", %{user: user, for: user})
459 end
460
461 test "shows unread_conversation_count only to the account owner" do
462 user = insert(:user)
463 other_user = insert(:user)
464
465 {:ok, _activity} =
466 CommonAPI.post(other_user, %{
467 "status" => "Hey @#{user.nickname}.",
468 "visibility" => "direct"
469 })
470
471 user = User.get_cached_by_ap_id(user.ap_id)
472
473 assert AccountView.render("show.json", %{user: user, for: other_user})[:pleroma][
474 :unread_conversation_count
475 ] == nil
476
477 assert AccountView.render("show.json", %{user: user, for: user})[:pleroma][
478 :unread_conversation_count
479 ] == 1
480 end
481 end
482
483 describe "follow requests counter" do
484 test "shows zero when no follow requests are pending" do
485 user = insert(:user)
486
487 assert %{follow_requests_count: 0} =
488 AccountView.render("show.json", %{user: user, for: user})
489
490 other_user = insert(:user)
491 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
492
493 assert %{follow_requests_count: 0} =
494 AccountView.render("show.json", %{user: user, for: user})
495 end
496
497 test "shows non-zero when follow requests are pending" do
498 user = insert(:user, locked: true)
499
500 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
501
502 other_user = insert(:user)
503 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
504
505 assert %{locked: true, follow_requests_count: 1} =
506 AccountView.render("show.json", %{user: user, for: user})
507 end
508
509 test "decreases when accepting a follow request" do
510 user = insert(:user, locked: true)
511
512 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
513
514 other_user = insert(:user)
515 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
516
517 assert %{locked: true, follow_requests_count: 1} =
518 AccountView.render("show.json", %{user: user, for: user})
519
520 {:ok, _other_user} = CommonAPI.accept_follow_request(other_user, user)
521
522 assert %{locked: true, follow_requests_count: 0} =
523 AccountView.render("show.json", %{user: user, for: user})
524 end
525
526 test "decreases when rejecting a follow request" do
527 user = insert(:user, locked: true)
528
529 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
530
531 other_user = insert(:user)
532 {:ok, other_user, user, _activity} = CommonAPI.follow(other_user, user)
533
534 assert %{locked: true, follow_requests_count: 1} =
535 AccountView.render("show.json", %{user: user, for: user})
536
537 {:ok, _other_user} = CommonAPI.reject_follow_request(other_user, user)
538
539 assert %{locked: true, follow_requests_count: 0} =
540 AccountView.render("show.json", %{user: user, for: user})
541 end
542
543 test "shows non-zero when historical unapproved requests are present" do
544 user = insert(:user, locked: true)
545
546 assert %{locked: true} = AccountView.render("show.json", %{user: user, for: user})
547
548 other_user = insert(:user)
549 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
550
551 {:ok, user} = User.update_and_set_cache(user, %{locked: false})
552
553 assert %{locked: false, follow_requests_count: 1} =
554 AccountView.render("show.json", %{user: user, for: user})
555 end
556 end
557 end