Skip to content

Commit

Permalink
nifty_authentication: adding rspec support
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Nov 4, 2008
1 parent 5b48631 commit 4949eb5
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 13 deletions.
Expand Up @@ -16,10 +16,6 @@ def manifest
m.directory "app/helpers"
m.directory "app/views"
m.directory "lib"
m.directory "test"
m.directory "test/fixtures"
m.directory "test/functional"
m.directory "test/unit"

m.directory "app/views/#{user_plural_name}"
m.template "user.rb", "app/models/#{user_singular_name}.rb"
Expand All @@ -43,10 +39,24 @@ def manifest

m.insert_into 'app/controllers/application.rb', 'include Authentication'

m.template "fixtures.yml", "test/fixtures/#{user_plural_name}.yml"
m.template "tests/testunit/user.rb", "test/unit/#{user_singular_name}_test.rb"
m.template "tests/testunit/users_controller.rb", "test/functional/#{user_plural_name}_controller_test.rb"
m.template "tests/testunit/sessions_controller.rb", "test/functional/#{sessions_underscore_name}_controller_test.rb"
if test_framework == :rspec
m.directory "spec/fixtures"
m.directory "spec/controllers"
m.directory "spec/models"
m.template "fixtures.yml", "spec/fixtures/#{user_plural_name}.yml"
m.template "tests/rspec/user.rb", "spec/models/#{user_singular_name}_spec.rb"
m.template "tests/rspec/users_controller.rb", "spec/controllers/#{user_plural_name}_controller_spec.rb"
m.template "tests/rspec/sessions_controller.rb", "spec/controllers/#{sessions_underscore_name}_controller_spec.rb"
else
m.directory "test"
m.directory "test/fixtures"
m.directory "test/functional"
m.directory "test/unit"
m.template "fixtures.yml", "test/fixtures/#{user_plural_name}.yml"
m.template "tests/testunit/user.rb", "test/unit/#{user_singular_name}_test.rb"
m.template "tests/testunit/users_controller.rb", "test/functional/#{user_plural_name}_controller_test.rb"
m.template "tests/testunit/sessions_controller.rb", "test/functional/#{sessions_underscore_name}_controller_test.rb"
end
end
end

Expand Down Expand Up @@ -74,13 +84,24 @@ def sessions_class_name
sessions_name.camelize
end

protected
protected

def banner
<<-EOS
def test_framework
options[:test_framework] ||= File.exist?(destination_path("spec")) ? :rspec : :testunit
end

def add_options!(opt)
opt.separator ''
opt.separator 'Options:'
opt.on("--testunit", "Use test/unit for test files.") { options[:test_framework] = :testunit }
opt.on("--rspec", "Use RSpec for test files.") { options[:test_framework] = :rspec }
end

def banner
<<-EOS
Creates user model and controllers to handle registration and authentication.
USAGE: #{$0} #{spec.name} [user_name] [sessions_controller_name]
EOS
end
end
end
@@ -0,0 +1,26 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe <%= sessions_class_name %>Controller do
fixtures :all
integrate_views

it "new action should render new template" do
get :new
response.should render_template(:new)
end

it "create action should render new when authentication is invalid" do
<%= user_class_name %>.stubs(:authenticate).returns(nil)
post :create
response.should render_template(:new)
session['<%= user_singular_name %>_id'].should be_nil
end
it "create action should redirect when authentication is valid" do
<%= user_class_name %>.stubs(:authenticate).returns(<%= user_class_name %>.first)
Project.any_instance.stubs(:valid?).returns(true)
post :create
response.should redirect_to(root_url)
session['<%= user_singular_name %>_id'].should == <%= user_class_name %>.first.id
end
end
@@ -0,0 +1,62 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe <%= user_class_name %> do
def new_<%= user_class_name %>(attributes = {})
attributes[:username] ||= 'foo'
attributes[:email] ||= 'foo@example.com'
attributes[:password] ||= 'abc123'
attributes[:password_confirmation] ||= attributes[:password]
<%= user_class_name %>.new(attributes)
end

it "should be valid" do
new_<%= user_class_name %>.should be_valid
end

it "should require username" do
new_<%= user_class_name %>(:username => '').should have(1).error_on(:username)
end

it "should require password" do
new_<%= user_class_name %>(:password => '').should have(1).error_on(:password)
end

it "should require well formed email" do
new_<%= user_class_name %>(:email => 'foo@bar@example.com').should have(1).error_on(:email)
end

it "should require matching password confirmation" do
new_<%= user_class_name %>(:password_confirmation => 'nonmatching').should have(1).error_on(:password)
end

it "should generate password hash and salt on create" do
<%= user_class_name %> = new_<%= user_class_name %>
<%= user_class_name %>.save!
<%= user_class_name %>.password_hash.should_not be_nil
<%= user_class_name %>.password_salt.should_not be_nil
end
it "should authenticate by username" do
<%= user_class_name %>.delete_all
<%= user_class_name %> = new_<%= user_class_name %>(:username => 'foobar', :password => 'secret')
<%= user_class_name %>.save!
<%= user_class_name %>.authenticate('foobar', 'secret').should == <%= user_class_name %>
end

it "should authenticate by email" do
<%= user_class_name %>.delete_all
<%= user_class_name %> = new_<%= user_class_name %>(:email => 'foo@bar.com', :password => 'secret')
<%= user_class_name %>.save!
<%= user_class_name %>.authenticate('foo@bar.com', 'secret').should == <%= user_class_name %>
end

it "should not authenticate bad username" do
<%= user_class_name %>.authenticate('nonexisting', 'secret').should be_nil
end
it "should not authenticate bad password" do
<%= user_class_name %>.delete_all
new_<%= user_class_name %>(:username => 'foobar', :password => 'secret').save!
<%= user_class_name %>.authenticate('foobar', 'badpassword').should be_nil
end
end
@@ -0,0 +1,24 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe <%= user_plural_class_name %>Controller do
fixtures :all
integrate_views

it "new action should render new template" do
get :new
response.should render_template(:new)
end

it "create action should render new template when model is invalid" do
<%= user_class_name %>.any_instance.stubs(:valid?).returns(false)
post :create
response.should render_template(:new)
end
it "create action should redirect when model is valid" do
<%= user_class_name %>.any_instance.stubs(:valid?).returns(true)
post :create
response.should redirect_to(root_url)
session['<%= user_singular_name %>_id'].should == assigns['<%= user_singular_name %>'].id
end
end
38 changes: 37 additions & 1 deletion test/test_nifty_authentication_generator.rb
Expand Up @@ -16,7 +16,7 @@ class TestNiftyAuthenticationGenerator < Test::Unit::TestCase
# app_root_files - put this in teardown to show files generated by the test method (e.g. p app_root_files)
# bare_setup - place this in setup method to create the APP_ROOT folder for each test
# bare_teardown - place this in teardown method to destroy the TMP_ROOT or APP_ROOT folder after each test
context "" do
context "" do # empty context so we can use setup block
setup do
Dir.mkdir("#{RAILS_ROOT}/config") unless File.exists?("#{RAILS_ROOT}/config")
File.open("#{RAILS_ROOT}/config/routes.rb", 'w') do |f|
Expand Down Expand Up @@ -94,5 +94,41 @@ class TestNiftyAuthenticationGenerator < Test::Unit::TestCase
end
end
end

context "generator with rspec option" do
rails_generator :nifty_authentication, :test_framework => :rspec
should_generate_file 'spec/fixtures/users.yml'
end

context "with spec dir" do
setup do
Dir.mkdir("#{RAILS_ROOT}/spec") unless File.exists?("#{RAILS_ROOT}/spec")
end

teardown do
FileUtils.rm_rf "#{RAILS_ROOT}/spec"
end

context "generator without arguments" do
rails_generator :nifty_authentication
should_generate_file 'spec/fixtures/users.yml'
should_generate_file 'spec/models/user_spec.rb'
should_generate_file 'spec/controllers/users_controller_spec.rb'
should_generate_file 'spec/controllers/sessions_controller_spec.rb'
end

context "generator with user and session names" do
rails_generator :nifty_authentication, "Account", "CurrentSessions"
should_generate_file 'spec/fixtures/accounts.yml'
should_generate_file 'spec/models/account_spec.rb'
should_generate_file 'spec/controllers/accounts_controller_spec.rb'
should_generate_file 'spec/controllers/current_sessions_controller_spec.rb'
end

context "generator with testunit option" do
rails_generator :nifty_authentication, :test_framework => :testunit
should_generate_file 'test/fixtures/users.yml'
end
end
end
end

0 comments on commit 4949eb5

Please sign in to comment.