]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/users/users.ts
Fix my videos counter
[github/Chocobozzz/PeerTube.git] / server / tests / api / users / users.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import { testImage } from '@server/tests/shared'
6 import { AbuseState, HttpStatusCode, OAuth2ErrorCode, UserAdminFlag, UserRole, VideoPlaylistType } from '@shared/models'
7 import {
8 cleanupTests,
9 createSingleServer,
10 killallServers,
11 makePutBodyRequest,
12 PeerTubeServer,
13 setAccessTokensToServers
14 } from '@shared/server-commands'
15
16 const expect = chai.expect
17
18 describe('Test users', function () {
19 let server: PeerTubeServer
20 let token: string
21 let userToken: string
22 let videoId: number
23 let userId: number
24 const user = {
25 username: 'user_1',
26 password: 'super password'
27 }
28
29 before(async function () {
30 this.timeout(30000)
31
32 server = await createSingleServer(1, {
33 rates_limit: {
34 login: {
35 max: 30
36 }
37 }
38 })
39
40 await setAccessTokensToServers([ server ])
41
42 await server.plugins.install({ npmName: 'peertube-theme-background-red' })
43 })
44
45 describe('OAuth client', function () {
46 it('Should create a new client')
47
48 it('Should return the first client')
49
50 it('Should remove the last client')
51
52 it('Should not login with an invalid client id', async function () {
53 const client = { id: 'client', secret: server.store.client.secret }
54 const body = await server.login.login({ client, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
55
56 expect(body.code).to.equal(OAuth2ErrorCode.INVALID_CLIENT)
57 expect(body.error).to.contain('client is invalid')
58 expect(body.type.startsWith('https://')).to.be.true
59 expect(body.type).to.contain(OAuth2ErrorCode.INVALID_CLIENT)
60 })
61
62 it('Should not login with an invalid client secret', async function () {
63 const client = { id: server.store.client.id, secret: 'coucou' }
64 const body = await server.login.login({ client, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
65
66 expect(body.code).to.equal(OAuth2ErrorCode.INVALID_CLIENT)
67 expect(body.error).to.contain('client is invalid')
68 expect(body.type.startsWith('https://')).to.be.true
69 expect(body.type).to.contain(OAuth2ErrorCode.INVALID_CLIENT)
70 })
71 })
72
73 describe('Login', function () {
74
75 it('Should not login with an invalid username', async function () {
76 const user = { username: 'captain crochet', password: server.store.user.password }
77 const body = await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
78
79 expect(body.code).to.equal(OAuth2ErrorCode.INVALID_GRANT)
80 expect(body.error).to.contain('credentials are invalid')
81 expect(body.type.startsWith('https://')).to.be.true
82 expect(body.type).to.contain(OAuth2ErrorCode.INVALID_GRANT)
83 })
84
85 it('Should not login with an invalid password', async function () {
86 const user = { username: server.store.user.username, password: 'mew_three' }
87 const body = await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
88
89 expect(body.code).to.equal(OAuth2ErrorCode.INVALID_GRANT)
90 expect(body.error).to.contain('credentials are invalid')
91 expect(body.type.startsWith('https://')).to.be.true
92 expect(body.type).to.contain(OAuth2ErrorCode.INVALID_GRANT)
93 })
94
95 it('Should not be able to upload a video', async function () {
96 token = 'my_super_token'
97
98 await server.videos.upload({ token, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
99 })
100
101 it('Should not be able to follow', async function () {
102 token = 'my_super_token'
103
104 await server.follows.follow({
105 hosts: [ 'http://example.com' ],
106 token,
107 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
108 })
109 })
110
111 it('Should not be able to unfollow')
112
113 it('Should be able to login', async function () {
114 const body = await server.login.login({ expectedStatus: HttpStatusCode.OK_200 })
115
116 token = body.access_token
117 })
118
119 it('Should be able to login with an insensitive username', async function () {
120 const user = { username: 'RoOt', password: server.store.user.password }
121 await server.login.login({ user, expectedStatus: HttpStatusCode.OK_200 })
122
123 const user2 = { username: 'rOoT', password: server.store.user.password }
124 await server.login.login({ user: user2, expectedStatus: HttpStatusCode.OK_200 })
125
126 const user3 = { username: 'ROOt', password: server.store.user.password }
127 await server.login.login({ user: user3, expectedStatus: HttpStatusCode.OK_200 })
128 })
129 })
130
131 describe('Logout', function () {
132 it('Should logout (revoke token)', async function () {
133 await server.login.logout({ token: server.accessToken })
134 })
135
136 it('Should not be able to get the user information', async function () {
137 await server.users.getMyInfo({ expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
138 })
139
140 it('Should not be able to upload a video', async function () {
141 await server.videos.upload({ attributes: { name: 'video' }, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
142 })
143
144 it('Should not be able to rate a video', async function () {
145 const path = '/api/v1/videos/'
146 const data = {
147 rating: 'likes'
148 }
149
150 const options = {
151 url: server.url,
152 path: path + videoId,
153 token: 'wrong token',
154 fields: data,
155 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
156 }
157 await makePutBodyRequest(options)
158 })
159
160 it('Should be able to login again', async function () {
161 const body = await server.login.login()
162 server.accessToken = body.access_token
163 server.refreshToken = body.refresh_token
164 })
165
166 it('Should be able to get my user information again', async function () {
167 await server.users.getMyInfo()
168 })
169
170 it('Should have an expired access token', async function () {
171 this.timeout(60000)
172
173 await server.sql.setTokenField(server.accessToken, 'accessTokenExpiresAt', new Date().toISOString())
174 await server.sql.setTokenField(server.accessToken, 'refreshTokenExpiresAt', new Date().toISOString())
175
176 await killallServers([ server ])
177 await server.run()
178
179 await server.users.getMyInfo({ expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
180 })
181
182 it('Should not be able to refresh an access token with an expired refresh token', async function () {
183 await server.login.refreshToken({ refreshToken: server.refreshToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
184 })
185
186 it('Should refresh the token', async function () {
187 this.timeout(15000)
188
189 const futureDate = new Date(new Date().getTime() + 1000 * 60).toISOString()
190 await server.sql.setTokenField(server.accessToken, 'refreshTokenExpiresAt', futureDate)
191
192 await killallServers([ server ])
193 await server.run()
194
195 const res = await server.login.refreshToken({ refreshToken: server.refreshToken })
196 server.accessToken = res.body.access_token
197 server.refreshToken = res.body.refresh_token
198 })
199
200 it('Should be able to get my user information again', async function () {
201 await server.users.getMyInfo()
202 })
203 })
204
205 describe('Creating a user', function () {
206
207 it('Should be able to create a new user', async function () {
208 await server.users.create({ ...user, videoQuota: 2 * 1024 * 1024, adminFlags: UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST })
209 })
210
211 it('Should be able to login with this user', async function () {
212 userToken = await server.login.getAccessToken(user)
213 })
214
215 it('Should be able to get user information', async function () {
216 const userMe = await server.users.getMyInfo({ token: userToken })
217
218 const userGet = await server.users.get({ userId: userMe.id, withStats: true })
219
220 for (const user of [ userMe, userGet ]) {
221 expect(user.username).to.equal('user_1')
222 expect(user.email).to.equal('user_1@example.com')
223 expect(user.nsfwPolicy).to.equal('display')
224 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
225 expect(user.roleLabel).to.equal('User')
226 expect(user.id).to.be.a('number')
227 expect(user.account.displayName).to.equal('user_1')
228 expect(user.account.description).to.be.null
229 }
230
231 expect(userMe.adminFlags).to.equal(UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST)
232 expect(userGet.adminFlags).to.equal(UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST)
233
234 expect(userMe.specialPlaylists).to.have.lengthOf(1)
235 expect(userMe.specialPlaylists[0].type).to.equal(VideoPlaylistType.WATCH_LATER)
236
237 // Check stats are included with withStats
238 expect(userGet.videosCount).to.be.a('number')
239 expect(userGet.videosCount).to.equal(0)
240 expect(userGet.videoCommentsCount).to.be.a('number')
241 expect(userGet.videoCommentsCount).to.equal(0)
242 expect(userGet.abusesCount).to.be.a('number')
243 expect(userGet.abusesCount).to.equal(0)
244 expect(userGet.abusesAcceptedCount).to.be.a('number')
245 expect(userGet.abusesAcceptedCount).to.equal(0)
246 })
247 })
248
249 describe('Users listing', function () {
250
251 it('Should list all the users', async function () {
252 const { data, total } = await server.users.list()
253
254 expect(total).to.equal(2)
255 expect(data).to.be.an('array')
256 expect(data.length).to.equal(2)
257
258 const user = data[0]
259 expect(user.username).to.equal('user_1')
260 expect(user.email).to.equal('user_1@example.com')
261 expect(user.nsfwPolicy).to.equal('display')
262
263 const rootUser = data[1]
264 expect(rootUser.username).to.equal('root')
265 expect(rootUser.email).to.equal('admin' + server.internalServerNumber + '@example.com')
266 expect(user.nsfwPolicy).to.equal('display')
267
268 expect(rootUser.lastLoginDate).to.exist
269 expect(user.lastLoginDate).to.exist
270
271 userId = user.id
272 })
273
274 it('Should list only the first user by username asc', async function () {
275 const { total, data } = await server.users.list({ start: 0, count: 1, sort: 'username' })
276
277 expect(total).to.equal(2)
278 expect(data.length).to.equal(1)
279
280 const user = data[0]
281 expect(user.username).to.equal('root')
282 expect(user.email).to.equal('admin' + server.internalServerNumber + '@example.com')
283 expect(user.roleLabel).to.equal('Administrator')
284 expect(user.nsfwPolicy).to.equal('display')
285 })
286
287 it('Should list only the first user by username desc', async function () {
288 const { total, data } = await server.users.list({ start: 0, count: 1, sort: '-username' })
289
290 expect(total).to.equal(2)
291 expect(data.length).to.equal(1)
292
293 const user = data[0]
294 expect(user.username).to.equal('user_1')
295 expect(user.email).to.equal('user_1@example.com')
296 expect(user.nsfwPolicy).to.equal('display')
297 })
298
299 it('Should list only the second user by createdAt desc', async function () {
300 const { data, total } = await server.users.list({ start: 0, count: 1, sort: '-createdAt' })
301 expect(total).to.equal(2)
302
303 expect(data.length).to.equal(1)
304
305 const user = data[0]
306 expect(user.username).to.equal('user_1')
307 expect(user.email).to.equal('user_1@example.com')
308 expect(user.nsfwPolicy).to.equal('display')
309 })
310
311 it('Should list all the users by createdAt asc', async function () {
312 const { data, total } = await server.users.list({ start: 0, count: 2, sort: 'createdAt' })
313
314 expect(total).to.equal(2)
315 expect(data.length).to.equal(2)
316
317 expect(data[0].username).to.equal('root')
318 expect(data[0].email).to.equal('admin' + server.internalServerNumber + '@example.com')
319 expect(data[0].nsfwPolicy).to.equal('display')
320
321 expect(data[1].username).to.equal('user_1')
322 expect(data[1].email).to.equal('user_1@example.com')
323 expect(data[1].nsfwPolicy).to.equal('display')
324 })
325
326 it('Should search user by username', async function () {
327 const { data, total } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', search: 'oot' })
328 expect(total).to.equal(1)
329 expect(data.length).to.equal(1)
330 expect(data[0].username).to.equal('root')
331 })
332
333 it('Should search user by email', async function () {
334 {
335 const { total, data } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', search: 'r_1@exam' })
336 expect(total).to.equal(1)
337 expect(data.length).to.equal(1)
338 expect(data[0].username).to.equal('user_1')
339 expect(data[0].email).to.equal('user_1@example.com')
340 }
341
342 {
343 const { total, data } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', search: 'example' })
344 expect(total).to.equal(2)
345 expect(data.length).to.equal(2)
346 expect(data[0].username).to.equal('root')
347 expect(data[1].username).to.equal('user_1')
348 }
349 })
350 })
351
352 describe('Update my account', function () {
353
354 it('Should update my password', async function () {
355 await server.users.updateMe({
356 token: userToken,
357 currentPassword: 'super password',
358 password: 'new password'
359 })
360 user.password = 'new password'
361
362 await server.login.login({ user })
363 })
364
365 it('Should be able to change the NSFW display attribute', async function () {
366 await server.users.updateMe({
367 token: userToken,
368 nsfwPolicy: 'do_not_list'
369 })
370
371 const user = await server.users.getMyInfo({ token: userToken })
372 expect(user.username).to.equal('user_1')
373 expect(user.email).to.equal('user_1@example.com')
374 expect(user.nsfwPolicy).to.equal('do_not_list')
375 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
376 expect(user.id).to.be.a('number')
377 expect(user.account.displayName).to.equal('user_1')
378 expect(user.account.description).to.be.null
379 })
380
381 it('Should be able to change the autoPlayVideo attribute', async function () {
382 await server.users.updateMe({
383 token: userToken,
384 autoPlayVideo: false
385 })
386
387 const user = await server.users.getMyInfo({ token: userToken })
388 expect(user.autoPlayVideo).to.be.false
389 })
390
391 it('Should be able to change the autoPlayNextVideo attribute', async function () {
392 await server.users.updateMe({
393 token: userToken,
394 autoPlayNextVideo: true
395 })
396
397 const user = await server.users.getMyInfo({ token: userToken })
398 expect(user.autoPlayNextVideo).to.be.true
399 })
400
401 it('Should be able to change the p2p attribute', async function () {
402 {
403 await server.users.updateMe({
404 token: userToken,
405 webTorrentEnabled: false
406 })
407
408 const user = await server.users.getMyInfo({ token: userToken })
409 expect(user.p2pEnabled).to.be.false
410 }
411
412 {
413 await server.users.updateMe({
414 token: userToken,
415 p2pEnabled: true
416 })
417
418 const user = await server.users.getMyInfo({ token: userToken })
419 expect(user.p2pEnabled).to.be.true
420 }
421 })
422
423 it('Should be able to change the email attribute', async function () {
424 await server.users.updateMe({
425 token: userToken,
426 currentPassword: 'new password',
427 email: 'updated@example.com'
428 })
429
430 const user = await server.users.getMyInfo({ token: userToken })
431 expect(user.username).to.equal('user_1')
432 expect(user.email).to.equal('updated@example.com')
433 expect(user.nsfwPolicy).to.equal('do_not_list')
434 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
435 expect(user.id).to.be.a('number')
436 expect(user.account.displayName).to.equal('user_1')
437 expect(user.account.description).to.be.null
438 })
439
440 it('Should be able to update my avatar with a gif', async function () {
441 const fixture = 'avatar.gif'
442
443 await server.users.updateMyAvatar({ token: userToken, fixture })
444
445 const user = await server.users.getMyInfo({ token: userToken })
446 for (const avatar of user.account.avatars) {
447 await testImage(server.url, `avatar-resized-${avatar.width}x${avatar.width}`, avatar.path, '.gif')
448 }
449 })
450
451 it('Should be able to update my avatar with a gif, and then a png', async function () {
452 for (const extension of [ '.png', '.gif' ]) {
453 const fixture = 'avatar' + extension
454
455 await server.users.updateMyAvatar({ token: userToken, fixture })
456
457 const user = await server.users.getMyInfo({ token: userToken })
458 for (const avatar of user.account.avatars) {
459 await testImage(server.url, `avatar-resized-${avatar.width}x${avatar.width}`, avatar.path, extension)
460 }
461 }
462 })
463
464 it('Should be able to update my display name', async function () {
465 await server.users.updateMe({ token: userToken, displayName: 'new display name' })
466
467 const user = await server.users.getMyInfo({ token: userToken })
468 expect(user.username).to.equal('user_1')
469 expect(user.email).to.equal('updated@example.com')
470 expect(user.nsfwPolicy).to.equal('do_not_list')
471 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
472 expect(user.id).to.be.a('number')
473 expect(user.account.displayName).to.equal('new display name')
474 expect(user.account.description).to.be.null
475 })
476
477 it('Should be able to update my description', async function () {
478 await server.users.updateMe({ token: userToken, description: 'my super description updated' })
479
480 const user = await server.users.getMyInfo({ token: userToken })
481 expect(user.username).to.equal('user_1')
482 expect(user.email).to.equal('updated@example.com')
483 expect(user.nsfwPolicy).to.equal('do_not_list')
484 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
485 expect(user.id).to.be.a('number')
486 expect(user.account.displayName).to.equal('new display name')
487 expect(user.account.description).to.equal('my super description updated')
488 expect(user.noWelcomeModal).to.be.false
489 expect(user.noInstanceConfigWarningModal).to.be.false
490 expect(user.noAccountSetupWarningModal).to.be.false
491 })
492
493 it('Should be able to update my theme', async function () {
494 for (const theme of [ 'background-red', 'default', 'instance-default' ]) {
495 await server.users.updateMe({ token: userToken, theme })
496
497 const user = await server.users.getMyInfo({ token: userToken })
498 expect(user.theme).to.equal(theme)
499 }
500 })
501
502 it('Should be able to update my modal preferences', async function () {
503 await server.users.updateMe({
504 token: userToken,
505 noInstanceConfigWarningModal: true,
506 noWelcomeModal: true,
507 noAccountSetupWarningModal: true
508 })
509
510 const user = await server.users.getMyInfo({ token: userToken })
511 expect(user.noWelcomeModal).to.be.true
512 expect(user.noInstanceConfigWarningModal).to.be.true
513 expect(user.noAccountSetupWarningModal).to.be.true
514 })
515 })
516
517 describe('Updating another user', function () {
518 it('Should be able to update another user', async function () {
519 await server.users.update({
520 userId,
521 token,
522 email: 'updated2@example.com',
523 emailVerified: true,
524 videoQuota: 42,
525 role: UserRole.MODERATOR,
526 adminFlags: UserAdminFlag.NONE,
527 pluginAuth: 'toto'
528 })
529
530 const user = await server.users.get({ token, userId })
531
532 expect(user.username).to.equal('user_1')
533 expect(user.email).to.equal('updated2@example.com')
534 expect(user.emailVerified).to.be.true
535 expect(user.nsfwPolicy).to.equal('do_not_list')
536 expect(user.videoQuota).to.equal(42)
537 expect(user.roleLabel).to.equal('Moderator')
538 expect(user.id).to.be.a('number')
539 expect(user.adminFlags).to.equal(UserAdminFlag.NONE)
540 expect(user.pluginAuth).to.equal('toto')
541 })
542
543 it('Should reset the auth plugin', async function () {
544 await server.users.update({ userId, token, pluginAuth: null })
545
546 const user = await server.users.get({ token, userId })
547 expect(user.pluginAuth).to.be.null
548 })
549
550 it('Should have removed the user token', async function () {
551 await server.users.getMyQuotaUsed({ token: userToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
552
553 userToken = await server.login.getAccessToken(user)
554 })
555
556 it('Should be able to update another user password', async function () {
557 await server.users.update({ userId, token, password: 'password updated' })
558
559 await server.users.getMyQuotaUsed({ token: userToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
560
561 await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
562
563 user.password = 'password updated'
564 userToken = await server.login.getAccessToken(user)
565 })
566 })
567
568 describe('Video blacklists', function () {
569
570 it('Should be able to list my video blacklist', async function () {
571 await server.blacklist.list({ token: userToken })
572 })
573 })
574
575 describe('Remove a user', function () {
576
577 before(async function () {
578 await server.users.update({
579 userId,
580 token,
581 videoQuota: 2 * 1024 * 1024
582 })
583
584 await server.videos.quickUpload({ name: 'user video', token: userToken, fixture: 'video_short.webm' })
585 await server.videos.quickUpload({ name: 'root video' })
586
587 const { total } = await server.videos.list()
588 expect(total).to.equal(2)
589 })
590
591 it('Should be able to remove this user', async function () {
592 await server.users.remove({ userId, token })
593 })
594
595 it('Should not be able to login with this user', async function () {
596 await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
597 })
598
599 it('Should not have videos of this user', async function () {
600 const { data, total } = await server.videos.list()
601 expect(total).to.equal(1)
602
603 const video = data[0]
604 expect(video.account.name).to.equal('root')
605 })
606 })
607
608 describe('Registering a new user', function () {
609 let user15AccessToken: string
610
611 it('Should register a new user', async function () {
612 const user = { displayName: 'super user 15', username: 'user_15', password: 'my super password' }
613 const channel = { name: 'my_user_15_channel', displayName: 'my channel rocks' }
614
615 await server.users.register({ ...user, channel })
616 })
617
618 it('Should be able to login with this registered user', async function () {
619 const user15 = {
620 username: 'user_15',
621 password: 'my super password'
622 }
623
624 user15AccessToken = await server.login.getAccessToken(user15)
625 })
626
627 it('Should have the correct display name', async function () {
628 const user = await server.users.getMyInfo({ token: user15AccessToken })
629 expect(user.account.displayName).to.equal('super user 15')
630 })
631
632 it('Should have the correct video quota', async function () {
633 const user = await server.users.getMyInfo({ token: user15AccessToken })
634 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
635 })
636
637 it('Should have created the channel', async function () {
638 const { displayName } = await server.channels.get({ channelName: 'my_user_15_channel' })
639
640 expect(displayName).to.equal('my channel rocks')
641 })
642
643 it('Should remove me', async function () {
644 {
645 const { data } = await server.users.list()
646 expect(data.find(u => u.username === 'user_15')).to.not.be.undefined
647 }
648
649 await server.users.deleteMe({ token: user15AccessToken })
650
651 {
652 const { data } = await server.users.list()
653 expect(data.find(u => u.username === 'user_15')).to.be.undefined
654 }
655 })
656 })
657
658 describe('User blocking', function () {
659 let user16Id
660 let user16AccessToken
661 const user16 = {
662 username: 'user_16',
663 password: 'my super password'
664 }
665
666 it('Should block a user', async function () {
667 const user = await server.users.create({ ...user16 })
668 user16Id = user.id
669
670 user16AccessToken = await server.login.getAccessToken(user16)
671
672 await server.users.getMyInfo({ token: user16AccessToken, expectedStatus: HttpStatusCode.OK_200 })
673 await server.users.banUser({ userId: user16Id })
674
675 await server.users.getMyInfo({ token: user16AccessToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
676 await server.login.login({ user: user16, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
677 })
678
679 it('Should search user by banned status', async function () {
680 {
681 const { data, total } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', blocked: true })
682 expect(total).to.equal(1)
683 expect(data.length).to.equal(1)
684
685 expect(data[0].username).to.equal(user16.username)
686 }
687
688 {
689 const { data, total } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', blocked: false })
690 expect(total).to.equal(1)
691 expect(data.length).to.equal(1)
692
693 expect(data[0].username).to.not.equal(user16.username)
694 }
695 })
696
697 it('Should unblock a user', async function () {
698 await server.users.unbanUser({ userId: user16Id })
699 user16AccessToken = await server.login.getAccessToken(user16)
700 await server.users.getMyInfo({ token: user16AccessToken, expectedStatus: HttpStatusCode.OK_200 })
701 })
702 })
703
704 describe('User stats', function () {
705 let user17Id: number
706 let user17AccessToken: string
707
708 it('Should report correct initial statistics about a user', async function () {
709 const user17 = {
710 username: 'user_17',
711 password: 'my super password'
712 }
713 const created = await server.users.create({ ...user17 })
714
715 user17Id = created.id
716 user17AccessToken = await server.login.getAccessToken(user17)
717
718 const user = await server.users.get({ userId: user17Id, withStats: true })
719 expect(user.videosCount).to.equal(0)
720 expect(user.videoCommentsCount).to.equal(0)
721 expect(user.abusesCount).to.equal(0)
722 expect(user.abusesCreatedCount).to.equal(0)
723 expect(user.abusesAcceptedCount).to.equal(0)
724 })
725
726 it('Should report correct videos count', async function () {
727 const attributes = { name: 'video to test user stats' }
728 await server.videos.upload({ token: user17AccessToken, attributes })
729
730 const { data } = await server.videos.list()
731 videoId = data.find(video => video.name === attributes.name).id
732
733 const user = await server.users.get({ userId: user17Id, withStats: true })
734 expect(user.videosCount).to.equal(1)
735 })
736
737 it('Should report correct video comments for user', async function () {
738 const text = 'super comment'
739 await server.comments.createThread({ token: user17AccessToken, videoId, text })
740
741 const user = await server.users.get({ userId: user17Id, withStats: true })
742 expect(user.videoCommentsCount).to.equal(1)
743 })
744
745 it('Should report correct abuses counts', async function () {
746 const reason = 'my super bad reason'
747 await server.abuses.report({ token: user17AccessToken, videoId, reason })
748
749 const body1 = await server.abuses.getAdminList()
750 const abuseId = body1.data[0].id
751
752 const user2 = await server.users.get({ userId: user17Id, withStats: true })
753 expect(user2.abusesCount).to.equal(1) // number of incriminations
754 expect(user2.abusesCreatedCount).to.equal(1) // number of reports created
755
756 await server.abuses.update({ abuseId, body: { state: AbuseState.ACCEPTED } })
757
758 const user3 = await server.users.get({ userId: user17Id, withStats: true })
759 expect(user3.abusesAcceptedCount).to.equal(1) // number of reports created accepted
760 })
761 })
762
763 after(async function () {
764 await cleanupTests([ server ])
765 })
766 })