diff --git a/server/controllers/user/signup.php b/server/controllers/user/signup.php index 26757658..26718364 100644 --- a/server/controllers/user/signup.php +++ b/server/controllers/user/signup.php @@ -1,5 +1,7 @@ 'any', - 'requestData' => [] + 'requestData' => [ + 'name' => [ + 'validation' => DataValidator::length(2, 55)->alpha(), + 'error' => ERRORS::INVALID_NAME + ], + 'email' => [ + 'validation' => DataValidator::email(), + 'error' => ERRORS::INVALID_EMAIL + ], + 'password' => [ + 'validation' => DataValidator::length(5, 200), + 'error' => ERRORS::INVALID_PASSWORD + ] + ] ]; } diff --git a/server/models/ERRORS.php b/server/models/ERRORS.php index 33c431fa..a8dbe39c 100644 --- a/server/models/ERRORS.php +++ b/server/models/ERRORS.php @@ -3,8 +3,6 @@ class ERRORS { const INVALID_CREDENTIALS = 'User or password is not defined'; const SESSION_EXISTS = 'User is already logged in'; const NO_PERMISSION = 'You have no permission to access'; - const INVALID_EMAIL = 'Invalid email'; const INVALID_TITLE = 'Invalid title'; const INVALID_CONTENT = 'Invalid content'; - const INVALID_PASSWORD = 'Invalid password'; } diff --git a/tests/scripts.rb b/tests/scripts.rb index 52956341..f248029b 100644 --- a/tests/scripts.rb +++ b/tests/scripts.rb @@ -1,6 +1,7 @@ class Scripts - def self.createUser(email = 'steve@jobs.com', password = 'custompassword') + def self.createUser(email = 'steve@jobs.com', password = 'custompassword', name = 'steve jobs') response = request('/user/signup', { + 'name' => name, 'email' => email, 'password' => password }) diff --git a/tests/user/signup.rb b/tests/user/signup.rb index c22af4c5..8344e998 100644 --- a/tests/user/signup.rb +++ b/tests/user/signup.rb @@ -1,6 +1,7 @@ describe '/user/signup' do it 'should create user in database' do response = request('/user/signup', { + 'name' => 'Steve Jobs', 'email' => 'steve@jobs.com', 'password' => 'custom' }) @@ -9,4 +10,80 @@ describe '/user/signup' do (userRow['email']).should.equal('steve@jobs.com') end + + it 'should fail if name is invalid' do + long_text = '' + 100.times {long_text << 'a'} + + result = request('/user/signup', { + name: 't', + email: 'tyrion@outlook.com', + password: 'Lannister' + }) + + (result['status']).should.equal('fail') + (result['message']).should.equal('Invalid name') + + result = request('/user/signup', { + name: long_text, + email: 'tyrion@outlook.com', + password: 'Lannister' + }) + + (result['status']).should.equal('fail') + (result['message']).should.equal('Invalid name') + + result = request('/user/signup', { + name: 'tyri0n', + email: 'tyrion@outlook.com', + password: 'Lannister' + }) + + (result['status']).should.equal('fail') + (result['message']).should.equal('Invalid name') + end + + it 'should fail if email is invalid' do + result = request('/user/signup', { + name: 'tyrion', + email: 'tyrionoutlook.com', + password: 'Lannister' + }) + + (result['status']).should.equal('fail') + (result['message']).should.equal('Invalid email') + + result = request('/user/signup', { + name: 'tyrion', + email: 'tyrion@outlookcom', + password: 'Lannister' + }) + + (result['status']).should.equal('fail') + (result['message']).should.equal('Invalid email') + end + + it 'should fail if password is invalid' do + result = request('/user/signup', { + name: 'tyrion', + email: 'tyrion@outlook.com', + password: 'Lann' + }) + + (result['status']).should.equal('fail') + (result['message']).should.equal('Invalid password') + + long_text = '' + 250.times {long_text << 'a'} + + result = request('/user/signup', { + name: 'tyrion', + email: 'tyrion@outlook.com', + password: long_text + }) + + (result['status']).should.equal('fail') + (result['message']).should.equal('Invalid password') + end + end