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