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