From 688a1783764b3581ab7b3efa4740b1cc4867c4b6 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Thu, 8 Sep 2011 15:40:30 +0000 Subject: [PATCH] Validate uniqueness of user email address * Make user.email attribute unique * Render validations error messages in users/new view --- app/models/user.rb | 5 ++++- app/views/users/new.html.haml | 4 ++++ features/step_definitions/user_step.rb | 12 ++++++++++++ features/user.feature | 5 +++++ spec/models/user_spec.rb | 8 ++++++++ spec/views/users/new.html.haml_spec.rb | 11 +++++++++++ 6 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 features/step_definitions/user_step.rb diff --git a/app/models/user.rb b/app/models/user.rb index 414b5bc..3e13cc3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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) diff --git a/app/views/users/new.html.haml b/app/views/users/new.html.haml index 131a57f..639f8ca 100644 --- a/app/views/users/new.html.haml +++ b/app/views/users/new.html.haml @@ -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 diff --git a/features/step_definitions/user_step.rb b/features/step_definitions/user_step.rb new file mode 100644 index 0000000..732f722 --- /dev/null +++ b/features/step_definitions/user_step.rb @@ -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 diff --git a/features/user.feature b/features/user.feature index f09a17a..f69edd0 100644 --- a/features/user.feature +++ b/features/user.feature @@ -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" diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index d1777e6..3135783 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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 diff --git a/spec/views/users/new.html.haml_spec.rb b/spec/views/users/new.html.haml_spec.rb index 3342e27..a9216cc 100644 --- a/spec/views/users/new.html.haml_spec.rb +++ b/spec/views/users/new.html.haml_spec.rb @@ -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