diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-04-09 12:08:36 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-04-09 12:08:36 +0200 |
commit | 2c2e90921646d223060e98d517fc8ac61c90eb9e (patch) | |
tree | cc36bfa79f0809e8c9602492788ef95b3521e003 /server | |
parent | db216afd980846c418a4ebab8190c3ead561dfc1 (diff) | |
download | PeerTube-2c2e90921646d223060e98d517fc8ac61c90eb9e.tar.gz PeerTube-2c2e90921646d223060e98d517fc8ac61c90eb9e.tar.zst PeerTube-2c2e90921646d223060e98d517fc8ac61c90eb9e.zip |
Server: add ability to register new user
Diffstat (limited to 'server')
-rw-r--r-- | server/controllers/api/users.js | 16 | ||||
-rw-r--r-- | server/tests/api/check-params/users.js | 127 | ||||
-rw-r--r-- | server/tests/api/users.js | 13 | ||||
-rw-r--r-- | server/tests/utils/users.js | 22 |
4 files changed, 176 insertions, 2 deletions
diff --git a/server/controllers/api/users.js b/server/controllers/api/users.js index 6b6c0774f..c7fe7bf85 100644 --- a/server/controllers/api/users.js +++ b/server/controllers/api/users.js | |||
@@ -44,6 +44,12 @@ router.post('/', | |||
44 | createUser | 44 | createUser |
45 | ) | 45 | ) |
46 | 46 | ||
47 | router.post('/register', | ||
48 | ensureRegistrationEnabled, | ||
49 | validatorsUsers.usersAdd, | ||
50 | createUser | ||
51 | ) | ||
52 | |||
47 | router.put('/:id', | 53 | router.put('/:id', |
48 | oAuth.authenticate, | 54 | oAuth.authenticate, |
49 | validatorsUsers.usersUpdate, | 55 | validatorsUsers.usersUpdate, |
@@ -66,6 +72,16 @@ module.exports = router | |||
66 | 72 | ||
67 | // --------------------------------------------------------------------------- | 73 | // --------------------------------------------------------------------------- |
68 | 74 | ||
75 | function ensureRegistrationEnabled (req, res, next) { | ||
76 | const registrationEnabled = constants.CONFIG.SIGNUP.ENABLED | ||
77 | |||
78 | if (registrationEnabled === true) { | ||
79 | return next() | ||
80 | } | ||
81 | |||
82 | return res.status(400).send('User registration is not enabled.') | ||
83 | } | ||
84 | |||
69 | function createUser (req, res, next) { | 85 | function createUser (req, res, next) { |
70 | const user = db.User.build({ | 86 | const user = db.User.build({ |
71 | username: req.body.username, | 87 | username: req.body.username, |
diff --git a/server/tests/api/check-params/users.js b/server/tests/api/check-params/users.js index 4a176e6c2..0aa9a4524 100644 --- a/server/tests/api/check-params/users.js +++ b/server/tests/api/check-params/users.js | |||
@@ -17,6 +17,7 @@ describe('Test users API validators', function () { | |||
17 | let rootId = null | 17 | let rootId = null |
18 | let videoId = null | 18 | let videoId = null |
19 | let server = null | 19 | let server = null |
20 | let serverWithRegistrationDisabled = null | ||
20 | let userAccessToken = null | 21 | let userAccessToken = null |
21 | 22 | ||
22 | // --------------------------------------------------------------- | 23 | // --------------------------------------------------------------- |
@@ -29,8 +30,15 @@ describe('Test users API validators', function () { | |||
29 | serversUtils.flushTests(next) | 30 | serversUtils.flushTests(next) |
30 | }, | 31 | }, |
31 | function (next) { | 32 | function (next) { |
32 | serversUtils.runServer(1, function (server1) { | 33 | serversUtils.runServer(1, function (serverCreated) { |
33 | server = server1 | 34 | server = serverCreated |
35 | |||
36 | next() | ||
37 | }) | ||
38 | }, | ||
39 | function (next) { | ||
40 | serversUtils.runServer(2, function (serverCreated) { | ||
41 | serverWithRegistrationDisabled = serverCreated | ||
34 | 42 | ||
35 | next() | 43 | next() |
36 | }) | 44 | }) |
@@ -394,6 +402,121 @@ describe('Test users API validators', function () { | |||
394 | }) | 402 | }) |
395 | }) | 403 | }) |
396 | 404 | ||
405 | describe('When register a new user', function () { | ||
406 | const registrationPath = path + '/register' | ||
407 | |||
408 | it('Should fail with a too small username', function (done) { | ||
409 | const data = { | ||
410 | username: 'ji', | ||
411 | email: 'test@example.com', | ||
412 | password: 'mysuperpassword' | ||
413 | } | ||
414 | |||
415 | requestsUtils.makePostBodyRequest(server.url, registrationPath, server.accessToken, data, done) | ||
416 | }) | ||
417 | |||
418 | it('Should fail with a too long username', function (done) { | ||
419 | const data = { | ||
420 | username: 'mysuperusernamewhichisverylong', | ||
421 | email: 'test@example.com', | ||
422 | password: 'mysuperpassword' | ||
423 | } | ||
424 | |||
425 | requestsUtils.makePostBodyRequest(server.url, registrationPath, server.accessToken, data, done) | ||
426 | }) | ||
427 | |||
428 | it('Should fail with an incorrect username', function (done) { | ||
429 | const data = { | ||
430 | username: 'my username', | ||
431 | email: 'test@example.com', | ||
432 | password: 'mysuperpassword' | ||
433 | } | ||
434 | |||
435 | requestsUtils.makePostBodyRequest(server.url, registrationPath, server.accessToken, data, done) | ||
436 | }) | ||
437 | |||
438 | it('Should fail with a missing email', function (done) { | ||
439 | const data = { | ||
440 | username: 'ji', | ||
441 | password: 'mysuperpassword' | ||
442 | } | ||
443 | |||
444 | requestsUtils.makePostBodyRequest(server.url, registrationPath, server.accessToken, data, done) | ||
445 | }) | ||
446 | |||
447 | it('Should fail with an invalid email', function (done) { | ||
448 | const data = { | ||
449 | username: 'mysuperusernamewhichisverylong', | ||
450 | email: 'testexample.com', | ||
451 | password: 'mysuperpassword' | ||
452 | } | ||
453 | |||
454 | requestsUtils.makePostBodyRequest(server.url, registrationPath, server.accessToken, data, done) | ||
455 | }) | ||
456 | |||
457 | it('Should fail with a too small password', function (done) { | ||
458 | const data = { | ||
459 | username: 'myusername', | ||
460 | email: 'test@example.com', | ||
461 | password: 'bla' | ||
462 | } | ||
463 | |||
464 | requestsUtils.makePostBodyRequest(server.url, registrationPath, server.accessToken, data, done) | ||
465 | }) | ||
466 | |||
467 | it('Should fail with a too long password', function (done) { | ||
468 | const data = { | ||
469 | username: 'myusername', | ||
470 | email: 'test@example.com', | ||
471 | password: 'my super long password which is very very very very very very very very very very very very very very' + | ||
472 | 'very very very very very very very very very very very very very very very veryv very very very very' + | ||
473 | 'very very very very very very very very very very very very very very very very very very very very long' | ||
474 | } | ||
475 | |||
476 | requestsUtils.makePostBodyRequest(server.url, registrationPath, server.accessToken, data, done) | ||
477 | }) | ||
478 | |||
479 | it('Should fail if we register a user with the same username', function (done) { | ||
480 | const data = { | ||
481 | username: 'root', | ||
482 | email: 'test@example.com', | ||
483 | password: 'my super password' | ||
484 | } | ||
485 | |||
486 | requestsUtils.makePostBodyRequest(server.url, registrationPath, server.accessToken, data, done, 409) | ||
487 | }) | ||
488 | |||
489 | it('Should fail if we register a user with the same email', function (done) { | ||
490 | const data = { | ||
491 | username: 'myusername', | ||
492 | email: 'admin1@example.com', | ||
493 | password: 'my super password' | ||
494 | } | ||
495 | |||
496 | requestsUtils.makePostBodyRequest(server.url, registrationPath, server.accessToken, data, done, 409) | ||
497 | }) | ||
498 | |||
499 | it('Should succeed with the correct params', function (done) { | ||
500 | const data = { | ||
501 | username: 'user3', | ||
502 | email: 'test3@example.com', | ||
503 | password: 'my super password' | ||
504 | } | ||
505 | |||
506 | requestsUtils.makePostBodyRequest(server.url, registrationPath, server.accessToken, data, done, 204) | ||
507 | }) | ||
508 | |||
509 | it('Should fail on a server with registration disabled', function (done) { | ||
510 | const data = { | ||
511 | username: 'user4', | ||
512 | email: 'test4@example.com', | ||
513 | password: 'my super password 4' | ||
514 | } | ||
515 | |||
516 | requestsUtils.makePostBodyRequest(serverWithRegistrationDisabled.url, registrationPath, serverWithRegistrationDisabled.accessToken, data, done, 400) | ||
517 | }) | ||
518 | }) | ||
519 | |||
397 | after(function (done) { | 520 | after(function (done) { |
398 | process.kill(-server.app.pid) | 521 | process.kill(-server.app.pid) |
399 | 522 | ||
diff --git a/server/tests/api/users.js b/server/tests/api/users.js index a5e8a7edf..10c96baeb 100644 --- a/server/tests/api/users.js +++ b/server/tests/api/users.js | |||
@@ -383,6 +383,19 @@ describe('Test users', function () { | |||
383 | }) | 383 | }) |
384 | }) | 384 | }) |
385 | 385 | ||
386 | it('Should register a new user', function (done) { | ||
387 | usersUtils.registerUser(server.url, 'user_15', 'my super password', done) | ||
388 | }) | ||
389 | |||
390 | it('Should be able to login with this registered user', function (done) { | ||
391 | server.user = { | ||
392 | username: 'user_15', | ||
393 | password: 'my super password' | ||
394 | } | ||
395 | |||
396 | loginUtils.loginAndGetAccessToken(server, done) | ||
397 | }) | ||
398 | |||
386 | after(function (done) { | 399 | after(function (done) { |
387 | process.kill(-server.app.pid) | 400 | process.kill(-server.app.pid) |
388 | 401 | ||
diff --git a/server/tests/utils/users.js b/server/tests/utils/users.js index 8138074d0..310dc0c6b 100644 --- a/server/tests/utils/users.js +++ b/server/tests/utils/users.js | |||
@@ -4,6 +4,7 @@ const request = require('supertest') | |||
4 | 4 | ||
5 | const usersUtils = { | 5 | const usersUtils = { |
6 | createUser, | 6 | createUser, |
7 | registerUser, | ||
7 | getUserInformation, | 8 | getUserInformation, |
8 | getUserVideoRating, | 9 | getUserVideoRating, |
9 | getUsersList, | 10 | getUsersList, |
@@ -36,6 +37,27 @@ function createUser (url, accessToken, username, password, specialStatus, end) { | |||
36 | .end(end) | 37 | .end(end) |
37 | } | 38 | } |
38 | 39 | ||
40 | function registerUser (url, username, password, specialStatus, end) { | ||
41 | if (!end) { | ||
42 | end = specialStatus | ||
43 | specialStatus = 204 | ||
44 | } | ||
45 | |||
46 | const path = '/api/v1/users/register' | ||
47 | const body = { | ||
48 | username, | ||
49 | password, | ||
50 | email: username + '@example.com' | ||
51 | } | ||
52 | |||
53 | request(url) | ||
54 | .post(path) | ||
55 | .set('Accept', 'application/json') | ||
56 | .send(body) | ||
57 | .expect(specialStatus) | ||
58 | .end(end) | ||
59 | } | ||
60 | |||
39 | function getUserInformation (url, accessToken, end) { | 61 | function getUserInformation (url, accessToken, end) { |
40 | const path = '/api/v1/users/me' | 62 | const path = '/api/v1/users/me' |
41 | 63 | ||