Skip to content

Commit

Permalink
Convert from Test::Unit to RSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Apr 14, 2011
1 parent 4f88938 commit e13e7e4
Show file tree
Hide file tree
Showing 19 changed files with 855 additions and 753 deletions.
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--format progress
12 changes: 5 additions & 7 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@ require 'rake'
require 'rake/gempackagetask'
require 'cucumber/rake/task'
require 'diesel/tasks'
require 'rspec/core/rake_task'

Rake::TestTask.new do |task|
task.libs << "lib"
task.libs << "test"
task.pattern = "test/*/*_test.rb"
task.verbose = false
RSpec::Core::RakeTask.new do |t|
t.pattern = 'spec/**/*_spec.rb'
end

Cucumber::Rake::Task.new(:cucumber) do |t|
t.fork = true
t.cucumber_opts = ['--format', (ENV['CUCUMBER_FORMAT'] || 'progress')]
end

desc "Default: run the unit tests and cucumber features"
task :default => [:test, :cucumber]
desc "Default: run the specs and cucumber features"
task :default => [:spec, :cucumber]

4 changes: 2 additions & 2 deletions lib/generators/clearance/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ def install
end

if File.exists?("spec")
template "test/factories.rb", "spec/factories/clearance.rb"
template "spec/factories.rb", "spec/factories/clearance.rb"
else
template "test/factories.rb", "test/factories/clearance.rb"
template "spec/factories.rb", "test/factories/clearance.rb"
end

readme "README"
Expand Down
177 changes: 177 additions & 0 deletions spec/controllers/passwords_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
require 'spec_helper'

describe Clearance::PasswordsController do

include Shoulda::ActionMailer::Matchers

it { should route(:get, '/users/1/password/edit').
to(:controller => 'clearance/passwords', :action => 'edit', :user_id => '1') }

describe "a signed up user" do
before do
@user = Factory(:user)
end

describe "on GET to #new" do
before { get :new, :user_id => @user.to_param }

it { should respond_with(:success) }
it { should render_template(:new) }
end

describe "on POST to #create" do
describe "with correct email address" do
before do
ActionMailer::Base.deliveries.clear
post :create, :password => { :email => @user.email }
end

it "should generate a token for the change your password email" do
@user.reload.confirmation_token.should_not be_nil
end

it { should have_sent_email.with_subject(/change your password/i) }

it { should set_the_flash.to(/password/i) }
it { should redirect_to_url_after_create }
end

describe "with incorrect email address" do
before do
email = "user1@example.com"
(::User.exists?(['email = ?', email])).should_not be
ActionMailer::Base.deliveries.clear
@user.reload.confirmation_token.should == @user.confirmation_token

post :create, :password => { :email => email }
end

it "should not generate a token for the change your password email" do
@user.reload.confirmation_token.should == @user.confirmation_token
end

it "should not send a password reminder email" do
ActionMailer::Base.deliveries.should be_empty
end

it "should set the failure flash to Unknown email" do
flash.now[:failure].should =~ /unknown email/i
end

it { should render_template(:new) }
end
end
end

describe "a signed up user and forgotten password" do
before do
@user = Factory(:user)
@user.forgot_password!
end

describe "on GET to #edit with correct id and token" do
before do
get :edit, :user_id => @user.to_param,
:token => @user.confirmation_token
end

it "should find the user" do
assigns(:user).should == @user
end

it { should respond_with(:success) }
it { should render_template(:edit) }
end

describe "on GET to #edit with correct id but blank token" do
before do
get :edit, :user_id => @user.to_param, :token => ""
end

it { should set_the_flash.to(/double check the URL/i) }
it { should render_template(:new) }
end

describe "on GET to #edit with correct id but no token" do
before do
get :edit, :user_id => @user.to_param
end

it { should set_the_flash.to(/double check the URL/i) }
it { should render_template(:new) }
end

describe "on PUT to #update with matching password and password confirmation" do
before do
new_password = "new_password"
@encrypted_new_password = @user.send(:encrypt, new_password)
@user.encrypted_password.should_not == @encrypted_new_password

put(:update,
:user_id => @user,
:token => @user.confirmation_token,
:user => {
:password => new_password,
:password_confirmation => new_password
})
@user.reload
end

it "should update password" do
@user.encrypted_password.should == @encrypted_new_password
end

it "should clear confirmation token" do
@user.confirmation_token.should be_nil
end

it "should set remember token" do
@user.remember_token.should_not be_nil
end

it { should set_the_flash.to(/signed in/i) }
it { should redirect_to_url_after_update }
end

describe "on PUT to #update with password but blank password confirmation" do
before do
new_password = "new_password"
@encrypted_new_password = @user.send(:encrypt, new_password)

put(:update,
:user_id => @user.to_param,
:token => @user.confirmation_token,
:user => {
:password => new_password,
:password_confirmation => ''
})
@user.reload
end

it "should not update password" do
@user.encrypted_password.should_not == @encrypted_new_password
end

it "should not clear token" do
@user.confirmation_token.should_not be_nil
end

it "should not be signed in" do
cookies[:remember_token].should be_nil
end

it { should_not set_the_flash }
it { should respond_with(:success) }
it { should render_template(:edit) }
end
end

describe "given two users and user one signs in" do
before do
@user_one = Factory(:user)
@user_two = Factory(:user)
sign_in_as @user_one
end
end

end
160 changes: 160 additions & 0 deletions spec/controllers/sessions_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
require 'spec_helper'

describe Clearance::SessionsController do
describe "on GET to /sessions/new" do
before { get :new }

it { should respond_with(:success) }
it { should render_template(:new) }
it { should_not set_the_flash }
end

describe "on POST to #create with good credentials" do
before do
@user = Factory(:user)
@user.update_attribute(:remember_token, "old-token")
post :create, :session => {
:email => @user.email,
:password => @user.password }
end

it { should set_the_flash.to(/signed in/i) }
it { should redirect_to_url_after_create }

it "sets a remember token cookie" do
should set_cookie("remember_token", "old-token", Clearance.configuration.cookie_expiration.call)
end

it "should have a default of 1 year from now" do
Clearance.configuration.cookie_expiration.call.should be_within(100).of(1.year.from_now)
end

it "should not change the remember token" do
@user.reload.remember_token.should == "old-token"
end
end

describe "on POST to #create with good credentials - cookie duration set to 2 weeks" do
custom_duration = 2.weeks.from_now.utc

before do
Clearance.configuration.cookie_expiration = lambda { custom_duration }
@user = Factory(:user)
@user.update_attribute(:remember_token, "old-token2")
post :create, :session => {
:email => @user.email,
:password => @user.password }
end

it "sets a remember token cookie" do
should set_cookie("remember_token", "old-token2", custom_duration)
end

after do
# restore default Clearance configuration
Clearance.configuration = nil
Clearance.configure {}
end
end

describe "on POST to #create with good credentials - cookie expiration set to nil (session cookie)" do
before do
Clearance.configuration.cookie_expiration = lambda { nil }
@user = Factory(:user)
@user.update_attribute(:remember_token, "old-token3")
post :create, :session => {
:email => @user.email,
:password => @user.password }
end

it "unsets a remember token cookie" do
should set_cookie("remember_token", "old-token3", nil)
end

after do
# restore default Clearance configuration
Clearance.configuration = nil
Clearance.configure {}
end
end

describe "on POST to #create with good credentials and a session return url" do
before do
@user = Factory(:user)
@return_url = '/url_in_the_session'
@request.session[:return_to] = @return_url
post :create, :session => {
:email => @user.email,
:password => @user.password }
end

it "redirects to the return URL" do
should redirect_to(@return_url)
end
end

describe "on POST to #create with good credentials and a request return url" do
before do
@user = Factory(:user)
@return_url = '/url_in_the_request'
post :create, :session => {
:email => @user.email,
:password => @user.password },
:return_to => @return_url
end

it "redirects to the return URL" do
should redirect_to(@return_url)
end
end

describe "on POST to #create with good credentials and a session return url and request return url" do
before do
@user = Factory(:user)
@return_url = '/url_in_the_session'
@request.session[:return_to] = @return_url
post :create, :session => {
:email => @user.email,
:password => @user.password },
:return_to => '/url_in_the_request'
end

it "redirects to the return url" do
should redirect_to(@return_url)
end
end

describe "on DELETE to #destroy given a signed out user" do
before do
sign_out
delete :destroy
end
it { should set_the_flash.to(/signed out/i) }
it { should redirect_to_url_after_destroy }
end

describe "on DELETE to #destroy with a cookie" do
before do
@user = Factory(:user)
@user.update_attribute(:remember_token, "old-token")
@request.cookies["remember_token"] = "old-token"
delete :destroy
end

it { should set_the_flash.to(/signed out/i) }
it { should redirect_to_url_after_destroy }

it "should delete the cookie token" do
cookies['remember_token'].should be_nil
end

it "should reset the remember token" do
@user.reload.remember_token.should_not == "old-token"
end

it "should unset the current user" do
@controller.current_user.should be_nil
end
end

end
Loading

0 comments on commit e13e7e4

Please sign in to comment.