Merge branch 'feature/move-activity' into 'develop'
[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, allow_following_move: true},
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} = User.mute(user, other_user, true)
195 {:ok, user} = CommonAPI.hide_reblogs(user, other_user)
196
197 expected = %{
198 id: to_string(other_user.id),
199 following: true,
200 followed_by: true,
201 blocking: false,
202 blocked_by: false,
203 muting: true,
204 muting_notifications: true,
205 subscribing: true,
206 requested: false,
207 domain_blocking: false,
208 showing_reblogs: false,
209 endorsed: false
210 }
211
212 assert expected ==
213 AccountView.render("relationship.json", %{user: user, target: other_user})
214 end
215
216 test "represent a relationship for the blocking and blocked user" do
217 user = insert(:user)
218 other_user = insert(:user)
219
220 {:ok, user} = User.follow(user, other_user)
221 {:ok, other_user} = User.subscribe(user, other_user)
222 {:ok, user} = User.block(user, other_user)
223 {:ok, other_user} = User.block(other_user, user)
224
225 expected = %{
226 id: to_string(other_user.id),
227 following: false,
228 followed_by: false,
229 blocking: true,
230 blocked_by: true,
231 muting: false,
232 muting_notifications: false,
233 subscribing: false,
234 requested: false,
235 domain_blocking: false,
236 showing_reblogs: true,
237 endorsed: false
238 }
239
240 assert expected ==
241 AccountView.render("relationship.json", %{user: user, target: other_user})
242 end
243
244 test "represent a relationship for the user blocking a domain" do
245 user = insert(:user)
246 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
247
248 {:ok, user} = User.block_domain(user, "bad.site")
249
250 assert %{domain_blocking: true, blocking: false} =
251 AccountView.render("relationship.json", %{user: user, target: other_user})
252 end
253
254 test "represent a relationship for the user with a pending follow request" do
255 user = insert(:user)
256 other_user = insert(:user, locked: true)
257
258 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
259 user = User.get_cached_by_id(user.id)
260 other_user = User.get_cached_by_id(other_user.id)
261
262 expected = %{
263 id: to_string(other_user.id),
264 following: false,
265 followed_by: false,
266 blocking: false,
267 blocked_by: false,
268 muting: false,
269 muting_notifications: false,
270 subscribing: false,
271 requested: true,
272 domain_blocking: false,
273 showing_reblogs: true,
274 endorsed: false
275 }
276
277 assert expected ==
278 AccountView.render("relationship.json", %{user: user, target: other_user})
279 end
280 end
281
282 test "represent an embedded relationship" do
283 user =
284 insert(:user, %{
285 follower_count: 0,
286 note_count: 5,
287 source_data: %{"type" => "Service"},
288 nickname: "shp@shitposter.club",
289 inserted_at: ~N[2017-08-15 15:47:06.597036]
290 })
291
292 other_user = insert(:user)
293 {:ok, other_user} = User.follow(other_user, user)
294 {:ok, other_user} = User.block(other_user, user)
295 {:ok, _} = User.follow(insert(:user), user)
296
297 expected = %{
298 id: to_string(user.id),
299 username: "shp",
300 acct: user.nickname,
301 display_name: user.name,
302 locked: false,
303 created_at: "2017-08-15T15:47:06.000Z",
304 followers_count: 1,
305 following_count: 0,
306 statuses_count: 5,
307 note: user.bio,
308 url: user.ap_id,
309 avatar: "http://localhost:4001/images/avi.png",
310 avatar_static: "http://localhost:4001/images/avi.png",
311 header: "http://localhost:4001/images/banner.png",
312 header_static: "http://localhost:4001/images/banner.png",
313 emojis: [],
314 fields: [],
315 bot: true,
316 source: %{
317 note: user.bio,
318 sensitive: false,
319 pleroma: %{
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 "sanitizes display names" do
373 user = insert(:user, name: "<marquee> username </marquee>")
374 result = AccountView.render("show.json", %{user: user})
375 refute 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