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
121 describe('Upload', function () {
123 it('Should upload the video with the correct token', async function () {
124 const videoAttributes = {}
125 await uploadVideo(server.url, accessToken, videoAttributes)
126 const res = await getVideosList(server.url)
127 const video = res.body.data[ 0 ]
129 expect(video.account.name).to.equal('root')
133 it('Should upload the video again with the correct token', async function () {
134 const videoAttributes = {}
135 await uploadVideo(server.url, accessToken, videoAttributes)
139 describe('Ratings', function () {
141 it('Should retrieve a video rating', async function () {
142 await rateVideo(server.url, accessToken, videoId, 'like')
143 const res = await getMyUserVideoRating(server.url, accessToken, videoId)
144 const rating = res.body
146 expect(rating.videoId).to.equal(videoId)
147 expect(rating.rating).to.equal('like')
150 it('Should retrieve ratings list', async function () {
151 await rateVideo(server.url, accessToken, videoId, 'like')
153 const res = await getAccountRatings(server.url, server.user.username, server.accessToken, null, 200)
154 const ratings = res.body
156 expect(ratings.total).to.equal(1)
157 expect(ratings.data[ 0 ].video.id).to.equal(videoId)
158 expect(ratings.data[ 0 ].rating).to.equal('like')
161 it('Should retrieve ratings list by rating type', async function () {
163 const res = await getAccountRatings(server.url, server.user.username, server.accessToken, 'like')
164 const ratings = res.body
165 expect(ratings.data.length).to.equal(1)
169 const res = await getAccountRatings(server.url, server.user.username, server.accessToken, 'dislike')
170 const ratings = res.body
171 expect(ratings.data.length).to.equal(0)
176 describe('Remove video', function () {
177 it('Should not be able to remove the video with an incorrect token', async function () {
178 await removeVideo(server.url, 'bad_token', videoId, 401)
181 it('Should not be able to remove the video with the token of another account')
183 it('Should be able to remove the video with the correct token', async function () {
184 await removeVideo(server.url, accessToken, videoId)
188 describe('Logout', function () {
189 it('Should logout (revoke token)')
191 it('Should not be able to get the user information')
193 it('Should not be able to upload a video')
195 it('Should not be able to remove a video')
197 it('Should not be able to rate a video', async function () {
198 const path = '/api/v1/videos/'
205 path: path + videoId,
206 token: 'wrong token',
208 statusCodeExpected: 401
210 await makePutBodyRequest(options)
213 it('Should be able to login again')
215 it('Should have an expired access token')
217 it('Should refresh the token')
219 it('Should be able to upload a video again')
222 describe('Creating a user', function () {
224 it('Should be able to create a new user', async function () {
227 accessToken: accessToken,
228 username: user.username,
229 password: user.password,
230 videoQuota: 2 * 1024 * 1024,
231 adminFlags: UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST
235 it('Should be able to login with this user', async function () {
236 accessTokenUser = await userLogin(server, user)
239 it('Should be able to get user information', async function () {
240 const res1 = await getMyUserInformation(server.url, accessTokenUser)
241 const userMe: User = res1.body
243 const res2 = await getUserInformation(server.url, server.accessToken, userMe.id)
244 const userGet: User = res2.body
246 for (const user of [ userMe, userGet ]) {
247 expect(user.username).to.equal('user_1')
248 expect(user.email).to.equal('user_1@example.com')
249 expect(user.nsfwPolicy).to.equal('display')
250 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
251 expect(user.roleLabel).to.equal('User')
252 expect(user.id).to.be.a('number')
253 expect(user.account.displayName).to.equal('user_1')
254 expect(user.account.description).to.be.null
257 expect(userMe.adminFlags).to.be.undefined
258 expect(userGet.adminFlags).to.equal(UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST)
262 describe('My videos & quotas', function () {
264 it('Should be able to upload a video with this user', async function () {
267 const videoAttributes = {
268 name: 'super user video',
269 fixture: 'video_short.webm'
271 await uploadVideo(server.url, accessTokenUser, videoAttributes)
274 it('Should have video quota updated', async function () {
275 const res = await getMyUserVideoQuotaUsed(server.url, accessTokenUser)
276 const data = res.body
278 expect(data.videoQuotaUsed).to.equal(218910)
280 const resUsers = await getUsersList(server.url, server.accessToken)
282 const users: User[] = resUsers.body.data
283 const tmpUser = users.find(u => u.username === user.username)
284 expect(tmpUser.videoQuotaUsed).to.equal(218910)
287 it('Should be able to list my videos', async function () {
288 const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
289 expect(res.body.total).to.equal(1)
291 const videos = res.body.data
292 expect(videos).to.have.lengthOf(1)
294 const video: Video = videos[ 0 ]
295 expect(video.name).to.equal('super user video')
296 expect(video.thumbnailPath).to.not.be.null
297 expect(video.previewPath).to.not.be.null
301 describe('Users listing', function () {
303 it('Should list all the users', async function () {
304 const res = await getUsersList(server.url, server.accessToken)
305 const result = res.body
306 const total = result.total
307 const users = result.data
309 expect(total).to.equal(2)
310 expect(users).to.be.an('array')
311 expect(users.length).to.equal(2)
313 const user = users[ 0 ]
314 expect(user.username).to.equal('user_1')
315 expect(user.email).to.equal('user_1@example.com')
316 expect(user.nsfwPolicy).to.equal('display')
318 const rootUser = users[ 1 ]
319 expect(rootUser.username).to.equal('root')
320 expect(rootUser.email).to.equal('admin' + server.internalServerNumber + '@example.com')
321 expect(user.nsfwPolicy).to.equal('display')
326 it('Should list only the first user by username asc', async function () {
327 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username')
329 const result = res.body
330 const total = result.total
331 const users = result.data
333 expect(total).to.equal(2)
334 expect(users.length).to.equal(1)
336 const user = users[ 0 ]
337 expect(user.username).to.equal('root')
338 expect(user.email).to.equal('admin' + server.internalServerNumber + '@example.com')
339 expect(user.roleLabel).to.equal('Administrator')
340 expect(user.nsfwPolicy).to.equal('display')
343 it('Should list only the first user by username desc', async function () {
344 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username')
345 const result = res.body
346 const total = result.total
347 const users = result.data
349 expect(total).to.equal(2)
350 expect(users.length).to.equal(1)
352 const user = users[ 0 ]
353 expect(user.username).to.equal('user_1')
354 expect(user.email).to.equal('user_1@example.com')
355 expect(user.nsfwPolicy).to.equal('display')
358 it('Should list only the second user by createdAt desc', async function () {
359 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt')
360 const result = res.body
361 const total = result.total
362 const users = result.data
364 expect(total).to.equal(2)
365 expect(users.length).to.equal(1)
367 const user = users[ 0 ]
368 expect(user.username).to.equal('user_1')
369 expect(user.email).to.equal('user_1@example.com')
370 expect(user.nsfwPolicy).to.equal('display')
373 it('Should list all the users by createdAt asc', async function () {
374 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt')
375 const result = res.body
376 const total = result.total
377 const users = result.data
379 expect(total).to.equal(2)
380 expect(users.length).to.equal(2)
382 expect(users[ 0 ].username).to.equal('root')
383 expect(users[ 0 ].email).to.equal('admin' + server.internalServerNumber + '@example.com')
384 expect(users[ 0 ].nsfwPolicy).to.equal('display')
386 expect(users[ 1 ].username).to.equal('user_1')
387 expect(users[ 1 ].email).to.equal('user_1@example.com')
388 expect(users[ 1 ].nsfwPolicy).to.equal('display')
391 it('Should search user by username', async function () {
392 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'oot')
393 const users = res.body.data as User[]
395 expect(res.body.total).to.equal(1)
396 expect(users.length).to.equal(1)
398 expect(users[ 0 ].username).to.equal('root')
401 it('Should search user by email', async function () {
403 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'r_1@exam')
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('user_1')
410 expect(users[ 0 ].email).to.equal('user_1@example.com')
414 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'example')
415 const users = res.body.data as User[]
417 expect(res.body.total).to.equal(2)
418 expect(users.length).to.equal(2)
420 expect(users[ 0 ].username).to.equal('root')
421 expect(users[ 1 ].username).to.equal('user_1')
426 describe('Update my account', function () {
427 it('Should update my password', async function () {
430 accessToken: accessTokenUser,
431 currentPassword: 'super password',
432 newPassword: 'new password'
434 user.password = 'new password'
436 await userLogin(server, user, 200)
439 it('Should be able to change the NSFW display attribute', async function () {
442 accessToken: accessTokenUser,
443 nsfwPolicy: 'do_not_list'
446 const res = await getMyUserInformation(server.url, accessTokenUser)
447 const user = res.body
449 expect(user.username).to.equal('user_1')
450 expect(user.email).to.equal('user_1@example.com')
451 expect(user.nsfwPolicy).to.equal('do_not_list')
452 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
453 expect(user.id).to.be.a('number')
454 expect(user.account.displayName).to.equal('user_1')
455 expect(user.account.description).to.be.null
458 it('Should be able to change the autoPlayVideo attribute', async function () {
461 accessToken: accessTokenUser,
465 const res = await getMyUserInformation(server.url, accessTokenUser)
466 const user = res.body
468 expect(user.autoPlayVideo).to.be.false
471 it('Should be able to change the email display attribute', async function () {
474 accessToken: accessTokenUser,
475 email: 'updated@example.com'
478 const res = await getMyUserInformation(server.url, accessTokenUser)
479 const user = res.body
481 expect(user.username).to.equal('user_1')
482 expect(user.email).to.equal('updated@example.com')
483 expect(user.nsfwPolicy).to.equal('do_not_list')
484 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
485 expect(user.id).to.be.a('number')
486 expect(user.account.displayName).to.equal('user_1')
487 expect(user.account.description).to.be.null
490 it('Should be able to update my avatar', async function () {
491 const fixture = 'avatar.png'
493 await updateMyAvatar({
495 accessToken: accessTokenUser,
499 const res = await getMyUserInformation(server.url, accessTokenUser)
500 const user = res.body
502 await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.png')
505 it('Should be able to update my display name', async function () {
508 accessToken: accessTokenUser,
509 displayName: 'new display name'
512 const res = await getMyUserInformation(server.url, accessTokenUser)
513 const user = res.body
515 expect(user.username).to.equal('user_1')
516 expect(user.email).to.equal('updated@example.com')
517 expect(user.nsfwPolicy).to.equal('do_not_list')
518 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
519 expect(user.id).to.be.a('number')
520 expect(user.account.displayName).to.equal('new display name')
521 expect(user.account.description).to.be.null
524 it('Should be able to update my description', async function () {
527 accessToken: accessTokenUser,
528 description: 'my super description updated'
531 const res = await getMyUserInformation(server.url, accessTokenUser)
532 const user = res.body
534 expect(user.username).to.equal('user_1')
535 expect(user.email).to.equal('updated@example.com')
536 expect(user.nsfwPolicy).to.equal('do_not_list')
537 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
538 expect(user.id).to.be.a('number')
539 expect(user.account.displayName).to.equal('new display name')
540 expect(user.account.description).to.equal('my super description updated')
544 describe('Updating another user', function () {
546 it('Should be able to update another user', async function () {
551 email: 'updated2@example.com',
554 role: UserRole.MODERATOR,
555 adminFlags: UserAdminFlag.NONE
558 const res = await getUserInformation(server.url, accessToken, userId)
559 const user = res.body
561 expect(user.username).to.equal('user_1')
562 expect(user.email).to.equal('updated2@example.com')
563 expect(user.emailVerified).to.be.true
564 expect(user.nsfwPolicy).to.equal('do_not_list')
565 expect(user.videoQuota).to.equal(42)
566 expect(user.roleLabel).to.equal('Moderator')
567 expect(user.id).to.be.a('number')
568 expect(user.adminFlags).to.equal(UserAdminFlag.NONE)
571 it('Should have removed the user token', async function () {
572 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
574 accessTokenUser = await userLogin(server, user)
577 it('Should be able to update another user password', async function () {
582 password: 'password updated'
585 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
587 await userLogin(server, user, 400)
589 user.password = 'password updated'
590 accessTokenUser = await userLogin(server, user)
594 describe('Video blacklists', function () {
595 it('Should be able to list video blacklist by a moderator', async function () {
596 await getBlacklistedVideosList({ url: server.url, token: accessTokenUser })
600 describe('Remove a user', function () {
601 it('Should be able to remove this user', async function () {
602 await removeUser(server.url, userId, accessToken)
605 it('Should not be able to login with this user', async function () {
606 await userLogin(server, user, 400)
609 it('Should not have videos of this user', async function () {
610 const res = await getVideosList(server.url)
612 expect(res.body.total).to.equal(1)
614 const video = res.body.data[ 0 ]
615 expect(video.account.name).to.equal('root')
619 describe('Registering a new user', function () {
620 it('Should register a new user', async function () {
621 const user = { displayName: 'super user 15', username: 'user_15', password: 'my super password' }
622 const channel = { name: 'my_user_15_channel', displayName: 'my channel rocks' }
624 await registerUserWithChannel({ url: server.url, user, channel })
627 it('Should be able to login with this registered user', async function () {
630 password: 'my super password'
633 accessToken = await userLogin(server, user15)
636 it('Should have the correct display name', async function () {
637 const res = await getMyUserInformation(server.url, accessToken)
638 const user: User = res.body
640 expect(user.account.displayName).to.equal('super user 15')
643 it('Should have the correct video quota', async function () {
644 const res = await getMyUserInformation(server.url, accessToken)
645 const user = res.body
647 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
650 it('Should have created the channel', async function () {
651 const res = await getVideoChannel(server.url, 'my_user_15_channel')
653 expect(res.body.displayName).to.equal('my channel rocks')
656 it('Should remove me', async function () {
658 const res = await getUsersList(server.url, server.accessToken)
659 expect(res.body.data.find(u => u.username === 'user_15')).to.not.be.undefined
662 await deleteMe(server.url, accessToken)
665 const res = await getUsersList(server.url, server.accessToken)
666 expect(res.body.data.find(u => u.username === 'user_15')).to.be.undefined
671 describe('User blocking', function () {
672 it('Should block and unblock a user', async function () {
675 password: 'my super password'
677 const resUser = await createUser({
679 accessToken: server.accessToken,
680 username: user16.username,
681 password: user16.password
683 const user16Id = resUser.body.user.id
685 accessToken = await userLogin(server, user16)
687 await getMyUserInformation(server.url, accessToken, 200)
688 await blockUser(server.url, user16Id, server.accessToken)
690 await getMyUserInformation(server.url, accessToken, 401)
691 await userLogin(server, user16, 400)
693 await unblockUser(server.url, user16Id, server.accessToken)
694 accessToken = await userLogin(server, user16)
695 await getMyUserInformation(server.url, accessToken, 200)
699 after(async function () {
700 await cleanupTests([ server ])