Validate uniqueness of user email address

* Make user.email attribute unique
* Render validations error messages in users/new view
This commit is contained in:
Thibault Jouan
2011-09-08 15:40:30 +00:00
parent 35b081ed68
commit 688a178376
6 changed files with 44 additions and 1 deletions

View File

@@ -32,6 +32,14 @@ describe User do
it { should_not be_valid }
end
context 'when a user with the same email address already exists' do
it 'should not be valid' do
user = Factory.create(:user, :email => 'unique@example.net')
new_user = Factory.build(:user, :email => user.email)
new_user.should_not be_valid
end
end
describe '#password=' do
it 'stores a bcrypt hash of the password in password_hash' do
BCrypt::Password.new(user.password_hash).should == user.password

View File

@@ -45,4 +45,15 @@ describe 'users/new.html.haml' do
:text => 'Password confirmation'
)
end
context 'when the user has some validation errors' do
it 'on the email address uniqueness' do
user = Factory.create(:user, :email => 'unique@example.net')
new_user = Factory.build(:user, :email => user.email)
new_user.save
assign :user, new_user
render
rendered.should have_content 'Email has already been taken'
end
end
end