Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Finish sign in

  • Loading branch information...
commit 3cad611725458c56e5f54d163a3864fe696e7a83 1 parent f6c43e9
Jason Kim authored
View
2  Gemfile
@@ -24,6 +24,8 @@ gem 'jquery-rails', '2.0.0'
group :test do
gem 'capybara', '1.1.2'
gem 'factory_girl_rails', '1.4.0'
+ gem 'cucumber-rails', '1.2.1'
+ gem 'database_cleaner', '0.7.0'
end
group :production do
View
18 Gemfile.lock
@@ -49,6 +49,17 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.2.0)
+ cucumber (1.1.9)
+ builder (>= 2.1.2)
+ diff-lcs (>= 1.1.2)
+ gherkin (~> 2.9.0)
+ json (>= 1.4.6)
+ term-ansicolor (>= 1.0.6)
+ cucumber-rails (1.2.1)
+ capybara (>= 1.1.2)
+ cucumber (>= 1.1.3)
+ nokogiri (>= 1.5.0)
+ database_cleaner (0.7.0)
diff-lcs (1.1.3)
erubis (2.7.0)
execjs (1.3.0)
@@ -59,6 +70,8 @@ GEM
factory_girl (~> 2.3.0)
railties (>= 3.0.0)
ffi (1.0.11)
+ gherkin (2.9.0)
+ json (>= 1.4.6)
hike (1.2.1)
i18n (0.6.0)
journey (1.0.3)
@@ -66,7 +79,7 @@ GEM
railties (>= 3.2.0.beta, < 5.0)
thor (~> 0.14)
json (1.6.5)
- mail (2.4.3)
+ mail (2.4.4)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
@@ -129,6 +142,7 @@ GEM
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.5)
+ term-ansicolor (1.0.7)
thor (0.14.6)
tilt (1.3.3)
treetop (1.4.10)
@@ -150,6 +164,8 @@ DEPENDENCIES
bootstrap-sass (= 2.0.0)
capybara (= 1.1.2)
coffee-rails (= 3.2.2)
+ cucumber-rails (= 1.2.1)
+ database_cleaner (= 0.7.0)
factory_girl_rails (= 1.4.0)
jquery-rails (= 2.0.0)
pg (= 0.12.2)
View
3  app/assets/javascripts/application.js
@@ -12,4 +12,5 @@
//
//= require jquery
//= require jquery_ujs
-//= require_tree .
+//= require bootstrap
+//= require_tree .
View
3  app/assets/javascripts/sessions.js.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
View
3  app/assets/stylesheets/sessions.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the Sessions controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
1  app/controllers/application_controller.rb
@@ -1,3 +1,4 @@
class ApplicationController < ActionController::Base
protect_from_forgery
+ include SessionsHelper
end
View
21 app/controllers/sessions_controller.rb
@@ -0,0 +1,21 @@
+class SessionsController < ApplicationController
+ def new
+ end
+
+ def create
+ user = User.find_by_email(params[:session][:email])
+ if user && user.authenticate(params[:session][:password])
+ sign_in user
+ redirect_to user
+ else
+ flash.now[:error] = 'Invalid email/password combination'
+ render 'new'
+ end
+ end
+
+ def destroy
+ sign_out
+ redirect_to root_path
+ end
+
+end
View
3  app/controllers/users_controller.rb
@@ -2,6 +2,7 @@ class UsersController < ApplicationController
def create
@user = User.new(params[:user])
if @user.save
+ sign_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
@@ -16,4 +17,6 @@ def show
def new
@user = User.new
end
+
+
end
View
33 app/helpers/sessions_helper.rb
@@ -0,0 +1,33 @@
+module SessionsHelper
+
+ def sign_in(user)
+ cookies.permanent[:remember_token] = user.remember_token
+ 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
+ current_user = nil
+ cookies.delete(:remember_token)
+ end
+
+ private
+
+ def user_from_remember_token
+ remember_token = cookies[:remember_token]
+ User.find_by_remember_token(remember_token) unless remember_token.nil?
+
+ end
+
+end
View
7 app/models/user.rb
@@ -12,6 +12,7 @@
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
+ before_save :create_remember_token
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
@@ -20,4 +21,10 @@ class User < ActiveRecord::Base
uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6 }
validates :password_confirmation, presence: true
+
+ private
+
+ def create_remember_token
+ self.remember_token = SecureRandom.urlsafe_base64
+ end
end
View
23 app/views/layouts/_header.html.erb
@@ -4,9 +4,26 @@
<%= link_to "sample app", root_path, id: "logo" %>
<nav>
<ul class="nav pull-right">
- <li><%= link_to "Home", root_path %></li>
- <li><%= link_to "Help", help_path %></li>
- <li><%= link_to "Sign in", '#' %></li>
+ <li><%= link_to "Home", root_path %></li>
+ <li><%= link_to "Help", help_path %></li>
+ <% if signed_in? %>
+ <li><%= link_to "Users", '#' %></li>
+ <li id="fat-menu" class="dropdown">
+ <a href="#" class="dropdown-toggle" data-toggle="dropdown">
+ Account <b class="caret"></b>
+ </a>
+ <ul class="dropdown-menu">
+ <li><%= link_to "Profile", current_user %></li>
+ <li><%= link_to "Settings", '#' %></li>
+ <li class="divider"></li>
+ <li>
+ <%= link_to "Sign out", signout_path, method: "delete" %>
+ </li>
+ </ul>
+ </li>
+ <% else %>
+ <li><%= link_to "Sign in", signin_path %></li>
+ <% end %>
</ul>
</nav>
</div>
View
18 app/views/sessions/new.html.erb
@@ -0,0 +1,18 @@
+<% provide(:title, "Sign in") %>
+<h1>Sign in</h1>
+
+<div class="row">
+ <div class="span6 offset3">
+ <%= form_for(:session, :url => sessions_path) do |f| %>
+ <%= f.label :email %>
+ <%= f.text_field :email %>
+
+ <%= f.label :password %>
+ <%= f.password_field :password %>
+
+ <%= f.submit "Sign in", :class => "btn btn-large btn-primary" %>
+ <% end %>
+
+ <p>New user? <%= link_to "Sign up now!", signup_path %></p>
+ </div>
+</div>
View
8 config/cucumber.yml
@@ -0,0 +1,8 @@
+<%
+rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
+rerun_opts = rerun.to_s.strip.empty? ? "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
+std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~@wip"
+%>
+default: <%= std_opts %> features
+wip: --tags @wip:3 --wip features
+rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip
View
5 config/database.yml
@@ -12,7 +12,7 @@ development:
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
-test:
+test: &test
adapter: sqlite3
database: db/test.sqlite3
pool: 5
@@ -23,3 +23,6 @@ production:
database: db/production.sqlite3
pool: 5
timeout: 5000
+
+cucumber:
+ <<: *test
View
7 config/routes.rb
@@ -1,10 +1,13 @@
SampleApp::Application.routes.draw do
resources :users
+ resources :sessions, only: [:new, :create, :destroy]
+
+ match '/signup', to: 'users#new'
+ match '/signin', to: 'sessions#new'
+ match '/signout', to: 'sessions#destroy', via: :delete
root to: 'static_pages#home'
# match '/', to: 'static_pages#home'
-
- match '/signup', to: 'users#new'
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
View
6 db/migrate/20120315044928_add_remember_token_to_users.rb
@@ -0,0 +1,6 @@
+class AddRememberTokenToUsers < ActiveRecord::Migration
+ def change
+ add_column :users, :remember_token, :string
+ add_index :users, :remember_token
+ end
+end
View
4 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120313003545) do
+ActiveRecord::Schema.define(:version => 20120315044928) do
create_table "users", :force => true do |t|
t.string "name"
@@ -19,8 +19,10 @@
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "password_digest"
+ t.string "remember_token"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
+ add_index "users", ["remember_token"], :name => "index_users_on_remember_token"
end
View
56 features/support/env.rb
@@ -0,0 +1,56 @@
+# IMPORTANT: This file is generated by cucumber-rails - edit at your own peril.
+# It is recommended to regenerate this file in the future when you upgrade to a
+# newer version of cucumber-rails. Consider adding your own code to a new file
+# instead of editing this one. Cucumber will automatically load all features/**/*.rb
+# files.
+
+require 'cucumber/rails'
+
+# Capybara defaults to XPath selectors rather than Webrat's default of CSS3. In
+# order to ease the transition to Capybara we set the default here. If you'd
+# prefer to use XPath just remove this line and adjust any selectors in your
+# steps to use the XPath syntax.
+Capybara.default_selector = :css
+
+# By default, any exception happening in your Rails application will bubble up
+# to Cucumber so that your scenario will fail. This is a different from how
+# your application behaves in the production environment, where an error page will
+# be rendered instead.
+#
+# Sometimes we want to override this default behaviour and allow Rails to rescue
+# exceptions and display an error page (just like when the app is running in production).
+# Typical scenarios where you want to do this is when you test your error pages.
+# There are two ways to allow Rails to rescue exceptions:
+#
+# 1) Tag your scenario (or feature) with @allow-rescue
+#
+# 2) Set the value below to true. Beware that doing this globally is not
+# recommended as it will mask a lot of errors for you!
+#
+ActionController::Base.allow_rescue = false
+
+# Remove/comment out the lines below if your app doesn't have a database.
+# For some databases (like MongoDB and CouchDB) you may need to use :truncation instead.
+begin
+ DatabaseCleaner.strategy = :transaction
+rescue NameError
+ raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
+end
+
+# You may also want to configure DatabaseCleaner to use different strategies for certain features and scenarios.
+# See the DatabaseCleaner documentation for details. Example:
+#
+# Before('@no-txn,@selenium,@culerity,@celerity,@javascript') do
+# DatabaseCleaner.strategy = :truncation, {:except => %w[widgets]}
+# end
+#
+# Before('~@no-txn', '~@selenium', '~@culerity', '~@celerity', '~@javascript') do
+# DatabaseCleaner.strategy = :transaction
+# end
+#
+
+# Possible values are :truncation and :transaction
+# The :transaction strategy is faster, but might give you threading problems.
+# See https://github.com/cucumber/cucumber-rails/blob/master/features/choose_javascript_database_strategy.feature
+Cucumber::Rails::Database.javascript_strategy = :truncation
+
View
65 lib/tasks/cucumber.rake
@@ -0,0 +1,65 @@
+# IMPORTANT: This file is generated by cucumber-rails - edit at your own peril.
+# It is recommended to regenerate this file in the future when you upgrade to a
+# newer version of cucumber-rails. Consider adding your own code to a new file
+# instead of editing this one. Cucumber will automatically load all features/**/*.rb
+# files.
+
+
+unless ARGV.any? {|a| a =~ /^gems/} # Don't load anything when running the gems:* tasks
+
+vendored_cucumber_bin = Dir["#{Rails.root}/vendor/{gems,plugins}/cucumber*/bin/cucumber"].first
+$LOAD_PATH.unshift(File.dirname(vendored_cucumber_bin) + '/../lib') unless vendored_cucumber_bin.nil?
+
+begin
+ require 'cucumber/rake/task'
+
+ namespace :cucumber do
+ Cucumber::Rake::Task.new({:ok => 'db:test:prepare'}, 'Run features that should pass') do |t|
+ t.binary = vendored_cucumber_bin # If nil, the gem's binary is used.
+ t.fork = true # You may get faster startup if you set this to false
+ t.profile = 'default'
+ end
+
+ Cucumber::Rake::Task.new({:wip => 'db:test:prepare'}, 'Run features that are being worked on') do |t|
+ t.binary = vendored_cucumber_bin
+ t.fork = true # You may get faster startup if you set this to false
+ t.profile = 'wip'
+ end
+
+ Cucumber::Rake::Task.new({:rerun => 'db:test:prepare'}, 'Record failing features and run only them if any exist') do |t|
+ t.binary = vendored_cucumber_bin
+ t.fork = true # You may get faster startup if you set this to false
+ t.profile = 'rerun'
+ end
+
+ desc 'Run all features'
+ task :all => [:ok, :wip]
+
+ task :statsetup do
+ require 'rails/code_statistics'
+ ::STATS_DIRECTORIES << %w(Cucumber\ features features) if File.exist?('features')
+ ::CodeStatistics::TEST_TYPES << "Cucumber features" if File.exist?('features')
+ end
+ end
+ desc 'Alias for cucumber:ok'
+ task :cucumber => 'cucumber:ok'
+
+ task :default => :cucumber
+
+ task :features => :cucumber do
+ STDERR.puts "*** The 'features' task is deprecated. See rake -T cucumber ***"
+ end
+
+ # In case we don't have ActiveRecord, append a no-op task that we can depend upon.
+ task 'db:test:prepare' do
+ end
+
+ task :stats => 'cucumber:statsetup'
+rescue LoadError
+ desc 'cucumber rake task not available (cucumber not installed)'
+ task :cucumber do
+ abort 'Cucumber rake task is not available. Be sure to install cucumber as a gem or plugin'
+ end
+end
+
+end
View
10 script/cucumber
@@ -0,0 +1,10 @@
+#!/usr/bin/env ruby
+
+vendored_cucumber_bin = Dir["#{File.dirname(__FILE__)}/../vendor/{gems,plugins}/cucumber*/bin/cucumber"].first
+if vendored_cucumber_bin
+ load File.expand_path(vendored_cucumber_bin)
+else
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
+ require 'cucumber'
+ load Cucumber::BINARY
+end
View
9 spec/models/user_spec.rb
@@ -22,7 +22,9 @@
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
- it { should respond_to(:authenticate) }
+ it { should respond_to(:remember_token) }
+ it { should respond_to(:authenticate) }
+
it { should be_valid }
@@ -94,5 +96,10 @@
specify { user_for_invalid_password.should be_false }
end
end
+
+ describe "remember token" do
+ before { @user.save }
+ its(:remember_token) { should_not be_blank }
+ end
end
View
49 spec/requests/authentication_pages_spec.rb
@@ -0,0 +1,49 @@
+require 'spec_helper'
+
+describe "Authentication" do
+
+ subject { page }
+
+ describe "signin page" do
+ before { visit signin_path }
+
+ it { should have_selector('h1', text: 'Sign in') }
+ it { should have_selector('title', text: 'Sign in') }
+ end
+
+ describe "signin" do
+ before { visit signin_path }
+
+ describe "with invalid information" do
+ before { click_button "Sign in" }
+
+ it { should have_selector('title', text: 'Sign in') }
+ it { should have_selector('div.alert.alert-error', text: 'Invalid') }
+
+ describe "after visiting another page" do
+ before { click_link "Home" }
+ it { should_not have_selector('div.alert.alert-error') }
+ end
+ end
+
+ describe "with valid information" do
+ let(:user) { FactoryGirl.create(:user) }
+ before do
+ fill_in "Email", with: user.email
+ fill_in "Password", with: user.password
+ click_button "Sign in"
+ end
+
+ it { should have_selector('title', text: user.name) }
+ it { should have_link('Profile', href: user_path(user)) }
+ it { should have_link('Sign out', href: signout_path) }
+ it { should_not have_link('Sign in', href: signin_path) }
+
+ describe "followed by signout" do
+ before { click_link "Sign out" }
+ it { should have_link('Sign in') }
+ end
+
+ end
+ end
+end
View
11 spec/requests/user_pages_spec.rb
@@ -42,6 +42,17 @@
click_button "Create my account"
end.to change(User, :count).by(1)
end
+
+ describe "after saving the user" do
+ before { click_button "Create my account" }
+ let(:user) { User.find_by_email('user@example.com') }
+
+ it { should have_selector('title', text: user.name) }
+ it { should have_selector('div.alert.alert-success', text: 'Welcome') }
+
+ it { should have_link('Sign out') }
+ end
+
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.