Add plugins check params tests
[oweals/peertube.git] / server / tests / api / check-params / users.ts
1 /* tslint:disable:no-unused-expression */
2
3 import { omit } from 'lodash'
4 import 'mocha'
5 import { join } from 'path'
6 import { UserRole, VideoImport, VideoImportState } from '../../../../shared'
7
8 import {
9   addVideoChannel,
10   blockUser,
11   cleanupTests,
12   createUser,
13   deleteMe,
14   flushAndRunServer,
15   getMyUserInformation,
16   getMyUserVideoRating,
17   getUsersList,
18   immutableAssign,
19   makeGetRequest,
20   makePostBodyRequest,
21   makePutBodyRequest,
22   makeUploadRequest,
23   registerUser,
24   removeUser,
25   ServerInfo,
26   setAccessTokensToServers,
27   unblockUser,
28   updateUser,
29   uploadVideo,
30   userLogin
31 } from '../../../../shared/extra-utils'
32 import {
33   checkBadCountPagination,
34   checkBadSortPagination,
35   checkBadStartPagination
36 } from '../../../../shared/extra-utils/requests/check-api-params'
37 import { getMagnetURI, getMyVideoImports, getYoutubeVideoUrl, importVideo } from '../../../../shared/extra-utils/videos/video-imports'
38 import { VideoPrivacy } from '../../../../shared/models/videos'
39 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
40 import { expect } from 'chai'
41 import { UserAdminFlag } from '../../../../shared/models/users/user-flag.model'
42
43 describe('Test users API validators', function () {
44   const path = '/api/v1/users/'
45   let userId: number
46   let rootId: number
47   let videoId: number
48   let server: ServerInfo
49   let serverWithRegistrationDisabled: ServerInfo
50   let userAccessToken = ''
51   let channelId: number
52   const user = {
53     username: 'user1',
54     password: 'my super password'
55   }
56
57   // ---------------------------------------------------------------
58
59   before(async function () {
60     this.timeout(30000)
61
62     server = await flushAndRunServer(1)
63     serverWithRegistrationDisabled = await flushAndRunServer(2)
64
65     await setAccessTokensToServers([ server ])
66
67     const videoQuota = 42000000
68     await createUser({
69       url: server.url,
70       accessToken: server.accessToken,
71       username: user.username,
72       password: user.password,
73       videoQuota: videoQuota
74     })
75     userAccessToken = await userLogin(server, user)
76
77     {
78       const res = await getMyUserInformation(server.url, server.accessToken)
79       channelId = res.body.videoChannels[ 0 ].id
80     }
81
82     {
83       const res = await uploadVideo(server.url, server.accessToken, {})
84       videoId = res.body.video.id
85     }
86   })
87
88   describe('When listing users', function () {
89     it('Should fail with a bad start pagination', async function () {
90       await checkBadStartPagination(server.url, path, server.accessToken)
91     })
92
93     it('Should fail with a bad count pagination', async function () {
94       await checkBadCountPagination(server.url, path, server.accessToken)
95     })
96
97     it('Should fail with an incorrect sort', async function () {
98       await checkBadSortPagination(server.url, path, server.accessToken)
99     })
100
101     it('Should fail with a non authenticated user', async function () {
102       await makeGetRequest({
103         url: server.url,
104         path,
105         statusCodeExpected: 401
106       })
107     })
108
109     it('Should fail with a non admin user', async function () {
110       await makeGetRequest({
111         url: server.url,
112         path,
113         token: userAccessToken,
114         statusCodeExpected: 403
115       })
116     })
117   })
118
119   describe('When adding a new user', function () {
120     const baseCorrectParams = {
121       username: 'user2',
122       email: 'test@example.com',
123       password: 'my super password',
124       videoQuota: -1,
125       videoQuotaDaily: -1,
126       role: UserRole.USER,
127       adminFlags: UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST
128     }
129
130     it('Should fail with a too small username', async function () {
131       const fields = immutableAssign(baseCorrectParams, { username: '' })
132
133       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
134     })
135
136     it('Should fail with a too long username', async function () {
137       const fields = immutableAssign(baseCorrectParams, { username: 'super'.repeat(50) })
138
139       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
140     })
141
142     it('Should fail with a not lowercase username', async function () {
143       const fields = immutableAssign(baseCorrectParams, { username: 'Toto' })
144
145       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
146     })
147
148     it('Should fail with an incorrect username', async function () {
149       const fields = immutableAssign(baseCorrectParams, { username: 'my username' })
150
151       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
152     })
153
154     it('Should fail with a missing email', async function () {
155       const fields = omit(baseCorrectParams, 'email')
156
157       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
158     })
159
160     it('Should fail with an invalid email', async function () {
161       const fields = immutableAssign(baseCorrectParams, { email: 'test_example.com' })
162
163       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
164     })
165
166     it('Should fail with a too small password', async function () {
167       const fields = immutableAssign(baseCorrectParams, { password: 'bla' })
168
169       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
170     })
171
172     it('Should fail with a too long password', async function () {
173       const fields = immutableAssign(baseCorrectParams, { password: 'super'.repeat(61) })
174
175       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
176     })
177
178     it('Should fail with invalid admin flags', async function () {
179       const fields = immutableAssign(baseCorrectParams, { adminFlags: 'toto' })
180
181       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
182     })
183
184     it('Should fail with an non authenticated user', async function () {
185       await makePostBodyRequest({
186         url: server.url,
187         path,
188         token: 'super token',
189         fields: baseCorrectParams,
190         statusCodeExpected: 401
191       })
192     })
193
194     it('Should fail if we add a user with the same username', async function () {
195       const fields = immutableAssign(baseCorrectParams, { username: 'user1' })
196
197       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 409 })
198     })
199
200     it('Should fail if we add a user with the same email', async function () {
201       const fields = immutableAssign(baseCorrectParams, { email: 'user1@example.com' })
202
203       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 409 })
204     })
205
206     it('Should fail without a videoQuota', async function () {
207       const fields = omit(baseCorrectParams, 'videoQuota')
208
209       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
210     })
211
212     it('Should fail without a videoQuotaDaily', async function () {
213       const fields = omit(baseCorrectParams, 'videoQuotaDaily')
214
215       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
216     })
217
218     it('Should fail with an invalid videoQuota', async function () {
219       const fields = immutableAssign(baseCorrectParams, { videoQuota: -5 })
220
221       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
222     })
223
224     it('Should fail with an invalid videoQuotaDaily', async function () {
225       const fields = immutableAssign(baseCorrectParams, { videoQuotaDaily: -7 })
226
227       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
228     })
229
230     it('Should fail without a user role', async function () {
231       const fields = omit(baseCorrectParams, 'role')
232
233       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
234     })
235
236     it('Should fail with an invalid user role', async function () {
237       const fields = immutableAssign(baseCorrectParams, { role: 88989 })
238
239       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
240     })
241
242     it('Should fail with a "peertube" username', async function () {
243       const fields = immutableAssign(baseCorrectParams, { username: 'peertube' })
244
245       await makePostBodyRequest({
246         url: server.url,
247         path,
248         token: server.accessToken,
249         fields,
250         statusCodeExpected: 409
251       })
252     })
253
254     it('Should succeed with the correct params', async function () {
255       await makePostBodyRequest({
256         url: server.url,
257         path,
258         token: server.accessToken,
259         fields: baseCorrectParams,
260         statusCodeExpected: 200
261       })
262     })
263
264     it('Should fail with a non admin user', async function () {
265       const user = {
266         username: 'user1',
267         password: 'my super password'
268       }
269       userAccessToken = await userLogin(server, user)
270
271       const fields = {
272         username: 'user3',
273         email: 'test@example.com',
274         password: 'my super password',
275         videoQuota: 42000000
276       }
277       await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields, statusCodeExpected: 403 })
278     })
279   })
280
281   describe('When updating my account', function () {
282     it('Should fail with an invalid email attribute', async function () {
283       const fields = {
284         email: 'blabla'
285       }
286
287       await makePutBodyRequest({ url: server.url, path: path + 'me', token: server.accessToken, fields })
288     })
289
290     it('Should fail with a too small password', async function () {
291       const fields = {
292         currentPassword: 'my super password',
293         password: 'bla'
294       }
295
296       await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields })
297     })
298
299     it('Should fail with a too long password', async function () {
300       const fields = {
301         currentPassword: 'my super password',
302         password: 'super'.repeat(61)
303       }
304
305       await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields })
306     })
307
308     it('Should fail without the current password', async function () {
309       const fields = {
310         currentPassword: 'my super password',
311         password: 'super'.repeat(61)
312       }
313
314       await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields })
315     })
316
317     it('Should fail with an invalid current password', async function () {
318       const fields = {
319         currentPassword: 'my super password fail',
320         password: 'super'.repeat(61)
321       }
322
323       await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields, statusCodeExpected: 401 })
324     })
325
326     it('Should fail with an invalid NSFW policy attribute', async function () {
327       const fields = {
328         nsfwPolicy: 'hello'
329       }
330
331       await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields })
332     })
333
334     it('Should fail with an invalid autoPlayVideo attribute', async function () {
335       const fields = {
336         autoPlayVideo: -1
337       }
338
339       await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields })
340     })
341
342     it('Should fail with an invalid videosHistoryEnabled attribute', async function () {
343       const fields = {
344         videosHistoryEnabled: -1
345       }
346
347       await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields })
348     })
349
350     it('Should fail with an non authenticated user', async function () {
351       const fields = {
352         currentPassword: 'my super password',
353         password: 'my super password'
354       }
355
356       await makePutBodyRequest({ url: server.url, path: path + 'me', token: 'super token', fields, statusCodeExpected: 401 })
357     })
358
359     it('Should fail with a too long description', async function () {
360       const fields = {
361         description: 'super'.repeat(201)
362       }
363
364       await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields })
365     })
366
367     it('Should fail with an invalid videoLanguages attribute', async function () {
368       {
369         const fields = {
370           videoLanguages: 'toto'
371         }
372
373         await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields })
374       }
375
376       {
377         const languages = []
378         for (let i = 0; i < 1000; i++) {
379           languages.push('fr')
380         }
381
382         const fields = {
383           videoLanguages: languages
384         }
385
386         await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields })
387       }
388     })
389
390     it('Should succeed to change password with the correct params', async function () {
391       const fields = {
392         currentPassword: 'my super password',
393         password: 'my super password',
394         nsfwPolicy: 'blur',
395         autoPlayVideo: false,
396         email: 'super_email@example.com'
397       }
398
399       await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields, statusCodeExpected: 204 })
400     })
401
402     it('Should succeed without password change with the correct params', async function () {
403       const fields = {
404         nsfwPolicy: 'blur',
405         autoPlayVideo: false
406       }
407
408       await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields, statusCodeExpected: 204 })
409     })
410   })
411
412   describe('When updating my avatar', function () {
413     it('Should fail without an incorrect input file', async function () {
414       const fields = {}
415       const attaches = {
416         'avatarfile': join(__dirname, '..', '..', 'fixtures', 'video_short.mp4')
417       }
418       await makeUploadRequest({ url: server.url, path: path + '/me/avatar/pick', token: server.accessToken, fields, attaches })
419     })
420
421     it('Should fail with a big file', async function () {
422       const fields = {}
423       const attaches = {
424         'avatarfile': join(__dirname, '..', '..', 'fixtures', 'avatar-big.png')
425       }
426       await makeUploadRequest({ url: server.url, path: path + '/me/avatar/pick', token: server.accessToken, fields, attaches })
427     })
428
429     it('Should fail with an unauthenticated user', async function () {
430       const fields = {}
431       const attaches = {
432         'avatarfile': join(__dirname, '..', '..', 'fixtures', 'avatar.png')
433       }
434       await makeUploadRequest({
435         url: server.url,
436         path: path + '/me/avatar/pick',
437         fields,
438         attaches,
439         statusCodeExpected: 401
440       })
441     })
442
443     it('Should succeed with the correct params', async function () {
444       const fields = {}
445       const attaches = {
446         'avatarfile': join(__dirname, '..', '..', 'fixtures', 'avatar.png')
447       }
448       await makeUploadRequest({
449         url: server.url,
450         path: path + '/me/avatar/pick',
451         token: server.accessToken,
452         fields,
453         attaches,
454         statusCodeExpected: 200
455       })
456     })
457   })
458
459   describe('When getting a user', function () {
460     before(async function () {
461       const res = await getUsersList(server.url, server.accessToken)
462
463       userId = res.body.data[1].id
464     })
465
466     it('Should fail with an non authenticated user', async function () {
467       await makeGetRequest({ url: server.url, path: path + userId, token: 'super token', statusCodeExpected: 401 })
468     })
469
470     it('Should fail with a non admin user', async function () {
471       await makeGetRequest({ url: server.url, path, token: userAccessToken, statusCodeExpected: 403 })
472     })
473
474     it('Should succeed with the correct params', async function () {
475       await makeGetRequest({ url: server.url, path: path + userId, token: server.accessToken, statusCodeExpected: 200 })
476     })
477   })
478
479   describe('When updating a user', function () {
480
481     before(async function () {
482       const res = await getUsersList(server.url, server.accessToken)
483
484       userId = res.body.data[1].id
485       rootId = res.body.data[2].id
486     })
487
488     it('Should fail with an invalid email attribute', async function () {
489       const fields = {
490         email: 'blabla'
491       }
492
493       await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
494     })
495
496     it('Should fail with an invalid emailVerified attribute', async function () {
497       const fields = {
498         emailVerified: 'yes'
499       }
500
501       await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
502     })
503
504     it('Should fail with an invalid videoQuota attribute', async function () {
505       const fields = {
506         videoQuota: -90
507       }
508
509       await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
510     })
511
512     it('Should fail with an invalid user role attribute', async function () {
513       const fields = {
514         role: 54878
515       }
516
517       await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
518     })
519
520     it('Should fail with a too small password', async function () {
521       const fields = {
522         currentPassword: 'my super password',
523         password: 'bla'
524       }
525
526       await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
527     })
528
529     it('Should fail with a too long password', async function () {
530       const fields = {
531         currentPassword: 'my super password',
532         password: 'super'.repeat(61)
533       }
534
535       await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
536     })
537
538     it('Should fail with an non authenticated user', async function () {
539       const fields = {
540         videoQuota: 42
541       }
542
543       await makePutBodyRequest({ url: server.url, path: path + userId, token: 'super token', fields, statusCodeExpected: 401 })
544     })
545
546     it('Should fail when updating root role', async function () {
547       const fields = {
548         role: UserRole.MODERATOR
549       }
550
551       await makePutBodyRequest({ url: server.url, path: path + rootId, token: server.accessToken, fields })
552     })
553
554     it('Should fail with invalid admin flags', async function () {
555       const fields = { adminFlags: 'toto' }
556
557       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
558     })
559
560     it('Should succeed with the correct params', async function () {
561       const fields = {
562         email: 'email@example.com',
563         emailVerified: true,
564         videoQuota: 42,
565         role: UserRole.USER
566       }
567
568       await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields, statusCodeExpected: 204 })
569     })
570   })
571
572   describe('When getting my information', function () {
573     it('Should fail with a non authenticated user', async function () {
574       await getMyUserInformation(server.url, 'fake_token', 401)
575     })
576
577     it('Should success with the correct parameters', async function () {
578       await getMyUserInformation(server.url, userAccessToken)
579     })
580   })
581
582   describe('When getting my video rating', function () {
583     it('Should fail with a non authenticated user', async function () {
584       await getMyUserVideoRating(server.url, 'fake_token', videoId, 401)
585     })
586
587     it('Should fail with an incorrect video uuid', async function () {
588       await getMyUserVideoRating(server.url, server.accessToken, 'blabla', 400)
589     })
590
591     it('Should fail with an unknown video', async function () {
592       await getMyUserVideoRating(server.url, server.accessToken, '4da6fde3-88f7-4d16-b119-108df5630b06', 404)
593     })
594
595     it('Should succeed with the correct parameters', async function () {
596       await getMyUserVideoRating(server.url, server.accessToken, videoId)
597     })
598   })
599
600   describe('When retrieving my global ratings', function () {
601     const path = '/api/v1/accounts/user1/ratings'
602
603     it('Should fail with a bad start pagination', async function () {
604       await checkBadStartPagination(server.url, path, userAccessToken)
605     })
606
607     it('Should fail with a bad count pagination', async function () {
608       await checkBadCountPagination(server.url, path, userAccessToken)
609     })
610
611     it('Should fail with an incorrect sort', async function () {
612       await checkBadSortPagination(server.url, path, userAccessToken)
613     })
614
615     it('Should fail with a unauthenticated user', async function () {
616       await makeGetRequest({ url: server.url, path, statusCodeExpected: 401 })
617     })
618
619     it('Should fail with a another user', async function () {
620       await makeGetRequest({ url: server.url, path, token: server.accessToken, statusCodeExpected: 403 })
621     })
622
623     it('Should fail with a bad type', async function () {
624       await makeGetRequest({ url: server.url, path, token: userAccessToken, query: { rating: 'toto ' }, statusCodeExpected: 400 })
625     })
626
627     it('Should succeed with the correct params', async function () {
628       await makeGetRequest({ url: server.url, path, token: userAccessToken, statusCodeExpected: 200 })
629     })
630   })
631
632   describe('When blocking/unblocking/removing user', function () {
633     it('Should fail with an incorrect id', async function () {
634       await removeUser(server.url, 'blabla', server.accessToken, 400)
635       await blockUser(server.url, 'blabla', server.accessToken, 400)
636       await unblockUser(server.url, 'blabla', server.accessToken, 400)
637     })
638
639     it('Should fail with the root user', async function () {
640       await removeUser(server.url, rootId, server.accessToken, 400)
641       await blockUser(server.url, rootId, server.accessToken, 400)
642       await unblockUser(server.url, rootId, server.accessToken, 400)
643     })
644
645     it('Should return 404 with a non existing id', async function () {
646       await removeUser(server.url, 4545454, server.accessToken, 404)
647       await blockUser(server.url, 4545454, server.accessToken, 404)
648       await unblockUser(server.url, 4545454, server.accessToken, 404)
649     })
650
651     it('Should fail with a non admin user', async function () {
652       await removeUser(server.url, userId, userAccessToken, 403)
653       await blockUser(server.url, userId, userAccessToken, 403)
654       await unblockUser(server.url, userId, userAccessToken, 403)
655     })
656   })
657
658   describe('When deleting our account', function () {
659     it('Should fail with with the root account', async function () {
660       await deleteMe(server.url, server.accessToken, 400)
661     })
662   })
663
664   describe('When registering a new user', function () {
665     const registrationPath = path + '/register'
666     const baseCorrectParams = {
667       username: 'user3',
668       displayName: 'super user',
669       email: 'test3@example.com',
670       password: 'my super password'
671     }
672
673     it('Should fail with a too small username', async function () {
674       const fields = immutableAssign(baseCorrectParams, { username: '' })
675
676       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
677     })
678
679     it('Should fail with a too long username', async function () {
680       const fields = immutableAssign(baseCorrectParams, { username: 'super'.repeat(50) })
681
682       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
683     })
684
685     it('Should fail with an incorrect username', async function () {
686       const fields = immutableAssign(baseCorrectParams, { username: 'my username' })
687
688       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
689     })
690
691     it('Should fail with a missing email', async function () {
692       const fields = omit(baseCorrectParams, 'email')
693
694       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
695     })
696
697     it('Should fail with an invalid email', async function () {
698       const fields = immutableAssign(baseCorrectParams, { email: 'test_example.com' })
699
700       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
701     })
702
703     it('Should fail with a too small password', async function () {
704       const fields = immutableAssign(baseCorrectParams, { password: 'bla' })
705
706       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
707     })
708
709     it('Should fail with a too long password', async function () {
710       const fields = immutableAssign(baseCorrectParams, { password: 'super'.repeat(61) })
711
712       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
713     })
714
715     it('Should fail if we register a user with the same username', async function () {
716       const fields = immutableAssign(baseCorrectParams, { username: 'root' })
717
718       await makePostBodyRequest({
719         url: server.url,
720         path: registrationPath,
721         token: server.accessToken,
722         fields,
723         statusCodeExpected: 409
724       })
725     })
726
727     it('Should fail with a "peertube" username', async function () {
728       const fields = immutableAssign(baseCorrectParams, { username: 'peertube' })
729
730       await makePostBodyRequest({
731         url: server.url,
732         path: registrationPath,
733         token: server.accessToken,
734         fields,
735         statusCodeExpected: 409
736       })
737     })
738
739     it('Should fail if we register a user with the same email', async function () {
740       const fields = immutableAssign(baseCorrectParams, { email: 'admin' + server.internalServerNumber + '@example.com' })
741
742       await makePostBodyRequest({
743         url: server.url,
744         path: registrationPath,
745         token: server.accessToken,
746         fields,
747         statusCodeExpected: 409
748       })
749     })
750
751     it('Should fail with a bad display name', async function () {
752       const fields = immutableAssign(baseCorrectParams, { displayName: 'a'.repeat(150) })
753
754       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
755     })
756
757     it('Should fail with a bad channel name', async function () {
758       const fields = immutableAssign(baseCorrectParams, { channel: { name: '[]azf', displayName: 'toto' } })
759
760       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
761     })
762
763     it('Should fail with a bad channel display name', async function () {
764       const fields = immutableAssign(baseCorrectParams, { channel: { name: 'toto', displayName: '' } })
765
766       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
767     })
768
769     it('Should fail with a channel name that is the same than user username', async function () {
770       const source = { username: 'super_user', channel: { name: 'super_user', displayName: 'display name' } }
771       const fields = immutableAssign(baseCorrectParams, source)
772
773       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
774     })
775
776     it('Should fail with an existing channel', async function () {
777       const videoChannelAttributesArg = { name: 'existing_channel', displayName: 'hello', description: 'super description' }
778       await addVideoChannel(server.url, server.accessToken, videoChannelAttributesArg)
779
780       const fields = immutableAssign(baseCorrectParams, { channel: { name: 'existing_channel', displayName: 'toto' } })
781
782       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields, statusCodeExpected: 409 })
783     })
784
785     it('Should succeed with the correct params', async function () {
786       const fields = immutableAssign(baseCorrectParams, { channel: { name: 'super_channel', displayName: 'toto' } })
787
788       await makePostBodyRequest({
789         url: server.url,
790         path: registrationPath,
791         token: server.accessToken,
792         fields: fields,
793         statusCodeExpected: 204
794       })
795     })
796
797     it('Should fail on a server with registration disabled', async function () {
798       const fields = {
799         username: 'user4',
800         email: 'test4@example.com',
801         password: 'my super password 4'
802       }
803
804       await makePostBodyRequest({
805         url: serverWithRegistrationDisabled.url,
806         path: registrationPath,
807         token: serverWithRegistrationDisabled.accessToken,
808         fields,
809         statusCodeExpected: 403
810       })
811     })
812   })
813
814   describe('When registering multiple users on a server with users limit', function () {
815     it('Should fail when after 3 registrations', async function () {
816       await registerUser(server.url, 'user42', 'super password', 403)
817     })
818   })
819
820   describe('When having a video quota', function () {
821     it('Should fail with a user having too many videos', async function () {
822       await updateUser({
823         url: server.url,
824         userId: rootId,
825         accessToken: server.accessToken,
826         videoQuota: 42
827       })
828
829       await uploadVideo(server.url, server.accessToken, {}, 403)
830     })
831
832     it('Should fail with a registered user having too many videos', async function () {
833       this.timeout(30000)
834
835       const user = {
836         username: 'user3',
837         password: 'my super password'
838       }
839       userAccessToken = await userLogin(server, user)
840
841       const videoAttributes = { fixture: 'video_short2.webm' }
842       await uploadVideo(server.url, userAccessToken, videoAttributes)
843       await uploadVideo(server.url, userAccessToken, videoAttributes)
844       await uploadVideo(server.url, userAccessToken, videoAttributes)
845       await uploadVideo(server.url, userAccessToken, videoAttributes)
846       await uploadVideo(server.url, userAccessToken, videoAttributes)
847       await uploadVideo(server.url, userAccessToken, videoAttributes, 403)
848     })
849
850     it('Should fail to import with HTTP/Torrent/magnet', async function () {
851       this.timeout(120000)
852
853       const baseAttributes = {
854         channelId: 1,
855         privacy: VideoPrivacy.PUBLIC
856       }
857       await importVideo(server.url, server.accessToken, immutableAssign(baseAttributes, { targetUrl: getYoutubeVideoUrl() }))
858       await importVideo(server.url, server.accessToken, immutableAssign(baseAttributes, { magnetUri: getMagnetURI() }))
859       await importVideo(server.url, server.accessToken, immutableAssign(baseAttributes, { torrentfile: 'video-720p.torrent' }))
860
861       await waitJobs([ server ])
862
863       const res = await getMyVideoImports(server.url, server.accessToken)
864
865       expect(res.body.total).to.equal(3)
866       const videoImports: VideoImport[] = res.body.data
867       expect(videoImports).to.have.lengthOf(3)
868
869       for (const videoImport of videoImports) {
870         expect(videoImport.state.id).to.equal(VideoImportState.FAILED)
871         expect(videoImport.error).not.to.be.undefined
872         expect(videoImport.error).to.contain('user video quota is exceeded')
873       }
874     })
875   })
876
877   describe('When having a daily video quota', function () {
878     it('Should fail with a user having too many videos', async function () {
879       await updateUser({
880         url: server.url,
881         userId: rootId,
882         accessToken: server.accessToken,
883         videoQuotaDaily: 42
884       })
885
886       await uploadVideo(server.url, server.accessToken, {}, 403)
887     })
888   })
889
890   describe('When having an absolute and daily video quota', function () {
891     it('Should fail if exceeding total quota', async function () {
892       await updateUser({
893         url: server.url,
894         userId: rootId,
895         accessToken: server.accessToken,
896         videoQuota: 42,
897         videoQuotaDaily: 1024 * 1024 * 1024
898       })
899
900       await uploadVideo(server.url, server.accessToken, {}, 403)
901     })
902
903     it('Should fail if exceeding daily quota', async function () {
904       await updateUser({
905         url: server.url,
906         userId: rootId,
907         accessToken: server.accessToken,
908         videoQuota: 1024 * 1024 * 1024,
909         videoQuotaDaily: 42
910       })
911
912       await uploadVideo(server.url, server.accessToken, {}, 403)
913     })
914   })
915
916   describe('When asking a password reset', function () {
917     const path = '/api/v1/users/ask-reset-password'
918
919     it('Should fail with a missing email', async function () {
920       const fields = {}
921
922       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
923     })
924
925     it('Should fail with an invalid email', async function () {
926       const fields = { email: 'hello' }
927
928       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
929     })
930
931     it('Should success with the correct params', async function () {
932       const fields = { email: 'admin@example.com' }
933
934       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 204 })
935     })
936   })
937
938   describe('When asking for an account verification email', function () {
939     const path = '/api/v1/users/ask-send-verify-email'
940
941     it('Should fail with a missing email', async function () {
942       const fields = {}
943
944       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
945     })
946
947     it('Should fail with an invalid email', async function () {
948       const fields = { email: 'hello' }
949
950       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
951     })
952
953     it('Should succeed with the correct params', async function () {
954       const fields = { email: 'admin@example.com' }
955
956       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 204 })
957     })
958   })
959
960   after(async function () {
961     await cleanupTests([ server, serverWithRegistrationDisabled ])
962   })
963 })