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