Implement user registration feature
* Add users/new action and view * Add users/create action * Add link to sign up in sign in page
This commit is contained in:
parent
d2f4ea59ff
commit
1057cab009
17
app/controllers/users_controller.rb
Normal file
17
app/controllers/users_controller.rb
Normal file
@ -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
|
@ -4,3 +4,4 @@
|
|||||||
= f.label :password
|
= f.label :password
|
||||||
= f.password_field :password
|
= f.password_field :password
|
||||||
= f.submit 'Sign in'
|
= f.submit 'Sign in'
|
||||||
|
= link_to 'Sign up', new_user_path
|
||||||
|
8
app/views/users/new.html.haml
Normal file
8
app/views/users/new.html.haml
Normal file
@ -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'
|
@ -1,4 +1,6 @@
|
|||||||
Scube::Application.routes.draw do
|
Scube::Application.routes.draw do
|
||||||
|
resources :users, :only => [:new, :create]
|
||||||
|
|
||||||
resources :sessions, :only => [:new, :create]
|
resources :sessions, :only => [:new, :create]
|
||||||
|
|
||||||
resources :tracks do
|
resources :tracks do
|
||||||
|
@ -15,3 +15,9 @@ Feature: User
|
|||||||
Then I should be redirected to the sign in page
|
Then I should be redirected to the sign in page
|
||||||
When I submit valid credentials
|
When I submit valid credentials
|
||||||
Then I should be redirected to the home page
|
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
|
||||||
|
@ -21,3 +21,15 @@ When /^I submit valid credentials$/ do
|
|||||||
fill_in('Password', :with => user.password)
|
fill_in('Password', :with => user.password)
|
||||||
click_button('Sign in')
|
click_button('Sign in')
|
||||||
end
|
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
|
||||||
|
44
spec/controllers/users_controller_spec.rb
Normal file
44
spec/controllers/users_controller_spec.rb
Normal file
@ -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
|
@ -23,4 +23,9 @@ describe 'sessions/new.html.haml' do
|
|||||||
rendered.should \
|
rendered.should \
|
||||||
have_selector('label[for=session_password]', :text => 'Password')
|
have_selector('label[for=session_password]', :text => 'Password')
|
||||||
end
|
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
|
end
|
||||||
|
48
spec/views/users/new.html.haml_spec.rb
Normal file
48
spec/views/users/new.html.haml_spec.rb
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user