-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Divide the test helpers into two files
- Loading branch information
Showing
6 changed files
with
85 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |