added User.NotificationSetting struct
[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 = %Pleroma.User.NotificationSetting{}
96 privacy = user.default_scope
97
98 assert %{
99 pleroma: %{notification_settings: ^notification_settings},
100 source: %{privacy: ^privacy}
101 } = AccountView.render("show.json", %{user: user, for: user})
102 end
103
104 test "Represent a Service(bot) account" do
105 user =
106 insert(:user, %{
107 follower_count: 3,
108 note_count: 5,
109 source_data: %{"type" => "Service"},
110 nickname: "shp@shitposter.club",
111 inserted_at: ~N[2017-08-15 15:47:06.597036]
112 })
113
114 expected = %{
115 id: to_string(user.id),
116 username: "shp",
117 acct: user.nickname,
118 display_name: user.name,
119 locked: false,
120 created_at: "2017-08-15T15:47:06.000Z",
121 followers_count: 3,
122 following_count: 0,
123 statuses_count: 5,
124 note: user.bio,
125 url: user.ap_id,
126 avatar: "http://localhost:4001/images/avi.png",
127 avatar_static: "http://localhost:4001/images/avi.png",
128 header: "http://localhost:4001/images/banner.png",
129 header_static: "http://localhost:4001/images/banner.png",
130 emojis: [],
131 fields: [],
132 bot: true,
133 source: %{
134 note: user.bio,
135 sensitive: false,
136 pleroma: %{
137 discoverable: false
138 },
139 fields: []
140 },
141 pleroma: %{
142 background_image: nil,
143 confirmation_pending: false,
144 tags: [],
145 is_admin: false,
146 is_moderator: false,
147 hide_favorites: true,
148 hide_followers: false,
149 hide_follows: false,
150 hide_followers_count: false,
151 hide_follows_count: false,
152 relationship: %{},
153 skip_thread_containment: false
154 }
155 }
156
157 assert expected == AccountView.render("show.json", %{user: user})
158 end
159
160 test "Represent a deactivated user for an admin" do
161 admin = insert(:user, is_admin: true)
162 deactivated_user = insert(:user, deactivated: true)
163 represented = AccountView.render("show.json", %{user: deactivated_user, for: admin})
164 assert represented[:pleroma][:deactivated] == true
165 end
166
167 test "Represent a smaller mention" do
168 user = insert(:user)
169
170 expected = %{
171 id: to_string(user.id),
172 acct: user.nickname,
173 username: user.nickname,
174 url: user.ap_id
175 }
176
177 assert expected == AccountView.render("mention.json", %{user: user})
178 end
179
180 describe "relationship" do
181 test "represent a relationship for the following and followed user" do
182 user = insert(:user)
183 other_user = insert(:user)
184
185 {:ok, user} = User.follow(user, other_user)
186 {:ok, other_user} = User.follow(other_user, user)
187 {:ok, other_user} = User.subscribe(user, other_user)
188 {:ok, user} = User.mute(user, other_user, true)
189 {:ok, user} = CommonAPI.hide_reblogs(user, other_user)
190
191 expected = %{
192 id: to_string(other_user.id),
193 following: true,
194 followed_by: true,
195 blocking: false,
196 blocked_by: false,
197 muting: true,
198 muting_notifications: true,
199 subscribing: true,
200 requested: false,
201 domain_blocking: false,
202 showing_reblogs: false,
203 endorsed: false
204 }
205
206 assert expected ==
207 AccountView.render("relationship.json", %{user: user, target: other_user})
208 end
209
210 test "represent a relationship for the blocking and blocked user" do
211 user = insert(:user)
212 other_user = insert(:user)
213
214 {:ok, user} = User.follow(user, other_user)
215 {:ok, other_user} = User.subscribe(user, other_user)
216 {:ok, user} = User.block(user, other_user)
217 {:ok, other_user} = User.block(other_user, user)
218
219 expected = %{
220 id: to_string(other_user.id),
221 following: false,
222 followed_by: false,
223 blocking: true,
224 blocked_by: true,
225 muting: false,
226 muting_notifications: false,
227 subscribing: false,
228 requested: false,
229 domain_blocking: false,
230 showing_reblogs: true,
231 endorsed: false
232 }
233
234 assert expected ==
235 AccountView.render("relationship.json", %{user: user, target: other_user})
236 end
237
238 test "represent a relationship for the user blocking a domain" do
239 user = insert(:user)
240 other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
241
242 {:ok, user} = User.block_domain(user, "bad.site")
243
244 assert %{domain_blocking: true, blocking: false} =
245 AccountView.render("relationship.json", %{user: user, target: other_user})
246 end
247
248 test "represent a relationship for the user with a pending follow request" do
249 user = insert(:user)
250 other_user = insert(:user, locked: true)
251
252 {:ok, user, other_user, _} = CommonAPI.follow(user, other_user)
253 user = User.get_cached_by_id(user.id)
254 other_user = User.get_cached_by_id(other_user.id)
255
256 expected = %{
257 id: to_string(other_user.id),
258 following: false,
259 followed_by: false,
260 blocking: false,
261 blocked_by: false,
262 muting: false,
263 muting_notifications: false,
264 subscribing: false,
265 requested: true,
266 domain_blocking: false,
267 showing_reblogs: true,
268 endorsed: false
269 }
270
271 assert expected ==
272 AccountView.render("relationship.json", %{user: user, target: other_user})
273 end
274 end
275
276 test "represent an embedded relationship" do
277 user =
278 insert(:user, %{
279 follower_count: 0,
280 note_count: 5,
281 source_data: %{"type" => "Service"},
282 nickname: "shp@shitposter.club",
283 inserted_at: ~N[2017-08-15 15:47:06.597036]
284 })
285
286 other_user = insert(:user)
287 {:ok, other_user} = User.follow(other_user, user)
288 {:ok, other_user} = User.block(other_user, user)
289 {:ok, _} = User.follow(insert(:user), user)
290
291 expected = %{
292 id: to_string(user.id),
293 username: "shp",
294 acct: user.nickname,
295 display_name: user.name,
296 locked: false,
297 created_at: "2017-08-15T15:47:06.000Z",
298 followers_count: 1,
299 following_count: 0,
300 statuses_count: 5,
301 note: user.bio,
302 url: user.ap_id,
303 avatar: "http://localhost:4001/images/avi.png",
304 avatar_static: "http://localhost:4001/images/avi.png",
305 header: "http://localhost:4001/images/banner.png",
306 header_static: "http://localhost:4001/images/banner.png",
307 emojis: [],
308 fields: [],
309 bot: true,
310 source: %{
311 note: user.bio,
312 sensitive: false,
313 pleroma: %{
314 discoverable: false
315 },
316 fields: []
317 },
318 pleroma: %{
319 background_image: nil,
320 confirmation_pending: false,
321 tags: [],
322 is_admin: false,
323 is_moderator: false,
324 hide_favorites: true,
325 hide_followers: false,
326 hide_follows: false,
327 hide_followers_count: false,
328 hide_follows_count: false,
329 relationship: %{
330 id: to_string(user.id),
331 following: false,
332 followed_by: false,
333 blocking: true,
334 blocked_by: false,
335 subscribing: false,
336 muting: false,
337 muting_notifications: false,
338 requested: false,
339 domain_blocking: false,
340 showing_reblogs: true,
341 endorsed: false
342 },
343 skip_thread_containment: false
344 }
345 }
346
347 assert expected ==
348 AccountView.render("show.json", %{user: refresh_record(user), for: other_user})
349 end
350
351 test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
352 user = insert(:user, pleroma_settings_store: %{fe: "test"})
353
354 result =
355 AccountView.render("show.json", %{user: user, for: user, with_pleroma_settings: true})
356
357 assert result.pleroma.settings_store == %{:fe => "test"}
358
359 result = AccountView.render("show.json", %{user: user, with_pleroma_settings: true})
360 assert result.pleroma[:settings_store] == nil
361
362 result = AccountView.render("show.json", %{user: user, for: user})
363 assert result.pleroma[:settings_store] == nil
364 end
365
366 test "sanitizes display names" do
367 user = insert(:user, name: "<marquee> username </marquee>")
368 result = AccountView.render("show.json", %{user: user})
369 refute result.display_name == "<marquee> username </marquee>"
370 end
371
372 test "never display nil user follow counts" do
373 user = insert(:user, following_count: 0, follower_count: 0)
374 result = AccountView.render("show.json", %{user: user})
375
376 assert result.following_count == 0
377 assert result.followers_count == 0
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