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