Rack::BackDoor

Inject a user into a session for integration and controller tests.

Installation

Add this line to your application's Gemfile:

gem 'rack-back_door'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rack-back_door

Usage

By default, Rack::BackDoor will handle the following URL:

http://example.com?as=1

By injecting 1 into the session as user_id

session[:user_id]   # => 1

Setup in Rails tests

Add the middleware to your stack in the config/environments/test.rb file

# config/environments/test.rb

MyApplication::Application.configure do |config|
# ...
  config.middleware.use Rack::BackDoor
# ...
end

or, in rspec spec helper

# spec/spec_helper.rb
MyApplication::Application.configure do |config|
  config.middleware.use Rack::BackDoor
end

Setup in Sinatra Tests

Add to your sinatra application:

# app.rb
class MySinatraApplication < Sinatra::Base
  enable :sessions
  use Rack::BackDoor if environment == :test
  # ...
end

If you use rspec you may prefer to inject middleware only for rspec tests:

Put into spec/spec_helper.rb:

# spec/spec_helper.rb
MySinatraApplication.configure do
  use Rack::BackDoor
end

Configuration

You can configure the following:

  • session_key - The key used to hold the user_id in the session
  • url_parameter - The query parameter the middleware will use as the user_id value
# When configured with these values and passed the following URL
#
#   http://example.com?login=1
#
# The middleware will set the session like so
#
#   session[:user]  # => 1
#
Rack::BackDoor.configure do |config|
  config.session_key   = "user"
  config.url_parameter = "login"
end

In your spec/spec_helper.rb, or any other testing setup file

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request