Validate uniqueness of user email address
* Make user.email attribute unique * Render validations error messages in users/new view
This commit is contained in:
parent
35b081ed68
commit
688a178376
@ -8,7 +8,10 @@ class User < ActiveRecord::Base
|
||||
|
||||
has_many :playlists
|
||||
|
||||
validates_presence_of :email, :password_hash
|
||||
validates :email,
|
||||
:presence => true,
|
||||
:uniqueness => true
|
||||
validates_presence_of :password_hash
|
||||
validates_confirmation_of :password
|
||||
|
||||
def password=(plain_password)
|
||||
|
@ -1,4 +1,8 @@
|
||||
= form_for(@user, :url => users_path) do |f|
|
||||
- if @user.errors.any?
|
||||
%ul
|
||||
- @user.errors.full_messages.each do |m|
|
||||
%li= m
|
||||
%table
|
||||
%tbody
|
||||
%tr
|
||||
|
12
features/step_definitions/user_step.rb
Normal file
12
features/step_definitions/user_step.rb
Normal file
@ -0,0 +1,12 @@
|
||||
Given /^a user with "([^"]*)" email address exists$/ do |email|
|
||||
Factory.create(:user, :email => email)
|
||||
end
|
||||
|
||||
When /^I register with "([^"]*)" email address$/ do |email|
|
||||
visit new_user_path
|
||||
user = Factory.build(:user)
|
||||
fill_in('Email', :with => email)
|
||||
fill_in('Password', :with => user.password)
|
||||
fill_in('Password confirmation', :with => user.password)
|
||||
click_button('Sign up')
|
||||
end
|
@ -9,3 +9,8 @@ Feature: User
|
||||
When I follow the sign up link
|
||||
And I fill in the sign up form
|
||||
Then I should be redirected to the home page
|
||||
|
||||
Scenario: User can't register twice with the same email address
|
||||
Given a user with "unique@example.net" email address exists
|
||||
When I register with "unique@example.net" email address
|
||||
Then I should see "Email has already been taken"
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user