warden 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,46 @@
1
+ require File.join(File.dirname(__FILE__), "..", 'spec_helper.rb')
2
+
3
+ describe Warden::Proxy::Errors do
4
+
5
+ before(:each) do
6
+ @errors = Warden::Proxy::Errors.new
7
+ end
8
+
9
+ it "should report that it is empty on first creation" do
10
+ @errors.empty?.should == true
11
+ end
12
+
13
+ it "should continue to report that it is empty even after being checked" do
14
+ @errors.on(:foo)
15
+ @errors.empty?.should == true
16
+ end
17
+
18
+ it "should add an error" do
19
+ @errors.add(:login, "Login or password incorrect")
20
+ @errors[:login].should == ["Login or password incorrect"]
21
+ end
22
+
23
+ it "should allow many errors to be added to the same field" do
24
+ @errors.add(:login, "bad 1")
25
+ @errors.add(:login, "bad 2")
26
+ @errors.on(:login).should == ["bad 1", "bad 2"]
27
+ end
28
+
29
+ it "should give the full messages for an error" do
30
+ @errors.add(:login, "login wrong")
31
+ @errors.add(:password, "password wrong")
32
+ ["password wrong", "login wrong"].each do |msg|
33
+ @errors.full_messages.should include(msg)
34
+ end
35
+ end
36
+
37
+ it "should return the error for a specific field / label" do
38
+ @errors.add(:login, "wrong")
39
+ @errors.on(:login).should == ["wrong"]
40
+ end
41
+
42
+ it "should return nil for a specific field if it's not been set" do
43
+ @errors.on(:not_there).should be_nil
44
+ end
45
+
46
+ end
@@ -0,0 +1,103 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe "standard authentication hooks" do
4
+
5
+ describe "after_set_user" do
6
+ before(:each) do
7
+ RAM = Warden::Manager unless defined?(RAM)
8
+ RAM._after_set_user.clear
9
+ end
10
+
11
+ after(:each) do
12
+ RAM._after_set_user.clear
13
+ end
14
+
15
+ it "should allow me to add an after_set_user hook" do
16
+ RAM.after_set_user do |user, auth, opts|
17
+ "boo"
18
+ end
19
+ RAM._after_set_user.should have(1).item
20
+ end
21
+
22
+ it "should allow me to add multiple after_set_user hooks" do
23
+ RAM.after_set_user{|user, auth, opts| "foo"}
24
+ RAM.after_set_user{|u,a| "bar"}
25
+ RAM._after_set_user.should have(2).items
26
+ end
27
+
28
+ it "should run each after_set_user hook after the user is set" do
29
+ RAM.after_set_user{|u,a,o| a.env['warden.spec.hook.foo'] = "run foo"}
30
+ RAM.after_set_user{|u,a,o| a.env['warden.spec.hook.bar'] = "run bar"}
31
+ app = lambda{|e| e['warden'].set_user("foo"); valid_response}
32
+ env = env_with_params
33
+ setup_rack(app).call(env)
34
+ env['warden.spec.hook.foo'].should == "run foo"
35
+ env['warden.spec.hook.bar'].should == "run bar"
36
+ end
37
+ end
38
+
39
+ describe "after_authenticate" do
40
+ before(:each) do
41
+ RAM = Warden::Manager unless defined?(RAM)
42
+ RAM._after_authentication.clear
43
+ end
44
+
45
+ after(:each) do
46
+ RAM._after_authentication.clear
47
+ end
48
+
49
+ it "should allow me to add an after_authetnication hook" do
50
+ RAM.after_authentication{|user, auth, opts| "foo"}
51
+ RAM._after_authentication.should have(1).item
52
+ end
53
+
54
+ it "should allow me to add multiple after_authetnication hooks" do
55
+ RAM.after_authentication{|u,a,o| "bar"}
56
+ RAM.after_authentication{|u,a,o| "baz"}
57
+ RAM._after_authentication.should have(2).items
58
+ end
59
+
60
+ it "should run each after_authentication hook after authentication is run" do
61
+ RAM.after_authentication{|u,a,o| a.env['warden.spec.hook.baz'] = "run baz"}
62
+ RAM.after_authentication{|u,a,o| a.env['warden.spec.hook.paz'] = "run paz"}
63
+ app = lambda{|e| e['warden'].authenticated?(:pass); valid_response}
64
+ env = env_with_params
65
+ setup_rack(app).call(env)
66
+ env['warden.spec.hook.baz'].should == 'run baz'
67
+ env['warden.spec.hook.paz'].should == 'run paz'
68
+ end
69
+ end
70
+
71
+ describe "before_failure" do
72
+ before(:each) do
73
+ RAM = Warden::Manager unless defined?(RAM)
74
+ RAM._before_failure.clear
75
+ end
76
+
77
+ after(:each) do
78
+ RAM._before_failure.clear
79
+ end
80
+
81
+ it "should allow me to add a before_failure hook" do
82
+ RAM.before_failure{|env, opts| "foo"}
83
+ RAM._before_failure.should have(1).item
84
+ end
85
+
86
+ it "should allow me to add multiple before_failure hooks" do
87
+ RAM.before_failure{|env, opts| "foo"}
88
+ RAM.before_failure{|env, opts| "bar"}
89
+ RAM._before_failure.should have(2).items
90
+ end
91
+
92
+ it "should run each before_failure hooks before failing" do
93
+ RAM.before_failure{|e,o| e['warden.spec.before_failure.foo'] = "foo"}
94
+ RAM.before_failure{|e,o| e['warden.spec.before_failure.bar'] = "bar"}
95
+ app = lambda{|e| e['warden'].authenticate!(:failz); valid_response}
96
+ env = env_with_params
97
+ setup_rack(app).call(env)
98
+ env['warden.spec.before_failure.foo'].should == "foo"
99
+ env['warden.spec.before_failure.bar'].should == "bar"
100
+ end
101
+ end
102
+
103
+ end
@@ -0,0 +1,158 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Warden::Manager do
4
+
5
+ it "should insert a Base object into the rack env" do
6
+ env = env_with_params
7
+ setup_rack(success_app).call(env)
8
+ env["warden"].should be_an_instance_of(Warden::Proxy)
9
+ end
10
+
11
+ describe "user storage" do
12
+ it "should take a user and store it in the provided session" do
13
+ session = {}
14
+ Warden::Manager._store_user("The User", session, "some_scope")
15
+ session["warden.user.some_scope.key"].should == "The User"
16
+ end
17
+ end
18
+
19
+ describe "thrown auth" do
20
+ before(:each) do
21
+ @basic_app = lambda{|env| [200,{'Content-Type' => 'text/plain'},'OK']}
22
+ @authd_app = lambda do |e|
23
+ if e['warden'].authenticated?
24
+ [200,{'Content-Type' => 'text/plain'},"OK"]
25
+ else
26
+ [401,{'Content-Type' => 'text/plain'},"You Fail"]
27
+ end
28
+ end
29
+ @env = Rack::MockRequest.
30
+ env_for('/', 'HTTP_VERSION' => '1.1', 'REQUEST_METHOD' => 'GET')
31
+ end # before(:each)
32
+
33
+ describe "Failure" do
34
+ it "should respond with a 401 response if the strategy fails authentication" do
35
+ env = env_with_params("/", :foo => "bar")
36
+ app = lambda do |env|
37
+ env['warden'].authenticate(:failz)
38
+ throw(:warden, :action => :unauthenticated)
39
+ end
40
+ result = setup_rack(app, :failure_app => @fail_app).call(env)
41
+ result.first.should == 401
42
+ end
43
+
44
+ it "should use the failure message given to the failure method" do
45
+ env = env_with_params("/", {})
46
+ app = lambda do |env|
47
+ env['warden'].authenticate(:failz)
48
+ throw(:warden, :action => :unauthenticated)
49
+ end
50
+ result = setup_rack(app, :failure_app => @fail_app).call(env)
51
+ result.last.should == ["You Fail!"]
52
+ end
53
+
54
+ it "should render the failure app when there's a failure" do
55
+ app = lambda do |e|
56
+ throw(:warden, :action => :unauthenticated) unless e['warden'].authenticated?(:failz)
57
+ end
58
+ fail_app = lambda do |e|
59
+ [401, {"Content-Type" => "text/plain"}, ["Failure App"]]
60
+ end
61
+ result = setup_rack(app, :failure_app => fail_app).call(env_with_params)
62
+ result.last.should == ["Failure App"]
63
+ end
64
+ end # failure
65
+
66
+ end
67
+
68
+ describe "integrated strategies" do
69
+ before(:each) do
70
+ RAS = Warden::Strategies unless defined?(RAS)
71
+ Warden::Strategies.clear!
72
+ @app = setup_rack do |env|
73
+ env['warden'].authenticate!(:foobar)
74
+ [200, {"Content-Type" => "text/plain"}, ["Foo Is A Winna"]]
75
+ end
76
+ end
77
+
78
+ describe "redirecting" do
79
+
80
+ it "should redirect with a message" do
81
+ RAS.add(:foobar) do
82
+ def authenticate!
83
+ redirect!("/foo/bar", {:foo => "bar"}, :message => "custom redirection message")
84
+ end
85
+ end
86
+ result = @app.call(env_with_params)
87
+ result[0].should == 302
88
+ result[1]["Location"].should == "/foo/bar?foo=bar"
89
+ result[2].should == ["custom redirection message"]
90
+ end
91
+
92
+ it "should redirect with a default message" do
93
+ RAS.add(:foobar) do
94
+ def authenticate!
95
+ redirect!("/foo/bar", {:foo => "bar"})
96
+ end
97
+ end
98
+ result = @app.call(env_with_params)
99
+ result[0].should == 302
100
+ result[1]['Location'].should == "/foo/bar?foo=bar"
101
+ result[2].should == ["You are being redirected to /foo/bar?foo=bar"]
102
+ end
103
+
104
+ it "should redirect with a permanent redirect" do
105
+ RAS.add(:foobar) do
106
+ def authenticate!
107
+ redirect!("/foo/bar", {}, :permanent => true)
108
+ end
109
+ end
110
+ result = @app.call(env_with_params)
111
+ result[0].should == 301
112
+ end
113
+ end
114
+
115
+ describe "failing" do
116
+ it "should fail according to the failure app" do
117
+ RAS.add(:foobar) do
118
+ def authenticate!
119
+ fail!
120
+ end
121
+ end
122
+ env = env_with_params
123
+ result = @app.call(env)
124
+ result[0].should == 401
125
+ result[2].should == ["You Fail!"]
126
+ env['PATH_INFO'].should == "/unauthenticated"
127
+ end
128
+ end # failing
129
+
130
+ describe "custom rack response" do
131
+ it "should return a custom rack response" do
132
+ RAS.add(:foobar) do
133
+ def authenticate!
134
+ custom!([523, {"Content-Type" => "text/plain", "Custom-Header" => "foo"}, ["Custom Stuff"]])
135
+ end
136
+ end
137
+ result = @app.call(env_with_params)
138
+ result[0].should == 523
139
+ result[1]["Custom-Header"].should == "foo"
140
+ result[2].should == ["Custom Stuff"]
141
+ end
142
+ end
143
+
144
+ describe "success" do
145
+ it "should pass through to the application when there is success" do
146
+ RAS.add(:foobar) do
147
+ def authenticate!
148
+ success!("A User")
149
+ end
150
+ end
151
+ env = env_with_params
152
+ result = @app.call(env)
153
+ result[0].should == 200
154
+ result[2].should == ["Foo Is A Winna"]
155
+ end
156
+ end
157
+ end # integrated strategies
158
+ end
@@ -0,0 +1,218 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Warden::Proxy do
4
+
5
+ before(:all) do
6
+ Dir[File.join(File.dirname(__FILE__), "strategies/**/*.rb")].each{|f| load f}
7
+ end
8
+
9
+ before(:each) do
10
+ @basic_app = lambda{|env| [200,{'Content-Type' => 'text/plain'},'OK']}
11
+ @authd_app = lambda do |e|
12
+ if e['warden'].authenticated?
13
+ [200,{'Content-Type' => 'text/plain'},"OK"]
14
+ else
15
+ [401,{'Content-Type' => 'text/plain'},"You Fail"]
16
+ end
17
+ end
18
+ @env = Rack::MockRequest.
19
+ env_for('/', 'HTTP_VERSION' => '1.1', 'REQUEST_METHOD' => 'GET')
20
+ end # before(:each)
21
+
22
+ describe "authentication" do
23
+
24
+ it "should not check the authentication if it is not checked" do
25
+ app = setup_rack(@basic_app)
26
+ app.call(@env).first.should == 200
27
+ end
28
+
29
+ it "should check the authentication if it is explicity checked" do
30
+ app = setup_rack(@authd_app)
31
+ app.call(@env).first.should == 401
32
+ end
33
+
34
+ it "should not allow the request if incorrect conditions are supplied" do
35
+ env = env_with_params("/", :foo => "bar")
36
+ app = setup_rack(@authd_app)
37
+ response = app.call(env)
38
+ response.first.should == 401
39
+ end
40
+
41
+ it "should allow the request if the correct conditions are supplied" do
42
+ env = env_with_params("/", :username => "fred", :password => "sekrit")
43
+ app = setup_rack(@authd_app)
44
+ resp = app.call(env)
45
+ resp.first.should == 200
46
+ end
47
+
48
+ describe "authenticate!" do
49
+
50
+ it "should allow authentication in my application" do
51
+ env = env_with_params('/', :username => "fred", :password => "sekrit")
52
+ app = lambda do |env|
53
+ env['warden'].should be_authenticated
54
+ env['warden.spec.strategies'].should == [:password]
55
+ end
56
+ end
57
+
58
+ it "should be false in my application" do
59
+ env = env_with_params("/", :foo => "bar")
60
+ app = lambda do |env|
61
+ env['warden'].should_not be_authenticated
62
+ env['warden.spec.strategies'].should == [:password]
63
+ valid_response
64
+ end
65
+ setup_rack(app).call(env)
66
+ end
67
+
68
+ it "should allow me to select which strategies I use in my appliction" do
69
+ env = env_with_params("/", :foo => "bar")
70
+ app = lambda do |env|
71
+ env['warden'].should_not be_authenticated(:failz)
72
+ env['warden.spec.strategies'].should == [:failz]
73
+ valid_response
74
+ end
75
+ setup_rack(app).call(env)
76
+ end
77
+
78
+ it "should allow me to get access to the user at warden.user." do
79
+ env = env_with_params("/")
80
+ app = lambda do |env|
81
+ env['warden'].should be_authenticated(:pass)
82
+ env['warden.spec.strategies'].should == [:pass]
83
+ valid_response
84
+ end
85
+ setup_rack(app).call(env)
86
+ end
87
+
88
+ it "should try multiple authentication strategies" do
89
+ env = env_with_params("/")
90
+ app = lambda do |env|
91
+ env['warden'].should be_authenticated(:password, :pass)
92
+ env['warden.spec.strategies'].should == [:password, :pass]
93
+ valid_response
94
+ end
95
+ setup_rack(app).call(env)
96
+ end
97
+
98
+ it "should look for an active user in the session with authenticate!" do
99
+ app = lambda do |env|
100
+ env['rack.session']["warden.user.default.key"] = "foo as a user"
101
+ env['warden'].authenticate!(:pass)
102
+ valid_response
103
+ end
104
+ env = env_with_params
105
+ setup_rack(app).call(env)
106
+ env['warden'].user.should == "foo as a user"
107
+ end
108
+
109
+ it "should look for an active user in the session with authenticate?" do
110
+ app = lambda do |env|
111
+ env['rack.session']['warden.user.foo_scope.key'] = "a foo user"
112
+ env['warden'].authenticated?(:pass, :scope => :foo_scope)
113
+ valid_response
114
+ end
115
+ env = env_with_params
116
+ setup_rack(app).call(env)
117
+ env['warden'].user(:foo_scope).should == "a foo user"
118
+ end
119
+
120
+ it "should login 2 different users from the session" do
121
+ app = lambda do |env|
122
+ env['rack.session']['warden.user.foo.key'] = 'foo user'
123
+ env['rack.session']['warden.user.bar.key'] = 'bar user'
124
+ env['warden'].authenticated?(:pass, :scope => :foo).should be_true
125
+ env['warden'].authenticated?(:pass, :scope => :bar).should be_true
126
+ env['warden'].authenticated?(:password).should be_false
127
+ valid_response
128
+ end
129
+ env = env_with_params
130
+ setup_rack(app).call(env)
131
+ env['warden'].user(:foo).should == 'foo user'
132
+ env['warden'].user(:bar).should == 'bar user'
133
+ env['warden'].user.should be_nil
134
+ end
135
+ end
136
+ end # describe "authentication"
137
+
138
+ describe "set user" do
139
+ it "should store the user into the session" do
140
+ env = env_with_params("/")
141
+ app = lambda do |env|
142
+ env['warden'].should be_authenticated(:pass)
143
+ env['warden'].user.should == "Valid User"
144
+ env['rack.session']["warden.user.default.key"].should == "Valid User"
145
+ valid_response
146
+ end
147
+ setup_rack(app).call(env)
148
+ end
149
+ end
150
+
151
+ describe "logout" do
152
+
153
+ before(:each) do
154
+ @env = env = env_with_params
155
+ @env['rack.session'] = {"warden.user.default.key" => "default key", "warden.user.foo.key" => "foo key", :foo => "bar"}
156
+ app = lambda do |e|
157
+ e['warden'].logout(env['warden.spec.which_logout'])
158
+ valid_response
159
+ end
160
+ @app = setup_rack(app)
161
+ end
162
+
163
+ it "should logout only the scoped foo user" do
164
+ @env['warden.spec.which_logout'] = :foo
165
+ @app.call(@env)
166
+ @env['rack.session']['warden.user.default.key'].should == "default key"
167
+ @env['rack.session']['warden.user.foo.key'].should be_nil
168
+ @env['rack.session'][:foo].should == "bar"
169
+ end
170
+
171
+ it "should logout only the scoped default user" do
172
+ @env['warden.spec.which_logout'] = :default
173
+ @app.call(@env)
174
+ @env['rack.session']['warden.user.default.key'].should be_nil
175
+ @env['rack.session']['warden.user.foo.key'].should == "foo key"
176
+ @env['rack.session'][:foo].should == "bar"
177
+ end
178
+
179
+ it "should clear the session when no argument is given to logout" do
180
+ @env['rack.session'].should_not be_nil
181
+ app = lambda do |e|
182
+ e['warden'].logout
183
+ valid_response
184
+ end
185
+ setup_rack(app).call(@env)
186
+ @env['rack.session'].should be_empty
187
+ end
188
+ end
189
+
190
+ describe "when all strategies are not valid?" do
191
+ it "should return false for authenticated when there are no valid? strategies" do
192
+ @env['rack.session'] = {}
193
+ app = lambda do |e|
194
+ e['warden'].authenticated?(:invalid).should be_false
195
+ end
196
+ setup_rack(app).call(@env)
197
+ end
198
+
199
+ it "should return nil for authenticate when there are no valid strategies" do
200
+ @env['rack.session'] = {}
201
+ app = lambda do |e|
202
+ e['warden'].authenticate(:invalid).should be_nil
203
+ end
204
+ setup_rack(app).call(@env)
205
+ end
206
+
207
+ it "should respond with a 401 when authenticate! cannot find any valid strategies" do
208
+ @env['rack.session'] = {}
209
+ app = lambda do |e|
210
+ e['warden'].authenticate!(:invalid)
211
+ end
212
+ result = setup_rack(app).call(@env)
213
+ result.first.should == 401
214
+ end
215
+
216
+ end
217
+
218
+ end