Skip to content

Commit

Permalink
Merge pull request #127 from acaron/add-mocked-warden-to-test-helpers
Browse files Browse the repository at this point in the history
Add mocked warden in test helpers
  • Loading branch information
Daniel Neighman committed Jan 28, 2016
2 parents 06860f0 + 7b3202a commit cbe6459
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
55 changes: 55 additions & 0 deletions lib/warden/test/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# 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 @@ -31,6 +33,59 @@ 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
8 changes: 8 additions & 0 deletions spec/warden/test/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@
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

0 comments on commit cbe6459

Please sign in to comment.