diff --git a/app/models/user.rb b/app/models/user.rb index 8c43a7f7..00ffa813 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -17,6 +17,8 @@ class User :message => "Make your username from letters, numbers, underscores('_'), and dots('.')." validates_length_of :username, :in => (1..32), :message => "Your username needs at least 1 character but no more than 32." + validates_uniqueness_of :username, + :message => "User name has been taken already. Please try another" def to_param self.username diff --git a/config/initializers/simple_form.rb b/config/initializers/simple_form.rb index dc586340..7cfda7fd 100644 --- a/config/initializers/simple_form.rb +++ b/config/initializers/simple_form.rb @@ -3,7 +3,7 @@ # Components used by the form builder to generate a complete input. You can remove # any of them, change the order, or even add your own components to the stack. # config.components = [ :placeholder, :label_input, :hint, :error ] - config.components = [ :placeholder, :label_input, :hint ] + config.components = [ :placeholder, :label_input, :hint, :error ] # Default tag used on hints. # config.hint_tag = :span diff --git a/features/signup.feature b/features/signup.feature index 2cb76b84..f1123432 100644 --- a/features/signup.feature +++ b/features/signup.feature @@ -4,4 +4,8 @@ Feature: Sign up for an account Scenario: Create an account via the signup form When I register a new account - Then I should be logged in with my new account \ No newline at end of file + Then I should be logged in with my new account + + Scenario: Try to sign up using existing user name + When I register a duplicate account + Then I should see validation errors diff --git a/features/step_definitions/signup_steps.rb b/features/step_definitions/signup_steps.rb index 65c64ce4..9d1cdb15 100644 --- a/features/step_definitions/signup_steps.rb +++ b/features/step_definitions/signup_steps.rb @@ -1,17 +1,31 @@ -When /^I register a new account$/ do - @user_info = {:username => "username", :password => "password", :email => "test@example.com"} - +def register_user(user) visit new_user_registration_path - - fill_in("Username", :with => @user_info[:username]) - fill_in("Email", :with => @user_info[:email]) - fill_in("Password", :with => @user_info[:password]) - fill_in("Password confirmation", :with => @user_info[:password]) + fill_in("Username", :with => user[:username]) + fill_in("Email", :with => user[:email]) + fill_in("Password", :with => user[:password]) + fill_in("Password confirmation", :with => user[:password]) click_button "Sign up" end +When /^I register a new account$/ do + @new_user = {:username => "username", :password => "password", :email => "test@example.com"} + register_user @new_user +end + +When /^I register a duplicate account$/ do + @existing_user = User.create!(username: "existing_user", + email: "existing_user@example.com", + password: "foobar", + password_confirmation: "foobar") + register_user @existing_user +end + When /^I should be logged in with my new account$/ do page.should have_content("You have signed up successfully") - page.should have_content(@user_info[:username]) -end \ No newline at end of file + page.should have_content(@new_user[:username]) +end + +Then /^I should see validation errors$/ do + page.should have_selector(".error_notification") +end