Skip to content

Commit a1f433b

Browse files
committed
Divide the test helpers into two files
1 parent a753059 commit a1f433b

File tree

6 files changed

+85
-63
lines changed

6 files changed

+85
-63
lines changed

lib/warden.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class NotAuthenticated < StandardError; end
1515
module Test
1616
autoload :WardenHelpers, 'warden/test/warden_helpers'
1717
autoload :Helpers, 'warden/test/helpers'
18+
autoload :Mock, 'warden/test/mock'
1819
end
1920

2021
# Provides helper methods to warden for testing.

lib/warden/test/helpers.rb

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# encoding: utf-8
22

3-
require 'rack'
4-
53
module Warden
64
module Test
75
# A collection of test helpers for testing full stack rack applications using Warden
@@ -33,59 +31,6 @@ def logout(*scopes)
3331
proxy.logout(*scopes)
3432
end
3533
end
36-
37-
# A helper method that provides the warden object by mocking the env variable.
38-
# @api public
39-
def warden
40-
@warden ||= begin
41-
env['warden']
42-
end
43-
end
44-
45-
private
46-
47-
def env
48-
@env ||= begin
49-
request = Rack::MockRequest.env_for(
50-
"/?#{Rack::Utils.build_query({})}",
51-
{ 'HTTP_VERSION' => '1.1', 'REQUEST_METHOD' => 'GET' }
52-
)
53-
app.call(request)
54-
55-
request
56-
end
57-
end
58-
59-
def app
60-
@app ||= begin
61-
opts = {
62-
failure_app: lambda {
63-
[401, { 'Content-Type' => 'text/plain' }, ['You Fail!']]
64-
},
65-
default_strategies: :password,
66-
default_serializers: :session
67-
}
68-
Rack::Builder.new do
69-
use Warden::Test::Helpers::Session
70-
use Warden::Manager, opts, &proc {}
71-
run lambda { |e|
72-
[200, { 'Content-Type' => 'text/plain' }, ['You Win']]
73-
}
74-
end
75-
end
76-
end
77-
78-
class Session
79-
attr_accessor :app
80-
def initialize(app,configs = {})
81-
@app = app
82-
end
83-
84-
def call(e)
85-
e['rack.session'] ||= {}
86-
@app.call(e)
87-
end
88-
end # session
8934
end
9035
end
9136
end

lib/warden/test/mock.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# encoding: utf-8
2+
3+
require 'rack'
4+
5+
module Warden
6+
module Test
7+
# A mock of an application to get a Warden object to test on
8+
# Note: During the teardown phase of your specs you should include: Warden.test_reset!
9+
module Mock
10+
def self.included(base)
11+
::Warden.test_mode!
12+
end
13+
14+
# A helper method that provides the warden object by mocking the env variable.
15+
# @api public
16+
def warden
17+
@warden ||= begin
18+
env['warden']
19+
end
20+
end
21+
22+
private
23+
24+
def env
25+
@env ||= begin
26+
request = Rack::MockRequest.env_for(
27+
"/?#{Rack::Utils.build_query({})}",
28+
{ 'HTTP_VERSION' => '1.1', 'REQUEST_METHOD' => 'GET' }
29+
)
30+
app.call(request)
31+
32+
request
33+
end
34+
end
35+
36+
def app
37+
@app ||= begin
38+
opts = {
39+
failure_app: lambda {
40+
[401, { 'Content-Type' => 'text/plain' }, ['You Fail!']]
41+
},
42+
default_strategies: :password,
43+
default_serializers: :session
44+
}
45+
Rack::Builder.new do
46+
use Warden::Test::Mock::Session
47+
use Warden::Manager, opts, &proc {}
48+
run lambda { |e|
49+
[200, { 'Content-Type' => 'text/plain' }, ['You Win']]
50+
}
51+
end
52+
end
53+
end
54+
55+
class Session
56+
attr_accessor :app
57+
def initialize(app,configs = {})
58+
@app = app
59+
end
60+
61+
def call(e)
62+
e['rack.session'] ||= {}
63+
@app.call(e)
64+
end
65+
end # session
66+
end
67+
end
68+
end

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
RSpec.configure do |config|
1616
config.include(Warden::Spec::Helpers)
1717
config.include(Warden::Test::Helpers)
18+
config.include(Warden::Test::Mock)
1819

1920
def load_strategies
2021
Dir[File.join(File.dirname(__FILE__), "helpers", "strategies", "**/*.rb")].each do |f|

spec/warden/test/helpers_spec.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,6 @@
8585
expect($captures).to eq([:run])
8686
end
8787

88-
it "should return a valid mocked warden" do
89-
user = "A User"
90-
login_as user
91-
92-
expect(warden.class).to eq(Warden::Proxy)
93-
expect(warden.user).to eq(user)
94-
end
95-
9688
describe "#asset_paths" do
9789
it "should default asset_paths to anything asset path regex" do
9890
expect(Warden.asset_paths).to eq([/^\/assets\//] )

spec/warden/test/mock_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# encoding: utf-8
2+
require 'spec_helper'
3+
4+
describe Warden::Test::Mock do
5+
before{ $captures = [] }
6+
after{ Warden.test_reset! }
7+
8+
it "should return a valid mocked warden" do
9+
user = "A User"
10+
login_as user
11+
12+
expect(warden.class).to eq(Warden::Proxy)
13+
expect(warden.user).to eq(user)
14+
end
15+
end

0 commit comments

Comments
 (0)