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