1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
11 setAccessTokensToServers,
14 } from '@shared/extra-utils'
15 import { AbuseState, HttpStatusCode, OAuth2ErrorCode, UserAdminFlag, UserRole, Video, VideoPlaylistType } from '@shared/models'
17 const expect = chai.expect
19 describe('Test users', function () {
20 let server: PeerTubeServer
27 password: 'super password'
30 before(async function () {
33 server = await createSingleServer(1, {
41 await setAccessTokensToServers([ server ])
43 await server.plugins.install({ npmName: 'peertube-theme-background-red' })
46 describe('OAuth client', function () {
47 it('Should create a new client')
49 it('Should return the first client')
51 it('Should remove the last client')
53 it('Should not login with an invalid client id', async function () {
54 const client = { id: 'client', secret: server.store.client.secret }
55 const body = await server.login.login({ client, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
57 expect(body.code).to.equal(OAuth2ErrorCode.INVALID_CLIENT)
58 expect(body.error).to.contain('client is invalid')
59 expect(body.type.startsWith('https://')).to.be.true
60 expect(body.type).to.contain(OAuth2ErrorCode.INVALID_CLIENT)
63 it('Should not login with an invalid client secret', async function () {
64 const client = { id: server.store.client.id, secret: 'coucou' }
65 const body = await server.login.login({ client, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
67 expect(body.code).to.equal(OAuth2ErrorCode.INVALID_CLIENT)
68 expect(body.error).to.contain('client is invalid')
69 expect(body.type.startsWith('https://')).to.be.true
70 expect(body.type).to.contain(OAuth2ErrorCode.INVALID_CLIENT)
74 describe('Login', function () {
76 it('Should not login with an invalid username', async function () {
77 const user = { username: 'captain crochet', password: server.store.user.password }
78 const body = await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
80 expect(body.code).to.equal(OAuth2ErrorCode.INVALID_GRANT)
81 expect(body.error).to.contain('credentials are invalid')
82 expect(body.type.startsWith('https://')).to.be.true
83 expect(body.type).to.contain(OAuth2ErrorCode.INVALID_GRANT)
86 it('Should not login with an invalid password', async function () {
87 const user = { username: server.store.user.username, password: 'mew_three' }
88 const body = await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
90 expect(body.code).to.equal(OAuth2ErrorCode.INVALID_GRANT)
91 expect(body.error).to.contain('credentials are invalid')
92 expect(body.type.startsWith('https://')).to.be.true
93 expect(body.type).to.contain(OAuth2ErrorCode.INVALID_GRANT)
96 it('Should not be able to upload a video', async function () {
97 token = 'my_super_token'
99 await server.videos.upload({ token, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
102 it('Should not be able to follow', async function () {
103 token = 'my_super_token'
105 await server.follows.follow({
106 hosts: [ 'http://example.com' ],
108 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
112 it('Should not be able to unfollow')
114 it('Should be able to login', async function () {
115 const body = await server.login.login({ expectedStatus: HttpStatusCode.OK_200 })
117 token = body.access_token
120 it('Should be able to login with an insensitive username', async function () {
121 const user = { username: 'RoOt', password: server.store.user.password }
122 await server.login.login({ user, expectedStatus: HttpStatusCode.OK_200 })
124 const user2 = { username: 'rOoT', password: server.store.user.password }
125 await server.login.login({ user: user2, expectedStatus: HttpStatusCode.OK_200 })
127 const user3 = { username: 'ROOt', password: server.store.user.password }
128 await server.login.login({ user: user3, expectedStatus: HttpStatusCode.OK_200 })
132 describe('Upload', function () {
134 it('Should upload the video with the correct token', async function () {
135 await server.videos.upload({ token })
136 const { data } = await server.videos.list()
137 const video = data[0]
139 expect(video.account.name).to.equal('root')
143 it('Should upload the video again with the correct token', async function () {
144 await server.videos.upload({ token })
148 describe('Ratings', function () {
150 it('Should retrieve a video rating', async function () {
151 await server.videos.rate({ id: videoId, rating: 'like' })
152 const rating = await server.users.getMyRating({ token, videoId })
154 expect(rating.videoId).to.equal(videoId)
155 expect(rating.rating).to.equal('like')
158 it('Should retrieve ratings list', async function () {
159 await server.videos.rate({ id: videoId, rating: 'like' })
161 const body = await server.accounts.listRatings({ accountName: server.store.user.username })
163 expect(body.total).to.equal(1)
164 expect(body.data[0].video.id).to.equal(videoId)
165 expect(body.data[0].rating).to.equal('like')
168 it('Should retrieve ratings list by rating type', async function () {
170 const body = await server.accounts.listRatings({ accountName: server.store.user.username, rating: 'like' })
171 expect(body.data.length).to.equal(1)
175 const body = await server.accounts.listRatings({ accountName: server.store.user.username, rating: 'dislike' })
176 expect(body.data.length).to.equal(0)
181 describe('Remove video', function () {
182 it('Should not be able to remove the video with an incorrect token', async function () {
183 await server.videos.remove({ token: 'bad_token', id: videoId, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
186 it('Should not be able to remove the video with the token of another account')
188 it('Should be able to remove the video with the correct token', async function () {
189 await server.videos.remove({ token, id: videoId })
193 describe('Logout', function () {
194 it('Should logout (revoke token)', async function () {
195 await server.login.logout({ token: server.accessToken })
198 it('Should not be able to get the user information', async function () {
199 await server.users.getMyInfo({ expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
202 it('Should not be able to upload a video', async function () {
203 await server.videos.upload({ attributes: { name: 'video' }, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
206 it('Should not be able to rate a video', async function () {
207 const path = '/api/v1/videos/'
214 path: path + videoId,
215 token: 'wrong token',
217 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
219 await makePutBodyRequest(options)
222 it('Should be able to login again', async function () {
223 const body = await server.login.login()
224 server.accessToken = body.access_token
225 server.refreshToken = body.refresh_token
228 it('Should be able to get my user information again', async function () {
229 await server.users.getMyInfo()
232 it('Should have an expired access token', async function () {
235 await server.sql.setTokenField(server.accessToken, 'accessTokenExpiresAt', new Date().toISOString())
236 await server.sql.setTokenField(server.accessToken, 'refreshTokenExpiresAt', new Date().toISOString())
238 await killallServers([ server ])
241 await server.users.getMyInfo({ expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
244 it('Should not be able to refresh an access token with an expired refresh token', async function () {
245 await server.login.refreshToken({ refreshToken: server.refreshToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
248 it('Should refresh the token', async function () {
251 const futureDate = new Date(new Date().getTime() + 1000 * 60).toISOString()
252 await server.sql.setTokenField(server.accessToken, 'refreshTokenExpiresAt', futureDate)
254 await killallServers([ server ])
257 const res = await server.login.refreshToken({ refreshToken: server.refreshToken })
258 server.accessToken = res.body.access_token
259 server.refreshToken = res.body.refresh_token
262 it('Should be able to get my user information again', async function () {
263 await server.users.getMyInfo()
267 describe('Creating a user', function () {
269 it('Should be able to create a new user', async function () {
270 await server.users.create({ ...user, videoQuota: 2 * 1024 * 1024, adminFlags: UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST })
273 it('Should be able to login with this user', async function () {
274 userToken = await server.login.getAccessToken(user)
277 it('Should be able to get user information', async function () {
278 const userMe = await server.users.getMyInfo({ token: userToken })
280 const userGet = await server.users.get({ userId: userMe.id, withStats: true })
282 for (const user of [ userMe, userGet ]) {
283 expect(user.username).to.equal('user_1')
284 expect(user.email).to.equal('user_1@example.com')
285 expect(user.nsfwPolicy).to.equal('display')
286 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
287 expect(user.roleLabel).to.equal('User')
288 expect(user.id).to.be.a('number')
289 expect(user.account.displayName).to.equal('user_1')
290 expect(user.account.description).to.be.null
293 expect(userMe.adminFlags).to.equal(UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST)
294 expect(userGet.adminFlags).to.equal(UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST)
296 expect(userMe.specialPlaylists).to.have.lengthOf(1)
297 expect(userMe.specialPlaylists[0].type).to.equal(VideoPlaylistType.WATCH_LATER)
299 // Check stats are included with withStats
300 expect(userGet.videosCount).to.be.a('number')
301 expect(userGet.videosCount).to.equal(0)
302 expect(userGet.videoCommentsCount).to.be.a('number')
303 expect(userGet.videoCommentsCount).to.equal(0)
304 expect(userGet.abusesCount).to.be.a('number')
305 expect(userGet.abusesCount).to.equal(0)
306 expect(userGet.abusesAcceptedCount).to.be.a('number')
307 expect(userGet.abusesAcceptedCount).to.equal(0)
311 describe('My videos & quotas', function () {
313 it('Should be able to upload a video with this user', async function () {
317 name: 'super user video',
318 fixture: 'video_short.webm'
320 await server.videos.upload({ token: userToken, attributes })
323 it('Should have video quota updated', async function () {
324 const quota = await server.users.getMyQuotaUsed({ token: userToken })
325 expect(quota.videoQuotaUsed).to.equal(218910)
327 const { data } = await server.users.list()
328 const tmpUser = data.find(u => u.username === user.username)
329 expect(tmpUser.videoQuotaUsed).to.equal(218910)
332 it('Should be able to list my videos', async function () {
333 const { total, data } = await server.videos.listMyVideos({ token: userToken })
334 expect(total).to.equal(1)
335 expect(data).to.have.lengthOf(1)
337 const video: Video = data[0]
338 expect(video.name).to.equal('super user video')
339 expect(video.thumbnailPath).to.not.be.null
340 expect(video.previewPath).to.not.be.null
343 it('Should be able to search in my videos', async function () {
345 const { total, data } = await server.videos.listMyVideos({ token: userToken, sort: '-createdAt', search: 'user video' })
346 expect(total).to.equal(1)
347 expect(data).to.have.lengthOf(1)
351 const { total, data } = await server.videos.listMyVideos({ token: userToken, sort: '-createdAt', search: 'toto' })
352 expect(total).to.equal(0)
353 expect(data).to.have.lengthOf(0)
357 it('Should disable webtorrent, enable HLS, and update my quota', async function () {
361 const config = await server.config.getCustomConfig()
362 config.transcoding.webtorrent.enabled = false
363 config.transcoding.hls.enabled = true
364 config.transcoding.enabled = true
365 await server.config.updateCustomSubConfig({ newConfig: config })
370 name: 'super user video 2',
371 fixture: 'video_short.webm'
373 await server.videos.upload({ token: userToken, attributes })
375 await waitJobs([ server ])
379 const data = await server.users.getMyQuotaUsed({ token: userToken })
380 expect(data.videoQuotaUsed).to.be.greaterThan(220000)
385 describe('Users listing', function () {
387 it('Should list all the users', async function () {
388 const { data, total } = await server.users.list()
390 expect(total).to.equal(2)
391 expect(data).to.be.an('array')
392 expect(data.length).to.equal(2)
395 expect(user.username).to.equal('user_1')
396 expect(user.email).to.equal('user_1@example.com')
397 expect(user.nsfwPolicy).to.equal('display')
399 const rootUser = data[1]
400 expect(rootUser.username).to.equal('root')
401 expect(rootUser.email).to.equal('admin' + server.internalServerNumber + '@example.com')
402 expect(user.nsfwPolicy).to.equal('display')
404 expect(rootUser.lastLoginDate).to.exist
405 expect(user.lastLoginDate).to.exist
410 it('Should list only the first user by username asc', async function () {
411 const { total, data } = await server.users.list({ start: 0, count: 1, sort: 'username' })
413 expect(total).to.equal(2)
414 expect(data.length).to.equal(1)
417 expect(user.username).to.equal('root')
418 expect(user.email).to.equal('admin' + server.internalServerNumber + '@example.com')
419 expect(user.roleLabel).to.equal('Administrator')
420 expect(user.nsfwPolicy).to.equal('display')
423 it('Should list only the first user by username desc', async function () {
424 const { total, data } = await server.users.list({ start: 0, count: 1, sort: '-username' })
426 expect(total).to.equal(2)
427 expect(data.length).to.equal(1)
430 expect(user.username).to.equal('user_1')
431 expect(user.email).to.equal('user_1@example.com')
432 expect(user.nsfwPolicy).to.equal('display')
435 it('Should list only the second user by createdAt desc', async function () {
436 const { data, total } = await server.users.list({ start: 0, count: 1, sort: '-createdAt' })
437 expect(total).to.equal(2)
439 expect(data.length).to.equal(1)
442 expect(user.username).to.equal('user_1')
443 expect(user.email).to.equal('user_1@example.com')
444 expect(user.nsfwPolicy).to.equal('display')
447 it('Should list all the users by createdAt asc', async function () {
448 const { data, total } = await server.users.list({ start: 0, count: 2, sort: 'createdAt' })
450 expect(total).to.equal(2)
451 expect(data.length).to.equal(2)
453 expect(data[0].username).to.equal('root')
454 expect(data[0].email).to.equal('admin' + server.internalServerNumber + '@example.com')
455 expect(data[0].nsfwPolicy).to.equal('display')
457 expect(data[1].username).to.equal('user_1')
458 expect(data[1].email).to.equal('user_1@example.com')
459 expect(data[1].nsfwPolicy).to.equal('display')
462 it('Should search user by username', async function () {
463 const { data, total } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', search: 'oot' })
464 expect(total).to.equal(1)
465 expect(data.length).to.equal(1)
466 expect(data[0].username).to.equal('root')
469 it('Should search user by email', async function () {
471 const { total, data } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', search: 'r_1@exam' })
472 expect(total).to.equal(1)
473 expect(data.length).to.equal(1)
474 expect(data[0].username).to.equal('user_1')
475 expect(data[0].email).to.equal('user_1@example.com')
479 const { total, data } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', search: 'example' })
480 expect(total).to.equal(2)
481 expect(data.length).to.equal(2)
482 expect(data[0].username).to.equal('root')
483 expect(data[1].username).to.equal('user_1')
488 describe('Update my account', function () {
490 it('Should update my password', async function () {
491 await server.users.updateMe({
493 currentPassword: 'super password',
494 password: 'new password'
496 user.password = 'new password'
498 await server.login.login({ user })
501 it('Should be able to change the NSFW display attribute', async function () {
502 await server.users.updateMe({
504 nsfwPolicy: 'do_not_list'
507 const user = await server.users.getMyInfo({ token: userToken })
508 expect(user.username).to.equal('user_1')
509 expect(user.email).to.equal('user_1@example.com')
510 expect(user.nsfwPolicy).to.equal('do_not_list')
511 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
512 expect(user.id).to.be.a('number')
513 expect(user.account.displayName).to.equal('user_1')
514 expect(user.account.description).to.be.null
517 it('Should be able to change the autoPlayVideo attribute', async function () {
518 await server.users.updateMe({
523 const user = await server.users.getMyInfo({ token: userToken })
524 expect(user.autoPlayVideo).to.be.false
527 it('Should be able to change the autoPlayNextVideo attribute', async function () {
528 await server.users.updateMe({
530 autoPlayNextVideo: true
533 const user = await server.users.getMyInfo({ token: userToken })
534 expect(user.autoPlayNextVideo).to.be.true
537 it('Should be able to change the email attribute', async function () {
538 await server.users.updateMe({
540 currentPassword: 'new password',
541 email: 'updated@example.com'
544 const user = await server.users.getMyInfo({ token: userToken })
545 expect(user.username).to.equal('user_1')
546 expect(user.email).to.equal('updated@example.com')
547 expect(user.nsfwPolicy).to.equal('do_not_list')
548 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
549 expect(user.id).to.be.a('number')
550 expect(user.account.displayName).to.equal('user_1')
551 expect(user.account.description).to.be.null
554 it('Should be able to update my avatar with a gif', async function () {
555 const fixture = 'avatar.gif'
557 await server.users.updateMyAvatar({ token: userToken, fixture })
559 const user = await server.users.getMyInfo({ token: userToken })
560 await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.gif')
563 it('Should be able to update my avatar with a gif, and then a png', async function () {
564 for (const extension of [ '.png', '.gif' ]) {
565 const fixture = 'avatar' + extension
567 await server.users.updateMyAvatar({ token: userToken, fixture })
569 const user = await server.users.getMyInfo({ token: userToken })
570 await testImage(server.url, 'avatar-resized', user.account.avatar.path, extension)
574 it('Should be able to update my display name', async function () {
575 await server.users.updateMe({ token: userToken, displayName: 'new display name' })
577 const user = await server.users.getMyInfo({ token: userToken })
578 expect(user.username).to.equal('user_1')
579 expect(user.email).to.equal('updated@example.com')
580 expect(user.nsfwPolicy).to.equal('do_not_list')
581 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
582 expect(user.id).to.be.a('number')
583 expect(user.account.displayName).to.equal('new display name')
584 expect(user.account.description).to.be.null
587 it('Should be able to update my description', async function () {
588 await server.users.updateMe({ token: userToken, description: 'my super description updated' })
590 const user = await server.users.getMyInfo({ token: userToken })
591 expect(user.username).to.equal('user_1')
592 expect(user.email).to.equal('updated@example.com')
593 expect(user.nsfwPolicy).to.equal('do_not_list')
594 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
595 expect(user.id).to.be.a('number')
596 expect(user.account.displayName).to.equal('new display name')
597 expect(user.account.description).to.equal('my super description updated')
598 expect(user.noWelcomeModal).to.be.false
599 expect(user.noInstanceConfigWarningModal).to.be.false
600 expect(user.noAccountSetupWarningModal).to.be.false
603 it('Should be able to update my theme', async function () {
604 for (const theme of [ 'background-red', 'default', 'instance-default' ]) {
605 await server.users.updateMe({ token: userToken, theme })
607 const user = await server.users.getMyInfo({ token: userToken })
608 expect(user.theme).to.equal(theme)
612 it('Should be able to update my modal preferences', async function () {
613 await server.users.updateMe({
615 noInstanceConfigWarningModal: true,
616 noWelcomeModal: true,
617 noAccountSetupWarningModal: true
620 const user = await server.users.getMyInfo({ token: userToken })
621 expect(user.noWelcomeModal).to.be.true
622 expect(user.noInstanceConfigWarningModal).to.be.true
623 expect(user.noAccountSetupWarningModal).to.be.true
627 describe('Updating another user', function () {
628 it('Should be able to update another user', async function () {
629 await server.users.update({
632 email: 'updated2@example.com',
635 role: UserRole.MODERATOR,
636 adminFlags: UserAdminFlag.NONE,
640 const user = await server.users.get({ token, userId })
642 expect(user.username).to.equal('user_1')
643 expect(user.email).to.equal('updated2@example.com')
644 expect(user.emailVerified).to.be.true
645 expect(user.nsfwPolicy).to.equal('do_not_list')
646 expect(user.videoQuota).to.equal(42)
647 expect(user.roleLabel).to.equal('Moderator')
648 expect(user.id).to.be.a('number')
649 expect(user.adminFlags).to.equal(UserAdminFlag.NONE)
650 expect(user.pluginAuth).to.equal('toto')
653 it('Should reset the auth plugin', async function () {
654 await server.users.update({ userId, token, pluginAuth: null })
656 const user = await server.users.get({ token, userId })
657 expect(user.pluginAuth).to.be.null
660 it('Should have removed the user token', async function () {
661 await server.users.getMyQuotaUsed({ token: userToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
663 userToken = await server.login.getAccessToken(user)
666 it('Should be able to update another user password', async function () {
667 await server.users.update({ userId, token, password: 'password updated' })
669 await server.users.getMyQuotaUsed({ token: userToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
671 await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
673 user.password = 'password updated'
674 userToken = await server.login.getAccessToken(user)
678 describe('Video blacklists', function () {
679 it('Should be able to list video blacklist by a moderator', async function () {
680 await server.blacklist.list({ token: userToken })
684 describe('Remove a user', function () {
685 it('Should be able to remove this user', async function () {
686 await server.users.remove({ userId, token })
689 it('Should not be able to login with this user', async function () {
690 await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
693 it('Should not have videos of this user', async function () {
694 const { data, total } = await server.videos.list()
695 expect(total).to.equal(1)
697 const video = data[0]
698 expect(video.account.name).to.equal('root')
702 describe('Registering a new user', function () {
703 let user15AccessToken
705 it('Should register a new user', async function () {
706 const user = { displayName: 'super user 15', username: 'user_15', password: 'my super password' }
707 const channel = { name: 'my_user_15_channel', displayName: 'my channel rocks' }
709 await server.users.register({ ...user, channel })
712 it('Should be able to login with this registered user', async function () {
715 password: 'my super password'
718 user15AccessToken = await server.login.getAccessToken(user15)
721 it('Should have the correct display name', async function () {
722 const user = await server.users.getMyInfo({ token: user15AccessToken })
723 expect(user.account.displayName).to.equal('super user 15')
726 it('Should have the correct video quota', async function () {
727 const user = await server.users.getMyInfo({ token: user15AccessToken })
728 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
731 it('Should have created the channel', async function () {
732 const { displayName } = await server.channels.get({ channelName: 'my_user_15_channel' })
734 expect(displayName).to.equal('my channel rocks')
737 it('Should remove me', async function () {
739 const { data } = await server.users.list()
740 expect(data.find(u => u.username === 'user_15')).to.not.be.undefined
743 await server.users.deleteMe({ token: user15AccessToken })
746 const { data } = await server.users.list()
747 expect(data.find(u => u.username === 'user_15')).to.be.undefined
752 describe('User blocking', function () {
754 let user16AccessToken
757 password: 'my super password'
760 it('Should block a user', async function () {
761 const user = await server.users.create({ ...user16 })
764 user16AccessToken = await server.login.getAccessToken(user16)
766 await server.users.getMyInfo({ token: user16AccessToken, expectedStatus: HttpStatusCode.OK_200 })
767 await server.users.banUser({ userId: user16Id })
769 await server.users.getMyInfo({ token: user16AccessToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
770 await server.login.login({ user: user16, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
773 it('Should search user by banned status', async function () {
775 const { data, total } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', blocked: true })
776 expect(total).to.equal(1)
777 expect(data.length).to.equal(1)
779 expect(data[0].username).to.equal(user16.username)
783 const { data, total } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', blocked: false })
784 expect(total).to.equal(1)
785 expect(data.length).to.equal(1)
787 expect(data[0].username).to.not.equal(user16.username)
791 it('Should unblock a user', async function () {
792 await server.users.unbanUser({ userId: user16Id })
793 user16AccessToken = await server.login.getAccessToken(user16)
794 await server.users.getMyInfo({ token: user16AccessToken, expectedStatus: HttpStatusCode.OK_200 })
798 describe('User stats', function () {
800 let user17AccessToken
802 it('Should report correct initial statistics about a user', async function () {
805 password: 'my super password'
807 const created = await server.users.create({ ...user17 })
809 user17Id = created.id
810 user17AccessToken = await server.login.getAccessToken(user17)
812 const user = await server.users.get({ userId: user17Id, withStats: true })
813 expect(user.videosCount).to.equal(0)
814 expect(user.videoCommentsCount).to.equal(0)
815 expect(user.abusesCount).to.equal(0)
816 expect(user.abusesCreatedCount).to.equal(0)
817 expect(user.abusesAcceptedCount).to.equal(0)
820 it('Should report correct videos count', async function () {
821 const attributes = { name: 'video to test user stats' }
822 await server.videos.upload({ token: user17AccessToken, attributes })
824 const { data } = await server.videos.list()
825 videoId = data.find(video => video.name === attributes.name).id
827 const user = await server.users.get({ userId: user17Id, withStats: true })
828 expect(user.videosCount).to.equal(1)
831 it('Should report correct video comments for user', async function () {
832 const text = 'super comment'
833 await server.comments.createThread({ token: user17AccessToken, videoId, text })
835 const user = await server.users.get({ userId: user17Id, withStats: true })
836 expect(user.videoCommentsCount).to.equal(1)
839 it('Should report correct abuses counts', async function () {
840 const reason = 'my super bad reason'
841 await server.abuses.report({ token: user17AccessToken, videoId, reason })
843 const body1 = await server.abuses.getAdminList()
844 const abuseId = body1.data[0].id
846 const user2 = await server.users.get({ userId: user17Id, withStats: true })
847 expect(user2.abusesCount).to.equal(1) // number of incriminations
848 expect(user2.abusesCreatedCount).to.equal(1) // number of reports created
850 await server.abuses.update({ abuseId, body: { state: AbuseState.ACCEPTED } })
852 const user3 = await server.users.get({ userId: user17Id, withStats: true })
853 expect(user3.abusesAcceptedCount).to.equal(1) // number of reports created accepted
857 after(async function () {
858 await cleanupTests([ server ])