1 /* tslint:disable:no-unused-expression */
3 import * as request from 'supertest'
14 loginAndGetAccessToken,
17 setAccessTokensToServers,
23 describe('Test users API validators', function () {
24 const path = '/api/v1/users/'
28 let server: ServerInfo
29 let serverWithRegistrationDisabled: ServerInfo
30 let userAccessToken = ''
32 // ---------------------------------------------------------------
34 before(async function () {
39 server = await runServer(1)
40 serverWithRegistrationDisabled = await runServer(2)
42 await setAccessTokensToServers([ server ])
44 const username = 'user1'
45 const password = 'my super password'
46 const videoQuota = 42000000
47 await createUser(server.url, server.accessToken, username, password, videoQuota)
49 const videoAttributes = {}
50 await uploadVideo(server.url, server.accessToken, videoAttributes)
52 const res = await getVideosList(server.url)
53 const videos = res.body.data
54 videoId = videos[0].id
58 password: 'my super password'
60 userAccessToken = await getUserAccessToken(server, user)
63 describe('When listing users', function () {
64 it('Should fail with a bad start pagination', async function () {
65 await request(server.url)
67 .query({ start: 'hello' })
68 .set('Accept', 'application/json')
72 it('Should fail with a bad count pagination', async function () {
73 await request(server.url)
75 .query({ count: 'hello' })
76 .set('Accept', 'application/json')
80 it('Should fail with an incorrect sort', async function () {
81 await request(server.url)
83 .query({ sort: 'hello' })
84 .set('Accept', 'application/json')
89 describe('When adding a new user', function () {
90 it('Should fail with a too small username', async function () {
93 email: 'test@example.com',
94 password: 'my_super_password',
98 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
101 it('Should fail with a too long username', async function () {
103 username: 'my_super_username_which_is_very_long',
104 email: 'test@example.com',
105 password: 'my_super_password',
109 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
112 it('Should fail with an incorrect username', async function () {
114 username: 'my username',
115 email: 'test@example.com',
116 password: 'my_super_password',
120 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
123 it('Should fail with a missing email', async function () {
126 password: 'my_super_password',
130 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
133 it('Should fail with an invalid email', async function () {
135 username: 'my_super_username_which_is_very_long',
136 email: 'test_example.com',
137 password: 'my_super_password',
141 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
144 it('Should fail with a too small password', async function () {
146 username: 'my_username',
147 email: 'test@example.com',
152 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
155 it('Should fail with a too long password', async function () {
157 username: 'my_username',
158 email: 'test@example.com',
159 password: 'my super long password which is very very very very very very very very very very very very very very' +
160 'very very very very very very very very very very very very very very very veryv very very very very' +
161 'very very very very very very very very very very very very very very very very very very very very long',
165 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
168 it('Should fail with an non authenticated user', async function () {
170 username: 'my_username',
171 email: 'test@example.com',
172 password: 'my super password',
176 await makePostBodyRequest({ url: server.url, path, token: 'super token', fields, statusCodeExpected: 401 })
179 it('Should fail if we add a user with the same username', async function () {
182 email: 'test@example.com',
183 password: 'my super password',
187 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 409 })
190 it('Should fail if we add a user with the same email', async function () {
192 username: 'my_username',
193 email: 'user1@example.com',
194 password: 'my super password',
198 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 409 })
201 it('Should fail without a videoQuota', async function () {
203 username: 'my_username',
204 email: 'user1@example.com',
205 password: 'my super password'
208 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
211 it('Should fail with an invalid videoQuota', async function () {
213 username: 'my_username',
214 email: 'user1@example.com',
215 password: 'my super password',
219 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
222 it('Should succeed with the correct params', async function () {
225 email: 'test@example.com',
226 password: 'my super password',
230 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 204 })
233 it('Should fail with a non admin user', async function () {
236 email: 'test@example.com',
237 password: 'my super password'
240 userAccessToken = await loginAndGetAccessToken(server)
243 email: 'test@example.com',
244 password: 'my super password',
247 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields, statusCodeExpected: 403 })
251 describe('When updating my account', function () {
252 it('Should fail with an invalid email attribute', async function () {
257 await makePutBodyRequest({ url: server.url, path: path + 'me', token: server.accessToken, fields })
260 it('Should fail with a too small password', async function () {
265 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields })
268 it('Should fail with a too long password', async function () {
270 password: 'my super long password which is very very very very very very very very very very very very very very' +
271 'very very very very very very very very very very very very very very very veryv very very very very' +
272 'very very very very very very very very very very very very very very very very very very very very long'
275 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields })
278 it('Should fail with an invalid display NSFW attribute', async function () {
283 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields })
286 it('Should fail with an non authenticated user', async function () {
288 password: 'my super password'
291 await makePutBodyRequest({ url: server.url, path: path + 'me', token: 'super token', fields, statusCodeExpected: 401 })
294 it('Should succeed with the correct params', async function () {
296 password: 'my super password',
298 email: 'super_email@example.com'
301 await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields, statusCodeExpected: 204 })
305 describe('When updating a user', function () {
307 before(async function () {
308 const res = await getUsersList(server.url)
310 userId = res.body.data[1].id
311 rootId = res.body.data[2].id
314 it('Should fail with an invalid email attribute', async function () {
319 await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
322 it('Should fail with an invalid videoQuota attribute', async function () {
327 await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
330 it('Should fail with an non authenticated user', async function () {
335 await makePutBodyRequest({ url: server.url, path: path + userId, token: 'super token', fields, statusCodeExpected: 401 })
338 it('Should succeed with the correct params', async function () {
340 email: 'email@example.com',
344 await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields, statusCodeExpected: 204 })
348 describe('When getting my information', function () {
349 it('Should fail with a non authenticated user', async function () {
350 await request(server.url)
352 .set('Authorization', 'Bearer fake_token')
353 .set('Accept', 'application/json')
357 it('Should success with the correct parameters', async function () {
358 await request(server.url)
360 .set('Authorization', 'Bearer ' + userAccessToken)
361 .set('Accept', 'application/json')
366 describe('When getting my video rating', function () {
367 it('Should fail with a non authenticated user', async function () {
368 await request(server.url)
369 .get(path + 'me/videos/' + videoId + '/rating')
370 .set('Authorization', 'Bearer fake_token')
371 .set('Accept', 'application/json')
375 it('Should fail with an incorrect video uuid', async function () {
376 await request(server.url)
377 .get(path + 'me/videos/blabla/rating')
378 .set('Authorization', 'Bearer ' + userAccessToken)
379 .set('Accept', 'application/json')
383 it('Should fail with an unknown video', async function () {
384 await request(server.url)
385 .get(path + 'me/videos/4da6fde3-88f7-4d16-b119-108df5630b06/rating')
386 .set('Authorization', 'Bearer ' + userAccessToken)
387 .set('Accept', 'application/json')
391 it('Should success with the correct parameters', async function () {
392 await request(server.url)
393 .get(path + 'me/videos/' + videoId + '/rating')
394 .set('Authorization', 'Bearer ' + userAccessToken)
395 .set('Accept', 'application/json')
400 describe('When removing an user', function () {
401 it('Should fail with an incorrect id', async function () {
402 await request(server.url)
403 .delete(path + 'bla-bla')
404 .set('Authorization', 'Bearer ' + server.accessToken)
408 it('Should fail with the root user', async function () {
409 await request(server.url)
410 .delete(path + rootId)
411 .set('Authorization', 'Bearer ' + server.accessToken)
415 it('Should return 404 with a non existing id', async function () {
416 await request(server.url)
418 .set('Authorization', 'Bearer ' + server.accessToken)
423 describe('When removing an user', function () {
424 it('Should fail with an incorrect id', async function () {
425 await request(server.url)
426 .delete(path + 'bla-bla')
427 .set('Authorization', 'Bearer ' + server.accessToken)
431 it('Should fail with the root user', async function () {
432 await request(server.url)
433 .delete(path + rootId)
434 .set('Authorization', 'Bearer ' + server.accessToken)
438 it('Should return 404 with a non existing id', async function () {
439 await request(server.url)
441 .set('Authorization', 'Bearer ' + server.accessToken)
446 describe('When register a new user', function () {
447 const registrationPath = path + '/register'
449 it('Should fail with a too small username', async function () {
452 email: 'test@example.com',
453 password: 'my_super_password'
456 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
459 it('Should fail with a too long username', async function () {
461 username: 'my_super_username_which_is_very_long',
462 email: 'test@example.com',
463 password: 'my_super_password'
466 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
469 it('Should fail with an incorrect username', async function () {
471 username: 'my username',
472 email: 'test@example.com',
473 password: 'my_super_password'
476 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
479 it('Should fail with a missing email', async function () {
482 password: 'my_super_password'
485 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
488 it('Should fail with an invalid email', async function () {
490 username: 'my_super_username_which_is_very_long',
491 email: 'test_example.com',
492 password: 'my_super_password'
495 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
498 it('Should fail with a too small password', async function () {
500 username: 'my_username',
501 email: 'test@example.com',
505 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
508 it('Should fail with a too long password', async function () {
510 username: 'my_username',
511 email: 'test@example.com',
512 password: 'my super long password which is very very very very very very very very very very very very very very' +
513 'very very very very very very very very very very very very very very very veryv very very very very' +
514 'very very very very very very very very very very very very very very very very very very very very long'
517 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
520 it('Should fail if we register a user with the same username', async function () {
523 email: 'test@example.com',
524 password: 'my super password'
527 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields, statusCodeExpected: 409 })
530 it('Should fail if we register a user with the same email', async function () {
532 username: 'my_username',
533 email: 'admin1@example.com',
534 password: 'my super password'
537 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields, statusCodeExpected: 409 })
540 it('Should succeed with the correct params', async function () {
543 email: 'test3@example.com',
544 password: 'my super password'
547 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields, statusCodeExpected: 204 })
550 it('Should fail on a server with registration disabled', async function () {
553 email: 'test4@example.com',
554 password: 'my super password 4'
557 await makePostBodyRequest({
558 url: serverWithRegistrationDisabled.url,
559 path: registrationPath,
560 token: serverWithRegistrationDisabled.accessToken,
562 statusCodeExpected: 403
567 describe('When registering multiple users on a server with users limit', function () {
568 it('Should fail when after 3 registrations', async function () {
569 await registerUser(server.url, 'user42', 'super password', 403)
573 describe('When having a video quota', function () {
574 it('Should fail with a user having too many video', async function () {
579 await makePutBodyRequest({ url: server.url, path: path + rootId, token: server.accessToken, fields, statusCodeExpected: 204 })
581 const videoAttributes = {}
582 await uploadVideo(server.url, server.accessToken, videoAttributes, 403)
585 it('Should fail with a registered user having too many video', async function () {
590 email: 'test3@example.com',
591 password: 'my super password'
593 userAccessToken = await loginAndGetAccessToken(server)
595 const videoAttributes = { fixture: 'video_short2.webm' }
596 await uploadVideo(server.url, userAccessToken, videoAttributes)
597 await uploadVideo(server.url, userAccessToken, videoAttributes)
598 await uploadVideo(server.url, userAccessToken, videoAttributes)
599 await uploadVideo(server.url, userAccessToken, videoAttributes)
600 await uploadVideo(server.url, userAccessToken, videoAttributes)
601 await uploadVideo(server.url, userAccessToken, videoAttributes, 403)
605 after(async function () {
606 killallServers([ server, serverWithRegistrationDisabled ])
608 // Keep the logs if the test failed