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 await createUser(server.url, server.accessToken, username, password)
48 const videoAttributes = {}
49 await uploadVideo(server.url, server.accessToken, videoAttributes)
51 const res = await getVideosList(server.url)
52 const videos = res.body.data
53 videoId = videos[0].id
57 password: 'my super password'
59 userAccessToken = await getUserAccessToken(server, user)
62 describe('When listing users', function () {
63 it('Should fail with a bad start pagination', async function () {
64 await request(server.url)
66 .query({ start: 'hello' })
67 .set('Accept', 'application/json')
71 it('Should fail with a bad count pagination', async function () {
72 await request(server.url)
74 .query({ count: 'hello' })
75 .set('Accept', 'application/json')
79 it('Should fail with an incorrect sort', async function () {
80 await request(server.url)
82 .query({ sort: 'hello' })
83 .set('Accept', 'application/json')
88 describe('When adding a new user', function () {
89 it('Should fail with a too small username', async function () {
92 email: 'test@example.com',
93 password: 'my_super_password'
96 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
99 it('Should fail with a too long username', async function () {
101 username: 'my_super_username_which_is_very_long',
102 email: 'test@example.com',
103 password: 'my_super_password'
106 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
109 it('Should fail with an incorrect username', async function () {
111 username: 'my username',
112 email: 'test@example.com',
113 password: 'my_super_password'
116 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
119 it('Should fail with a missing email', async function () {
122 password: 'my_super_password'
125 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
128 it('Should fail with an invalid email', async function () {
130 username: 'my_super_username_which_is_very_long',
131 email: 'test_example.com',
132 password: 'my_super_password'
135 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
138 it('Should fail with a too small password', async function () {
140 username: 'my_username',
141 email: 'test@example.com',
145 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
148 it('Should fail with a too long password', async function () {
150 username: 'my_username',
151 email: 'test@example.com',
152 password: 'my super long password which is very very very very very very very very very very very very very very' +
153 'very very very very very very very very very very very very very very very veryv very very very very' +
154 'very very very very very very very very very very very very very very very very very very very very long'
157 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
160 it('Should fail with an non authenticated user', async function () {
162 username: 'my_username',
163 email: 'test@example.com',
164 password: 'my super password'
167 await makePostBodyRequest({ url: server.url, path, token: 'super token', fields, statusCodeExpected: 401 })
170 it('Should fail if we add a user with the same username', async function () {
173 email: 'test@example.com',
174 password: 'my super password'
177 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 409 })
180 it('Should fail if we add a user with the same email', async function () {
182 username: 'my_username',
183 email: 'user1@example.com',
184 password: 'my super password'
187 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 409 })
190 it('Should succeed with the correct params', async function () {
193 email: 'test@example.com',
194 password: 'my super password'
197 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 204 })
200 it('Should fail with a non admin user', async function () {
203 email: 'test@example.com',
204 password: 'my super password'
207 userAccessToken = await loginAndGetAccessToken(server)
210 email: 'test@example.com',
211 password: 'my super password'
213 await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields, statusCodeExpected: 403 })
217 describe('When updating a user', function () {
218 before(async function () {
219 const res = await getUsersList(server.url)
221 userId = res.body.data[1].id
222 rootId = res.body.data[2].id
225 it('Should fail with a too small password', async function () {
230 await makePutBodyRequest({ url: server.url, path: path + userId, token: userAccessToken, fields })
233 it('Should fail with a too long password', async function () {
235 password: 'my super long password which is very very very very very very very very very very very very very very' +
236 'very very very very very very very very very very very very very very very veryv very very very very' +
237 'very very very very very very very very very very very very very very very very very very very very long'
240 await makePutBodyRequest({ url: server.url, path: path + userId, token: userAccessToken, fields })
243 it('Should fail with an invalid display NSFW attribute', async function () {
248 await makePutBodyRequest({ url: server.url, path: path + userId, token: userAccessToken, fields })
251 it('Should fail with an non authenticated user', async function () {
253 password: 'my super password'
256 await makePutBodyRequest({ url: server.url, path: path + userId, token: 'super token', fields, statusCodeExpected: 401 })
259 it('Should succeed with the correct params', async function () {
261 password: 'my super password',
265 await makePutBodyRequest({ url: server.url, path: path + userId, token: userAccessToken, fields, statusCodeExpected: 204 })
269 describe('When getting my information', function () {
270 it('Should fail with a non authenticated user', async function () {
271 await request(server.url)
273 .set('Authorization', 'Bearer fake_token')
274 .set('Accept', 'application/json')
278 it('Should success with the correct parameters', async function () {
279 await request(server.url)
281 .set('Authorization', 'Bearer ' + userAccessToken)
282 .set('Accept', 'application/json')
287 describe('When getting my video rating', function () {
288 it('Should fail with a non authenticated user', async function () {
289 await request(server.url)
290 .get(path + 'me/videos/' + videoId + '/rating')
291 .set('Authorization', 'Bearer fake_token')
292 .set('Accept', 'application/json')
296 it('Should fail with an incorrect video uuid', async function () {
297 await request(server.url)
298 .get(path + 'me/videos/blabla/rating')
299 .set('Authorization', 'Bearer ' + userAccessToken)
300 .set('Accept', 'application/json')
304 it('Should fail with an unknown video', async function () {
305 await request(server.url)
306 .get(path + 'me/videos/4da6fde3-88f7-4d16-b119-108df5630b06/rating')
307 .set('Authorization', 'Bearer ' + userAccessToken)
308 .set('Accept', 'application/json')
312 it('Should success with the correct parameters', async function () {
313 await request(server.url)
314 .get(path + 'me/videos/' + videoId + '/rating')
315 .set('Authorization', 'Bearer ' + userAccessToken)
316 .set('Accept', 'application/json')
321 describe('When removing an user', function () {
322 it('Should fail with an incorrect id', async function () {
323 await request(server.url)
324 .delete(path + 'bla-bla')
325 .set('Authorization', 'Bearer ' + server.accessToken)
329 it('Should fail with the root user', async function () {
330 await request(server.url)
331 .delete(path + rootId)
332 .set('Authorization', 'Bearer ' + server.accessToken)
336 it('Should return 404 with a non existing id', async function () {
337 await request(server.url)
339 .set('Authorization', 'Bearer ' + server.accessToken)
344 describe('When removing an user', function () {
345 it('Should fail with an incorrect id', async function () {
346 await request(server.url)
347 .delete(path + 'bla-bla')
348 .set('Authorization', 'Bearer ' + server.accessToken)
352 it('Should fail with the root user', async function () {
353 await request(server.url)
354 .delete(path + rootId)
355 .set('Authorization', 'Bearer ' + server.accessToken)
359 it('Should return 404 with a non existing id', async function () {
360 await request(server.url)
362 .set('Authorization', 'Bearer ' + server.accessToken)
367 describe('When register a new user', function () {
368 const registrationPath = path + '/register'
370 it('Should fail with a too small username', async function () {
373 email: 'test@example.com',
374 password: 'my_super_password'
377 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
380 it('Should fail with a too long username', async function () {
382 username: 'my_super_username_which_is_very_long',
383 email: 'test@example.com',
384 password: 'my_super_password'
387 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
390 it('Should fail with an incorrect username', async function () {
392 username: 'my username',
393 email: 'test@example.com',
394 password: 'my_super_password'
397 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
400 it('Should fail with a missing email', async function () {
403 password: 'my_super_password'
406 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
409 it('Should fail with an invalid email', async function () {
411 username: 'my_super_username_which_is_very_long',
412 email: 'test_example.com',
413 password: 'my_super_password'
416 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
419 it('Should fail with a too small password', async function () {
421 username: 'my_username',
422 email: 'test@example.com',
426 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
429 it('Should fail with a too long password', async function () {
431 username: 'my_username',
432 email: 'test@example.com',
433 password: 'my super long password which is very very very very very very very very very very very very very very' +
434 'very very very very very very very very very very very very very very very veryv very very very very' +
435 'very very very very very very very very very very very very very very very very very very very very long'
438 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
441 it('Should fail if we register a user with the same username', async function () {
444 email: 'test@example.com',
445 password: 'my super password'
448 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields, statusCodeExpected: 409 })
451 it('Should fail if we register a user with the same email', async function () {
453 username: 'my_username',
454 email: 'admin1@example.com',
455 password: 'my super password'
458 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields, statusCodeExpected: 409 })
461 it('Should succeed with the correct params', async function () {
464 email: 'test3@example.com',
465 password: 'my super password'
468 await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields, statusCodeExpected: 204 })
471 it('Should fail on a server with registration disabled', async function () {
474 email: 'test4@example.com',
475 password: 'my super password 4'
478 await makePostBodyRequest({
479 url: serverWithRegistrationDisabled.url,
480 path: registrationPath,
481 token: serverWithRegistrationDisabled.accessToken,
483 statusCodeExpected: 403
488 describe('When registering multiple users on a server with users limit', function () {
489 it('Should fail when after 3 registrations', async function () {
490 await registerUser(server.url, 'user42', 'super password', 403)
494 after(async function () {
495 killallServers([ server, serverWithRegistrationDisabled ])
497 // Keep the logs if the test failed