Permalink
Browse files

done with chapter 9 sign in and out

  • Loading branch information...
1 parent f7681da commit a20e2076d0e1742b79f7189682da05a8d61dea35 @turbulents committed Sep 15, 2011
View
BIN .DS_Store
Binary file not shown.
View
Binary file not shown.
@@ -1,3 +1,4 @@
class ApplicationController < ActionController::Base
protect_from_forgery
+ include SessionsHelper
end
@@ -0,0 +1,22 @@
+class SessionsController < ApplicationController
+ def new
+ @title = "Sign in"
+ end
+
+ def create
+ user = User.authenticate(params[:session][:email], params[:session][:password])
+ if user.nil?
+ flash.now[:error] = "Invalid email/password combination."
+ @title = "Sign in"
+ render 'new'
+ else
+ sign_in user
+ redirect_to user
+ end
+ end
+
+ def destroy
+ sign_out
+ redirect_to root_path
+ end
+end
@@ -13,6 +13,7 @@ def new
def create
@user = User.new(params[:user])
if @user.save
+ sign_in @user
flash[:success] = "Welcome!"
redirect_to @user
else
@@ -8,4 +8,9 @@ def title
else "#{base_title} | #{@title}"
end
end
+
+ def logo
+ image_tag("logo.gif", :alt => "it is a mystery", :class => "round")
+ end
+
end
@@ -0,0 +1,35 @@
+module SessionsHelper
+
+ def sign_in(user)
+ cookies.permanent.signed[:remember_token] = [user.id, user.salt]
+ current_user = user
+ end
+
+ def current_user=(user)
+ @current_user = user
+ end
+
+ def current_user
+ @current_user ||= user_from_remember_token
+ end
+
+ def signed_in?
+ !current_user.nil?
+ end
+
+ def sign_out
+ cookies.delete(:remember_token)
+ current_user = nil
+ end
+
+ private
+
+ def user_from_remember_token
+ User.authenticate_with_salt(*remember_token)
+ end
+
+ def remember_token
+ cookies.signed[:remember_token] || [nil, nil]
+ end
+
+end
View
@@ -27,6 +27,14 @@ def self.authenticate(email, submitted_password)
return user if user.has_password?(submitted_password)
end
+ def self.authenticate_with_salt(id, cookie_salt)
+ user = find_by_id(id)
+ (user && user.salt == cookie_salt) ? user : nil
+ # the above line is the same as saying:
+ # return nil if user.nil?
+ # return user if user.salt == cookie_salt
+ end
+
private
def encrypt_password
@@ -1,11 +1,17 @@
<header>
- <% logo = image_tag("logo.png", :alt => "Sample App", :class => "round") %>
<%= link_to logo, root_path %>
<nav class="round">
<ul>
<li><%= link_to "Home", root_path %></li>
+ <% if signed_in? %>
+ <li><%= link_to "Profile", current_user %></li>
+ <% end %>
<li><%= link_to "Help", help_path %></li>
- <li><%= link_to "Sign in", '#' %></li>
+ <% if signed_in? %>
+ <li><%= link_to "Sign Out", signout_path, :method => :delete %></li>
+ <% else %>
+ <li><%= link_to "Sign In", signin_path %></li>
+ <% end %>
</ul>
</nav>
</header>
@@ -9,7 +9,7 @@
<div class="container">
<%= render 'layouts/header' %>
<section class="round">
- <%= flash.each do |key, value| %>
+ <% flash.each do |key, value| %>
<div class="flash <%= key %>"><%= value %></div>
<% end %>
<%= yield %>
@@ -0,0 +1,17 @@
+<h1>Sign In</h1>
+
+<%= form_for(:session, :url => sessions_path) do |f| %>
+ <div class="field">
+ <%= f.label :email %><br />
+ <%= f.text_field :email %>
+ </div>
+ <div class="field">
+ <%= f.label :password %><br />
+ <%= f.text_field :password %>
+ </div>
+ <div class="actions">
+ <%= f.submit "Sign in" %>
+ </div>
+<% end %>
+
+<p>New user? <%= link_to "Sign up now!", signup_path %>
View
@@ -1,8 +1,12 @@
SampleApp::Application.routes.draw do
+ get "sessions/new"
+
resources :users
+ resources :sessions, :only => [ :new, :create, :destroy ]
match '/signup', :to => 'users#new'
-
+ match '/signin', :to => 'sessions#new'
+ match '/signout', :to => 'sessions#destroy'
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
View
Binary file not shown.
View
Binary file not shown.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Deleted file not rendered
@@ -0,0 +1,74 @@
+require 'spec_helper'
+
+describe SessionsController do
+ render_views
+
+ describe "GET 'new'" do
+
+ it "should be successful" do
+ get :new
+ response.should be_success
+ end
+
+ it "should have the right title" do
+ get :new
+ response.should have_selector("title", :content => "Sign in")
+ end
+ end
+
+ describe "POST 'create'" do
+
+ describe "invalid signin" do
+
+ before(:each) do
+ @attr = { :email => "email@example.com", :password => "invalid" }
+ end
+
+ it "should re-render the new page" do
+ post :create, :session => @attr
+ response.should render_template('new')
+ end
+
+ it "should have the right title" do
+ post :create, :session => @attr
+ response.should have_selector("title", :content => "Sign in")
+ end
+
+ it "should have a flash message" do
+ post :create, :session => @attr
+ flash.now[:error].should =~ /invalid/i
+ end
+ end
+
+ describe "with valid email and password" do
+
+ before(:each) do
+ @user = Factory(:user)
+ @attr = { :email => @user.email, :password => @user.password }
+ end
+
+ it "should sign the user in" do
+ post :create, :session => @attr
+ controller.current_user.should == @user
+ controller.should be_signed_in
+ end
+
+ it "should redirect to the user show page" do
+ post :create, :session => @attr
+ response.should redirect_to(user_path(@user))
+ end
+
+ end
+
+ end
+
+ describe "DELETE 'destroy'" do
+
+ it "should sign a user out" do
+ test_sign_in(Factory(:user))
+ delete :destroy
+ controller.should_not be_signed_in
+ response.should redirect_to(root_path)
+ end
+ end
+end
@@ -94,6 +94,12 @@
post :create, :user => @attr
flash[:success].should =~ /welcome/i
end
+
+ it "should sign in the user" do
+ post :create, :user => @attr
+ controller.should be_signed_in
+ end
+
end
end
end
@@ -6,21 +6,56 @@
get '/'
response.should have_selector('title', :content => 'Home')
end
+
it "should have a Contact page at '/'" do
get '/contact'
response.should have_selector('title', :content => 'Contact')
end
+
it "should have a Help page at '/'" do
get '/help'
response.should have_selector('title', :content => 'Help')
end
+
it "should have a About page at '/'" do
get '/about'
response.should have_selector('title', :content => 'About')
end
+
it "should have a sign-up page at '/'" do
get '/signup'
response.should have_selector('title', :content => 'Sign up')
end
-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
@@ -36,4 +36,33 @@
end
end
end
+
+ describe "sign in/out" 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")
+ 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
View
@@ -53,6 +53,10 @@
# Emulate initializer set_clear_dependencies_hook in
# railties/lib/rails/application/bootstrap.rb
ActiveSupport::Dependencies.clear
+
+ def test_sign_in(user)
+ controller.sign_in(user)
+ end
end
end
Oops, something went wrong.

0 comments on commit a20e207

Please sign in to comment.