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'
17 setAccessTokensToServers,
21 } from '@shared/extra-utils'
22 import { AbuseState, OAuth2ErrorCode, UserAdminFlag, UserRole, Video, VideoPlaylistType } from '@shared/models'
24 const expect = chai.expect
26 describe('Test users', function () {
27 let server: ServerInfo
28 let accessToken: string
29 let accessTokenUser: string
34 password: 'super password'
37 before(async function () {
40 server = await flushAndRunServer(1, {
48 await setAccessTokensToServers([ server ])
50 await server.pluginsCommand.install({ npmName: 'peertube-theme-background-red' })
53 describe('OAuth client', function () {
54 it('Should create a new client')
56 it('Should return the first client')
58 it('Should remove the last client')
60 it('Should not login with an invalid client id', async function () {
61 const client = { id: 'client', secret: server.client.secret }
62 const body = await server.loginCommand.login({ client, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
64 expect(body.code).to.equal(OAuth2ErrorCode.INVALID_CLIENT)
65 expect(body.error).to.contain('client is invalid')
66 expect(body.type.startsWith('https://')).to.be.true
67 expect(body.type).to.contain(OAuth2ErrorCode.INVALID_CLIENT)
70 it('Should not login with an invalid client secret', async function () {
71 const client = { id: server.client.id, secret: 'coucou' }
72 const body = await server.loginCommand.login({ client, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
74 expect(body.code).to.equal(OAuth2ErrorCode.INVALID_CLIENT)
75 expect(body.error).to.contain('client is invalid')
76 expect(body.type.startsWith('https://')).to.be.true
77 expect(body.type).to.contain(OAuth2ErrorCode.INVALID_CLIENT)
81 describe('Login', function () {
83 it('Should not login with an invalid username', async function () {
84 const user = { username: 'captain crochet', password: server.user.password }
85 const body = await server.loginCommand.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
87 expect(body.code).to.equal(OAuth2ErrorCode.INVALID_GRANT)
88 expect(body.error).to.contain('credentials are invalid')
89 expect(body.type.startsWith('https://')).to.be.true
90 expect(body.type).to.contain(OAuth2ErrorCode.INVALID_GRANT)
93 it('Should not login with an invalid password', async function () {
94 const user = { username: server.user.username, password: 'mew_three' }
95 const body = await server.loginCommand.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
97 expect(body.code).to.equal(OAuth2ErrorCode.INVALID_GRANT)
98 expect(body.error).to.contain('credentials are invalid')
99 expect(body.type.startsWith('https://')).to.be.true
100 expect(body.type).to.contain(OAuth2ErrorCode.INVALID_GRANT)
103 it('Should not be able to upload a video', async function () {
104 accessToken = 'my_super_token'
106 const videoAttributes = {}
107 await uploadVideo(server.url, accessToken, videoAttributes, HttpStatusCode.UNAUTHORIZED_401)
110 it('Should not be able to follow', async function () {
111 accessToken = 'my_super_token'
113 await server.followsCommand.follow({
114 targets: [ 'http://example.com' ],
116 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
120 it('Should not be able to unfollow')
122 it('Should be able to login', async function () {
123 const body = await server.loginCommand.login({ expectedStatus: HttpStatusCode.OK_200 })
125 accessToken = body.access_token
128 it('Should be able to login with an insensitive username', async function () {
129 const user = { username: 'RoOt', password: server.user.password }
130 await server.loginCommand.login({ user, expectedStatus: HttpStatusCode.OK_200 })
132 const user2 = { username: 'rOoT', password: server.user.password }
133 await server.loginCommand.login({ user: user2, expectedStatus: HttpStatusCode.OK_200 })
135 const user3 = { username: 'ROOt', password: server.user.password }
136 await server.loginCommand.login({ user: user3, expectedStatus: HttpStatusCode.OK_200 })
140 describe('Upload', function () {
142 it('Should upload the video with the correct token', async function () {
143 const videoAttributes = {}
144 await uploadVideo(server.url, accessToken, videoAttributes)
145 const res = await getVideosList(server.url)
146 const video = res.body.data[0]
148 expect(video.account.name).to.equal('root')
152 it('Should upload the video again with the correct token', async function () {
153 const videoAttributes = {}
154 await uploadVideo(server.url, accessToken, videoAttributes)
158 describe('Ratings', function () {
160 it('Should retrieve a video rating', async function () {
161 await rateVideo(server.url, accessToken, videoId, 'like')
162 const rating = await server.usersCommand.getMyRating({ token: accessToken, videoId })
164 expect(rating.videoId).to.equal(videoId)
165 expect(rating.rating).to.equal('like')
168 it('Should retrieve ratings list', async function () {
169 await rateVideo(server.url, accessToken, videoId, 'like')
171 const body = await server.accountsCommand.listRatings({ accountName: server.user.username })
173 expect(body.total).to.equal(1)
174 expect(body.data[0].video.id).to.equal(videoId)
175 expect(body.data[0].rating).to.equal('like')
178 it('Should retrieve ratings list by rating type', async function () {
180 const body = await server.accountsCommand.listRatings({ accountName: server.user.username, rating: 'like' })
181 expect(body.data.length).to.equal(1)
185 const body = await server.accountsCommand.listRatings({ accountName: server.user.username, rating: 'dislike' })
186 expect(body.data.length).to.equal(0)
191 describe('Remove video', function () {
192 it('Should not be able to remove the video with an incorrect token', async function () {
193 await removeVideo(server.url, 'bad_token', videoId, HttpStatusCode.UNAUTHORIZED_401)
196 it('Should not be able to remove the video with the token of another account')
198 it('Should be able to remove the video with the correct token', async function () {
199 await removeVideo(server.url, accessToken, videoId)
203 describe('Logout', function () {
204 it('Should logout (revoke token)', async function () {
205 await server.loginCommand.logout({ token: server.accessToken })
208 it('Should not be able to get the user information', async function () {
209 await server.usersCommand.getMyInfo({ expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
212 it('Should not be able to upload a video', async function () {
213 await uploadVideo(server.url, server.accessToken, { name: 'video' }, HttpStatusCode.UNAUTHORIZED_401)
216 it('Should not be able to rate a video', async function () {
217 const path = '/api/v1/videos/'
224 path: path + videoId,
225 token: 'wrong token',
227 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
229 await makePutBodyRequest(options)
232 it('Should be able to login again', async function () {
233 const body = await server.loginCommand.login()
234 server.accessToken = body.access_token
235 server.refreshToken = body.refresh_token
238 it('Should be able to get my user information again', async function () {
239 await server.usersCommand.getMyInfo()
242 it('Should have an expired access token', async function () {
245 await server.sqlCommand.setTokenField(server.accessToken, 'accessTokenExpiresAt', new Date().toISOString())
246 await server.sqlCommand.setTokenField(server.accessToken, 'refreshTokenExpiresAt', new Date().toISOString())
248 await killallServers([ server ])
249 await reRunServer(server)
251 await server.usersCommand.getMyInfo({ expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
254 it('Should not be able to refresh an access token with an expired refresh token', async function () {
255 await server.loginCommand.refreshToken({ refreshToken: server.refreshToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
258 it('Should refresh the token', async function () {
261 const futureDate = new Date(new Date().getTime() + 1000 * 60).toISOString()
262 await server.sqlCommand.setTokenField(server.accessToken, 'refreshTokenExpiresAt', futureDate)
264 await killallServers([ server ])
265 await reRunServer(server)
267 const res = await server.loginCommand.refreshToken({ refreshToken: server.refreshToken })
268 server.accessToken = res.body.access_token
269 server.refreshToken = res.body.refresh_token
272 it('Should be able to get my user information again', async function () {
273 await server.usersCommand.getMyInfo()
277 describe('Creating a user', function () {
279 it('Should be able to create a new user', async function () {
280 await server.usersCommand.create({ ...user, videoQuota: 2 * 1024 * 1024, adminFlags: UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST })
283 it('Should be able to login with this user', async function () {
284 accessTokenUser = await server.loginCommand.getAccessToken(user)
287 it('Should be able to get user information', async function () {
288 const userMe = await server.usersCommand.getMyInfo({ token: accessTokenUser })
290 const userGet = await server.usersCommand.get({ userId: userMe.id, withStats: true })
292 for (const user of [ userMe, userGet ]) {
293 expect(user.username).to.equal('user_1')
294 expect(user.email).to.equal('user_1@example.com')
295 expect(user.nsfwPolicy).to.equal('display')
296 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
297 expect(user.roleLabel).to.equal('User')
298 expect(user.id).to.be.a('number')
299 expect(user.account.displayName).to.equal('user_1')
300 expect(user.account.description).to.be.null
303 expect(userMe.adminFlags).to.be.undefined
304 expect(userGet.adminFlags).to.equal(UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST)
306 expect(userMe.specialPlaylists).to.have.lengthOf(1)
307 expect(userMe.specialPlaylists[0].type).to.equal(VideoPlaylistType.WATCH_LATER)
309 // Check stats are included with withStats
310 expect(userGet.videosCount).to.be.a('number')
311 expect(userGet.videosCount).to.equal(0)
312 expect(userGet.videoCommentsCount).to.be.a('number')
313 expect(userGet.videoCommentsCount).to.equal(0)
314 expect(userGet.abusesCount).to.be.a('number')
315 expect(userGet.abusesCount).to.equal(0)
316 expect(userGet.abusesAcceptedCount).to.be.a('number')
317 expect(userGet.abusesAcceptedCount).to.equal(0)
321 describe('My videos & quotas', function () {
323 it('Should be able to upload a video with this user', async function () {
326 const videoAttributes = {
327 name: 'super user video',
328 fixture: 'video_short.webm'
330 await uploadVideo(server.url, accessTokenUser, videoAttributes)
333 it('Should have video quota updated', async function () {
334 const quota = await server.usersCommand.getMyQuotaUsed({ token: accessTokenUser })
335 expect(quota.videoQuotaUsed).to.equal(218910)
337 const { data } = await server.usersCommand.list()
338 const tmpUser = data.find(u => u.username === user.username)
339 expect(tmpUser.videoQuotaUsed).to.equal(218910)
342 it('Should be able to list my videos', async function () {
343 const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
344 expect(res.body.total).to.equal(1)
346 const videos = res.body.data
347 expect(videos).to.have.lengthOf(1)
349 const video: Video = videos[0]
350 expect(video.name).to.equal('super user video')
351 expect(video.thumbnailPath).to.not.be.null
352 expect(video.previewPath).to.not.be.null
355 it('Should be able to search in my videos', async function () {
357 const res = await getMyVideos(server.url, accessTokenUser, 0, 5, '-createdAt', 'user video')
358 expect(res.body.total).to.equal(1)
360 const videos = res.body.data
361 expect(videos).to.have.lengthOf(1)
365 const res = await getMyVideos(server.url, accessTokenUser, 0, 5, '-createdAt', 'toto')
366 expect(res.body.total).to.equal(0)
368 const videos = res.body.data
369 expect(videos).to.have.lengthOf(0)
373 it('Should disable webtorrent, enable HLS, and update my quota', async function () {
377 const config = await server.configCommand.getCustomConfig()
378 config.transcoding.webtorrent.enabled = false
379 config.transcoding.hls.enabled = true
380 config.transcoding.enabled = true
381 await server.configCommand.updateCustomSubConfig({ newConfig: config })
385 const videoAttributes = {
386 name: 'super user video 2',
387 fixture: 'video_short.webm'
389 await uploadVideo(server.url, accessTokenUser, videoAttributes)
391 await waitJobs([ server ])
395 const data = await server.usersCommand.getMyQuotaUsed({ token: accessTokenUser })
396 expect(data.videoQuotaUsed).to.be.greaterThan(220000)
401 describe('Users listing', function () {
403 it('Should list all the users', async function () {
404 const { data, total } = await server.usersCommand.list()
406 expect(total).to.equal(2)
407 expect(data).to.be.an('array')
408 expect(data.length).to.equal(2)
411 expect(user.username).to.equal('user_1')
412 expect(user.email).to.equal('user_1@example.com')
413 expect(user.nsfwPolicy).to.equal('display')
415 const rootUser = data[1]
416 expect(rootUser.username).to.equal('root')
417 expect(rootUser.email).to.equal('admin' + server.internalServerNumber + '@example.com')
418 expect(user.nsfwPolicy).to.equal('display')
420 expect(rootUser.lastLoginDate).to.exist
421 expect(user.lastLoginDate).to.exist
426 it('Should list only the first user by username asc', async function () {
427 const { total, data } = await server.usersCommand.list({ start: 0, count: 1, sort: 'username' })
429 expect(total).to.equal(2)
430 expect(data.length).to.equal(1)
433 expect(user.username).to.equal('root')
434 expect(user.email).to.equal('admin' + server.internalServerNumber + '@example.com')
435 expect(user.roleLabel).to.equal('Administrator')
436 expect(user.nsfwPolicy).to.equal('display')
439 it('Should list only the first user by username desc', async function () {
440 const { total, data } = await server.usersCommand.list({ start: 0, count: 1, sort: '-username' })
442 expect(total).to.equal(2)
443 expect(data.length).to.equal(1)
446 expect(user.username).to.equal('user_1')
447 expect(user.email).to.equal('user_1@example.com')
448 expect(user.nsfwPolicy).to.equal('display')
451 it('Should list only the second user by createdAt desc', async function () {
452 const { data, total } = await server.usersCommand.list({ start: 0, count: 1, sort: '-createdAt' })
453 expect(total).to.equal(2)
455 expect(data.length).to.equal(1)
458 expect(user.username).to.equal('user_1')
459 expect(user.email).to.equal('user_1@example.com')
460 expect(user.nsfwPolicy).to.equal('display')
463 it('Should list all the users by createdAt asc', async function () {
464 const { data, total } = await server.usersCommand.list({ start: 0, count: 2, sort: 'createdAt' })
466 expect(total).to.equal(2)
467 expect(data.length).to.equal(2)
469 expect(data[0].username).to.equal('root')
470 expect(data[0].email).to.equal('admin' + server.internalServerNumber + '@example.com')
471 expect(data[0].nsfwPolicy).to.equal('display')
473 expect(data[1].username).to.equal('user_1')
474 expect(data[1].email).to.equal('user_1@example.com')
475 expect(data[1].nsfwPolicy).to.equal('display')
478 it('Should search user by username', async function () {
479 const { data, total } = await server.usersCommand.list({ start: 0, count: 2, sort: 'createdAt', search: 'oot' })
480 expect(total).to.equal(1)
481 expect(data.length).to.equal(1)
482 expect(data[0].username).to.equal('root')
485 it('Should search user by email', async function () {
487 const { total, data } = await server.usersCommand.list({ start: 0, count: 2, sort: 'createdAt', search: 'r_1@exam' })
488 expect(total).to.equal(1)
489 expect(data.length).to.equal(1)
490 expect(data[0].username).to.equal('user_1')
491 expect(data[0].email).to.equal('user_1@example.com')
495 const { total, data } = await server.usersCommand.list({ start: 0, count: 2, sort: 'createdAt', search: 'example' })
496 expect(total).to.equal(2)
497 expect(data.length).to.equal(2)
498 expect(data[0].username).to.equal('root')
499 expect(data[1].username).to.equal('user_1')
504 describe('Update my account', function () {
506 it('Should update my password', async function () {
507 await server.usersCommand.updateMe({
508 token: accessTokenUser,
509 currentPassword: 'super password',
510 password: 'new password'
512 user.password = 'new password'
514 await server.loginCommand.login({ user })
517 it('Should be able to change the NSFW display attribute', async function () {
518 await server.usersCommand.updateMe({
519 token: accessTokenUser,
520 nsfwPolicy: 'do_not_list'
523 const user = await server.usersCommand.getMyInfo({ token: accessTokenUser })
524 expect(user.username).to.equal('user_1')
525 expect(user.email).to.equal('user_1@example.com')
526 expect(user.nsfwPolicy).to.equal('do_not_list')
527 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
528 expect(user.id).to.be.a('number')
529 expect(user.account.displayName).to.equal('user_1')
530 expect(user.account.description).to.be.null
533 it('Should be able to change the autoPlayVideo attribute', async function () {
534 await server.usersCommand.updateMe({
535 token: accessTokenUser,
539 const user = await server.usersCommand.getMyInfo({ token: accessTokenUser })
540 expect(user.autoPlayVideo).to.be.false
543 it('Should be able to change the autoPlayNextVideo attribute', async function () {
544 await server.usersCommand.updateMe({
545 token: accessTokenUser,
546 autoPlayNextVideo: true
549 const user = await server.usersCommand.getMyInfo({ token: accessTokenUser })
550 expect(user.autoPlayNextVideo).to.be.true
553 it('Should be able to change the email attribute', async function () {
554 await server.usersCommand.updateMe({
555 token: accessTokenUser,
556 currentPassword: 'new password',
557 email: 'updated@example.com'
560 const user = await server.usersCommand.getMyInfo({ token: accessTokenUser })
561 expect(user.username).to.equal('user_1')
562 expect(user.email).to.equal('updated@example.com')
563 expect(user.nsfwPolicy).to.equal('do_not_list')
564 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
565 expect(user.id).to.be.a('number')
566 expect(user.account.displayName).to.equal('user_1')
567 expect(user.account.description).to.be.null
570 it('Should be able to update my avatar with a gif', async function () {
571 const fixture = 'avatar.gif'
573 await server.usersCommand.updateMyAvatar({ token: accessTokenUser, fixture })
575 const user = await server.usersCommand.getMyInfo({ token: accessTokenUser })
576 await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.gif')
579 it('Should be able to update my avatar with a gif, and then a png', async function () {
580 for (const extension of [ '.png', '.gif' ]) {
581 const fixture = 'avatar' + extension
583 await server.usersCommand.updateMyAvatar({ token: accessTokenUser, fixture })
585 const user = await server.usersCommand.getMyInfo({ token: accessTokenUser })
586 await testImage(server.url, 'avatar-resized', user.account.avatar.path, extension)
590 it('Should be able to update my display name', async function () {
591 await server.usersCommand.updateMe({ token: accessTokenUser, displayName: 'new display name' })
593 const user = await server.usersCommand.getMyInfo({ token: accessTokenUser })
594 expect(user.username).to.equal('user_1')
595 expect(user.email).to.equal('updated@example.com')
596 expect(user.nsfwPolicy).to.equal('do_not_list')
597 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
598 expect(user.id).to.be.a('number')
599 expect(user.account.displayName).to.equal('new display name')
600 expect(user.account.description).to.be.null
603 it('Should be able to update my description', async function () {
604 await server.usersCommand.updateMe({ token: accessTokenUser, description: 'my super description updated' })
606 const user = await server.usersCommand.getMyInfo({ token: accessTokenUser })
607 expect(user.username).to.equal('user_1')
608 expect(user.email).to.equal('updated@example.com')
609 expect(user.nsfwPolicy).to.equal('do_not_list')
610 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
611 expect(user.id).to.be.a('number')
612 expect(user.account.displayName).to.equal('new display name')
613 expect(user.account.description).to.equal('my super description updated')
614 expect(user.noWelcomeModal).to.be.false
615 expect(user.noInstanceConfigWarningModal).to.be.false
618 it('Should be able to update my theme', async function () {
619 for (const theme of [ 'background-red', 'default', 'instance-default' ]) {
620 await server.usersCommand.updateMe({ token: accessTokenUser, theme })
622 const user = await server.usersCommand.getMyInfo({ token: accessTokenUser })
623 expect(user.theme).to.equal(theme)
627 it('Should be able to update my modal preferences', async function () {
628 await server.usersCommand.updateMe({
629 token: accessTokenUser,
630 noInstanceConfigWarningModal: true,
634 const user = await server.usersCommand.getMyInfo({ token: accessTokenUser })
635 expect(user.noWelcomeModal).to.be.true
636 expect(user.noInstanceConfigWarningModal).to.be.true
640 describe('Updating another user', function () {
641 it('Should be able to update another user', async function () {
642 await server.usersCommand.update({
645 email: 'updated2@example.com',
648 role: UserRole.MODERATOR,
649 adminFlags: UserAdminFlag.NONE,
653 const user = await server.usersCommand.get({ token: accessToken, userId })
655 expect(user.username).to.equal('user_1')
656 expect(user.email).to.equal('updated2@example.com')
657 expect(user.emailVerified).to.be.true
658 expect(user.nsfwPolicy).to.equal('do_not_list')
659 expect(user.videoQuota).to.equal(42)
660 expect(user.roleLabel).to.equal('Moderator')
661 expect(user.id).to.be.a('number')
662 expect(user.adminFlags).to.equal(UserAdminFlag.NONE)
663 expect(user.pluginAuth).to.equal('toto')
666 it('Should reset the auth plugin', async function () {
667 await server.usersCommand.update({ userId, token: accessToken, pluginAuth: null })
669 const user = await server.usersCommand.get({ token: accessToken, userId })
670 expect(user.pluginAuth).to.be.null
673 it('Should have removed the user token', async function () {
674 await server.usersCommand.getMyQuotaUsed({ token: accessTokenUser, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
676 accessTokenUser = await server.loginCommand.getAccessToken(user)
679 it('Should be able to update another user password', async function () {
680 await server.usersCommand.update({ userId, token: accessToken, password: 'password updated' })
682 await server.usersCommand.getMyQuotaUsed({ token: accessTokenUser, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
684 await server.loginCommand.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
686 user.password = 'password updated'
687 accessTokenUser = await server.loginCommand.getAccessToken(user)
691 describe('Video blacklists', function () {
692 it('Should be able to list video blacklist by a moderator', async function () {
693 await server.blacklistCommand.list({ token: accessTokenUser })
697 describe('Remove a user', function () {
698 it('Should be able to remove this user', async function () {
699 await server.usersCommand.remove({ userId, token: accessToken })
702 it('Should not be able to login with this user', async function () {
703 await server.loginCommand.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
706 it('Should not have videos of this user', async function () {
707 const res = await getVideosList(server.url)
709 expect(res.body.total).to.equal(1)
711 const video = res.body.data[0]
712 expect(video.account.name).to.equal('root')
716 describe('Registering a new user', function () {
717 let user15AccessToken
719 it('Should register a new user', async function () {
720 const user = { displayName: 'super user 15', username: 'user_15', password: 'my super password' }
721 const channel = { name: 'my_user_15_channel', displayName: 'my channel rocks' }
723 await server.usersCommand.register({ ...user, channel })
726 it('Should be able to login with this registered user', async function () {
729 password: 'my super password'
732 user15AccessToken = await server.loginCommand.getAccessToken(user15)
735 it('Should have the correct display name', async function () {
736 const user = await server.usersCommand.getMyInfo({ token: user15AccessToken })
737 expect(user.account.displayName).to.equal('super user 15')
740 it('Should have the correct video quota', async function () {
741 const user = await server.usersCommand.getMyInfo({ token: user15AccessToken })
742 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
745 it('Should have created the channel', async function () {
746 const { displayName } = await server.channelsCommand.get({ channelName: 'my_user_15_channel' })
748 expect(displayName).to.equal('my channel rocks')
751 it('Should remove me', async function () {
753 const { data } = await server.usersCommand.list()
754 expect(data.find(u => u.username === 'user_15')).to.not.be.undefined
757 await server.usersCommand.deleteMe({ token: user15AccessToken })
760 const { data } = await server.usersCommand.list()
761 expect(data.find(u => u.username === 'user_15')).to.be.undefined
766 describe('User blocking', function () {
768 let user16AccessToken
771 password: 'my super password'
774 it('Should block a user', async function () {
775 const user = await server.usersCommand.create({ ...user16 })
778 user16AccessToken = await server.loginCommand.getAccessToken(user16)
780 await server.usersCommand.getMyInfo({ token: user16AccessToken, expectedStatus: HttpStatusCode.OK_200 })
781 await server.usersCommand.banUser({ userId: user16Id })
783 await server.usersCommand.getMyInfo({ token: user16AccessToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
784 await server.loginCommand.login({ user: user16, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
787 it('Should search user by banned status', async function () {
789 const { data, total } = await server.usersCommand.list({ start: 0, count: 2, sort: 'createdAt', blocked: true })
790 expect(total).to.equal(1)
791 expect(data.length).to.equal(1)
793 expect(data[0].username).to.equal(user16.username)
797 const { data, total } = await server.usersCommand.list({ start: 0, count: 2, sort: 'createdAt', blocked: false })
798 expect(total).to.equal(1)
799 expect(data.length).to.equal(1)
801 expect(data[0].username).to.not.equal(user16.username)
805 it('Should unblock a user', async function () {
806 await server.usersCommand.unbanUser({ userId: user16Id })
807 user16AccessToken = await server.loginCommand.getAccessToken(user16)
808 await server.usersCommand.getMyInfo({ token: user16AccessToken, expectedStatus: HttpStatusCode.OK_200 })
812 describe('User stats', function () {
814 let user17AccessToken
816 it('Should report correct initial statistics about a user', async function () {
819 password: 'my super password'
821 const created = await server.usersCommand.create({ ...user17 })
823 user17Id = created.id
824 user17AccessToken = await server.loginCommand.getAccessToken(user17)
826 const user = await server.usersCommand.get({ userId: user17Id, withStats: true })
827 expect(user.videosCount).to.equal(0)
828 expect(user.videoCommentsCount).to.equal(0)
829 expect(user.abusesCount).to.equal(0)
830 expect(user.abusesCreatedCount).to.equal(0)
831 expect(user.abusesAcceptedCount).to.equal(0)
834 it('Should report correct videos count', async function () {
835 const videoAttributes = {
836 name: 'video to test user stats'
838 await uploadVideo(server.url, user17AccessToken, videoAttributes)
839 const res1 = await getVideosList(server.url)
840 videoId = res1.body.data.find(video => video.name === videoAttributes.name).id
842 const user = await server.usersCommand.get({ userId: user17Id, withStats: true })
843 expect(user.videosCount).to.equal(1)
846 it('Should report correct video comments for user', async function () {
847 const text = 'super comment'
848 await server.commentsCommand.createThread({ token: user17AccessToken, videoId, text })
850 const user = await server.usersCommand.get({ userId: user17Id, withStats: true })
851 expect(user.videoCommentsCount).to.equal(1)
854 it('Should report correct abuses counts', async function () {
855 const reason = 'my super bad reason'
856 await server.abusesCommand.report({ token: user17AccessToken, videoId, reason })
858 const body1 = await server.abusesCommand.getAdminList()
859 const abuseId = body1.data[0].id
861 const user2 = await server.usersCommand.get({ userId: user17Id, withStats: true })
862 expect(user2.abusesCount).to.equal(1) // number of incriminations
863 expect(user2.abusesCreatedCount).to.equal(1) // number of reports created
865 await server.abusesCommand.update({ abuseId, body: { state: AbuseState.ACCEPTED } })
867 const user3 = await server.usersCommand.get({ userId: user17Id, withStats: true })
868 expect(user3.abusesAcceptedCount).to.equal(1) // number of reports created accepted
872 after(async function () {
873 await cleanupTests([ server ])