Skip to content

Repository files navigation

rack_session_access

RackSessionAccess provides rack middleware for 'rack.session' environment management.

Problem

Acceptance testing assumes that you can't directly access an applications session. For example, if you use capybara with selenium webdriver you can't change some session values because your tests use the same browser that accesses the application(via the backend server).

Solution

But what if you still want to change session values? One possible solution is to inject into the application some code that will manage the session independently. If you use rack based framework this gem does it!

Installation

geminstallrack_session_access

Using with Rails

Add to Gemfile:

gem'rack_session_access'

Add RackSessionAccess middleware to rails middleware stack. Add the following inconfig/environments/test.rb:

[MyRailsApp]::Application.configuredo |config|
...
# Access to rack sessionconfig.middleware.useRackSessionAccess::Middleware
...
end

Note Ensure you include rack_session_access middleware only for test environment otherwise you will have security issue.

Using with Sinatra

Add to your sinatra application:

classMySinatraApplication < Sinatra::Baseenable:sessionsuseRackSessionAccessifenvironment == :test
...
end

If you use rspec you may prefer to inject middleware only for rspec tests: Put into spec/spec_helper:

MySinatraApplication.configuredo |app|
app.useRackSessionAccess::Middlewareend

Using with Rack builder

Rack::Builder.newdo
...
useRack::Session::CookieuseRackSessionAccess::MiddlewareuseMyRackApplicationend.to_app

Testing with Capybara

Add to spec/spec_helper.rb

require"rack_session_access/capybara"

Use:

  • page.set_rack_session to set your desired session data
  • page.get_rack_session to obtain your application session data
  • page.get_rack_session_key to obtain certain key if your application session data

Example:

require'spec_helper'feature"My feature"dogiven!(:user){create(:user,email: 'jack@daniels.com')}scenario"logged in user access profile page"dopage.set_rack_session(user_id: user.id)visit"/profile"expect(page).tohave_content("Hi, jack@daniels.com")endscenario"visit landing page"dovisit"/landing?ref=123"expect(page.get_rack_session_key('ref')).toeq("123")endend

Authlogic integration

moduleFeatureHelpersdeflogged_as(user)page.set_rack_session('user_credentials'=>user.persistence_token)endend

Devise integration

moduleFeatureHelpersdeflogged_as(user)page.set_rack_session('warden.user.user.key'=>User.serialize_into_session(user).unshift("User"))endend

Authentication helper

Put corresponding implementation of logged_as in spec/support/feature_helpers.rb and include module into rspec config:

RSpec.configuredo |config|
...
config.includeFeatureHelpers,type: :feature...
end

Start your scenarios with already logged in user:

feature'User dashboard',type: :featuredogiven(:user){create(:user)}backgrounddologged_asuserendscenario'User reviews a dashboard'do
...
endend

Notes

Thus we use marshalized data it's possible to set any ruby object into application session hash.

Enjoy!

Running rack_session_access tests

Against Rails4, Sinatra, rack applications

bundle install
bundle exec rspec -fd spec/

Against Rails3, Sinatra, rack applications

BUNDLE_GEMFILE=Gemfile.rails3 bundle install
BUNDLE_GEMFILE=Gemfile.rails3 bundle exec rspec -fd spec/

Author

Andriy Yanko

License

References

About

Rack middleware that provides access to rack.session environment

Resources

Stars

0 stars

Watchers

6 watching

Forks

Releases

Packages

Contributors

Languages