Skip to content

Commit

Permalink
Reset session in integration tests after changing routes to reload th…
Browse files Browse the repository at this point in the history
…e middleware stack
  • Loading branch information
josh committed Aug 27, 2009
1 parent f3ed0de commit ba5995d
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 28 deletions.
4 changes: 3 additions & 1 deletion actionpack/lib/action_controller/testing/process.rb
Expand Up @@ -249,13 +249,15 @@ def with_routing

temporary_routes = ActionController::Routing::RouteSet.new
ActionController::Routing.module_eval { const_set :Routes, temporary_routes }
ActionController::Dispatcher.router = temporary_routes

yield temporary_routes
ensure
if ActionController::Routing.const_defined? :Routes
ActionController::Routing.module_eval { remove_const :Routes }
end
ActionController::Routing.const_set(:Routes, real_routes) if real_routes
ActionController::Dispatcher.router = ActionController::Routing::Routes
end
end
end
end
25 changes: 14 additions & 11 deletions actionpack/test/activerecord/active_record_store_test.rb
@@ -1,12 +1,6 @@
require 'active_record_unit'

class ActiveRecordStoreTest < ActionController::IntegrationTest
DispatcherApp = ActionController::Dispatcher.new
SessionApp = ActiveRecord::SessionStore.new(DispatcherApp,
:key => '_session_id')
SessionAppWithFixation = ActiveRecord::SessionStore.new(DispatcherApp,
:key => '_session_id', :cookie_only => false)

class TestController < ActionController::Base
def no_session_access
head :ok
Expand Down Expand Up @@ -39,7 +33,7 @@ def rescue_action(e) raise end

def setup
ActiveRecord::SessionStore.session_class.create_table!
@integration_session = open_session(SessionApp)
reset_app!
end

def teardown
Expand Down Expand Up @@ -138,9 +132,9 @@ def test_prevents_session_fixation
end

def test_allows_session_fixation
@integration_session = open_session(SessionAppWithFixation)

with_test_route_set do
reset_with_fixation!

get '/set_session_value'
assert_response :success
assert cookies['_session_id']
Expand All @@ -151,8 +145,7 @@ def test_allows_session_fixation
session_id = cookies['_session_id']
assert session_id

reset!
@integration_session = open_session(SessionAppWithFixation)
reset_with_fixation!

get '/set_session_value', :_session_id => session_id, :foo => "baz"
assert_response :success
Expand All @@ -166,6 +159,16 @@ def test_allows_session_fixation
end

private
def reset_app!
app = ActiveRecord::SessionStore.new(ActionController::Dispatcher.new, :key => '_session_id')
@integration_session = open_session(app)
end

def reset_with_fixation!
app = ActiveRecord::SessionStore.new(ActionController::Dispatcher.new, :key => '_session_id', :cookie_only => false)
@integration_session = open_session(app)
end

def with_test_route_set
with_routing do |set|
set.draw do |map|
Expand Down
1 change: 1 addition & 0 deletions actionpack/test/controller/webservice_test.rb
Expand Up @@ -259,6 +259,7 @@ def with_test_route_set
c.connect "/", :action => "assign_parameters"
end
end
reset!
yield
end
end
Expand Down
Expand Up @@ -59,6 +59,7 @@ def with_test_routing
set.draw do |map|
map.connect ':action', :controller => "json_params_parsing_test/test"
end
reset!
yield
end
end
Expand Down
Expand Up @@ -153,6 +153,7 @@ def with_test_routing
set.draw do |map|
map.connect ':action', :controller => "multipart_params_parsing_test/test"
end
reset!
yield
end
end
Expand Down
Expand Up @@ -111,6 +111,7 @@ def assert_parses(expected, actual)
set.draw do |map|
map.connect ':action', :controller => "query_string_parsing_test/test"
end
reset!

get "/parse", actual
assert_response :ok
Expand Down
Expand Up @@ -132,6 +132,7 @@ def with_test_routing
set.draw do |map|
map.connect ':action', :controller => "url_encoded_params_parsing_test/test"
end
reset!
yield
end
end
Expand Down
Expand Up @@ -86,6 +86,7 @@ def with_test_routing
set.draw do |map|
map.connect ':action', :controller => "xml_params_parsing_test/test"
end
reset!
yield
end
end
Expand Down
19 changes: 11 additions & 8 deletions actionpack/test/dispatch/session/cookie_store_test.rb
Expand Up @@ -8,10 +8,6 @@ class CookieStoreTest < ActionController::IntegrationTest
# Make sure Session middleware doesnt get included in the middleware stack
ActionController::Base.session_store = nil

DispatcherApp = ActionController::Dispatcher.new
CookieStoreApp = ActionDispatch::Session::CookieStore.new(DispatcherApp,
:key => SessionKey, :secret => SessionSecret)

Verifier = ActiveSupport::MessageVerifier.new(SessionSecret, 'SHA1')
SignedBar = Verifier.generate(:foo => "bar", :session_id => ActiveSupport::SecureRandom.hex(16))

Expand Down Expand Up @@ -51,7 +47,7 @@ def rescue_action(e) raise end
end

def setup
@integration_session = open_session(CookieStoreApp)
reset_app!
end

def test_raises_argument_error_if_missing_session_key
Expand Down Expand Up @@ -197,10 +193,10 @@ def test_persistent_session_id
end

def test_session_store_with_expire_after
app = ActionDispatch::Session::CookieStore.new(DispatcherApp, :key => SessionKey, :secret => SessionSecret, :expire_after => 5.hours)
@integration_session = open_session(app)

with_test_route_set do
app = ActionDispatch::Session::CookieStore.new(ActionController::Dispatcher.new, :key => SessionKey, :secret => SessionSecret, :expire_after => 5.hours)
@integration_session = open_session(app)

# First request accesses the session
time = Time.local(2008, 4, 24)
Time.stubs(:now).returns(time)
Expand Down Expand Up @@ -230,13 +226,20 @@ def test_session_store_with_expire_after
end

private
def reset_app!
app = ActionDispatch::Session::CookieStore.new(ActionController::Dispatcher.new,
:key => SessionKey, :secret => SessionSecret)
@integration_session = open_session(app)
end

def with_test_route_set
with_routing do |set|
set.draw do |map|
map.with_options :controller => "cookie_store_test/test" do |c|
c.connect "/:action"
end
end
reset_app!
yield
end
end
Expand Down
16 changes: 8 additions & 8 deletions actionpack/test/dispatch/session/mem_cache_store_test.rb
Expand Up @@ -32,14 +32,7 @@ def rescue_action(e) raise end
end

begin
DispatcherApp = ActionController::Dispatcher.new
MemCacheStoreApp = ActionDispatch::Session::MemCacheStore.new(
DispatcherApp, :key => '_session_id')


def setup
@integration_session = open_session(MemCacheStoreApp)
end
App = ActionDispatch::Session::MemCacheStore.new(ActionController::Dispatcher.new, :key => '_session_id')

def test_setting_and_getting_session_value
with_test_route_set do
Expand Down Expand Up @@ -114,13 +107,20 @@ def test_prevents_session_fixation
end

private
def reset_app!
app = ActionDispatch::Session::MemCacheStore.new(
ActionController::Dispatcher.new, :key => '_session_id')
@integration_session = open_session(app)
end

def with_test_route_set
with_routing do |set|
set.draw do |map|
map.with_options :controller => "mem_cache_store_test/test" do |c|
c.connect "/:action"
end
end
reset_app!
yield
end
end
Expand Down

0 comments on commit ba5995d

Please sign in to comment.