diff options
author | Gareth Rees <gareth@mysociety.org> | 2014-10-29 13:00:00 +0000 |
---|---|---|
committer | Gareth Rees <gareth@mysociety.org> | 2014-10-29 13:01:10 +0000 |
commit | 60ad14c40fe6dc4a45d0e093697671fa292c6b8d (patch) | |
tree | 91f0ed941daeb0dcd8f9eb25c5c744183312bd02 | |
parent | a2bc82815cbf1e94825528a6da23e430bcb8a36a (diff) |
Add ChangeEmailValidator spec
-rw-r--r-- | spec/models/change_email_validator_spec.rb | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/spec/models/change_email_validator_spec.rb b/spec/models/change_email_validator_spec.rb new file mode 100644 index 000000000..b667a23d1 --- /dev/null +++ b/spec/models/change_email_validator_spec.rb @@ -0,0 +1,124 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +def validator_with_user_and_params(user, params = {}) + validator = ChangeEmailValidator.new(params) + validator.logged_in_user = user + validator +end + +describe ChangeEmailValidator do + + let(:user) { FactoryGirl.create(:user) } + + describe :old_email do + + it 'must have an old email' do + params = { :old_email => nil, + :new_email => 'new@example.com', + :user_circumstance => 'change_email', + :password => 'jonespassword' } + validator = validator_with_user_and_params(user, params) + + msg = 'Please enter your old email address' + expect(validator.errors_on(:old_email)).to include(msg) + end + + it 'must be a valid email' do + params = { :old_email => 'old', + :new_email => 'new@example.com', + :user_circumstance => 'change_email', + :password => 'jonespassword' } + validator = validator_with_user_and_params(user, params) + + msg = "Old email doesn't look like a valid address" + expect(validator.errors_on(:old_email)).to include(msg) + end + + it 'must have the same email as the logged in user' do + params = { :old_email => user.email, + :new_email => 'new@example.com', + :user_circumstance => 'change_email', + :password => 'jonespassword' } + validator = validator_with_user_and_params(user, params) + validator.logged_in_user = FactoryGirl.build(:user) + + msg = "Old email address isn't the same as the address of the account you are logged in with" + expect(validator.errors_on(:old_email)).to include(msg) + end + + end + + describe :new_email do + + it 'must have a new email' do + params = { :old_email => user.email, + :new_email => nil, + :user_circumstance => 'change_email', + :password => 'jonespassword' } + validator = validator_with_user_and_params(user, params) + + msg = 'Please enter your new email address' + expect(validator.errors_on(:new_email)).to include(msg) + end + + it 'must be a valid email' do + params = { :old_email => user.email, + :new_email => 'new', + :user_circumstance => 'change_email', + :password => 'jonespassword' } + validator = validator_with_user_and_params(user, params) + + msg = "New email doesn't look like a valid address" + expect(validator.errors_on(:new_email)).to include(msg) + end + + end + + describe :password do + + pending 'password_and_format_of_email validation fails when password is nil' do + it 'must have a password' do + params = { :old_email => user.email, + :new_email => 'new@example.com', + :password => nil } + validator = validator_with_user_and_params(user, params) + + msg = 'Please enter your password' + expect(validator.errors_on(:password)).to include(msg) + end + end + + it 'does not require a password if changing email' do + params = { :old_email => user.email, + :new_email => 'new@example.com', + :user_circumstance => 'change_email', + :password => '' } + validator = validator_with_user_and_params(user, params) + + expect(validator).to have(0).errors_on(:password) + end + + it 'must have a password if not changing email' do + params = { :old_email => user.email, + :new_email => 'new@example.com', + :user_circumstance => 'unknown', + :password => '' } + validator = validator_with_user_and_params(user, params) + + msg = 'Please enter your password' + expect(validator.errors_on(:password)).to include(msg) + end + + it 'must be the correct password' do + params = { :old_email => user.email, + :new_email => 'new@example.com', + :password => 'incorrectpass' } + validator = validator_with_user_and_params(user, params) + + msg = 'Password is not correct' + expect(validator.errors_on(:password)).to include(msg) + end + + end + +end |