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