1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { User, UserRole, Video } from '../../../../shared/index'
13 getBlacklistedVideosList,
15 getMyUserVideoQuotaUsed,
19 getUsersListPaginationAndSort,
25 registerUserWithChannel,
36 } from '../../../../shared/extra-utils'
37 import { follow } from '../../../../shared/extra-utils/server/follows'
38 import { setAccessTokensToServers } from '../../../../shared/extra-utils/users/login'
39 import { getMyVideos } from '../../../../shared/extra-utils/videos/videos'
40 import { UserAdminFlag } from '../../../../shared/models/users/user-flag.model'
42 const expect = chai.expect
44 describe('Test users', function () {
45 let server: ServerInfo
46 let accessToken: string
47 let accessTokenUser: string
52 password: 'super password'
55 before(async function () {
57 server = await flushAndRunServer(1)
59 await setAccessTokensToServers([ server ])
62 describe('OAuth client', function () {
63 it('Should create a new client')
65 it('Should return the first client')
67 it('Should remove the last client')
69 it('Should not login with an invalid client id', async function () {
70 const client = { id: 'client', secret: server.client.secret }
71 const res = await login(server.url, client, server.user, 400)
73 expect(res.body.error).to.contain('client is invalid')
76 it('Should not login with an invalid client secret', async function () {
77 const client = { id: server.client.id, secret: 'coucou' }
78 const res = await login(server.url, client, server.user, 400)
80 expect(res.body.error).to.contain('client is invalid')
84 describe('Login', function () {
86 it('Should not login with an invalid username', async function () {
87 const user = { username: 'captain crochet', password: server.user.password }
88 const res = await login(server.url, server.client, user, 400)
90 expect(res.body.error).to.contain('credentials are invalid')
93 it('Should not login with an invalid password', async function () {
94 const user = { username: server.user.username, password: 'mew_three' }
95 const res = await login(server.url, server.client, user, 400)
97 expect(res.body.error).to.contain('credentials are invalid')
100 it('Should not be able to upload a video', async function () {
101 accessToken = 'my_super_token'
103 const videoAttributes = {}
104 await uploadVideo(server.url, accessToken, videoAttributes, 401)
107 it('Should not be able to follow', async function () {
108 accessToken = 'my_super_token'
109 await follow(server.url, [ 'http://example.com' ], accessToken, 401)
112 it('Should not be able to unfollow')
114 it('Should be able to login', async function () {
115 const res = await login(server.url, server.client, server.user, 200)
117 accessToken = res.body.access_token
120 it('Should be able to login with an insensitive username', async function () {
121 const user = { username: 'RoOt', password: server.user.password }
122 const res = await login(server.url, server.client, user, 200)
124 const user2 = { username: 'rOoT', password: server.user.password }
125 const res2 = await login(server.url, server.client, user2, 200)
127 const user3 = { username: 'ROOt', password: server.user.password }
128 const res3 = await login(server.url, server.client, user3, 200)
132 describe('Upload', function () {
134 it('Should upload the video with the correct token', async function () {
135 const videoAttributes = {}
136 await uploadVideo(server.url, accessToken, videoAttributes)
137 const res = await getVideosList(server.url)
138 const video = res.body.data[ 0 ]
140 expect(video.account.name).to.equal('root')
144 it('Should upload the video again with the correct token', async function () {
145 const videoAttributes = {}
146 await uploadVideo(server.url, accessToken, videoAttributes)
150 describe('Ratings', function () {
152 it('Should retrieve a video rating', async function () {
153 await rateVideo(server.url, accessToken, videoId, 'like')
154 const res = await getMyUserVideoRating(server.url, accessToken, videoId)
155 const rating = res.body
157 expect(rating.videoId).to.equal(videoId)
158 expect(rating.rating).to.equal('like')
161 it('Should retrieve ratings list', async function () {
162 await rateVideo(server.url, accessToken, videoId, 'like')
164 const res = await getAccountRatings(server.url, server.user.username, server.accessToken, null, 200)
165 const ratings = res.body
167 expect(ratings.total).to.equal(1)
168 expect(ratings.data[ 0 ].video.id).to.equal(videoId)
169 expect(ratings.data[ 0 ].rating).to.equal('like')
172 it('Should retrieve ratings list by rating type', async function () {
174 const res = await getAccountRatings(server.url, server.user.username, server.accessToken, 'like')
175 const ratings = res.body
176 expect(ratings.data.length).to.equal(1)
180 const res = await getAccountRatings(server.url, server.user.username, server.accessToken, 'dislike')
181 const ratings = res.body
182 expect(ratings.data.length).to.equal(0)
187 describe('Remove video', function () {
188 it('Should not be able to remove the video with an incorrect token', async function () {
189 await removeVideo(server.url, 'bad_token', videoId, 401)
192 it('Should not be able to remove the video with the token of another account')
194 it('Should be able to remove the video with the correct token', async function () {
195 await removeVideo(server.url, accessToken, videoId)
199 describe('Logout', function () {
200 it('Should logout (revoke token)')
202 it('Should not be able to get the user information')
204 it('Should not be able to upload a video')
206 it('Should not be able to remove a video')
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: 401
221 await makePutBodyRequest(options)
224 it('Should be able to login again')
226 it('Should have an expired access token')
228 it('Should refresh the token')
230 it('Should be able to upload a video again')
233 describe('Creating a user', function () {
235 it('Should be able to create a new user', async function () {
238 accessToken: accessToken,
239 username: user.username,
240 password: user.password,
241 videoQuota: 2 * 1024 * 1024,
242 adminFlags: UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST
246 it('Should be able to login with this user', async function () {
247 accessTokenUser = await userLogin(server, user)
250 it('Should be able to get user information', async function () {
251 const res1 = await getMyUserInformation(server.url, accessTokenUser)
252 const userMe: User = res1.body
254 const res2 = await getUserInformation(server.url, server.accessToken, userMe.id)
255 const userGet: User = res2.body
257 for (const user of [ userMe, userGet ]) {
258 expect(user.username).to.equal('user_1')
259 expect(user.email).to.equal('user_1@example.com')
260 expect(user.nsfwPolicy).to.equal('display')
261 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
262 expect(user.roleLabel).to.equal('User')
263 expect(user.id).to.be.a('number')
264 expect(user.account.displayName).to.equal('user_1')
265 expect(user.account.description).to.be.null
268 expect(userMe.adminFlags).to.be.undefined
269 expect(userGet.adminFlags).to.equal(UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST)
273 describe('My videos & quotas', function () {
275 it('Should be able to upload a video with this user', async function () {
278 const videoAttributes = {
279 name: 'super user video',
280 fixture: 'video_short.webm'
282 await uploadVideo(server.url, accessTokenUser, videoAttributes)
285 it('Should have video quota updated', async function () {
286 const res = await getMyUserVideoQuotaUsed(server.url, accessTokenUser)
287 const data = res.body
289 expect(data.videoQuotaUsed).to.equal(218910)
291 const resUsers = await getUsersList(server.url, server.accessToken)
293 const users: User[] = resUsers.body.data
294 const tmpUser = users.find(u => u.username === user.username)
295 expect(tmpUser.videoQuotaUsed).to.equal(218910)
298 it('Should be able to list my videos', async function () {
299 const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
300 expect(res.body.total).to.equal(1)
302 const videos = res.body.data
303 expect(videos).to.have.lengthOf(1)
305 const video: Video = videos[ 0 ]
306 expect(video.name).to.equal('super user video')
307 expect(video.thumbnailPath).to.not.be.null
308 expect(video.previewPath).to.not.be.null
312 describe('Users listing', function () {
314 it('Should list all the users', async function () {
315 const res = await getUsersList(server.url, server.accessToken)
316 const result = res.body
317 const total = result.total
318 const users = result.data
320 expect(total).to.equal(2)
321 expect(users).to.be.an('array')
322 expect(users.length).to.equal(2)
324 const user = users[ 0 ]
325 expect(user.username).to.equal('user_1')
326 expect(user.email).to.equal('user_1@example.com')
327 expect(user.nsfwPolicy).to.equal('display')
329 const rootUser = users[ 1 ]
330 expect(rootUser.username).to.equal('root')
331 expect(rootUser.email).to.equal('admin' + server.internalServerNumber + '@example.com')
332 expect(user.nsfwPolicy).to.equal('display')
337 it('Should list only the first user by username asc', async function () {
338 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username')
340 const result = res.body
341 const total = result.total
342 const users = result.data
344 expect(total).to.equal(2)
345 expect(users.length).to.equal(1)
347 const user = users[ 0 ]
348 expect(user.username).to.equal('root')
349 expect(user.email).to.equal('admin' + server.internalServerNumber + '@example.com')
350 expect(user.roleLabel).to.equal('Administrator')
351 expect(user.nsfwPolicy).to.equal('display')
354 it('Should list only the first user by username desc', async function () {
355 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username')
356 const result = res.body
357 const total = result.total
358 const users = result.data
360 expect(total).to.equal(2)
361 expect(users.length).to.equal(1)
363 const user = users[ 0 ]
364 expect(user.username).to.equal('user_1')
365 expect(user.email).to.equal('user_1@example.com')
366 expect(user.nsfwPolicy).to.equal('display')
369 it('Should list only the second user by createdAt desc', async function () {
370 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt')
371 const result = res.body
372 const total = result.total
373 const users = result.data
375 expect(total).to.equal(2)
376 expect(users.length).to.equal(1)
378 const user = users[ 0 ]
379 expect(user.username).to.equal('user_1')
380 expect(user.email).to.equal('user_1@example.com')
381 expect(user.nsfwPolicy).to.equal('display')
384 it('Should list all the users by createdAt asc', async function () {
385 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt')
386 const result = res.body
387 const total = result.total
388 const users = result.data
390 expect(total).to.equal(2)
391 expect(users.length).to.equal(2)
393 expect(users[ 0 ].username).to.equal('root')
394 expect(users[ 0 ].email).to.equal('admin' + server.internalServerNumber + '@example.com')
395 expect(users[ 0 ].nsfwPolicy).to.equal('display')
397 expect(users[ 1 ].username).to.equal('user_1')
398 expect(users[ 1 ].email).to.equal('user_1@example.com')
399 expect(users[ 1 ].nsfwPolicy).to.equal('display')
402 it('Should search user by username', async function () {
403 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'oot')
404 const users = res.body.data as User[]
406 expect(res.body.total).to.equal(1)
407 expect(users.length).to.equal(1)
409 expect(users[ 0 ].username).to.equal('root')
412 it('Should search user by email', async function () {
414 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'r_1@exam')
415 const users = res.body.data as User[]
417 expect(res.body.total).to.equal(1)
418 expect(users.length).to.equal(1)
420 expect(users[ 0 ].username).to.equal('user_1')
421 expect(users[ 0 ].email).to.equal('user_1@example.com')
425 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'example')
426 const users = res.body.data as User[]
428 expect(res.body.total).to.equal(2)
429 expect(users.length).to.equal(2)
431 expect(users[ 0 ].username).to.equal('root')
432 expect(users[ 1 ].username).to.equal('user_1')
437 describe('Update my account', function () {
438 it('Should update my password', async function () {
441 accessToken: accessTokenUser,
442 currentPassword: 'super password',
443 newPassword: 'new password'
445 user.password = 'new password'
447 await userLogin(server, user, 200)
450 it('Should be able to change the NSFW display attribute', async function () {
453 accessToken: accessTokenUser,
454 nsfwPolicy: 'do_not_list'
457 const res = await getMyUserInformation(server.url, accessTokenUser)
458 const user = res.body
460 expect(user.username).to.equal('user_1')
461 expect(user.email).to.equal('user_1@example.com')
462 expect(user.nsfwPolicy).to.equal('do_not_list')
463 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
464 expect(user.id).to.be.a('number')
465 expect(user.account.displayName).to.equal('user_1')
466 expect(user.account.description).to.be.null
469 it('Should be able to change the autoPlayVideo attribute', async function () {
472 accessToken: accessTokenUser,
476 const res = await getMyUserInformation(server.url, accessTokenUser)
477 const user = res.body
479 expect(user.autoPlayVideo).to.be.false
482 it('Should be able to change the email attribute', async function () {
485 accessToken: accessTokenUser,
486 currentPassword: 'new password',
487 email: 'updated@example.com'
490 const res = await getMyUserInformation(server.url, accessTokenUser)
491 const user = res.body
493 expect(user.username).to.equal('user_1')
494 expect(user.email).to.equal('updated@example.com')
495 expect(user.nsfwPolicy).to.equal('do_not_list')
496 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
497 expect(user.id).to.be.a('number')
498 expect(user.account.displayName).to.equal('user_1')
499 expect(user.account.description).to.be.null
502 it('Should be able to update my avatar', async function () {
503 const fixture = 'avatar.png'
505 await updateMyAvatar({
507 accessToken: accessTokenUser,
511 const res = await getMyUserInformation(server.url, accessTokenUser)
512 const user = res.body
514 await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.png')
517 it('Should be able to update my display name', async function () {
520 accessToken: accessTokenUser,
521 displayName: 'new display name'
524 const res = await getMyUserInformation(server.url, accessTokenUser)
525 const user = res.body
527 expect(user.username).to.equal('user_1')
528 expect(user.email).to.equal('updated@example.com')
529 expect(user.nsfwPolicy).to.equal('do_not_list')
530 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
531 expect(user.id).to.be.a('number')
532 expect(user.account.displayName).to.equal('new display name')
533 expect(user.account.description).to.be.null
536 it('Should be able to update my description', async function () {
539 accessToken: accessTokenUser,
540 description: 'my super description updated'
543 const res = await getMyUserInformation(server.url, accessTokenUser)
544 const user = res.body
546 expect(user.username).to.equal('user_1')
547 expect(user.email).to.equal('updated@example.com')
548 expect(user.nsfwPolicy).to.equal('do_not_list')
549 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
550 expect(user.id).to.be.a('number')
551 expect(user.account.displayName).to.equal('new display name')
552 expect(user.account.description).to.equal('my super description updated')
556 describe('Updating another user', function () {
558 it('Should be able to update another user', async function () {
563 email: 'updated2@example.com',
566 role: UserRole.MODERATOR,
567 adminFlags: UserAdminFlag.NONE
570 const res = await getUserInformation(server.url, accessToken, userId)
571 const user = res.body
573 expect(user.username).to.equal('user_1')
574 expect(user.email).to.equal('updated2@example.com')
575 expect(user.emailVerified).to.be.true
576 expect(user.nsfwPolicy).to.equal('do_not_list')
577 expect(user.videoQuota).to.equal(42)
578 expect(user.roleLabel).to.equal('Moderator')
579 expect(user.id).to.be.a('number')
580 expect(user.adminFlags).to.equal(UserAdminFlag.NONE)
583 it('Should have removed the user token', async function () {
584 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
586 accessTokenUser = await userLogin(server, user)
589 it('Should be able to update another user password', async function () {
594 password: 'password updated'
597 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
599 await userLogin(server, user, 400)
601 user.password = 'password updated'
602 accessTokenUser = await userLogin(server, user)
606 describe('Video blacklists', function () {
607 it('Should be able to list video blacklist by a moderator', async function () {
608 await getBlacklistedVideosList({ url: server.url, token: accessTokenUser })
612 describe('Remove a user', function () {
613 it('Should be able to remove this user', async function () {
614 await removeUser(server.url, userId, accessToken)
617 it('Should not be able to login with this user', async function () {
618 await userLogin(server, user, 400)
621 it('Should not have videos of this user', async function () {
622 const res = await getVideosList(server.url)
624 expect(res.body.total).to.equal(1)
626 const video = res.body.data[ 0 ]
627 expect(video.account.name).to.equal('root')
631 describe('Registering a new user', function () {
632 it('Should register a new user', async function () {
633 const user = { displayName: 'super user 15', username: 'user_15', password: 'my super password' }
634 const channel = { name: 'my_user_15_channel', displayName: 'my channel rocks' }
636 await registerUserWithChannel({ url: server.url, user, channel })
639 it('Should be able to login with this registered user', async function () {
642 password: 'my super password'
645 accessToken = await userLogin(server, user15)
648 it('Should have the correct display name', async function () {
649 const res = await getMyUserInformation(server.url, accessToken)
650 const user: User = res.body
652 expect(user.account.displayName).to.equal('super user 15')
655 it('Should have the correct video quota', async function () {
656 const res = await getMyUserInformation(server.url, accessToken)
657 const user = res.body
659 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
662 it('Should have created the channel', async function () {
663 const res = await getVideoChannel(server.url, 'my_user_15_channel')
665 expect(res.body.displayName).to.equal('my channel rocks')
668 it('Should remove me', async function () {
670 const res = await getUsersList(server.url, server.accessToken)
671 expect(res.body.data.find(u => u.username === 'user_15')).to.not.be.undefined
674 await deleteMe(server.url, accessToken)
677 const res = await getUsersList(server.url, server.accessToken)
678 expect(res.body.data.find(u => u.username === 'user_15')).to.be.undefined
683 describe('User blocking', function () {
684 it('Should block and unblock a user', async function () {
687 password: 'my super password'
689 const resUser = await createUser({
691 accessToken: server.accessToken,
692 username: user16.username,
693 password: user16.password
695 const user16Id = resUser.body.user.id
697 accessToken = await userLogin(server, user16)
699 await getMyUserInformation(server.url, accessToken, 200)
700 await blockUser(server.url, user16Id, server.accessToken)
702 await getMyUserInformation(server.url, accessToken, 401)
703 await userLogin(server, user16, 400)
705 await unblockUser(server.url, user16Id, server.accessToken)
706 accessToken = await userLogin(server, user16)
707 await getMyUserInformation(server.url, accessToken, 200)
711 after(async function () {
712 await cleanupTests([ server ])