Add ability to disable video comments
[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 } from '../../../../shared'
7
8 import {
9   createUser, flushTests, getMyUserInformation, getMyUserVideoRating, getUsersList, immutableAssign, killallServers, makeGetRequest,
10   makePostBodyRequest, makePostUploadRequest, makePutBodyRequest, registerUser, removeUser, runServer, ServerInfo, setAccessTokensToServers,
11   updateUser, uploadVideo, userLogin
12 } from '../../utils'
13 import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '../../utils/requests/check-api-params'
14
15 describe('Test users API validators', function () {
16   const path = '/api/v1/users/'
17   let userId: number
18   let rootId: number
19   let videoId: number
20   let server: ServerInfo
21   let serverWithRegistrationDisabled: ServerInfo
22   let userAccessToken = ''
23
24   // ---------------------------------------------------------------
25
26   before(async function () {
27     this.timeout(20000)
28
29     await flushTests()
30
31     server = await runServer(1)
32     serverWithRegistrationDisabled = await runServer(2)
33
34     await setAccessTokensToServers([ server ])
35
36     const user = {
37       username: 'user1',
38       password: 'my super password'
39     }
40     const videoQuota = 42000000
41     await createUser(server.url, server.accessToken, user.username, user.password, videoQuota)
42     userAccessToken = await userLogin(server, user)
43
44     const res = await uploadVideo(server.url, server.accessToken, {})
45     videoId = res.body.video.id
46   })
47
48   describe('When listing users', function () {
49     it('Should fail with a bad start pagination', async function () {
50       await checkBadStartPagination(server.url, path, server.accessToken)
51     })
52
53     it('Should fail with a bad count pagination', async function () {
54       await checkBadCountPagination(server.url, path, server.accessToken)
55     })
56
57     it('Should fail with an incorrect sort', async function () {
58       await checkBadSortPagination(server.url, path, server.accessToken)
59     })
60
61     it('Should fail with a non authenticated user', async function () {
62       await makeGetRequest({
63         url: server.url,
64         path,
65         statusCodeExpected: 401
66       })
67     })
68
69     it('Should fail with a non admin user', async function () {
70       await makeGetRequest({
71         url: server.url,
72         path,
73         token: userAccessToken,
74         statusCodeExpected: 403
75       })
76     })
77   })
78
79   describe('When adding a new user', function () {
80     const baseCorrectParams = {
81       username: 'user2',
82       email: 'test@example.com',
83       password: 'my super password',
84       videoQuota: -1,
85       role: UserRole.USER
86     }
87
88     it('Should fail with a too small username', async function () {
89       const fields = immutableAssign(baseCorrectParams, { username: 'fi' })
90
91       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
92     })
93
94     it('Should fail with a too long username', async function () {
95       const fields = immutableAssign(baseCorrectParams, { username: 'my_super_username_which_is_very_long' })
96
97       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
98     })
99
100     it('Should fail with a not lowercase username', async function () {
101       const fields = immutableAssign(baseCorrectParams, { username: 'Toto' })
102
103       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
104     })
105
106     it('Should fail with an incorrect username', async function () {
107       const fields = immutableAssign(baseCorrectParams, { username: 'my username' })
108
109       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
110     })
111
112     it('Should fail with a missing email', async function () {
113       const fields = omit(baseCorrectParams, 'email')
114
115       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
116     })
117
118     it('Should fail with an invalid email', async function () {
119       const fields = immutableAssign(baseCorrectParams, { email: 'test_example.com' })
120
121       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
122     })
123
124     it('Should fail with a too small password', async function () {
125       const fields = immutableAssign(baseCorrectParams, { password: 'bla' })
126
127       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
128     })
129
130     it('Should fail with a too long password', async function () {
131       const fields = immutableAssign(baseCorrectParams, { password: 'super'.repeat(61) })
132
133       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
134     })
135
136     it('Should fail with an non authenticated user', async function () {
137       await makePostBodyRequest({
138         url: server.url,
139         path,
140         token: 'super token',
141         fields: baseCorrectParams,
142         statusCodeExpected: 401
143       })
144     })
145
146     it('Should fail if we add a user with the same username', async function () {
147       const fields = immutableAssign(baseCorrectParams, { username: 'user1' })
148
149       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 409 })
150     })
151
152     it('Should fail if we add a user with the same email', async function () {
153       const fields = immutableAssign(baseCorrectParams, { email: 'user1@example.com' })
154
155       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 409 })
156     })
157
158     it('Should fail without a videoQuota', async function () {
159       const fields = omit(baseCorrectParams, 'videoQuota')
160
161       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
162     })
163
164     it('Should fail with an invalid videoQuota', async function () {
165       const fields = immutableAssign(baseCorrectParams, { videoQuota: -5 })
166
167       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
168     })
169
170     it('Should fail without a user role', async function () {
171       const fields = omit(baseCorrectParams, 'role')
172
173       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
174     })
175
176     it('Should fail with an invalid user role', async function () {
177       const fields = immutableAssign(baseCorrectParams, { role: 88989 })
178
179       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
180     })
181
182     it('Should succeed with the correct params', async function () {
183       await makePostBodyRequest({
184         url: server.url,
185         path,
186         token: server.accessToken,
187         fields: baseCorrectParams,
188         statusCodeExpected: 204
189       })
190     })
191
192     it('Should fail with a non admin user', async function () {
193       const user = {
194         username: 'user1',
195         password: 'my super password'
196       }
197       userAccessToken = await userLogin(server, user)
198
199       const fields = {
200         username: 'user3',
201         email: 'test@example.com',
202         password: 'my super password',
203         videoQuota: 42000000
204       }
205       await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields, statusCodeExpected: 403 })
206     })
207   })
208
209   describe('When updating my account', function () {
210     it('Should fail with an invalid email attribute', async function () {
211       const fields = {
212         email: 'blabla'
213       }
214
215       await makePutBodyRequest({ url: server.url, path: path + 'me', token: server.accessToken, fields })
216     })
217
218     it('Should fail with a too small password', async function () {
219       const fields = {
220         password: 'bla'
221       }
222
223       await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields })
224     })
225
226     it('Should fail with a too long password', async function () {
227       const fields = {
228         password: 'super'.repeat(61)
229       }
230
231       await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields })
232     })
233
234     it('Should fail with an invalid display NSFW attribute', async function () {
235       const fields = {
236         displayNSFW: -1
237       }
238
239       await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields })
240     })
241
242     it('Should fail with an invalid autoPlayVideo attribute', async function () {
243       const fields = {
244         autoPlayVideo: -1
245       }
246
247       await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields })
248     })
249
250     it('Should fail with an non authenticated user', async function () {
251       const fields = {
252         password: 'my super password'
253       }
254
255       await makePutBodyRequest({ url: server.url, path: path + 'me', token: 'super token', fields, statusCodeExpected: 401 })
256     })
257
258     it('Should succeed with the correct params', async function () {
259       const fields = {
260         password: 'my super password',
261         displayNSFW: true,
262         autoPlayVideo: false,
263         email: 'super_email@example.com'
264       }
265
266       await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields, statusCodeExpected: 204 })
267     })
268   })
269
270   describe('When updating my avatar', function () {
271     it('Should fail without an incorrect input file', async function () {
272       const fields = {}
273       const attaches = {
274         'avatarfile': join(__dirname, '..', 'fixtures', 'video_short.mp4')
275       }
276       await makePostUploadRequest({ url: server.url, path: path + '/me/avatar/pick', token: server.accessToken, fields, attaches })
277     })
278
279     it('Should succeed with the correct params', async function () {
280       const fields = {}
281       const attaches = {
282         'avatarfile': join(__dirname, '..', 'fixtures', 'avatar.png')
283       }
284       await makePostUploadRequest({
285         url: server.url,
286         path: path + '/me/avatar/pick',
287         token: server.accessToken,
288         fields,
289         attaches,
290         statusCodeExpected: 200
291       })
292     })
293   })
294
295   describe('When updating a user', function () {
296
297     before(async function () {
298       const res = await getUsersList(server.url, server.accessToken)
299
300       userId = res.body.data[1].id
301       rootId = res.body.data[2].id
302     })
303
304     it('Should fail with an invalid email attribute', async function () {
305       const fields = {
306         email: 'blabla'
307       }
308
309       await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
310     })
311
312     it('Should fail with an invalid videoQuota attribute', async function () {
313       const fields = {
314         videoQuota: -90
315       }
316
317       await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
318     })
319
320     it('Should fail with an invalid user role attribute', async function () {
321       const fields = {
322         role: 54878
323       }
324
325       await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields })
326     })
327
328     it('Should fail with an non authenticated user', async function () {
329       const fields = {
330         videoQuota: 42
331       }
332
333       await makePutBodyRequest({ url: server.url, path: path + userId, token: 'super token', fields, statusCodeExpected: 401 })
334     })
335
336     it('Should succeed with the correct params', async function () {
337       const fields = {
338         email: 'email@example.com',
339         videoQuota: 42,
340         role: UserRole.MODERATOR
341       }
342
343       await makePutBodyRequest({ url: server.url, path: path + userId, token: server.accessToken, fields, statusCodeExpected: 204 })
344     })
345   })
346
347   describe('When getting my information', function () {
348     it('Should fail with a non authenticated user', async function () {
349       await getMyUserInformation(server.url, 'fake_token', 401)
350     })
351
352     it('Should success with the correct parameters', async function () {
353       await getMyUserInformation(server.url, userAccessToken)
354     })
355   })
356
357   describe('When getting my video rating', function () {
358     it('Should fail with a non authenticated user', async function () {
359       await getMyUserVideoRating(server.url, 'fake_token', videoId, 401)
360     })
361
362     it('Should fail with an incorrect video uuid', async function () {
363       await getMyUserVideoRating(server.url, server.accessToken, 'blabla', 400)
364     })
365
366     it('Should fail with an unknown video', async function () {
367       await getMyUserVideoRating(server.url, server.accessToken, '4da6fde3-88f7-4d16-b119-108df5630b06', 404)
368     })
369
370     it('Should succeed with the correct parameters', async function () {
371       await getMyUserVideoRating(server.url, server.accessToken, videoId)
372     })
373   })
374
375   describe('When removing an user', function () {
376     it('Should fail with an incorrect id', async function () {
377       await removeUser(server.url, 'blabla', server.accessToken, 400)
378     })
379
380     it('Should fail with the root user', async function () {
381       await removeUser(server.url, rootId, server.accessToken, 400)
382     })
383
384     it('Should return 404 with a non existing id', async function () {
385       await removeUser(server.url, 4545454, server.accessToken, 404)
386     })
387   })
388
389   describe('When register a new user', function () {
390     const registrationPath = path + '/register'
391     const baseCorrectParams = {
392       username: 'user3',
393       email: 'test3@example.com',
394       password: 'my super password'
395     }
396
397     it('Should fail with a too small username', async function () {
398       const fields = immutableAssign(baseCorrectParams, { username: 'ji' })
399
400       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
401     })
402
403     it('Should fail with a too long username', async function () {
404       const fields = immutableAssign(baseCorrectParams, { username: 'my_super_username_which_is_very_long' })
405
406       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
407     })
408
409     it('Should fail with an incorrect username', async function () {
410       const fields = immutableAssign(baseCorrectParams, { username: 'my username' })
411
412       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
413     })
414
415     it('Should fail with a missing email', async function () {
416       const fields = omit(baseCorrectParams, 'email')
417
418       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
419     })
420
421     it('Should fail with an invalid email', async function () {
422       const fields = immutableAssign(baseCorrectParams, { email: 'test_example.com' })
423
424       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
425     })
426
427     it('Should fail with a too small password', async function () {
428       const fields = immutableAssign(baseCorrectParams, { password: 'bla' })
429
430       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
431     })
432
433     it('Should fail with a too long password', async function () {
434       const fields = immutableAssign(baseCorrectParams, { password: 'super'.repeat(61) })
435
436       await makePostBodyRequest({ url: server.url, path: registrationPath, token: server.accessToken, fields })
437     })
438
439     it('Should fail if we register a user with the same username', async function () {
440       const fields = immutableAssign(baseCorrectParams, { username: 'root' })
441
442       await makePostBodyRequest({
443         url: server.url,
444         path: registrationPath,
445         token: server.accessToken,
446         fields,
447         statusCodeExpected: 409
448       })
449     })
450
451     it('Should fail if we register a user with the same email', async function () {
452       const fields = immutableAssign(baseCorrectParams, { email: 'admin1@example.com' })
453
454       await makePostBodyRequest({
455         url: server.url,
456         path: registrationPath,
457         token: server.accessToken,
458         fields,
459         statusCodeExpected: 409
460       })
461     })
462
463     it('Should succeed with the correct params', async function () {
464       await makePostBodyRequest({
465         url: server.url,
466         path: registrationPath,
467         token: server.accessToken,
468         fields: baseCorrectParams,
469         statusCodeExpected: 204
470       })
471     })
472
473     it('Should fail on a server with registration disabled', async function () {
474       const fields = {
475         username: 'user4',
476         email: 'test4@example.com',
477         password: 'my super password 4'
478       }
479
480       await makePostBodyRequest({
481         url: serverWithRegistrationDisabled.url,
482         path: registrationPath,
483         token: serverWithRegistrationDisabled.accessToken,
484         fields,
485         statusCodeExpected: 403
486       })
487     })
488   })
489
490   describe('When registering multiple users on a server with users limit', function () {
491     it('Should fail when after 3 registrations', async function () {
492       await registerUser(server.url, 'user42', 'super password', 403)
493     })
494   })
495
496   describe('When having a video quota', function () {
497     it('Should fail with a user having too many video', async function () {
498       await updateUser({
499         url: server.url,
500         userId: rootId,
501         accessToken: server.accessToken,
502         videoQuota: 42
503       })
504
505       await uploadVideo(server.url, server.accessToken, {}, 403)
506     })
507
508     it('Should fail with a registered user having too many video', async function () {
509       this.timeout(10000)
510
511       const user = {
512         username: 'user3',
513         password: 'my super password'
514       }
515       userAccessToken = await userLogin(server, user)
516
517       const videoAttributes = { fixture: 'video_short2.webm' }
518       await uploadVideo(server.url, userAccessToken, videoAttributes)
519       await uploadVideo(server.url, userAccessToken, videoAttributes)
520       await uploadVideo(server.url, userAccessToken, videoAttributes)
521       await uploadVideo(server.url, userAccessToken, videoAttributes)
522       await uploadVideo(server.url, userAccessToken, videoAttributes)
523       await uploadVideo(server.url, userAccessToken, videoAttributes, 403)
524     })
525   })
526
527   after(async function () {
528     killallServers([ server, serverWithRegistrationDisabled ])
529
530     // Keep the logs if the test failed
531     if (this['ok']) {
532       await flushTests()
533     }
534   })
535 })