Skip to content

Commit

Permalink
Divide the test helpers into two files
Browse files Browse the repository at this point in the history
  • Loading branch information
acaron committed Jan 30, 2016
1 parent a753059 commit a1f433b
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 63 deletions.
1 change: 1 addition & 0 deletions lib/warden.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class NotAuthenticated < StandardError; end
module Test
autoload :WardenHelpers, 'warden/test/warden_helpers'
autoload :Helpers, 'warden/test/helpers'
autoload :Mock, 'warden/test/mock'
end

# Provides helper methods to warden for testing.
Expand Down
55 changes: 0 additions & 55 deletions lib/warden/test/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# encoding: utf-8

require 'rack'

module Warden
module Test
# A collection of test helpers for testing full stack rack applications using Warden
Expand Down Expand Up @@ -33,59 +31,6 @@ def logout(*scopes)
proxy.logout(*scopes)
end
end

# A helper method that provides the warden object by mocking the env variable.
# @api public
def warden
@warden ||= begin
env['warden']
end
end

private

def env
@env ||= begin
request = Rack::MockRequest.env_for(
"/?#{Rack::Utils.build_query({})}",
{ 'HTTP_VERSION' => '1.1', 'REQUEST_METHOD' => 'GET' }
)
app.call(request)

request
end
end

def app
@app ||= begin
opts = {
failure_app: lambda {
[401, { 'Content-Type' => 'text/plain' }, ['You Fail!']]
},
default_strategies: :password,
default_serializers: :session
}
Rack::Builder.new do
use Warden::Test::Helpers::Session
use Warden::Manager, opts, &proc {}
run lambda { |e|
[200, { 'Content-Type' => 'text/plain' }, ['You Win']]
}
end
end
end

class Session
attr_accessor :app
def initialize(app,configs = {})
@app = app
end

def call(e)
e['rack.session'] ||= {}
@app.call(e)
end
end # session
end
end
end
68 changes: 68 additions & 0 deletions lib/warden/test/mock.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# encoding: utf-8

require 'rack'

module Warden
module Test
# A mock of an application to get a Warden object to test on
# Note: During the teardown phase of your specs you should include: Warden.test_reset!
module Mock
def self.included(base)
::Warden.test_mode!
end

# A helper method that provides the warden object by mocking the env variable.
# @api public
def warden
@warden ||= begin
env['warden']
end
end

private

def env
@env ||= begin
request = Rack::MockRequest.env_for(
"/?#{Rack::Utils.build_query({})}",
{ 'HTTP_VERSION' => '1.1', 'REQUEST_METHOD' => 'GET' }
)
app.call(request)

request
end
end

def app
@app ||= begin
opts = {
failure_app: lambda {
[401, { 'Content-Type' => 'text/plain' }, ['You Fail!']]
},
default_strategies: :password,
default_serializers: :session
}
Rack::Builder.new do
use Warden::Test::Mock::Session
use Warden::Manager, opts, &proc {}
run lambda { |e|
[200, { 'Content-Type' => 'text/plain' }, ['You Win']]
}
end
end
end

class Session
attr_accessor :app
def initialize(app,configs = {})
@app = app
end

def call(e)
e['rack.session'] ||= {}
@app.call(e)
end
end # session
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
RSpec.configure do |config|
config.include(Warden::Spec::Helpers)
config.include(Warden::Test::Helpers)
config.include(Warden::Test::Mock)

def load_strategies
Dir[File.join(File.dirname(__FILE__), "helpers", "strategies", "**/*.rb")].each do |f|
Expand Down
8 changes: 0 additions & 8 deletions spec/warden/test/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,6 @@
expect($captures).to eq([:run])
end

it "should return a valid mocked warden" do
user = "A User"
login_as user

expect(warden.class).to eq(Warden::Proxy)
expect(warden.user).to eq(user)
end

describe "#asset_paths" do
it "should default asset_paths to anything asset path regex" do
expect(Warden.asset_paths).to eq([/^\/assets\//] )
Expand Down
15 changes: 15 additions & 0 deletions spec/warden/test/mock_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# encoding: utf-8
require 'spec_helper'

describe Warden::Test::Mock do
before{ $captures = [] }
after{ Warden.test_reset! }

it "should return a valid mocked warden" do
user = "A User"
login_as user

expect(warden.class).to eq(Warden::Proxy)
expect(warden.user).to eq(user)
end
end

0 comments on commit a1f433b

Please sign in to comment.