Skip to content

Commit

Permalink
Merge pull request #129 from acaron/fix-test-helpers
Browse files Browse the repository at this point in the history
Divide the test helpers into two files. Thanks!
  • Loading branch information
Daniel Neighman committed Jan 31, 2016
2 parents a753059 + 43b6a29 commit 906edf8
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 64 deletions.
3 changes: 3 additions & 0 deletions History.rdoc
@@ -1,3 +1,6 @@
== Version 1.2.6 / 2016-01-31
* Separate test helpers to encapsulate Warden object mocking inside it's own class

== Version 1.2.5 / 2016-01-28
* Expands on the test helpers available to make it easier for testing gems

Expand Down
1 change: 1 addition & 0 deletions lib/warden.rb
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
@@ -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
@@ -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
2 changes: 1 addition & 1 deletion lib/warden/version.rb
@@ -1,4 +1,4 @@
# encoding: utf-8
module Warden
VERSION = "1.2.5".freeze
VERSION = "1.2.6".freeze
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
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
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
@@ -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 906edf8

Please sign in to comment.