Code Monkey home page Code Monkey logo

rack_session_access's Introduction

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

gem install rack_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.configure do |config|
  ...
  # Access to rack session
  config.middleware.use RackSessionAccess::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:

class MySinatraApplication < Sinatra::Base
  enable :sessions
  use RackSessionAccess if environment == :test
  ...
end

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

MySinatraApplication.configure do |app|
  app.use RackSessionAccess::Middleware
end

Using with Rack builder

Rack::Builder.new do
  ...
  use Rack::Session::Cookie
  use RackSessionAccess::Middleware
  use MyRackApplication
end.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" do
  given!(:user) { create(:user, email: '[email protected]') }

  scenario "logged in user access profile page" do
    page.set_rack_session(user_id: user.id)
    visit "/profile"
    expect(page).to have_content("Hi, [email protected]")
  end

  scenario "visit landing page" do
    visit "/landing?ref=123"
    expect(page.get_rack_session_key('ref')).to eq("123")
  end
end

Authlogic integration

module FeatureHelpers
  def logged_as(user)
    page.set_rack_session('user_credentials' => user.persistence_token)
  end
end

Devise integration

module FeatureHelpers
  def logged_as(user)
    # Devise v3.x.x
    page.set_rack_session('warden.user.user.key' => User.serialize_into_session(user).unshift('User'))

    # Devise v4.x.x
    page.set_rack_session('warden.user.user.key' => User.serialize_into_session(user)
  end
end

Authentication helper

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

RSpec.configure do |config|
  ...
  config.include FeatureHelpers, type: :feature
  ...
end

Start your scenarios with already logged in user:

feature 'User dashboard', type: :feature do
  given(:user) { create(:user) }
  background do
    logged_as user
  end
  scenario 'User reviews a dashboard' do
    ...
  end
end

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

rack_session_access's People

Contributors

allcentury avatar amatsuda avatar arthurnn avatar ayanko avatar chulkilee avatar hiroshi avatar hotchpotch avatar ryanong avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.