1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { User, UserRole } from '../../../../shared/index'
12 getBlacklistedVideosList,
14 getMyUserVideoQuotaUsed,
18 getUsersListPaginationAndSort,
36 } from '../../../../shared/utils'
37 import { follow } from '../../../../shared/utils/server/follows'
38 import { setAccessTokensToServers } from '../../../../shared/utils/users/login'
39 import { getMyVideos } from '../../../../shared/utils/videos/videos'
41 const expect = chai.expect
43 describe('Test users', function () {
44 let server: ServerInfo
45 let accessToken: string
46 let accessTokenUser: string
51 password: 'super password'
54 before(async function () {
58 server = await runServer(1)
60 await setAccessTokensToServers([ server ])
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')
83 it('Should not login with an invalid username', async function () {
84 const user = { username: 'captain crochet', password: server.user.password }
85 const res = await login(server.url, server.client, user, 400)
87 expect(res.body.error).to.contain('credentials are invalid')
90 it('Should not login with an invalid password', async function () {
91 const user = { username: server.user.username, password: 'mew_three' }
92 const res = await login(server.url, server.client, user, 400)
94 expect(res.body.error).to.contain('credentials are invalid')
97 it('Should not be able to upload a video', async function () {
98 accessToken = 'my_super_token'
100 const videoAttributes = {}
101 await uploadVideo(server.url, accessToken, videoAttributes, 401)
104 it('Should not be able to follow', async function () {
105 accessToken = 'my_super_token'
106 await follow(server.url, [ 'http://example.com' ], accessToken, 401)
109 it('Should not be able to unfollow')
111 it('Should be able to login', async function () {
112 const res = await login(server.url, server.client, server.user, 200)
114 accessToken = res.body.access_token
117 it('Should upload the video with the correct token', async function () {
118 const videoAttributes = {}
119 await uploadVideo(server.url, accessToken, videoAttributes)
120 const res = await getVideosList(server.url)
121 const video = res.body.data[ 0 ]
123 expect(video.account.name).to.equal('root')
127 it('Should upload the video again with the correct token', async function () {
128 const videoAttributes = {}
129 await uploadVideo(server.url, accessToken, videoAttributes)
132 it('Should retrieve a video rating', async function () {
133 await rateVideo(server.url, accessToken, videoId, 'like')
134 const res = await getMyUserVideoRating(server.url, accessToken, videoId)
135 const rating = res.body
137 expect(rating.videoId).to.equal(videoId)
138 expect(rating.rating).to.equal('like')
141 it('Should retrieve ratings list', async function () {
142 await rateVideo(server.url, accessToken, videoId, 'like')
144 const res = await getAccountRatings(server.url, server.user.username, server.accessToken, null, 200)
145 const ratings = res.body
147 expect(ratings.total).to.equal(1)
148 expect(ratings.data[0].video.id).to.equal(videoId)
149 expect(ratings.data[0].rating).to.equal('like')
152 it('Should retrieve ratings list by rating type', async function () {
154 const res = await getAccountRatings(server.url, server.user.username, server.accessToken, 'like')
155 const ratings = res.body
156 expect(ratings.data.length).to.equal(1)
160 const res = await getAccountRatings(server.url, server.user.username, server.accessToken, 'dislike')
161 const ratings = res.body
162 expect(ratings.data.length).to.equal(0)
166 it('Should not be able to remove the video with an incorrect token', async function () {
167 await removeVideo(server.url, 'bad_token', videoId, 401)
170 it('Should not be able to remove the video with the token of another account')
172 it('Should be able to remove the video with the correct token', async function () {
173 await removeVideo(server.url, accessToken, videoId)
176 it('Should logout (revoke token)')
178 it('Should not be able to get the user information')
180 it('Should not be able to upload a video')
182 it('Should not be able to remove a video')
184 it('Should not be able to rate a video', async function () {
185 const path = '/api/v1/videos/'
192 path: path + videoId,
193 token: 'wrong token',
195 statusCodeExpected: 401
197 await makePutBodyRequest(options)
200 it('Should be able to login again')
202 it('Should have an expired access token')
204 it('Should refresh the token')
206 it('Should be able to upload a video again')
208 it('Should be able to create a new user', async function () {
209 await createUser(server.url, accessToken, user.username, user.password, 2 * 1024 * 1024)
212 it('Should be able to login with this user', async function () {
213 accessTokenUser = await userLogin(server, user)
216 it('Should be able to get the user information', async function () {
217 const res = await getMyUserInformation(server.url, accessTokenUser)
218 const user = res.body
220 expect(user.username).to.equal('user_1')
221 expect(user.email).to.equal('user_1@example.com')
222 expect(user.nsfwPolicy).to.equal('display')
223 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
224 expect(user.roleLabel).to.equal('User')
225 expect(user.id).to.be.a('number')
226 expect(user.account.displayName).to.equal('user_1')
227 expect(user.account.description).to.be.null
230 it('Should be able to upload a video with this user', async function () {
233 const videoAttributes = {
234 name: 'super user video',
235 fixture: 'video_short.webm'
237 await uploadVideo(server.url, accessTokenUser, videoAttributes)
240 it('Should have video quota updated', async function () {
241 const res = await getMyUserVideoQuotaUsed(server.url, accessTokenUser)
242 const data = res.body
244 expect(data.videoQuotaUsed).to.equal(218910)
246 const resUsers = await getUsersList(server.url, server.accessToken)
248 const users: User[] = resUsers.body.data
249 const tmpUser = users.find(u => u.username === user.username)
250 expect(tmpUser.videoQuotaUsed).to.equal(218910)
253 it('Should be able to list my videos', async function () {
254 const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
255 expect(res.body.total).to.equal(1)
257 const videos = res.body.data
258 expect(videos).to.have.lengthOf(1)
260 expect(videos[ 0 ].name).to.equal('super user video')
263 it('Should list all the users', async function () {
264 const res = await getUsersList(server.url, server.accessToken)
265 const result = res.body
266 const total = result.total
267 const users = result.data
269 expect(total).to.equal(2)
270 expect(users).to.be.an('array')
271 expect(users.length).to.equal(2)
273 const user = users[ 0 ]
274 expect(user.username).to.equal('user_1')
275 expect(user.email).to.equal('user_1@example.com')
276 expect(user.nsfwPolicy).to.equal('display')
278 const rootUser = users[ 1 ]
279 expect(rootUser.username).to.equal('root')
280 expect(rootUser.email).to.equal('admin1@example.com')
281 expect(user.nsfwPolicy).to.equal('display')
286 it('Should list only the first user by username asc', async function () {
287 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username')
289 const result = res.body
290 const total = result.total
291 const users = result.data
293 expect(total).to.equal(2)
294 expect(users.length).to.equal(1)
296 const user = users[ 0 ]
297 expect(user.username).to.equal('root')
298 expect(user.email).to.equal('admin1@example.com')
299 expect(user.roleLabel).to.equal('Administrator')
300 expect(user.nsfwPolicy).to.equal('display')
303 it('Should list only the first user by username desc', async function () {
304 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username')
305 const result = res.body
306 const total = result.total
307 const users = result.data
309 expect(total).to.equal(2)
310 expect(users.length).to.equal(1)
312 const user = users[ 0 ]
313 expect(user.username).to.equal('user_1')
314 expect(user.email).to.equal('user_1@example.com')
315 expect(user.nsfwPolicy).to.equal('display')
318 it('Should list only the second user by createdAt desc', async function () {
319 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt')
320 const result = res.body
321 const total = result.total
322 const users = result.data
324 expect(total).to.equal(2)
325 expect(users.length).to.equal(1)
327 const user = users[ 0 ]
328 expect(user.username).to.equal('user_1')
329 expect(user.email).to.equal('user_1@example.com')
330 expect(user.nsfwPolicy).to.equal('display')
333 it('Should list all the users by createdAt asc', async function () {
334 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt')
335 const result = res.body
336 const total = result.total
337 const users = result.data
339 expect(total).to.equal(2)
340 expect(users.length).to.equal(2)
342 expect(users[ 0 ].username).to.equal('root')
343 expect(users[ 0 ].email).to.equal('admin1@example.com')
344 expect(users[ 0 ].nsfwPolicy).to.equal('display')
346 expect(users[ 1 ].username).to.equal('user_1')
347 expect(users[ 1 ].email).to.equal('user_1@example.com')
348 expect(users[ 1 ].nsfwPolicy).to.equal('display')
351 it('Should search user by username', async function () {
352 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'oot')
353 const users = res.body.data as User[]
355 expect(res.body.total).to.equal(1)
356 expect(users.length).to.equal(1)
358 expect(users[ 0 ].username).to.equal('root')
361 it('Should search user by email', async function () {
363 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'r_1@exam')
364 const users = res.body.data as User[]
366 expect(res.body.total).to.equal(1)
367 expect(users.length).to.equal(1)
369 expect(users[ 0 ].username).to.equal('user_1')
370 expect(users[ 0 ].email).to.equal('user_1@example.com')
374 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt', 'example')
375 const users = res.body.data as User[]
377 expect(res.body.total).to.equal(2)
378 expect(users.length).to.equal(2)
380 expect(users[ 0 ].username).to.equal('root')
381 expect(users[ 1 ].username).to.equal('user_1')
385 it('Should update my password', async function () {
388 accessToken: accessTokenUser,
389 currentPassword: 'super password',
390 newPassword: 'new password'
392 user.password = 'new password'
394 await userLogin(server, user, 200)
397 it('Should be able to change the NSFW display attribute', async function () {
400 accessToken: accessTokenUser,
401 nsfwPolicy: 'do_not_list'
404 const res = await getMyUserInformation(server.url, accessTokenUser)
405 const user = res.body
407 expect(user.username).to.equal('user_1')
408 expect(user.email).to.equal('user_1@example.com')
409 expect(user.nsfwPolicy).to.equal('do_not_list')
410 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
411 expect(user.id).to.be.a('number')
412 expect(user.account.displayName).to.equal('user_1')
413 expect(user.account.description).to.be.null
416 it('Should be able to change the autoPlayVideo attribute', async function () {
419 accessToken: accessTokenUser,
423 const res = await getMyUserInformation(server.url, accessTokenUser)
424 const user = res.body
426 expect(user.autoPlayVideo).to.be.false
429 it('Should be able to change the email display attribute', async function () {
432 accessToken: accessTokenUser,
433 email: 'updated@example.com'
436 const res = await getMyUserInformation(server.url, accessTokenUser)
437 const user = res.body
439 expect(user.username).to.equal('user_1')
440 expect(user.email).to.equal('updated@example.com')
441 expect(user.nsfwPolicy).to.equal('do_not_list')
442 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
443 expect(user.id).to.be.a('number')
444 expect(user.account.displayName).to.equal('user_1')
445 expect(user.account.description).to.be.null
448 it('Should be able to update my avatar', async function () {
449 const fixture = 'avatar.png'
451 await updateMyAvatar({
453 accessToken: accessTokenUser,
457 const res = await getMyUserInformation(server.url, accessTokenUser)
458 const user = res.body
460 await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.png')
463 it('Should be able to update my display name', async function () {
466 accessToken: accessTokenUser,
467 displayName: 'new display name'
470 const res = await getMyUserInformation(server.url, accessTokenUser)
471 const user = res.body
473 expect(user.username).to.equal('user_1')
474 expect(user.email).to.equal('updated@example.com')
475 expect(user.nsfwPolicy).to.equal('do_not_list')
476 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
477 expect(user.id).to.be.a('number')
478 expect(user.account.displayName).to.equal('new display name')
479 expect(user.account.description).to.be.null
482 it('Should be able to update my description', async function () {
485 accessToken: accessTokenUser,
486 description: 'my super description updated'
489 const res = await getMyUserInformation(server.url, accessTokenUser)
490 const user = res.body
492 expect(user.username).to.equal('user_1')
493 expect(user.email).to.equal('updated@example.com')
494 expect(user.nsfwPolicy).to.equal('do_not_list')
495 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
496 expect(user.id).to.be.a('number')
497 expect(user.account.displayName).to.equal('new display name')
498 expect(user.account.description).to.equal('my super description updated')
501 it('Should be able to update another user', async function () {
506 email: 'updated2@example.com',
509 role: UserRole.MODERATOR
512 const res = await getUserInformation(server.url, accessToken, userId)
513 const user = res.body
515 expect(user.username).to.equal('user_1')
516 expect(user.email).to.equal('updated2@example.com')
517 expect(user.emailVerified).to.be.true
518 expect(user.nsfwPolicy).to.equal('do_not_list')
519 expect(user.videoQuota).to.equal(42)
520 expect(user.roleLabel).to.equal('Moderator')
521 expect(user.id).to.be.a('number')
524 it('Should have removed the user token', async function () {
525 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
527 accessTokenUser = await userLogin(server, user)
530 it('Should be able to update another user password', async function () {
535 password: 'password updated'
538 await getMyUserVideoQuotaUsed(server.url, accessTokenUser, 401)
540 await userLogin(server, user, 400)
542 user.password = 'password updated'
543 accessTokenUser = await userLogin(server, user)
546 it('Should be able to list video blacklist by a moderator', async function () {
547 await getBlacklistedVideosList(server.url, accessTokenUser)
550 it('Should be able to remove this user', async function () {
551 await removeUser(server.url, userId, accessToken)
554 it('Should not be able to login with this user', async function () {
555 await userLogin(server, user, 400)
558 it('Should not have videos of this user', async function () {
559 const res = await getVideosList(server.url)
561 expect(res.body.total).to.equal(1)
563 const video = res.body.data[ 0 ]
564 expect(video.account.name).to.equal('root')
567 it('Should register a new user', async function () {
568 await registerUser(server.url, 'user_15', 'my super password')
571 it('Should be able to login with this registered user', async function () {
574 password: 'my super password'
577 accessToken = await userLogin(server, user15)
580 it('Should have the correct video quota', async function () {
581 const res = await getMyUserInformation(server.url, accessToken)
582 const user = res.body
584 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
587 it('Should remove me', async function () {
589 const res = await getUsersList(server.url, server.accessToken)
590 expect(res.body.data.find(u => u.username === 'user_15')).to.not.be.undefined
593 await deleteMe(server.url, accessToken)
596 const res = await getUsersList(server.url, server.accessToken)
597 expect(res.body.data.find(u => u.username === 'user_15')).to.be.undefined
601 it('Should block and unblock a user', async function () {
604 password: 'my super password'
606 const resUser = await createUser(server.url, server.accessToken, user16.username, user16.password)
607 const user16Id = resUser.body.user.id
609 accessToken = await userLogin(server, user16)
611 await getMyUserInformation(server.url, accessToken, 200)
612 await blockUser(server.url, user16Id, server.accessToken)
614 await getMyUserInformation(server.url, accessToken, 401)
615 await userLogin(server, user16, 400)
617 await unblockUser(server.url, user16Id, server.accessToken)
618 accessToken = await userLogin(server, user16)
619 await getMyUserInformation(server.url, accessToken, 200)
622 after(async function () {
623 killallServers([ server ])
625 // Keep the logs if the test failed