• Stars
    star
    259
  • Rank 151,798 (Top 4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 12 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Rack middleware that provides access to rack.session environment

Rack Session Access Build Status

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::Middleware 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 of 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

Run to test against Rails4, Sinatra and Rack applications

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

Author

Andriy Yanko

License

References

More Repositories

1

upterm

A terminal emulator for the 21st century.
TypeScript
19,318
star
2

js-routes

Brings Rails named routes to javascript
Ruby
1,592
star
3

Sleipnir

BDD-style framework for Swift
Swift
847
star
4

bozon

🛠 Command line tool for building, testing and publishing modern Electron applications
JavaScript
757
star
5

BloodMagic

BloodMagic is a framework, which gives you a way to create custom property attributes.
Objective-C
317
star
6

global

"Global" provides accessor methods for your configuration data
Ruby
282
star
7

applepie

Semantic and Modular CSS Toolkit
CSS
280
star
8

caphub

Generate centralized capistrano skeleton for multiple deployment
Ruby
230
star
9

smt_rails

Shared mustache templates for rails 3.
Ruby
110
star
10

http_logger

Log your http api calls just like SQL queries
Ruby
106
star
11

rspec-example_steps

Given/When/Then steps for RSpec examples
Ruby
85
star
12

sht_rails

Shared handlebars templates for Rails 3
Ruby
76
star
13

actionmailer-balancer

A Ruby gem to send your ActionMailer mail through one of several delivery methods, selected by weight.
Ruby
76
star
14

capistrano-multiconfig

Capistrano extension that allows to use multiple configurations
Ruby
66
star
15

passenger-initscript

Manage multiple passenger instances
Shell
54
star
16

skypekit

Ruby FFI interface to libskypekit C library
Ruby
47
star
17

newrelic_platform_plugins

Ruby
41
star
18

piro

PiRo - it's Rocket for you Pivotal Tracker account
CoffeeScript
40
star
19

zero_deploy

Significantly improves typical deployment speed
Ruby
40
star
20

mailtrap-nodejs

Official mailtrap.io Node.js client
TypeScript
35
star
21

capistrano-calendar

Deployment event creation on (google) calendar service
Ruby
29
star
22

capistrano-patch

Capistrano patch recipes
Ruby
29
star
23

scaffy.railsware.com

Approach for writing and organizing your CSS for large-scale projects
JavaScript
29
star
24

mailtrap-php

The official mailtrap.io PHP client
PHP
20
star
25

libskypekit

Thread-safe C library with synchronous API using asynchronous C++ SkypeKit SDK
C
19
star
26

backfiller

The backfill machine for database records with null columns
Ruby
16
star
27

capistrano-ci

Ruby
16
star
28

indeed

Indeed.com integration plugin
Ruby
16
star
29

mailtrap-ruby

The official mailtrap.io Ruby client
Ruby
15
star
30

RBRouteBuilder

Build routes without strings and headache
C++
15
star
31

sprinkle_recipes

Railsware sprinkle recipes
Ruby
14
star
32

capistrano-uptodate

Capistrano extension that automatically check local repository with remote repository
Ruby
14
star
33

activeresource-persistent

HTTP persistent connection support for ActiveResource
Ruby
11
star
34

haproxy-slow-fast-request-balancer

HAProxy as "Slow-Fast" Request Balancer
10
star
35

generator-electron-app

Yeoman generator to scaffold Electron app
JavaScript
9
star
36

scout-app-plugins

Useful plugins for the Scout Server Monitoring and Reporting Tool
Ruby
9
star
37

mailtrap-python

Official mailtrap.io Python client
Python
8
star
38

web-bundler

WebResourceBundler bundling particular resource (css or js) in one file. Encoding images in base64 and putting then in css directly.
Ruby
8
star
39

gcal4ruby

Author: Mike Reich. GCal4Ruby is a Ruby Gem that can be used to interact with the current version of the Google Calendar API. GCal4Ruby provides the following features: Create and edit calendar events, Add and invite users to events, Set reminders, Make recurring events.
Ruby
8
star
40

db_structure_ext

Extended rails tasks db:structure:dump/load that supports mysql views/triggers/routines
Ruby
7
star
41

github-actions

A collection of GitHub actions used at Railsware
JavaScript
7
star
42

jdt

JSON Driven Templates
JavaScript
7
star
43

shelltoad

Command line interface for airbrake (http://airbrake.io/)
Ruby
7
star
44

i18n_template

18nTemplate is made to extract phrases and translate html/xhtml/xml document or erb templates
Ruby
6
star
45

capistrano-changelog

Chagelog based on Git commits with a Pivotal story tags
Ruby
5
star
46

go-global

Golang configuration reader for AWS Parameter Store and more
Go
4
star
47

chain_flow

Helps to refactor complex data processing
Ruby
4
star
48

fakes3server

Fake AWS S3 server for local development
Go
4
star
49

jOverlay

Overlay jQuery plugin
JavaScript
4
star
50

dev_vagrant_box

Vagrant box for development (ruby, rails)
4
star
51

yaml_settings

YamlSettings is a simple configuration / settings solution that uses an ERB enabled YAML file.
Ruby
3
star
52

multiversion

Use Bundler and RVM to test your library against different gem versions and/or ruby versions.
Ruby
3
star
53

ui-library

UI Library Template
CSS
2
star
54

clicktale

Clicktale rails plugin by Railsware
Ruby
2
star
55

railsware.github.com

CSS
2
star
56

capybara-feature_helpers

Ruby
2
star
57

backbone_showcase

Ruby
2
star
58

em-rest-client

EventMachine::HttpRequest adapter for HTTP REST client
Ruby
2
star
59

newrelic_em_http

New Relic EM::HTTP instrumentation
Ruby
2
star
60

capistrano-strategy-copy-partial

Capistrano deploy strategy to transfer subdirectory of repository
Ruby
2
star
61

hbase-driver

Small and handy Ruby driver for HBase
Ruby
2
star
62

acme-aws-lambda

AWS Lambda function to generate Letsencrypt certificates (need AWS S3 and Route53)
Ruby
2
star
63

showroom

Ruby
2
star
64

email_templates

HTML
2
star
65

rest_facebook

Lightweight ruby facebook client
Ruby
1
star
66

plunger

Code review tool
Python
1
star
67

cat-aws-ssm-param

Go
1
star
68

rw-study-wishlist

Ruby
1
star
69

template

Rails project template
JavaScript
1
star
70

chef-rwci

Ruby
1
star
71

office-iot

Objective-C
1
star
72

simple_on_couch

Example of CouchApp application for RW articles
JavaScript
1
star
73

gdoc_mapreduce

JavaScript
1
star
74

scaffy

Repo moved to scaffy.railsware.com
1
star
75

highrise_assist

Assist for 37signals' highrise
Ruby
1
star
76

sidekiq_unique_retries

Unique Retries for Sidekiq
Ruby
1
star
77

skypekit_pure

Skypekit on pure ruby
Ruby
1
star
78

aws-ecs-tools

Ruby
1
star
79

capybara_mock

CapybaraMock
Ruby
1
star
80

handlebars_assets_i18n

Very simple handlebars_assets internationalization for .hamlbars and .slimbars templates
Ruby
1
star
81

paypal-sdk-http-adapters

HTTP Adapters for PayPal SDK
Ruby
1
star
82

harvest_report

Harvert reports for non-admin harvest users
Ruby
1
star
83

rest-client-adapters

RestClient Adapters
Ruby
1
star
84

versioned_item

Ruby
1
star
85

cordova-baxi-plugin

Java
1
star
86

column_info_reset

Reset ActiveRecord column info when unknown column exception occurs
Ruby
1
star
87

gdoc_pivotal

Gdoc updater for pivotal
JavaScript
1
star
88

mailtrap-examples

Mailtrap repo examples
HTML
1
star
89

mailtrap-elixir

The official mailtrap.io Elixir client
Elixir
1
star