From 1057cab0096968e8c25a772181ac833ad30d4bdc Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Tue, 6 Sep 2011 20:46:01 +0000 Subject: [PATCH] Implement user registration feature * Add users/new action and view * Add users/create action * Add link to sign up in sign in page --- app/controllers/users_controller.rb | 17 ++++++++ app/views/sessions/new.html.haml | 1 + app/views/users/new.html.haml | 8 ++++ config/routes.rb | 2 + features/sessions.feature | 6 +++ features/step_definitions/sessions_step.rb | 12 ++++++ spec/controllers/users_controller_spec.rb | 44 ++++++++++++++++++++ spec/views/sessions/new.html.haml_spec.rb | 5 +++ spec/views/users/new.html.haml_spec.rb | 48 ++++++++++++++++++++++ 9 files changed, 143 insertions(+) create mode 100644 app/controllers/users_controller.rb create mode 100644 app/views/users/new.html.haml create mode 100644 spec/controllers/users_controller_spec.rb create mode 100644 spec/views/users/new.html.haml_spec.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 0000000..ec472cf --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,17 @@ +class UsersController < ApplicationController + skip_before_filter :authenticate!, :only => [:new, :create] + + def new + @user = User.new + end + + def create + @user = User.new(params[:user]) + if ! @user.save + render :new + else + self.current_user = @user + redirect_to :root + end + end +end diff --git a/app/views/sessions/new.html.haml b/app/views/sessions/new.html.haml index d6fe311..be1d084 100644 --- a/app/views/sessions/new.html.haml +++ b/app/views/sessions/new.html.haml @@ -4,3 +4,4 @@ = f.label :password = f.password_field :password = f.submit 'Sign in' += link_to 'Sign up', new_user_path diff --git a/app/views/users/new.html.haml b/app/views/users/new.html.haml new file mode 100644 index 0000000..7b8e6bf --- /dev/null +++ b/app/views/users/new.html.haml @@ -0,0 +1,8 @@ += form_for(@user, :url => users_path) do |f| + = f.label :email + = f.text_field :email + = f.label :password + = f.password_field :password + = f.label :password_confirmation + = f.password_field :password_confirmation + = f.submit 'Sign up' diff --git a/config/routes.rb b/config/routes.rb index 206586f..bd23103 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ Scube::Application.routes.draw do + resources :users, :only => [:new, :create] + resources :sessions, :only => [:new, :create] resources :tracks do diff --git a/features/sessions.feature b/features/sessions.feature index 765973e..a8cea4c 100644 --- a/features/sessions.feature +++ b/features/sessions.feature @@ -15,3 +15,9 @@ Feature: User Then I should be redirected to the sign in page When I submit valid credentials Then I should be redirected to the home page + + Scenario: User registration + Given I go to the home page + When I follow the sign up link + And I fill in the sign up form + Then I should be redirected to the home page diff --git a/features/step_definitions/sessions_step.rb b/features/step_definitions/sessions_step.rb index aa3535f..11de1f6 100644 --- a/features/step_definitions/sessions_step.rb +++ b/features/step_definitions/sessions_step.rb @@ -21,3 +21,15 @@ When /^I submit valid credentials$/ do fill_in('Password', :with => user.password) click_button('Sign in') end + +When /^I follow the sign up link$/ do + click_link('Sign up') +end + +When /^I fill in the sign up form$/ do + @user = Factory.build(:user) + fill_in('Email', :with => @user.email) + fill_in('Password', :with => @user.password) + fill_in('Password confirmation', :with => @user.password) + click_button('Sign up') +end diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb new file mode 100644 index 0000000..fbf39a7 --- /dev/null +++ b/spec/controllers/users_controller_spec.rb @@ -0,0 +1,44 @@ +require 'spec_helper' + +describe UsersController do + describe 'GET new' do + it 'assigns a new user as @user' do + get :new + assigns[:user].should be_a_new(User) + end + end + + describe 'POST create' do + context 'whith valid params' do + it 'creates a new user' do + expect { + post :create, :user => Factory.attributes_for(:user) + }.to change(User, :count).by(1) + end + + it 'signs the user in' do + post :create, :user => Factory.attributes_for(:user) + controller.current_user.should_not be_nil + end + + it 'redirects to the home page' do + post :create, :user => Factory.attributes_for(:user) + response.should redirect_to(:root) + end + end + + context 'whith invalid params' do + before { User.any_instance.stub(:save).and_return(false) } + + it 'assigns the user as @user' do + post :create, :user => {} + assigns[:user].should be_a_new(User) + end + + it 'renders the new template' do + post :create, :user => {} + response.should render_template('new') + end + end + end +end diff --git a/spec/views/sessions/new.html.haml_spec.rb b/spec/views/sessions/new.html.haml_spec.rb index 0b4cffd..30e3603 100644 --- a/spec/views/sessions/new.html.haml_spec.rb +++ b/spec/views/sessions/new.html.haml_spec.rb @@ -23,4 +23,9 @@ describe 'sessions/new.html.haml' do rendered.should \ have_selector('label[for=session_password]', :text => 'Password') end + + it 'renders a link to the sign in page' do + render + rendered.should have_selector("a[href='#{new_user_path}']", :text => 'Sign up') + end end diff --git a/spec/views/users/new.html.haml_spec.rb b/spec/views/users/new.html.haml_spec.rb new file mode 100644 index 0000000..3342e27 --- /dev/null +++ b/spec/views/users/new.html.haml_spec.rb @@ -0,0 +1,48 @@ +require 'spec_helper' + +describe 'users/new.html.haml' do + let(:user) do + mock_model('User').as_new_record.as_null_object + end + + before do + assign :user, user + end + + it 'renders a form to sign up' do + render + rendered.should have_selector("form[method=post][action='#{users_path}']") + rendered.should have_selector('input[type=submit]') + end + + it 'renders a text field with a label for the mail address' do + render + rendered.should have_selector( + "input[type=text][name='user[email]']" + ) + rendered.should have_selector( + 'label[for=user_email]', :text => 'Email' + ) + end + + it 'renders a password field with a label for the password' do + render + rendered.should have_selector( + "input[type=password][name='user[password]']" + ) + rendered.should have_selector( + 'label[for=user_password]', :text => 'Password' + ) + end + + it 'renders a password field with a label for the password confirmation' do + render + rendered.should have_selector( + "input[type=password][name='user[password_confirmation]']" + ) + rendered.should have_selector( + 'label[for=user_password_confirmation]', + :text => 'Password confirmation' + ) + end +end