Permalink
Browse files

Upgraded to rspec 3. Updated README

  • Loading branch information...
ayanko committed Feb 10, 2015
1 parent fb4b039 commit a40ea1815e05b465731b3ee6e0f4adca6c3b7bc7
Showing with 121 additions and 84 deletions.
  1. +42 −11 README.md
  2. +2 −2 rack_session_access.gemspec
  3. +57 −59 spec/middleware_spec.rb
  4. +16 −12 spec/rack_session_access_spec.rb
  5. +4 −0 spec/spec_helper.rb
View
@@ -96,33 +96,64 @@ Example:
require 'spec_helper'
feature "My feature" do
background do
@user = Factory(:user, email: 'jack@daniels.com')
end
given!(:user) { create(:user, email: 'jack@daniels.com') }
scenario "logged in user access profile page" do
page.set_rack_session(user_id: user.id)
page.visit "/profile"
page.should have_content("Hi, jack@daniels.com")
visit "/profile"
expect(page).to have_content("Hi, jack@daniels.com")
end
scenario "visit landing page" do
page.visit "/landing?ref=123"
page.get_rack_session_key('ref').should == "123"
visit "/landing?ref=123"
expect(page.get_rack_session_key('ref')).to eq("123")
end
end
```
## Authlogic integration
### Authlogic integration
```ruby
page.set_rack_session("user_credentials" => @user.persistence_token)
module FeatureHelpers
def logged_as(user)
page.set_rack_session('user_credentials' => user.persistence_token)
end
end
```
## Devise integration
### Devise integration
```ruby
module FeatureHelpers
def logged_as(user)
page.set_rack_session('warden.user.user.key' => User.serialize_into_session(user).unshift("User"))
end
end
```
## Authentication helper
Put corresponding implementation of `logged_as` in `spec/support/feature_helpers.rb` and include module into rspec config:
```ruby
page.set_rack_session("warden.user.user.key" => User.serialize_into_session(@user).unshift("User"))
RSpec.configure do |config|
...
config.include FeatureHelpers, type: :feature
...
end
```
Start your scenarios with already logged in user:
```ruby
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
@@ -21,8 +21,8 @@ Gem::Specification.new do |s|
s.add_runtime_dependency "rack", ">=1.0.0"
s.add_runtime_dependency "builder", ">=2.0.0"
s.add_development_dependency 'rspec', '2.12.0'
s.add_development_dependency 'capybara', '1.1.4'
s.add_development_dependency 'rspec', '~>3.2.0'
s.add_development_dependency 'capybara', '~>2.3.0'
s.add_development_dependency 'selenium-webdriver', '2.44.0'
s.add_development_dependency 'sinatra', '1.3.4'
s.add_development_dependency 'rails', '>=3.0.0'
View
@@ -10,54 +10,54 @@
end
scenario "changing session data" do
page.visit RackSessionAccess.edit_path
page.should have_content("Update rack session")
page.fill_in "data", :with => RackSessionAccess.encode({'user_id' => 1})
page.click_button "Update"
page.should have_content("Rack session data")
page.should have_content('"user_id" : 1')
page.current_path.should == RackSessionAccess.path
visit RackSessionAccess.edit_path
expect(page).to have_content("Update rack session")
fill_in "data", with: RackSessionAccess.encode({'user_id' => 1})
click_button "Update"
expect(page).to have_content("Rack session data")
expect(page).to have_content('"user_id" : 1')
expect(page.current_path).to eq(RackSessionAccess.path)
end
scenario "providing no session data" do
page.visit RackSessionAccess.edit_path
page.should have_content("Update rack session")
visit RackSessionAccess.edit_path
expect(page).to have_content("Update rack session")
page.click_button "Update"
page.should have_content("Bad data")
page.current_path.should == RackSessionAccess.path
click_button "Update"
expect(page).to have_content("Bad data")
expect(page.current_path).to eq(RackSessionAccess.path)
end
scenario "providing bad data" do
page.visit RackSessionAccess.edit_path
page.should have_content("Update rack session")
visit RackSessionAccess.edit_path
expect(page).to have_content("Update rack session")
page.fill_in "data", :with => "qwertyuiop"
page.click_button "Update"
page.should have_content("Bad data")
page.current_path.should == RackSessionAccess.path
fill_in "data", :with => "qwertyuiop"
click_button "Update"
expect(page).to have_content("Bad data")
expect(page.current_path).to eq(RackSessionAccess.path)
end
scenario "modify session data with set_rack_session helper" do
page.set_rack_session(data)
page.visit(RackSessionAccess.path)
page.should have_content('"user_email" : "jack@daniels.com"')
page.should have_content('"user_profile" : {:age=>12}')
page.should have_content('"role_ids" : [1, 20, 30]')
visit(RackSessionAccess.path)
expect(page).to have_content('"user_email" : "jack@daniels.com"')
expect(page).to have_content('"user_profile" : {:age=>12}')
expect(page).to have_content('"role_ids" : [1, 20, 30]')
end
scenario "accessing raw session data" do
page.set_rack_session(data)
page.visit(RackSessionAccess.path + '.raw')
raw_data = page.find(:xpath, "//body/pre").text
raw_data.should be_present
visit(RackSessionAccess.path + '.raw')
raw_data = find(:xpath, "//body/pre").text
expect(raw_data).to be_present
actual_data = RackSessionAccess.decode(raw_data)
actual_data.should be_kind_of(Hash)
expect(actual_data).to be_kind_of(Hash)
data.each do |key, value|
actual_data[key].should == value
expect(actual_data[key]).to eq(value)
end
end
@@ -66,63 +66,63 @@
actual_data = page.get_rack_session
actual_data.should be_kind_of(Hash)
expect(actual_data).to be_kind_of(Hash)
data.each do |key, value|
actual_data[key].should == value
expect(actual_data[key]).to eq(value)
end
end
scenario "accessing raw session data using get_rack_session_key helper" do
page.set_rack_session(data)
page.get_rack_session_key('role_ids').should == [1, 20, 30]
expect(page.get_rack_session_key('role_ids')).to eq([1, 20, 30])
end
end
shared_examples "rack scenarios" do
shared_context "rack scenarios" do
scenario "accessing application" do
page.visit "/welcome"
page.text.should == "DUMMY"
visit "/welcome"
expect(page.text).to eq("DUMMY")
end
end
shared_examples "sinatra scenarios" do
shared_context "sinatra scenarios" do
scenario "test application itself" do
page.visit "/login"
page.should have_content("Please log in")
visit "/login"
expect(page).to have_content("Please log in")
page.visit "/profile"
page.should have_content("Please log in")
visit "/profile"
expect(page).to have_content("Please log in")
end
scenario "accessing application" do
page.visit "/profile"
page.text.should == "Please log in"
visit "/profile"
expect(page.text).to eq("Please log in")
page.set_rack_session({:user_email => "jack@daniels.com"})
page.set_rack_session(user_email: "jack@daniels.com")
page.visit "/profile"
page.text.should == "Welcome, jack@daniels.com!"
visit "/profile"
expect(page.text).to eq("Welcome, jack@daniels.com!")
end
end
shared_examples "rails scenarios" do
shared_context "rails scenarios" do
scenario "test application itself" do
page.visit "/login"
page.should have_content("Please log in")
visit "/login"
expect(page).to have_content("Please log in")
page.visit "/profile"
page.should have_content("Please log in")
visit "/profile"
expect(page).to have_content("Please log in")
end
scenario "accessing application" do
page.visit "/profile"
page.text.should == "Please log in"
visit "/profile"
expect(page.text).to eq("Please log in")
page.set_rack_session({:user_email => "jack@daniels.com"})
page.set_rack_session(user_email: "jack@daniels.com")
page.visit "/profile"
page.text.should == "Welcome, jack@daniels.com!"
visit "/profile"
expect(page.text).to eq("Welcome, jack@daniels.com!")
end
end
@@ -132,11 +132,11 @@
So I can write faster tests
) do
context ":rack_test driver", :driver => :rack_test do
context ":rack_test driver", driver: :rack_test do
context "rack application" do
background { Capybara.app = TestRackApp }
include_examples "common scenarios"
include_examples "rack scenarios"
#include_examples "rack scenarios"
end
context "sinatra application" do
@@ -152,9 +152,7 @@
end
end
context ":selenium driver", :driver => :selenium do
context ":selenium driver", driver: :selenium do
context "rack application" do
background { Capybara.app = TestRackApp }
include_examples "common scenarios"
@@ -3,36 +3,40 @@
describe RackSessionAccess do
subject { described_class }
its(:path) { should == '/rack_session' }
it 'should have configured default path' do
expect(subject.path).to eq('/rack_session')
end
its(:edit_path) { should == '/rack_session/edit' }
it 'should have configured default edit_path' do
expect(subject.edit_path).to eq('/rack_session/edit')
end
describe ".encode" do
it "should encode ruby hash to string" do
describe '.encode' do
it 'should encode ruby hash to string' do
result = subject.encode( { 'a' => 'b' })
result.should be_kind_of(String)
expect(result).to be_kind_of(String)
end
end
describe ".decode" do
it "should decode marshalized and base64 encoded string" do
describe '.decode' do
it 'should decode marshalized and base64 encoded string' do
# Array(Marshal.dump({ :user_id => 100 })).pack("m")
# => "BAh7BjoMdXNlcl9pZGlp\n"
subject.decode("BAh7BjoMdXNlcl9pZGlp\n").should == { :user_id => 100 }
expect(subject.decode("BAh7BjoMdXNlcl9pZGlp\n")).to eq(user_id: 100)
end
end
it "should encode and decode value" do
it 'should encode and decode value' do
source = { 'klass' => Class, :id => 100, '1' => 2 }
data = subject.encode(source)
result = subject.decode(data)
result.should == source
expect(result).to eq(source)
end
it "should encode and decode values with line brake characters" do
it 'should encode and decode values with line brake characters' do
source = { 'line' => "one\ntwo" }
data = subject.encode(source)
result = subject.decode(data)
result.should == source
expect(result).to eq(source)
end
end
View
@@ -7,6 +7,10 @@
require f
end
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, browser: ENV.fetch('SELENIUM_BROWSER', 'firefox').to_sym)
end
TestSinatraApp.configure do |app|
app.environment = :test
app.use RackSessionAccess::Middleware

0 comments on commit a40ea18

Please sign in to comment.