Add CLI plugins 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 fail with an invalid theme', async function () {
391       const fields = { theme: 'invalid' }
392       await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields })
393     })
394
395     it('Should fail with an unknown theme', async function () {
396       const fields = { theme: 'peertube-theme-unknown' }
397       await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields })
398     })
399
400     it('Should succeed to change password with the correct params', async function () {
401       const fields = {
402         currentPassword: 'my super password',
403         password: 'my super password',
404         nsfwPolicy: 'blur',
405         autoPlayVideo: false,
406         email: 'super_email@example.com',
407         theme: 'default'
408       }
409
410       await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields, statusCodeExpected: 204 })
411     })
412
413     it('Should succeed without password change with the correct params', async function () {
414       const fields = {
415         nsfwPolicy: 'blur',
416         autoPlayVideo: false
417       }
418
419       await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields, statusCodeExpected: 204 })
420     })
421   })
422
423   describe('When updating my avatar', function () {
424     it('Should fail without an incorrect input file', async function () {
425       const fields = {}
426       const attaches = {
427         'avatarfile': join(__dirname, '..', '..', 'fixtures', 'video_short.mp4')
428       }
429       await makeUploadRequest({ url: server.url, path: path + '/me/avatar/pick', token: server.accessToken, fields, attaches })
430     })
431
432     it('Should fail with a big file', async function () {
433       const fields = {}
434       const attaches = {
435         'avatarfile': join(__dirname, '..', '..', 'fixtures', 'avatar-big.png')
436       }
437       await makeUploadRequest({ url: server.url, path: path + '/me/avatar/pick', token: server.accessToken, fields, attaches })
438     })
439
440     it('Should fail with an unauthenticated user', async function () {
441       const fields = {}
442       const attaches = {
443         'avatarfile': join(__dirname, '..', '..', 'fixtures', 'avatar.png')
444       }
445       await makeUploadRequest({
446         url: server.url,
447         path: path + '/me/avatar/pick',
448         fields,
449         attaches,
450         statusCodeExpected: 401
451       })
452     })
453
454     it('Should succeed with the correct params', async function () {
455       const fields = {}
456       const attaches = {
457         'avatarfile': join(__dirname, '..', '..', 'fixtures', 'avatar.png')
458       }
459       await makeUploadRequest({
460         url: server.url,
461         path: path + '/me/avatar/pick',
462         token: server.accessToken,
463         fields,
464         attaches,
465         statusCodeExpected: 200
466       })
467     })
468   })
469
470   describe('When getting a user', function () {
471     before(async function () {
472       const res = await getUsersList(server.url, server.accessToken)
473
474       userId = res.body.data[1].id
475     })
476
477     it('Should fail with an non authenticated user', async function () {
478       await makeGetRequest({ url: server.url, path: path + userId, token: 'super token', statusCodeExpected: 401 })
479     })
480
481     it('Should fail with a non admin user', async function () {
482       await makeGetRequest({ url: server.url, path, token: userAccessToken, statusCodeExpected: 403 })
483     })
484
485     it('Should succeed with the correct params', async function () {
486       await makeGetRequest({ url: server.url, path: path + userId, token: server.accessToken, statusCodeExpected: 200 })
487     })
488   })
489
490   describe('When updating a user', function () {
491
492     before(async function () {
493       const res = await getUsersList(server.url, server.accessToken)
494
495       userId = res.body.data[1].id
496       rootId = res.body.data[2].id
497     })
498
499     it('Should fail with an invalid email attribute', async function () {
500       const fields = {
501         email: 'blabla'
502       }
503
504       await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
505     })
506
507     it('Should fail with an invalid emailVerified attribute', async function () {
508       const fields = {
509         emailVerified: 'yes'
510       }
511
512       await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
513     })
514
515     it('Should fail with an invalid videoQuota attribute', async function () {
516       const fields = {
517         videoQuota: -90
518       }
519
520       await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
521     })
522
523     it('Should fail with an invalid user role attribute', async function () {
524       const fields = {
525         role: 54878
526       }
527
528       await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
529     })
530
531     it('Should fail with a too small password', async function () {
532       const fields = {
533         currentPassword: 'my super password',
534         password: 'bla'
535       }
536
537       await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
538     })
539
540     it('Should fail with a too long password', async function () {
541       const fields = {
542         currentPassword: 'my super password',
543         password: 'super'.repeat(61)
544       }
545
546       await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
547     })
548
549     it('Should fail with an non authenticated user', async function () {
550       const fields = {
551         videoQuota: 42
552       }
553
554       await makePutBodyRequest({ url: server.url, path: path + userId, token: 'super token', fields, statusCodeExpected: 401 })
555     })
556
557     it('Should fail when updating root role', async function () {
558       const fields = {
559         role: UserRole.MODERATOR
560       }
561
562       await makePutBodyRequest({ url: server.url, path: path + rootId, token: server.accessToken, fields })
563     })
564
565     it('Should fail with invalid admin flags', async function () {
566       const fields = { adminFlags: 'toto' }
567
568       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
569     })
570
571     it('Should succeed with the correct params', async function () {
572       const fields = {
573         email: 'email@example.com',
574         emailVerified: true,
575         videoQuota: 42,
576         role: UserRole.USER
577       }
578
579       await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields, statusCodeExpected: 204 })
580     })
581   })
582
583   describe('When getting my information', function () {
584     it('Should fail with a non authenticated user', async function () {
585       await getMyUserInformation(server.url, 'fake_token', 401)
586     })
587
588     it('Should success with the correct parameters', async function () {
589       await getMyUserInformation(server.url, userAccessToken)
590     })
591   })
592
593   describe('When getting my video rating', function () {
594     it('Should fail with a non authenticated user', async function () {
595       await getMyUserVideoRating(server.url, 'fake_token', videoId, 401)
596     })
597
598     it('Should fail with an incorrect video uuid', async function () {
599       await getMyUserVideoRating(server.url, server.accessToken, 'blabla', 400)
600     })
601
602     it('Should fail with an unknown video', async function () {
603       await getMyUserVideoRating(server.url, server.accessToken, '4da6fde3-88f7-4d16-b119-108df5630b06', 404)
604     })
605
606     it('Should succeed with the correct parameters', async function () {
607       await getMyUserVideoRating(server.url, server.accessToken, videoId)
608     })
609   })
610
611   describe('When retrieving my global ratings', function () {
612     const path = '/api/v1/accounts/user1/ratings'
613
614     it('Should fail with a bad start pagination', async function () {
615       await checkBadStartPagination(server.url, path, userAccessToken)
616     })
617
618     it('Should fail with a bad count pagination', async function () {
619       await checkBadCountPagination(server.url, path, userAccessToken)
620     })
621
622     it('Should fail with an incorrect sort', async function () {
623       await checkBadSortPagination(server.url, path, userAccessToken)
624     })
625
626     it('Should fail with a unauthenticated user', async function () {
627       await makeGetRequest({ url: server.url, path, statusCodeExpected: 401 })
628     })
629
630     it('Should fail with a another user', async function () {
631       await makeGetRequest({ url: server.url, path, token: server.accessToken, statusCodeExpected: 403 })
632     })
633
634     it('Should fail with a bad type', async function () {
635       await makeGetRequest({ url: server.url, path, token: userAccessToken, query: { rating: 'toto ' }, statusCodeExpected: 400 })
636     })
637
638     it('Should succeed with the correct params', async function () {
639       await makeGetRequest({ url: server.url, path, token: userAccessToken, statusCodeExpected: 200 })
640     })
641   })
642
643   describe('When blocking/unblocking/removing user', function () {
644     it('Should fail with an incorrect id', async function () {
645       await removeUser(server.url, 'blabla', server.accessToken, 400)
646       await blockUser(server.url, 'blabla', server.accessToken, 400)
647       await unblockUser(server.url, 'blabla', server.accessToken, 400)
648     })
649
650     it('Should fail with the root user', async function () {
651       await removeUser(server.url, rootId, server.accessToken, 400)
652       await blockUser(server.url, rootId, server.accessToken, 400)
653       await unblockUser(server.url, rootId, server.accessToken, 400)
654     })
655
656     it('Should return 404 with a non existing id', async function () {
657       await removeUser(server.url, 4545454, server.accessToken, 404)
658       await blockUser(server.url, 4545454, server.accessToken, 404)
659       await unblockUser(server.url, 4545454, server.accessToken, 404)
660     })
661
662     it('Should fail with a non admin user', async function () {
663       await removeUser(server.url, userId, userAccessToken, 403)
664       await blockUser(server.url, userId, userAccessToken, 403)
665       await unblockUser(server.url, userId, userAccessToken, 403)
666     })
667   })
668
669   describe('When deleting our account', function () {
670     it('Should fail with with the root account', async function () {
671       await deleteMe(server.url, server.accessToken, 400)
672     })
673   })
674
675   describe('When registering a new user', function () {
676     const registrationPath = path + '/register'
677     const baseCorrectParams = {
678       username: 'user3',
679       displayName: 'super user',
680       email: 'test3@example.com',
681       password: 'my super password'
682     }
683
684     it('Should fail with a too small username', async function () {
685       const fields = immutableAssign(baseCorrectParams, { username: '' })
686
687       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
688     })
689
690     it('Should fail with a too long username', async function () {
691       const fields = immutableAssign(baseCorrectParams, { username: 'super'.repeat(50) })
692
693       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
694     })
695
696     it('Should fail with an incorrect username', async function () {
697       const fields = immutableAssign(baseCorrectParams, { username: 'my username' })
698
699       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
700     })
701
702     it('Should fail with a missing email', async function () {
703       const fields = omit(baseCorrectParams, 'email')
704
705       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
706     })
707
708     it('Should fail with an invalid email', async function () {
709       const fields = immutableAssign(baseCorrectParams, { email: 'test_example.com' })
710
711       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
712     })
713
714     it('Should fail with a too small password', async function () {
715       const fields = immutableAssign(baseCorrectParams, { password: 'bla' })
716
717       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
718     })
719
720     it('Should fail with a too long password', async function () {
721       const fields = immutableAssign(baseCorrectParams, { password: 'super'.repeat(61) })
722
723       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
724     })
725
726     it('Should fail if we register a user with the same username', async function () {
727       const fields = immutableAssign(baseCorrectParams, { username: 'root' })
728
729       await makePostBodyRequest({
730         url: server.url,
731         path: registrationPath,
732         token: server.accessToken,
733         fields,
734         statusCodeExpected: 409
735       })
736     })
737
738     it('Should fail with a "peertube" username', async function () {
739       const fields = immutableAssign(baseCorrectParams, { username: 'peertube' })
740
741       await makePostBodyRequest({
742         url: server.url,
743         path: registrationPath,
744         token: server.accessToken,
745         fields,
746         statusCodeExpected: 409
747       })
748     })
749
750     it('Should fail if we register a user with the same email', async function () {
751       const fields = immutableAssign(baseCorrectParams, { email: 'admin' + server.internalServerNumber + '@example.com' })
752
753       await makePostBodyRequest({
754         url: server.url,
755         path: registrationPath,
756         token: server.accessToken,
757         fields,
758         statusCodeExpected: 409
759       })
760     })
761
762     it('Should fail with a bad display name', async function () {
763       const fields = immutableAssign(baseCorrectParams, { displayName: 'a'.repeat(150) })
764
765       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
766     })
767
768     it('Should fail with a bad channel name', async function () {
769       const fields = immutableAssign(baseCorrectParams, { channel: { name: '[]azf', displayName: 'toto' } })
770
771       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
772     })
773
774     it('Should fail with a bad channel display name', async function () {
775       const fields = immutableAssign(baseCorrectParams, { channel: { name: 'toto', displayName: '' } })
776
777       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
778     })
779
780     it('Should fail with a channel name that is the same than user username', async function () {
781       const source = { username: 'super_user', channel: { name: 'super_user', displayName: 'display name' } }
782       const fields = immutableAssign(baseCorrectParams, source)
783
784       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
785     })
786
787     it('Should fail with an existing channel', async function () {
788       const videoChannelAttributesArg = { name: 'existing_channel', displayName: 'hello', description: 'super description' }
789       await addVideoChannel(server.url, server.accessToken, videoChannelAttributesArg)
790
791       const fields = immutableAssign(baseCorrectParams, { channel: { name: 'existing_channel', displayName: 'toto' } })
792
793       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields, statusCodeExpected: 409 })
794     })
795
796     it('Should succeed with the correct params', async function () {
797       const fields = immutableAssign(baseCorrectParams, { channel: { name: 'super_channel', displayName: 'toto' } })
798
799       await makePostBodyRequest({
800         url: server.url,
801         path: registrationPath,
802         token: server.accessToken,
803         fields: fields,
804         statusCodeExpected: 204
805       })
806     })
807
808     it('Should fail on a server with registration disabled', async function () {
809       const fields = {
810         username: 'user4',
811         email: 'test4@example.com',
812         password: 'my super password 4'
813       }
814
815       await makePostBodyRequest({
816         url: serverWithRegistrationDisabled.url,
817         path: registrationPath,
818         token: serverWithRegistrationDisabled.accessToken,
819         fields,
820         statusCodeExpected: 403
821       })
822     })
823   })
824
825   describe('When registering multiple users on a server with users limit', function () {
826     it('Should fail when after 3 registrations', async function () {
827       await registerUser(server.url, 'user42', 'super password', 403)
828     })
829   })
830
831   describe('When having a video quota', function () {
832     it('Should fail with a user having too many videos', async function () {
833       await updateUser({
834         url: server.url,
835         userId: rootId,
836         accessToken: server.accessToken,
837         videoQuota: 42
838       })
839
840       await uploadVideo(server.url, server.accessToken, {}, 403)
841     })
842
843     it('Should fail with a registered user having too many videos', async function () {
844       this.timeout(30000)
845
846       const user = {
847         username: 'user3',
848         password: 'my super password'
849       }
850       userAccessToken = await userLogin(server, user)
851
852       const videoAttributes = { fixture: 'video_short2.webm' }
853       await uploadVideo(server.url, userAccessToken, videoAttributes)
854       await uploadVideo(server.url, userAccessToken, videoAttributes)
855       await uploadVideo(server.url, userAccessToken, videoAttributes)
856       await uploadVideo(server.url, userAccessToken, videoAttributes)
857       await uploadVideo(server.url, userAccessToken, videoAttributes)
858       await uploadVideo(server.url, userAccessToken, videoAttributes, 403)
859     })
860
861     it('Should fail to import with HTTP/Torrent/magnet', async function () {
862       this.timeout(120000)
863
864       const baseAttributes = {
865         channelId: 1,
866         privacy: VideoPrivacy.PUBLIC
867       }
868       await importVideo(server.url, server.accessToken, immutableAssign(baseAttributes, { targetUrl: getYoutubeVideoUrl() }))
869       await importVideo(server.url, server.accessToken, immutableAssign(baseAttributes, { magnetUri: getMagnetURI() }))
870       await importVideo(server.url, server.accessToken, immutableAssign(baseAttributes, { torrentfile: 'video-720p.torrent' }))
871
872       await waitJobs([ server ])
873
874       const res = await getMyVideoImports(server.url, server.accessToken)
875
876       expect(res.body.total).to.equal(3)
877       const videoImports: VideoImport[] = res.body.data
878       expect(videoImports).to.have.lengthOf(3)
879
880       for (const videoImport of videoImports) {
881         expect(videoImport.state.id).to.equal(VideoImportState.FAILED)
882         expect(videoImport.error).not.to.be.undefined
883         expect(videoImport.error).to.contain('user video quota is exceeded')
884       }
885     })
886   })
887
888   describe('When having a daily video quota', function () {
889     it('Should fail with a user having too many videos', async function () {
890       await updateUser({
891         url: server.url,
892         userId: rootId,
893         accessToken: server.accessToken,
894         videoQuotaDaily: 42
895       })
896
897       await uploadVideo(server.url, server.accessToken, {}, 403)
898     })
899   })
900
901   describe('When having an absolute and daily video quota', function () {
902     it('Should fail if exceeding total quota', async function () {
903       await updateUser({
904         url: server.url,
905         userId: rootId,
906         accessToken: server.accessToken,
907         videoQuota: 42,
908         videoQuotaDaily: 1024 * 1024 * 1024
909       })
910
911       await uploadVideo(server.url, server.accessToken, {}, 403)
912     })
913
914     it('Should fail if exceeding daily quota', async function () {
915       await updateUser({
916         url: server.url,
917         userId: rootId,
918         accessToken: server.accessToken,
919         videoQuota: 1024 * 1024 * 1024,
920         videoQuotaDaily: 42
921       })
922
923       await uploadVideo(server.url, server.accessToken, {}, 403)
924     })
925   })
926
927   describe('When asking a password reset', function () {
928     const path = '/api/v1/users/ask-reset-password'
929
930     it('Should fail with a missing email', async function () {
931       const fields = {}
932
933       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
934     })
935
936     it('Should fail with an invalid email', async function () {
937       const fields = { email: 'hello' }
938
939       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
940     })
941
942     it('Should success with the correct params', async function () {
943       const fields = { email: 'admin@example.com' }
944
945       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 204 })
946     })
947   })
948
949   describe('When asking for an account verification email', function () {
950     const path = '/api/v1/users/ask-send-verify-email'
951
952     it('Should fail with a missing email', async function () {
953       const fields = {}
954
955       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
956     })
957
958     it('Should fail with an invalid email', async function () {
959       const fields = { email: 'hello' }
960
961       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
962     })
963
964     it('Should succeed with the correct params', async function () {
965       const fields = { email: 'admin@example.com' }
966
967       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 204 })
968     })
969   })
970
971   after(async function () {
972     await cleanupTests([ server, serverWithRegistrationDisabled ])
973   })
974 })