1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { HttpStatusCode } from '@shared/core-utils'
13 setAccessTokensToServers,
16 } from '@shared/extra-utils'
17 import { AbuseState, OAuth2ErrorCode, UserAdminFlag, UserRole, Video, VideoPlaylistType } from '@shared/models'
19 const expect = chai.expect
21 describe('Test users', function () {
22 let server: ServerInfo
29 password: 'super password'
32 before(async function () {
35 server = await flushAndRunServer(1, {
43 await setAccessTokensToServers([ server ])
45 await server.plugins.install({ npmName: 'peertube-theme-background-red' })
48 describe('OAuth client', function () {
49 it('Should create a new client')
51 it('Should return the first client')
53 it('Should remove the last client')
55 it('Should not login with an invalid client id', async function () {
56 const client = { id: 'client', secret: server.store.client.secret }
57 const body = await server.login.login({ client, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
59 expect(body.code).to.equal(OAuth2ErrorCode.INVALID_CLIENT)
60 expect(body.error).to.contain('client is invalid')
61 expect(body.type.startsWith('https://')).to.be.true
62 expect(body.type).to.contain(OAuth2ErrorCode.INVALID_CLIENT)
65 it('Should not login with an invalid client secret', async function () {
66 const client = { id: server.store.client.id, secret: 'coucou' }
67 const body = await server.login.login({ client, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
69 expect(body.code).to.equal(OAuth2ErrorCode.INVALID_CLIENT)
70 expect(body.error).to.contain('client is invalid')
71 expect(body.type.startsWith('https://')).to.be.true
72 expect(body.type).to.contain(OAuth2ErrorCode.INVALID_CLIENT)
76 describe('Login', function () {
78 it('Should not login with an invalid username', async function () {
79 const user = { username: 'captain crochet', password: server.store.user.password }
80 const body = await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
82 expect(body.code).to.equal(OAuth2ErrorCode.INVALID_GRANT)
83 expect(body.error).to.contain('credentials are invalid')
84 expect(body.type.startsWith('https://')).to.be.true
85 expect(body.type).to.contain(OAuth2ErrorCode.INVALID_GRANT)
88 it('Should not login with an invalid password', async function () {
89 const user = { username: server.store.user.username, password: 'mew_three' }
90 const body = await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
92 expect(body.code).to.equal(OAuth2ErrorCode.INVALID_GRANT)
93 expect(body.error).to.contain('credentials are invalid')
94 expect(body.type.startsWith('https://')).to.be.true
95 expect(body.type).to.contain(OAuth2ErrorCode.INVALID_GRANT)
98 it('Should not be able to upload a video', async function () {
99 token = 'my_super_token'
101 await server.videos.upload({ token, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
104 it('Should not be able to follow', async function () {
105 token = 'my_super_token'
107 await server.follows.follow({
108 targets: [ 'http://example.com' ],
110 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
114 it('Should not be able to unfollow')
116 it('Should be able to login', async function () {
117 const body = await server.login.login({ expectedStatus: HttpStatusCode.OK_200 })
119 token = body.access_token
122 it('Should be able to login with an insensitive username', async function () {
123 const user = { username: 'RoOt', password: server.store.user.password }
124 await server.login.login({ user, expectedStatus: HttpStatusCode.OK_200 })
126 const user2 = { username: 'rOoT', password: server.store.user.password }
127 await server.login.login({ user: user2, expectedStatus: HttpStatusCode.OK_200 })
129 const user3 = { username: 'ROOt', password: server.store.user.password }
130 await server.login.login({ user: user3, expectedStatus: HttpStatusCode.OK_200 })
134 describe('Upload', function () {
136 it('Should upload the video with the correct token', async function () {
137 await server.videos.upload({ token })
138 const { data } = await server.videos.list()
139 const video = data[0]
141 expect(video.account.name).to.equal('root')
145 it('Should upload the video again with the correct token', async function () {
146 await server.videos.upload({ token })
150 describe('Ratings', function () {
152 it('Should retrieve a video rating', async function () {
153 await server.videos.rate({ id: videoId, rating: 'like' })
154 const rating = await server.users.getMyRating({ token, videoId })
156 expect(rating.videoId).to.equal(videoId)
157 expect(rating.rating).to.equal('like')
160 it('Should retrieve ratings list', async function () {
161 await server.videos.rate({ id: videoId, rating: 'like' })
163 const body = await server.accounts.listRatings({ accountName: server.store.user.username })
165 expect(body.total).to.equal(1)
166 expect(body.data[0].video.id).to.equal(videoId)
167 expect(body.data[0].rating).to.equal('like')
170 it('Should retrieve ratings list by rating type', async function () {
172 const body = await server.accounts.listRatings({ accountName: server.store.user.username, rating: 'like' })
173 expect(body.data.length).to.equal(1)
177 const body = await server.accounts.listRatings({ accountName: server.store.user.username, rating: 'dislike' })
178 expect(body.data.length).to.equal(0)
183 describe('Remove video', function () {
184 it('Should not be able to remove the video with an incorrect token', async function () {
185 await server.videos.remove({ token: 'bad_token', id: videoId, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
188 it('Should not be able to remove the video with the token of another account')
190 it('Should be able to remove the video with the correct token', async function () {
191 await server.videos.remove({ token, id: videoId })
195 describe('Logout', function () {
196 it('Should logout (revoke token)', async function () {
197 await server.login.logout({ token: server.accessToken })
200 it('Should not be able to get the user information', async function () {
201 await server.users.getMyInfo({ expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
204 it('Should not be able to upload a video', async function () {
205 await server.videos.upload({ attributes: { name: 'video' }, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
208 it('Should not be able to rate a video', async function () {
209 const path = '/api/v1/videos/'
216 path: path + videoId,
217 token: 'wrong token',
219 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
221 await makePutBodyRequest(options)
224 it('Should be able to login again', async function () {
225 const body = await server.login.login()
226 server.accessToken = body.access_token
227 server.refreshToken = body.refresh_token
230 it('Should be able to get my user information again', async function () {
231 await server.users.getMyInfo()
234 it('Should have an expired access token', async function () {
237 await server.sql.setTokenField(server.accessToken, 'accessTokenExpiresAt', new Date().toISOString())
238 await server.sql.setTokenField(server.accessToken, 'refreshTokenExpiresAt', new Date().toISOString())
240 await killallServers([ server ])
241 await reRunServer(server)
243 await server.users.getMyInfo({ expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
246 it('Should not be able to refresh an access token with an expired refresh token', async function () {
247 await server.login.refreshToken({ refreshToken: server.refreshToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
250 it('Should refresh the token', async function () {
253 const futureDate = new Date(new Date().getTime() + 1000 * 60).toISOString()
254 await server.sql.setTokenField(server.accessToken, 'refreshTokenExpiresAt', futureDate)
256 await killallServers([ server ])
257 await reRunServer(server)
259 const res = await server.login.refreshToken({ refreshToken: server.refreshToken })
260 server.accessToken = res.body.access_token
261 server.refreshToken = res.body.refresh_token
264 it('Should be able to get my user information again', async function () {
265 await server.users.getMyInfo()
269 describe('Creating a user', function () {
271 it('Should be able to create a new user', async function () {
272 await server.users.create({ ...user, videoQuota: 2 * 1024 * 1024, adminFlags: UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST })
275 it('Should be able to login with this user', async function () {
276 userToken = await server.login.getAccessToken(user)
279 it('Should be able to get user information', async function () {
280 const userMe = await server.users.getMyInfo({ token: userToken })
282 const userGet = await server.users.get({ userId: userMe.id, withStats: true })
284 for (const user of [ userMe, userGet ]) {
285 expect(user.username).to.equal('user_1')
286 expect(user.email).to.equal('user_1@example.com')
287 expect(user.nsfwPolicy).to.equal('display')
288 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
289 expect(user.roleLabel).to.equal('User')
290 expect(user.id).to.be.a('number')
291 expect(user.account.displayName).to.equal('user_1')
292 expect(user.account.description).to.be.null
295 expect(userMe.adminFlags).to.be.undefined
296 expect(userGet.adminFlags).to.equal(UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST)
298 expect(userMe.specialPlaylists).to.have.lengthOf(1)
299 expect(userMe.specialPlaylists[0].type).to.equal(VideoPlaylistType.WATCH_LATER)
301 // Check stats are included with withStats
302 expect(userGet.videosCount).to.be.a('number')
303 expect(userGet.videosCount).to.equal(0)
304 expect(userGet.videoCommentsCount).to.be.a('number')
305 expect(userGet.videoCommentsCount).to.equal(0)
306 expect(userGet.abusesCount).to.be.a('number')
307 expect(userGet.abusesCount).to.equal(0)
308 expect(userGet.abusesAcceptedCount).to.be.a('number')
309 expect(userGet.abusesAcceptedCount).to.equal(0)
313 describe('My videos & quotas', function () {
315 it('Should be able to upload a video with this user', async function () {
319 name: 'super user video',
320 fixture: 'video_short.webm'
322 await server.videos.upload({ token: userToken, attributes })
325 it('Should have video quota updated', async function () {
326 const quota = await server.users.getMyQuotaUsed({ token: userToken })
327 expect(quota.videoQuotaUsed).to.equal(218910)
329 const { data } = await server.users.list()
330 const tmpUser = data.find(u => u.username === user.username)
331 expect(tmpUser.videoQuotaUsed).to.equal(218910)
334 it('Should be able to list my videos', async function () {
335 const { total, data } = await server.videos.listMyVideos({ token: userToken })
336 expect(total).to.equal(1)
337 expect(data).to.have.lengthOf(1)
339 const video: Video = data[0]
340 expect(video.name).to.equal('super user video')
341 expect(video.thumbnailPath).to.not.be.null
342 expect(video.previewPath).to.not.be.null
345 it('Should be able to search in my videos', async function () {
347 const { total, data } = await server.videos.listMyVideos({ token: userToken, sort: '-createdAt', search: 'user video' })
348 expect(total).to.equal(1)
349 expect(data).to.have.lengthOf(1)
353 const { total, data } = await server.videos.listMyVideos({ token: userToken, sort: '-createdAt', search: 'toto' })
354 expect(total).to.equal(0)
355 expect(data).to.have.lengthOf(0)
359 it('Should disable webtorrent, enable HLS, and update my quota', async function () {
363 const config = await server.config.getCustomConfig()
364 config.transcoding.webtorrent.enabled = false
365 config.transcoding.hls.enabled = true
366 config.transcoding.enabled = true
367 await server.config.updateCustomSubConfig({ newConfig: config })
372 name: 'super user video 2',
373 fixture: 'video_short.webm'
375 await server.videos.upload({ token: userToken, attributes })
377 await waitJobs([ server ])
381 const data = await server.users.getMyQuotaUsed({ token: userToken })
382 expect(data.videoQuotaUsed).to.be.greaterThan(220000)
387 describe('Users listing', function () {
389 it('Should list all the users', async function () {
390 const { data, total } = await server.users.list()
392 expect(total).to.equal(2)
393 expect(data).to.be.an('array')
394 expect(data.length).to.equal(2)
397 expect(user.username).to.equal('user_1')
398 expect(user.email).to.equal('user_1@example.com')
399 expect(user.nsfwPolicy).to.equal('display')
401 const rootUser = data[1]
402 expect(rootUser.username).to.equal('root')
403 expect(rootUser.email).to.equal('admin' + server.internalServerNumber + '@example.com')
404 expect(user.nsfwPolicy).to.equal('display')
406 expect(rootUser.lastLoginDate).to.exist
407 expect(user.lastLoginDate).to.exist
412 it('Should list only the first user by username asc', async function () {
413 const { total, data } = await server.users.list({ start: 0, count: 1, sort: 'username' })
415 expect(total).to.equal(2)
416 expect(data.length).to.equal(1)
419 expect(user.username).to.equal('root')
420 expect(user.email).to.equal('admin' + server.internalServerNumber + '@example.com')
421 expect(user.roleLabel).to.equal('Administrator')
422 expect(user.nsfwPolicy).to.equal('display')
425 it('Should list only the first user by username desc', async function () {
426 const { total, data } = await server.users.list({ start: 0, count: 1, sort: '-username' })
428 expect(total).to.equal(2)
429 expect(data.length).to.equal(1)
432 expect(user.username).to.equal('user_1')
433 expect(user.email).to.equal('user_1@example.com')
434 expect(user.nsfwPolicy).to.equal('display')
437 it('Should list only the second user by createdAt desc', async function () {
438 const { data, total } = await server.users.list({ start: 0, count: 1, sort: '-createdAt' })
439 expect(total).to.equal(2)
441 expect(data.length).to.equal(1)
444 expect(user.username).to.equal('user_1')
445 expect(user.email).to.equal('user_1@example.com')
446 expect(user.nsfwPolicy).to.equal('display')
449 it('Should list all the users by createdAt asc', async function () {
450 const { data, total } = await server.users.list({ start: 0, count: 2, sort: 'createdAt' })
452 expect(total).to.equal(2)
453 expect(data.length).to.equal(2)
455 expect(data[0].username).to.equal('root')
456 expect(data[0].email).to.equal('admin' + server.internalServerNumber + '@example.com')
457 expect(data[0].nsfwPolicy).to.equal('display')
459 expect(data[1].username).to.equal('user_1')
460 expect(data[1].email).to.equal('user_1@example.com')
461 expect(data[1].nsfwPolicy).to.equal('display')
464 it('Should search user by username', async function () {
465 const { data, total } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', search: 'oot' })
466 expect(total).to.equal(1)
467 expect(data.length).to.equal(1)
468 expect(data[0].username).to.equal('root')
471 it('Should search user by email', async function () {
473 const { total, data } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', search: 'r_1@exam' })
474 expect(total).to.equal(1)
475 expect(data.length).to.equal(1)
476 expect(data[0].username).to.equal('user_1')
477 expect(data[0].email).to.equal('user_1@example.com')
481 const { total, data } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', search: 'example' })
482 expect(total).to.equal(2)
483 expect(data.length).to.equal(2)
484 expect(data[0].username).to.equal('root')
485 expect(data[1].username).to.equal('user_1')
490 describe('Update my account', function () {
492 it('Should update my password', async function () {
493 await server.users.updateMe({
495 currentPassword: 'super password',
496 password: 'new password'
498 user.password = 'new password'
500 await server.login.login({ user })
503 it('Should be able to change the NSFW display attribute', async function () {
504 await server.users.updateMe({
506 nsfwPolicy: 'do_not_list'
509 const user = await server.users.getMyInfo({ token: userToken })
510 expect(user.username).to.equal('user_1')
511 expect(user.email).to.equal('user_1@example.com')
512 expect(user.nsfwPolicy).to.equal('do_not_list')
513 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
514 expect(user.id).to.be.a('number')
515 expect(user.account.displayName).to.equal('user_1')
516 expect(user.account.description).to.be.null
519 it('Should be able to change the autoPlayVideo attribute', async function () {
520 await server.users.updateMe({
525 const user = await server.users.getMyInfo({ token: userToken })
526 expect(user.autoPlayVideo).to.be.false
529 it('Should be able to change the autoPlayNextVideo attribute', async function () {
530 await server.users.updateMe({
532 autoPlayNextVideo: true
535 const user = await server.users.getMyInfo({ token: userToken })
536 expect(user.autoPlayNextVideo).to.be.true
539 it('Should be able to change the email attribute', async function () {
540 await server.users.updateMe({
542 currentPassword: 'new password',
543 email: 'updated@example.com'
546 const user = await server.users.getMyInfo({ token: userToken })
547 expect(user.username).to.equal('user_1')
548 expect(user.email).to.equal('updated@example.com')
549 expect(user.nsfwPolicy).to.equal('do_not_list')
550 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
551 expect(user.id).to.be.a('number')
552 expect(user.account.displayName).to.equal('user_1')
553 expect(user.account.description).to.be.null
556 it('Should be able to update my avatar with a gif', async function () {
557 const fixture = 'avatar.gif'
559 await server.users.updateMyAvatar({ token: userToken, fixture })
561 const user = await server.users.getMyInfo({ token: userToken })
562 await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.gif')
565 it('Should be able to update my avatar with a gif, and then a png', async function () {
566 for (const extension of [ '.png', '.gif' ]) {
567 const fixture = 'avatar' + extension
569 await server.users.updateMyAvatar({ token: userToken, fixture })
571 const user = await server.users.getMyInfo({ token: userToken })
572 await testImage(server.url, 'avatar-resized', user.account.avatar.path, extension)
576 it('Should be able to update my display name', async function () {
577 await server.users.updateMe({ token: userToken, displayName: 'new display name' })
579 const user = await server.users.getMyInfo({ token: userToken })
580 expect(user.username).to.equal('user_1')
581 expect(user.email).to.equal('updated@example.com')
582 expect(user.nsfwPolicy).to.equal('do_not_list')
583 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
584 expect(user.id).to.be.a('number')
585 expect(user.account.displayName).to.equal('new display name')
586 expect(user.account.description).to.be.null
589 it('Should be able to update my description', async function () {
590 await server.users.updateMe({ token: userToken, description: 'my super description updated' })
592 const user = await server.users.getMyInfo({ token: userToken })
593 expect(user.username).to.equal('user_1')
594 expect(user.email).to.equal('updated@example.com')
595 expect(user.nsfwPolicy).to.equal('do_not_list')
596 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
597 expect(user.id).to.be.a('number')
598 expect(user.account.displayName).to.equal('new display name')
599 expect(user.account.description).to.equal('my super description updated')
600 expect(user.noWelcomeModal).to.be.false
601 expect(user.noInstanceConfigWarningModal).to.be.false
604 it('Should be able to update my theme', async function () {
605 for (const theme of [ 'background-red', 'default', 'instance-default' ]) {
606 await server.users.updateMe({ token: userToken, theme })
608 const user = await server.users.getMyInfo({ token: userToken })
609 expect(user.theme).to.equal(theme)
613 it('Should be able to update my modal preferences', async function () {
614 await server.users.updateMe({
616 noInstanceConfigWarningModal: true,
620 const user = await server.users.getMyInfo({ token: userToken })
621 expect(user.noWelcomeModal).to.be.true
622 expect(user.noInstanceConfigWarningModal).to.be.true
626 describe('Updating another user', function () {
627 it('Should be able to update another user', async function () {
628 await server.users.update({
631 email: 'updated2@example.com',
634 role: UserRole.MODERATOR,
635 adminFlags: UserAdminFlag.NONE,
639 const user = await server.users.get({ token, userId })
641 expect(user.username).to.equal('user_1')
642 expect(user.email).to.equal('updated2@example.com')
643 expect(user.emailVerified).to.be.true
644 expect(user.nsfwPolicy).to.equal('do_not_list')
645 expect(user.videoQuota).to.equal(42)
646 expect(user.roleLabel).to.equal('Moderator')
647 expect(user.id).to.be.a('number')
648 expect(user.adminFlags).to.equal(UserAdminFlag.NONE)
649 expect(user.pluginAuth).to.equal('toto')
652 it('Should reset the auth plugin', async function () {
653 await server.users.update({ userId, token, pluginAuth: null })
655 const user = await server.users.get({ token, userId })
656 expect(user.pluginAuth).to.be.null
659 it('Should have removed the user token', async function () {
660 await server.users.getMyQuotaUsed({ token: userToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
662 userToken = await server.login.getAccessToken(user)
665 it('Should be able to update another user password', async function () {
666 await server.users.update({ userId, token, password: 'password updated' })
668 await server.users.getMyQuotaUsed({ token: userToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
670 await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
672 user.password = 'password updated'
673 userToken = await server.login.getAccessToken(user)
677 describe('Video blacklists', function () {
678 it('Should be able to list video blacklist by a moderator', async function () {
679 await server.blacklist.list({ token: userToken })
683 describe('Remove a user', function () {
684 it('Should be able to remove this user', async function () {
685 await server.users.remove({ userId, token })
688 it('Should not be able to login with this user', async function () {
689 await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
692 it('Should not have videos of this user', async function () {
693 const { data, total } = await server.videos.list()
694 expect(total).to.equal(1)
696 const video = data[0]
697 expect(video.account.name).to.equal('root')
701 describe('Registering a new user', function () {
702 let user15AccessToken
704 it('Should register a new user', async function () {
705 const user = { displayName: 'super user 15', username: 'user_15', password: 'my super password' }
706 const channel = { name: 'my_user_15_channel', displayName: 'my channel rocks' }
708 await server.users.register({ ...user, channel })
711 it('Should be able to login with this registered user', async function () {
714 password: 'my super password'
717 user15AccessToken = await server.login.getAccessToken(user15)
720 it('Should have the correct display name', async function () {
721 const user = await server.users.getMyInfo({ token: user15AccessToken })
722 expect(user.account.displayName).to.equal('super user 15')
725 it('Should have the correct video quota', async function () {
726 const user = await server.users.getMyInfo({ token: user15AccessToken })
727 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
730 it('Should have created the channel', async function () {
731 const { displayName } = await server.channels.get({ channelName: 'my_user_15_channel' })
733 expect(displayName).to.equal('my channel rocks')
736 it('Should remove me', async function () {
738 const { data } = await server.users.list()
739 expect(data.find(u => u.username === 'user_15')).to.not.be.undefined
742 await server.users.deleteMe({ token: user15AccessToken })
745 const { data } = await server.users.list()
746 expect(data.find(u => u.username === 'user_15')).to.be.undefined
751 describe('User blocking', function () {
753 let user16AccessToken
756 password: 'my super password'
759 it('Should block a user', async function () {
760 const user = await server.users.create({ ...user16 })
763 user16AccessToken = await server.login.getAccessToken(user16)
765 await server.users.getMyInfo({ token: user16AccessToken, expectedStatus: HttpStatusCode.OK_200 })
766 await server.users.banUser({ userId: user16Id })
768 await server.users.getMyInfo({ token: user16AccessToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
769 await server.login.login({ user: user16, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
772 it('Should search user by banned status', async function () {
774 const { data, total } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', blocked: true })
775 expect(total).to.equal(1)
776 expect(data.length).to.equal(1)
778 expect(data[0].username).to.equal(user16.username)
782 const { data, total } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', blocked: false })
783 expect(total).to.equal(1)
784 expect(data.length).to.equal(1)
786 expect(data[0].username).to.not.equal(user16.username)
790 it('Should unblock a user', async function () {
791 await server.users.unbanUser({ userId: user16Id })
792 user16AccessToken = await server.login.getAccessToken(user16)
793 await server.users.getMyInfo({ token: user16AccessToken, expectedStatus: HttpStatusCode.OK_200 })
797 describe('User stats', function () {
799 let user17AccessToken
801 it('Should report correct initial statistics about a user', async function () {
804 password: 'my super password'
806 const created = await server.users.create({ ...user17 })
808 user17Id = created.id
809 user17AccessToken = await server.login.getAccessToken(user17)
811 const user = await server.users.get({ userId: user17Id, withStats: true })
812 expect(user.videosCount).to.equal(0)
813 expect(user.videoCommentsCount).to.equal(0)
814 expect(user.abusesCount).to.equal(0)
815 expect(user.abusesCreatedCount).to.equal(0)
816 expect(user.abusesAcceptedCount).to.equal(0)
819 it('Should report correct videos count', async function () {
820 const attributes = { name: 'video to test user stats' }
821 await server.videos.upload({ token: user17AccessToken, attributes })
823 const { data } = await server.videos.list()
824 videoId = data.find(video => video.name === attributes.name).id
826 const user = await server.users.get({ userId: user17Id, withStats: true })
827 expect(user.videosCount).to.equal(1)
830 it('Should report correct video comments for user', async function () {
831 const text = 'super comment'
832 await server.comments.createThread({ token: user17AccessToken, videoId, text })
834 const user = await server.users.get({ userId: user17Id, withStats: true })
835 expect(user.videoCommentsCount).to.equal(1)
838 it('Should report correct abuses counts', async function () {
839 const reason = 'my super bad reason'
840 await server.abuses.report({ token: user17AccessToken, videoId, reason })
842 const body1 = await server.abuses.getAdminList()
843 const abuseId = body1.data[0].id
845 const user2 = await server.users.get({ userId: user17Id, withStats: true })
846 expect(user2.abusesCount).to.equal(1) // number of incriminations
847 expect(user2.abusesCreatedCount).to.equal(1) // number of reports created
849 await server.abuses.update({ abuseId, body: { state: AbuseState.ACCEPTED } })
851 const user3 = await server.users.get({ userId: user17Id, withStats: true })
852 expect(user3.abusesAcceptedCount).to.equal(1) // number of reports created accepted
856 after(async function () {
857 await cleanupTests([ server ])