From 71e1e7a7f6a69075c142f42ab12d46b3bf2d9aa9 Mon Sep 17 00:00:00 2001 From: Michael Hartl Date: Thu, 26 Aug 2010 17:22:41 -0700 Subject: [PATCH] Completed signin/out --- app/controllers/users_controller.rb | 1 + app/views/layouts/_header.html.erb | 9 ++++++- spec/controllers/users_controller_spec.rb | 5 ++++ spec/requests/layout_links_spec.rb | 31 +++++++++++++++++++++++ spec/requests/users_spec.rb | 28 +++++++++++++++++++- 5 files changed, 72 insertions(+), 2 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index d7b2067..222fe2f 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -13,6 +13,7 @@ def new def create @user = User.new(params[:user]) if @user.save + sign_in @user redirect_to @user, :flash => { :success => "Welcome to the Sample App!" } else @title = "Sign up" diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index fb02347..90be70e 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -3,8 +3,15 @@ \ No newline at end of file diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index a239aa9..c069545 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -102,6 +102,11 @@ post :create, :user => @attr flash[:success].should =~ /welcome to the sample app/i end + + it "should sign the user in" do + post :create, :user => @attr + controller.should be_signed_in + end end end end diff --git a/spec/requests/layout_links_spec.rb b/spec/requests/layout_links_spec.rb index 01b4ded..9670c6d 100644 --- a/spec/requests/layout_links_spec.rb +++ b/spec/requests/layout_links_spec.rb @@ -45,4 +45,35 @@ response.should have_selector('title', :content => "Sign up") response.should have_selector('a[href="/"]>img') end + + describe "when not signed in" do + it "should have a signin link" do + visit root_path + response.should have_selector("a", :href => signin_path, + :content => "Sign in") + end + end + + describe "when signed in" do + + before(:each) do + @user = Factory(:user) + visit signin_path + fill_in :email, :with => @user.email + fill_in :password, :with => @user.password + click_button + end + + it "should have a signout link" do + visit root_path + response.should have_selector("a", :href => signout_path, + :content => "Sign out") + end + + it "should have a profile link" do + visit root_path + response.should have_selector("a", :href => user_path(@user), + :content => "Profile") + end + end end diff --git a/spec/requests/users_spec.rb b/spec/requests/users_spec.rb index 7b62d94..4f40c4f 100644 --- a/spec/requests/users_spec.rb +++ b/spec/requests/users_spec.rb @@ -35,6 +35,32 @@ end end + describe "signin" do + + describe "failure" do + it "should not sign a user in" do + visit signin_path + fill_in "Email", :with => "" + fill_in "Password", :with => "" + click_button + response.should have_selector('div.flash.error', + :content => "Invalid") + response.should render_template('sessions/new') + end + end + + describe "success" do + it "should sign a user in and out" do + user = Factory(:user) + visit signin_path + fill_in "Email", :with => user.email + fill_in "Password", :with => user.password + click_button + controller.should be_signed_in + click_link "Sign out" + controller.should_not be_signed_in + end + end + end end - end