-
-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
855 additions
and
753 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--color | ||
--format progress |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.