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