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