loyal_warden 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +11 -0
- data/History.rdoc +150 -0
- data/LICENSE +20 -0
- data/README.textile +9 -0
- data/Rakefile +12 -0
- data/lib/loyal_warden.rb +2 -0
- data/lib/warden.rb +45 -0
- data/lib/warden/config.rb +112 -0
- data/lib/warden/errors.rb +66 -0
- data/lib/warden/hooks.rb +211 -0
- data/lib/warden/manager.rb +136 -0
- data/lib/warden/mixins/common.rb +44 -0
- data/lib/warden/proxy.rb +371 -0
- data/lib/warden/session_serializer.rb +52 -0
- data/lib/warden/strategies.rb +47 -0
- data/lib/warden/strategies/base.rb +175 -0
- data/lib/warden/test/helpers.rb +36 -0
- data/lib/warden/test/warden_helpers.rb +43 -0
- data/lib/warden/version.rb +4 -0
- data/loyal_warden.gemspec +26 -0
- data/spec/helpers/request_helper.rb +51 -0
- data/spec/helpers/strategies/failz.rb +8 -0
- data/spec/helpers/strategies/invalid.rb +8 -0
- data/spec/helpers/strategies/pass.rb +8 -0
- data/spec/helpers/strategies/pass_with_message.rb +8 -0
- data/spec/helpers/strategies/password.rb +13 -0
- data/spec/helpers/strategies/single.rb +12 -0
- data/spec/spec_helper.rb +24 -0
- data/spec/warden/authenticated_data_store_spec.rb +114 -0
- data/spec/warden/config_spec.rb +48 -0
- data/spec/warden/errors_spec.rb +47 -0
- data/spec/warden/hooks_spec.rb +373 -0
- data/spec/warden/manager_spec.rb +316 -0
- data/spec/warden/proxy_spec.rb +1041 -0
- data/spec/warden/scoped_session_serializer.rb +123 -0
- data/spec/warden/session_serializer_spec.rb +53 -0
- data/spec/warden/strategies/base_spec.rb +313 -0
- data/spec/warden/strategies_spec.rb +93 -0
- data/spec/warden/test/helpers_spec.rb +93 -0
- data/spec/warden/test/test_mode_spec.rb +76 -0
- data/warden.gemspec +24 -0
- metadata +105 -0
@@ -0,0 +1,8 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
Warden::Strategies.add(:pass_with_message) do
|
3
|
+
def authenticate!
|
4
|
+
request.env['warden.spec.strategies'] ||= []
|
5
|
+
request.env['warden.spec.strategies'] << :pass_with_message
|
6
|
+
success!("Valid User", "The Success Strategy Has Accepted You") unless scope == :failz
|
7
|
+
end
|
8
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
Warden::Strategies.add(:password) do
|
3
|
+
def authenticate!
|
4
|
+
request.env['warden.spec.strategies'] ||= []
|
5
|
+
request.env['warden.spec.strategies'] << :password
|
6
|
+
if params["password"] || params["username"]
|
7
|
+
params["password"] == "sekrit" && params["username"] == "fred" ?
|
8
|
+
success!("Authenticated User") : fail!("Username or password is incorrect")
|
9
|
+
else
|
10
|
+
pass
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$TESTING=true
|
3
|
+
|
4
|
+
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
5
|
+
$:.unshift File.expand_path(File.join(File.dirname(__FILE__)))
|
6
|
+
require 'warden'
|
7
|
+
|
8
|
+
require 'rubygems'
|
9
|
+
require 'rack'
|
10
|
+
|
11
|
+
Dir[File.join(File.dirname(__FILE__), "helpers", "**/*.rb")].each do |f|
|
12
|
+
require f
|
13
|
+
end
|
14
|
+
|
15
|
+
RSpec.configure do |config|
|
16
|
+
config.include(Warden::Spec::Helpers)
|
17
|
+
config.include(Warden::Test::Helpers)
|
18
|
+
|
19
|
+
def load_strategies
|
20
|
+
Dir[File.join(File.dirname(__FILE__), "helpers", "strategies", "**/*.rb")].each do |f|
|
21
|
+
load f
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe "authenticated data store" do
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
@env = env_with_params
|
8
|
+
@env['rack.session'] = {
|
9
|
+
"warden.user.foo.key" => "foo user",
|
10
|
+
"warden.user.default.key" => "default user",
|
11
|
+
:foo => "bar"
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should store data for the default scope" do
|
16
|
+
app = lambda do |e|
|
17
|
+
e['warden'].authenticate(:pass)
|
18
|
+
e['warden'].authenticate(:pass, :scope => :foo)
|
19
|
+
e['warden'].should be_authenticated
|
20
|
+
e['warden'].should be_authenticated(:foo)
|
21
|
+
|
22
|
+
# Store the data for :default
|
23
|
+
e['warden'].session[:key] = "value"
|
24
|
+
valid_response
|
25
|
+
end
|
26
|
+
setup_rack(app).call(@env)
|
27
|
+
@env['rack.session']['warden.user.default.session'].should == {:key => "value"}
|
28
|
+
@env['rack.session']['warden.user.foo.session'].should be_nil
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should store data for the foo user" do
|
32
|
+
app = lambda do |e|
|
33
|
+
e['warden'].session(:foo)[:key] = "value"
|
34
|
+
valid_response
|
35
|
+
end
|
36
|
+
setup_rack(app).call(@env)
|
37
|
+
@env['rack.session']['warden.user.foo.session'].should == {:key => "value"}
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should store the data seperately" do
|
41
|
+
app = lambda do |e|
|
42
|
+
e['warden'].session[:key] = "value"
|
43
|
+
e['warden'].session(:foo)[:key] = "another value"
|
44
|
+
valid_response
|
45
|
+
end
|
46
|
+
setup_rack(app).call(@env)
|
47
|
+
@env['rack.session']['warden.user.default.session'].should == {:key => "value"}
|
48
|
+
@env['rack.session']['warden.user.foo.session' ].should == {:key => "another value"}
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should clear the foo scoped data when foo logs out" do
|
52
|
+
app = lambda do |e|
|
53
|
+
e['warden'].session[:key] = "value"
|
54
|
+
e['warden'].session(:foo)[:key] = "another value"
|
55
|
+
e['warden'].logout(:foo)
|
56
|
+
valid_response
|
57
|
+
end
|
58
|
+
setup_rack(app).call(@env)
|
59
|
+
@env['rack.session']['warden.user.default.session'].should == {:key => "value"}
|
60
|
+
@env['rack.session']['warden.user.foo.session' ].should be_nil
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should clear out the default data when :default logs out" do
|
64
|
+
app = lambda do |e|
|
65
|
+
e['warden'].session[:key] = "value"
|
66
|
+
e['warden'].session(:foo)[:key] = "another value"
|
67
|
+
e['warden'].logout(:default)
|
68
|
+
valid_response
|
69
|
+
end
|
70
|
+
setup_rack(app).call(@env)
|
71
|
+
@env['rack.session']['warden.user.default.session'].should be_nil
|
72
|
+
@env['rack.session']['warden.user.foo.session' ].should == {:key => "another value"}
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should clear out all data when a general logout is performed" do
|
76
|
+
app = lambda do |e|
|
77
|
+
e['warden'].session[:key] = "value"
|
78
|
+
e['warden'].session(:foo)[:key] = "another value"
|
79
|
+
e['warden'].logout
|
80
|
+
valid_response
|
81
|
+
end
|
82
|
+
setup_rack(app).call(@env)
|
83
|
+
@env['rack.session']['warden.user.default.session'].should be_nil
|
84
|
+
@env['rack.session']['warden.user.foo.session' ].should be_nil
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should logout multuiple personas at once" do
|
88
|
+
@env['rack.session']['warden.user.bar.key'] = "bar user"
|
89
|
+
|
90
|
+
app = lambda do |e|
|
91
|
+
e['warden'].session[:key] = "value"
|
92
|
+
e['warden'].session(:foo)[:key] = "another value"
|
93
|
+
e['warden'].session(:bar)[:key] = "yet another"
|
94
|
+
e['warden'].logout(:bar, :default)
|
95
|
+
valid_response
|
96
|
+
end
|
97
|
+
setup_rack(app).call(@env)
|
98
|
+
@env['rack.session']['warden.user.default.session'].should be_nil
|
99
|
+
@env['rack.session']['warden.user.foo.session' ].should == {:key => "another value"}
|
100
|
+
@env['rack.session']['warden.user.bar.session' ].should be_nil
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should not store data for a user who is not logged in" do
|
104
|
+
@env['rack.session']
|
105
|
+
app = lambda do |e|
|
106
|
+
e['warden'].session(:not_here)[:key] = "value"
|
107
|
+
valid_response
|
108
|
+
end
|
109
|
+
|
110
|
+
lambda do
|
111
|
+
setup_rack(app).call(@env)
|
112
|
+
end.should raise_error(Warden::NotAuthenticated)
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Warden::Config do
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
@config = Warden::Config.new
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should behave like a hash" do
|
11
|
+
@config[:foo] = :bar
|
12
|
+
@config[:foo].should == :bar
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should provide hash accessors" do
|
16
|
+
@config.failure_app = :foo
|
17
|
+
@config[:failure_app].should == :foo
|
18
|
+
@config[:failure_app] = :bar
|
19
|
+
@config.failure_app.should == :bar
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should allow to read and set default strategies" do
|
23
|
+
@config.default_strategies :foo, :bar
|
24
|
+
@config.default_strategies.should == [:foo, :bar]
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should allow to silence missing strategies" do
|
28
|
+
@config.silence_missing_strategies!
|
29
|
+
@config.silence_missing_strategies?.should be_true
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should set the default_scope" do
|
33
|
+
@config.default_scope.should == :default
|
34
|
+
@config.default_scope = :foo
|
35
|
+
@config.default_scope.should == :foo
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should merge given options on initialization" do
|
39
|
+
Warden::Config.new(:foo => :bar)[:foo].should == :bar
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should setup defaults with the scope_defaults method" do
|
43
|
+
c = Warden::Config.new
|
44
|
+
c.scope_defaults :foo, :strategies => [:foo, :bar], :store => false
|
45
|
+
c.default_strategies(:scope => :foo).should == [:foo, :bar]
|
46
|
+
c.scope_defaults(:foo).should == {:store => false}
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Warden::Proxy::Errors do
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
@errors = Warden::Proxy::Errors.new
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should report that it is empty on first creation" do
|
11
|
+
@errors.empty?.should == true
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should continue to report that it is empty even after being checked" do
|
15
|
+
@errors.on(:foo)
|
16
|
+
@errors.empty?.should == true
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should add an error" do
|
20
|
+
@errors.add(:login, "Login or password incorrect")
|
21
|
+
@errors[:login].should == ["Login or password incorrect"]
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should allow many errors to be added to the same field" do
|
25
|
+
@errors.add(:login, "bad 1")
|
26
|
+
@errors.add(:login, "bad 2")
|
27
|
+
@errors.on(:login).should == ["bad 1", "bad 2"]
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should give the full messages for an error" do
|
31
|
+
@errors.add(:login, "login wrong")
|
32
|
+
@errors.add(:password, "password wrong")
|
33
|
+
["password wrong", "login wrong"].each do |msg|
|
34
|
+
@errors.full_messages.should include(msg)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should return the error for a specific field / label" do
|
39
|
+
@errors.add(:login, "wrong")
|
40
|
+
@errors.on(:login).should == ["wrong"]
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should return nil for a specific field if it's not been set" do
|
44
|
+
@errors.on(:not_there).should be_nil
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,373 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe "standard authentication hooks" do
|
5
|
+
|
6
|
+
before(:all) do
|
7
|
+
load_strategies
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "after_set_user" do
|
11
|
+
before(:each) do
|
12
|
+
RAM = Warden::Manager unless defined?(RAM)
|
13
|
+
RAM._after_set_user.clear
|
14
|
+
end
|
15
|
+
|
16
|
+
after(:each) do
|
17
|
+
RAM._after_set_user.clear
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should allow me to add an after_set_user hook" do
|
21
|
+
RAM.after_set_user do |user, auth, opts|
|
22
|
+
"boo"
|
23
|
+
end
|
24
|
+
RAM._after_set_user.should have(1).item
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should allow me to add multiple after_set_user hooks" do
|
28
|
+
RAM.after_set_user{|user, auth, opts| "foo"}
|
29
|
+
RAM.after_set_user{|u,a| "bar"}
|
30
|
+
RAM._after_set_user.should have(2).items
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should run each after_set_user hook after the user is set" do
|
34
|
+
RAM.after_set_user{|u,a,o| a.env['warden.spec.hook.foo'] = "run foo"}
|
35
|
+
RAM.after_set_user{|u,a,o| a.env['warden.spec.hook.bar'] = "run bar"}
|
36
|
+
RAM.after_set_user{|u,a,o| a.logout}
|
37
|
+
app = lambda do |e|
|
38
|
+
e['warden'].set_user("foo")
|
39
|
+
valid_response
|
40
|
+
end
|
41
|
+
env = env_with_params
|
42
|
+
setup_rack(app).call(env)
|
43
|
+
env['warden'].user.should be_nil
|
44
|
+
env['warden.spec.hook.foo'].should == "run foo"
|
45
|
+
env['warden.spec.hook.bar'].should == "run bar"
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should not run the event specified with except" do
|
49
|
+
RAM.after_set_user(:except => :set_user){|u,a,o| fail}
|
50
|
+
app = lambda do |e|
|
51
|
+
e['warden'].set_user("foo")
|
52
|
+
valid_response
|
53
|
+
end
|
54
|
+
env = env_with_params
|
55
|
+
setup_rack(app).call(env)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should only run the event specified with only" do
|
59
|
+
RAM.after_set_user(:only => :set_user){|u,a,o| fail}
|
60
|
+
app = lambda do |e|
|
61
|
+
e['warden'].authenticate(:pass)
|
62
|
+
valid_response
|
63
|
+
end
|
64
|
+
env = env_with_params
|
65
|
+
setup_rack(app).call(env)
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should run filters in the given order" do
|
69
|
+
RAM.after_set_user{|u,a,o| a.env['warden.spec.order'] << 2}
|
70
|
+
RAM.after_set_user{|u,a,o| a.env['warden.spec.order'] << 3}
|
71
|
+
RAM.prepend_after_set_user{|u,a,o| a.env['warden.spec.order'] << 1}
|
72
|
+
app = lambda do |e|
|
73
|
+
e['warden.spec.order'] = []
|
74
|
+
e['warden'].set_user("foo")
|
75
|
+
valid_response
|
76
|
+
end
|
77
|
+
env = env_with_params
|
78
|
+
setup_rack(app).call(env)
|
79
|
+
env['warden.spec.order'].should == [1,2,3]
|
80
|
+
end
|
81
|
+
|
82
|
+
context "after_authentication" do
|
83
|
+
it "should be a wrapper to after_set_user behavior" do
|
84
|
+
RAM.after_authentication{|u,a,o| a.env['warden.spec.hook.baz'] = "run baz"}
|
85
|
+
RAM.after_authentication{|u,a,o| a.env['warden.spec.hook.paz'] = "run paz"}
|
86
|
+
RAM.after_authentication{|u,a,o| o[:event].should == :authentication }
|
87
|
+
app = lambda do |e|
|
88
|
+
e['warden'].authenticate(:pass)
|
89
|
+
valid_response
|
90
|
+
end
|
91
|
+
env = env_with_params
|
92
|
+
setup_rack(app).call(env)
|
93
|
+
env['warden.spec.hook.baz'].should == 'run baz'
|
94
|
+
env['warden.spec.hook.paz'].should == 'run paz'
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should not be invoked on default after_set_user scenario" do
|
98
|
+
RAM.after_authentication{|u,a,o| fail}
|
99
|
+
app = lambda do |e|
|
100
|
+
e['warden'].set_user("foo")
|
101
|
+
valid_response
|
102
|
+
end
|
103
|
+
env = env_with_params
|
104
|
+
setup_rack(app).call(env)
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should run filters in the given order" do
|
108
|
+
RAM.after_authentication{|u,a,o| a.env['warden.spec.order'] << 2}
|
109
|
+
RAM.after_authentication{|u,a,o| a.env['warden.spec.order'] << 3}
|
110
|
+
RAM.prepend_after_authentication{|u,a,o| a.env['warden.spec.order'] << 1}
|
111
|
+
app = lambda do |e|
|
112
|
+
e['warden.spec.order'] = []
|
113
|
+
e['warden'].authenticate(:pass)
|
114
|
+
valid_response
|
115
|
+
end
|
116
|
+
env = env_with_params
|
117
|
+
setup_rack(app).call(env)
|
118
|
+
env['warden.spec.order'].should == [1,2,3]
|
119
|
+
end
|
120
|
+
|
121
|
+
it "should allow me to log out a user in an after_set_user block" do
|
122
|
+
RAM.after_set_user{|u,a,o| a.logout}
|
123
|
+
|
124
|
+
app = lambda do |e|
|
125
|
+
e['warden'].authenticate(:pass)
|
126
|
+
valid_response
|
127
|
+
end
|
128
|
+
env = env_with_params
|
129
|
+
setup_rack(app).call(env)
|
130
|
+
env['warden'].authenticated?.should be_false
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
context "after_fetch" do
|
135
|
+
it "should be a wrapper to after_set_user behavior" do
|
136
|
+
RAM.after_fetch{|u,a,o| a.env['warden.spec.hook.baz'] = "run baz"}
|
137
|
+
RAM.after_fetch{|u,a,o| a.env['warden.spec.hook.paz'] = "run paz"}
|
138
|
+
RAM.after_fetch{|u,a,o| o[:event].should == :fetch }
|
139
|
+
env = env_with_params
|
140
|
+
setup_rack(lambda { |e| valid_response }).call(env)
|
141
|
+
env['rack.session']['warden.user.default.key'] = "Foo"
|
142
|
+
env['warden'].user.should == "Foo"
|
143
|
+
env['warden.spec.hook.baz'].should == 'run baz'
|
144
|
+
env['warden.spec.hook.paz'].should == 'run paz'
|
145
|
+
end
|
146
|
+
|
147
|
+
it "should not be invoked on default after_set_user scenario" do
|
148
|
+
RAM.after_fetch{|u,a,o| fail}
|
149
|
+
app = lambda do |e|
|
150
|
+
e['warden'].set_user("foo")
|
151
|
+
valid_response
|
152
|
+
end
|
153
|
+
env = env_with_params
|
154
|
+
setup_rack(app).call(env)
|
155
|
+
end
|
156
|
+
|
157
|
+
it "should not be invoked if fetched user is nil" do
|
158
|
+
RAM.after_fetch{|u,a,o| fail}
|
159
|
+
env = env_with_params
|
160
|
+
setup_rack(lambda { |e| valid_response }).call(env)
|
161
|
+
env['rack.session']['warden.user.default.key'] = nil
|
162
|
+
env['warden'].user.should be_nil
|
163
|
+
end
|
164
|
+
|
165
|
+
it "should run filters in the given order" do
|
166
|
+
RAM.after_fetch{|u,a,o| a.env['warden.spec.order'] << 2}
|
167
|
+
RAM.after_fetch{|u,a,o| a.env['warden.spec.order'] << 3}
|
168
|
+
RAM.prepend_after_fetch{|u,a,o| a.env['warden.spec.order'] << 1}
|
169
|
+
app = lambda do |e|
|
170
|
+
e['warden.spec.order'] = []
|
171
|
+
e['rack.session']['warden.user.default.key'] = "Foo"
|
172
|
+
e['warden'].user
|
173
|
+
valid_response
|
174
|
+
end
|
175
|
+
env = env_with_params
|
176
|
+
setup_rack(app).call(env)
|
177
|
+
env['warden.spec.order'].should == [1,2,3]
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
|
183
|
+
describe "after_failed_fetch" do
|
184
|
+
before(:each) do
|
185
|
+
RAM = Warden::Manager unless defined?(RAM)
|
186
|
+
RAM._after_failed_fetch.clear
|
187
|
+
end
|
188
|
+
|
189
|
+
after(:each) do
|
190
|
+
RAM._after_failed_fetch.clear
|
191
|
+
end
|
192
|
+
|
193
|
+
it "should not be called when user is fetched" do
|
194
|
+
RAM.after_failed_fetch{|u,a,o| fail }
|
195
|
+
env = env_with_params
|
196
|
+
setup_rack(lambda { |e| valid_response }).call(env)
|
197
|
+
env['rack.session']['warden.user.default.key'] = "Foo"
|
198
|
+
env['warden'].user.should == "Foo"
|
199
|
+
end
|
200
|
+
|
201
|
+
it "should be called if fetched user is nil" do
|
202
|
+
calls = 0
|
203
|
+
RAM.after_failed_fetch{|u,a,o| calls += 1 }
|
204
|
+
env = env_with_params
|
205
|
+
setup_rack(lambda { |e| valid_response }).call(env)
|
206
|
+
env['warden'].user.should be_nil
|
207
|
+
calls.should == 1
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
describe "before_failure" do
|
212
|
+
before(:each) do
|
213
|
+
RAM = Warden::Manager unless defined?(RAM)
|
214
|
+
RAM._before_failure.clear
|
215
|
+
end
|
216
|
+
|
217
|
+
after(:each) do
|
218
|
+
RAM._before_failure.clear
|
219
|
+
end
|
220
|
+
|
221
|
+
it "should allow me to add a before_failure hook" do
|
222
|
+
RAM.before_failure{|env, opts| "foo"}
|
223
|
+
RAM._before_failure.should have(1).item
|
224
|
+
end
|
225
|
+
|
226
|
+
it "should allow me to add multiple before_failure hooks" do
|
227
|
+
RAM.before_failure{|env, opts| "foo"}
|
228
|
+
RAM.before_failure{|env, opts| "bar"}
|
229
|
+
RAM._before_failure.should have(2).items
|
230
|
+
end
|
231
|
+
|
232
|
+
it "should run each before_failure hooks before failing" do
|
233
|
+
RAM.before_failure{|e,o| e['warden.spec.before_failure.foo'] = "foo"}
|
234
|
+
RAM.before_failure{|e,o| e['warden.spec.before_failure.bar'] = "bar"}
|
235
|
+
app = lambda{|e| e['warden'].authenticate!(:failz); valid_response}
|
236
|
+
env = env_with_params
|
237
|
+
setup_rack(app).call(env)
|
238
|
+
env['warden.spec.before_failure.foo'].should == "foo"
|
239
|
+
env['warden.spec.before_failure.bar'].should == "bar"
|
240
|
+
end
|
241
|
+
|
242
|
+
it "should run filters in the given order" do
|
243
|
+
RAM.before_failure{|e,o| e['warden.spec.order'] << 2}
|
244
|
+
RAM.before_failure{|e,o| e['warden.spec.order'] << 3}
|
245
|
+
RAM.prepend_before_failure{|e,o| e['warden.spec.order'] << 1}
|
246
|
+
app = lambda do |e|
|
247
|
+
e['warden.spec.order'] = []
|
248
|
+
e['warden'].authenticate!(:failz)
|
249
|
+
valid_response
|
250
|
+
end
|
251
|
+
env = env_with_params
|
252
|
+
setup_rack(app).call(env)
|
253
|
+
env['warden.spec.order'].should == [1,2,3]
|
254
|
+
end
|
255
|
+
end
|
256
|
+
|
257
|
+
describe "before_logout" do
|
258
|
+
before(:each) do
|
259
|
+
RAM = Warden::Manager unless defined?(RAM)
|
260
|
+
RAM._before_logout.clear
|
261
|
+
end
|
262
|
+
|
263
|
+
after(:each) do
|
264
|
+
RAM._before_logout.clear
|
265
|
+
end
|
266
|
+
|
267
|
+
it "should allow me to add an before_logout hook" do
|
268
|
+
RAM.before_logout{|user, auth, scopes| "foo"}
|
269
|
+
RAM._before_logout.should have(1).item
|
270
|
+
end
|
271
|
+
|
272
|
+
it "should allow me to add multiple after_authentication hooks" do
|
273
|
+
RAM.before_logout{|u,a,o| "bar"}
|
274
|
+
RAM.before_logout{|u,a,o| "baz"}
|
275
|
+
RAM._before_logout.should have(2).items
|
276
|
+
end
|
277
|
+
|
278
|
+
it "should run each before_logout hook before logout is run" do
|
279
|
+
RAM.before_logout{|u,a,o| a.env['warden.spec.hook.lorem'] = "run lorem"}
|
280
|
+
RAM.before_logout{|u,a,o| a.env['warden.spec.hook.ipsum'] = "run ipsum"}
|
281
|
+
app = lambda{|e| e['warden'].authenticate(:pass); valid_response}
|
282
|
+
env = env_with_params
|
283
|
+
setup_rack(app).call(env)
|
284
|
+
env['warden'].logout
|
285
|
+
env['warden.spec.hook.lorem'].should == 'run lorem'
|
286
|
+
env['warden.spec.hook.ipsum'].should == 'run ipsum'
|
287
|
+
end
|
288
|
+
|
289
|
+
it "should run before_logout hook for a specified scope" do
|
290
|
+
RAM.before_logout(:scope => :scope1){|u,a,o| a.env["warden.spec.hook.a"] << :scope1 }
|
291
|
+
RAM.before_logout(:scope => [:scope2]){|u,a,o| a.env["warden.spec.hook.b"] << :scope2 }
|
292
|
+
|
293
|
+
app = lambda do |e|
|
294
|
+
e['warden'].authenticate(:pass, :scope => :scope1)
|
295
|
+
e['warden'].authenticate(:pass, :scope => :scope2)
|
296
|
+
valid_response
|
297
|
+
end
|
298
|
+
env = env_with_params
|
299
|
+
env["warden.spec.hook.a"] ||= []
|
300
|
+
env["warden.spec.hook.b"] ||= []
|
301
|
+
setup_rack(app).call(env)
|
302
|
+
|
303
|
+
env['warden'].logout(:scope1)
|
304
|
+
env['warden.spec.hook.a'].should == [:scope1]
|
305
|
+
env['warden.spec.hook.b'].should == []
|
306
|
+
|
307
|
+
env['warden'].logout(:scope2)
|
308
|
+
env['warden.spec.hook.a'].should == [:scope1]
|
309
|
+
env['warden.spec.hook.b'].should == [:scope2]
|
310
|
+
end
|
311
|
+
|
312
|
+
it "should run filters in the given order" do
|
313
|
+
RAM.before_logout{|u,a,o| a.env['warden.spec.order'] << 2}
|
314
|
+
RAM.before_logout{|u,a,o| a.env['warden.spec.order'] << 3}
|
315
|
+
RAM.prepend_before_logout{|u,a,o| a.env['warden.spec.order'] << 1}
|
316
|
+
app = lambda do |e|
|
317
|
+
e['warden.spec.order'] = []
|
318
|
+
e['warden'].authenticate(:pass)
|
319
|
+
e['warden'].logout
|
320
|
+
valid_response
|
321
|
+
end
|
322
|
+
env = env_with_params
|
323
|
+
setup_rack(app).call(env)
|
324
|
+
env['warden.spec.order'].should == [1,2,3]
|
325
|
+
end
|
326
|
+
end
|
327
|
+
|
328
|
+
describe "on_request" do
|
329
|
+
before(:each) do
|
330
|
+
@old_on_request = RAM._on_request.dup
|
331
|
+
RAM = Warden::Manager unless defined?(RAM)
|
332
|
+
RAM._on_request.clear
|
333
|
+
end
|
334
|
+
|
335
|
+
after(:each) do
|
336
|
+
RAM._on_request.clear
|
337
|
+
RAM._on_request.replace(@old_on_request)
|
338
|
+
end
|
339
|
+
|
340
|
+
it "should allow me to add an on_request hook" do
|
341
|
+
RAM.on_request{|proxy| "foo"}
|
342
|
+
RAM._on_request.should have(1).item
|
343
|
+
end
|
344
|
+
|
345
|
+
it "should allow me to add multiple on_request hooks" do
|
346
|
+
RAM.on_request{|proxy| "foo"}
|
347
|
+
RAM.on_request{|proxy| "bar"}
|
348
|
+
RAM._on_request.should have(2).items
|
349
|
+
end
|
350
|
+
|
351
|
+
it "should run each on_request hooks when initializing" do
|
352
|
+
RAM.on_request{|proxy| proxy.env['warden.spec.on_request.foo'] = "foo"}
|
353
|
+
RAM.on_request{|proxy| proxy.env['warden.spec.on_request.bar'] = "bar"}
|
354
|
+
app = lambda{|e| valid_response}
|
355
|
+
env = env_with_params
|
356
|
+
setup_rack(app).call(env)
|
357
|
+
env['warden.spec.on_request.foo'].should == "foo"
|
358
|
+
env['warden.spec.on_request.bar'].should == "bar"
|
359
|
+
end
|
360
|
+
|
361
|
+
it "should run filters in the given order" do
|
362
|
+
RAM.on_request{|proxy| proxy.env['warden.spec.order'] << 2}
|
363
|
+
RAM.on_request{|proxy| proxy.env['warden.spec.order'] << 3}
|
364
|
+
RAM.prepend_on_request{|proxy| proxy.env['warden.spec.order'] << 1}
|
365
|
+
app = lambda do |e|
|
366
|
+
valid_response
|
367
|
+
end
|
368
|
+
env = Rack::MockRequest.env_for("/", "warden.spec.order" => [])
|
369
|
+
setup_rack(app).call(env)
|
370
|
+
env['warden.spec.order'].should == [1,2,3]
|
371
|
+
end
|
372
|
+
end
|
373
|
+
end
|