diff --git a/lib/warden.rb b/lib/warden.rb index df556d7..7e64040 100644 --- a/lib/warden.rb +++ b/lib/warden.rb @@ -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. diff --git a/lib/warden/test/helpers.rb b/lib/warden/test/helpers.rb index 3950433..2f3397c 100644 --- a/lib/warden/test/helpers.rb +++ b/lib/warden/test/helpers.rb @@ -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 @@ -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 diff --git a/lib/warden/test/mock.rb b/lib/warden/test/mock.rb new file mode 100644 index 0000000..3b5531c --- /dev/null +++ b/lib/warden/test/mock.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2c27af6..0334f72 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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| diff --git a/spec/warden/test/helpers_spec.rb b/spec/warden/test/helpers_spec.rb index 4268880..2905c3f 100644 --- a/spec/warden/test/helpers_spec.rb +++ b/spec/warden/test/helpers_spec.rb @@ -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\//] ) diff --git a/spec/warden/test/mock_spec.rb b/spec/warden/test/mock_spec.rb new file mode 100644 index 0000000..e43a970 --- /dev/null +++ b/spec/warden/test/mock_spec.rb @@ -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