Skip to content

Commit

Permalink
[thoughtbot#42 state:resolved] Rack-based session change altered how …
Browse files Browse the repository at this point in the history
…to test remember me cookie. Hat-tip Mihai Anca.
  • Loading branch information
Dan Croak committed Feb 27, 2009
1 parent 90dbd00 commit 0f97c7e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.textile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ h1. 0.5.1 (not yet released)
(Marcel Görner)
* Refactored user_from_cookie, user_from_session, User#authenticate to use
more direct return code instead of ugly, harder to read ternary. (Dan Croak)
* Switch order of cookies and sessions to take advantage of Rails 2.3's "Rack-based lazy-loaded sessions":http://is.gd/i23E. (Dan Croak)
* Altered generator to interact with application_controller.rb instead of
application.rb in Rails 2.3 apps. (Dan Croak)
* [#42] Bug fix. Rack-based session change altered how to test remember me
cookie. (Mihai Anca)

h2. 0.5.0 (2/27/2009)

Expand Down
54 changes: 29 additions & 25 deletions lib/clearance/test/functional/sessions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ module Clearance
module Test
module Functional
module SessionsControllerTest

def self.included(controller_test)
controller_test.class_eval do

should_filter_params :password

context "on GET to /sessions/new" do
setup { get :new }

should_respond_with :success
should_render_template :new
should_not_set_the_flash

should_display_a_sign_in_form
end

Expand Down Expand Up @@ -42,7 +41,7 @@ def self.included(controller_test)
context "Given an email confirmed user" do
setup do
@user = Factory(:user)
@user.confirm_email!
@user.confirm_email!
end

context "a POST to #create with good credentials" do
Expand All @@ -69,7 +68,7 @@ def self.included(controller_test)
should_render_template :new
should_not_be_signed_in
end

context "a POST to #create with good credentials and remember me" do
setup do
post :create, :session => {
Expand All @@ -81,7 +80,7 @@ def self.included(controller_test)
should_set_the_flash_to /success/i
should_redirect_to_url_after_create
should_be_signed_in_as { @user }

should 'set the cookie' do
assert ! cookies['remember_token'].empty?
end
Expand All @@ -91,7 +90,7 @@ def self.included(controller_test)
assert_not_nil @user.reload.token_expires_at
end
end

context "a POST to #create with bad credentials and remember me" do
setup do
post :create, :session => {
Expand All @@ -104,7 +103,7 @@ def self.included(controller_test)
should_respond_with :unauthorized
should_render_template :new
should_return_from_session :user_id, "nil"

should 'not create the cookie' do
assert_nil cookies['remember_token']
end
Expand All @@ -114,42 +113,42 @@ def self.included(controller_test)
assert_nil @user.reload.token_expires_at
end
end

context "a POST to #create with good credentials and A URL to return back" do
context "in the session" do
setup do
@request.session[:return_to] = '/url_in_the_session'
post :create, :session => {
:email => @user.email,
:password => @user.password }
post :create, :session => {
:email => @user.email,
:password => @user.password }
end

should_redirect_to "'/url_in_the_session'"
end

context "in the request" do
setup do
post :create, :session => {
:email => @user.email,
:password => @user.password },
:return_to => '/url_in_the_request'
:return_to => '/url_in_the_request'
end

should_redirect_to "'/url_in_the_request'"
end
end

context "in the request and in the session" do
setup do
@request.session[:return_to] = '/url_in_the_session'
post :create, :session => {
:email => @user.email,
:password => @user.password },
:return_to => '/url_in_the_request'
:return_to => '/url_in_the_request'
end

should_redirect_to "'/url_in_the_session'"
end
end
end
end

public_context do
Expand All @@ -169,12 +168,17 @@ def self.included(controller_test)

context 'a DELETE to #destroy with a cookie' do
setup do
cookies['remember_token'] = CGI::Cookie.new('token', 'value')
@request.cookies['remember_token'] = {
:name => 'token',
:value => 'value'
}
@controller.request = @request
delete :destroy
end

should 'delete the cookie' do
assert cookies['remember_token'].empty?
assert cookies['remember_token'].nil? || # Rails >= 2.3
cookies['remember_token'].empty? # Rails < 2.3
end

should 'delete the remember me token in users table' do
Expand All @@ -183,7 +187,7 @@ def self.included(controller_test)
end
end
end

end
end

Expand Down

0 comments on commit 0f97c7e

Please sign in to comment.