warden 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.rdoc ADDED
@@ -0,0 +1,15 @@
1
+ === Version 0.4.0 / 2009-10-12
2
+
3
+ * enhancements
4
+ * add Content-Type header to redirects (staugaard)
5
+ * Make scope available to strategies (josevalim)
6
+
7
+ * bug fixes
8
+ * Do not consume opts twice, otherwise just the first will parse the scope. (josevalim)
9
+
10
+ === Version 0.3.2 / 2009-09-15
11
+
12
+ * enhancements
13
+ * add a hook for plugins to specify how they can clear the whole section
14
+
15
+
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.5.0
@@ -1,19 +1,19 @@
1
1
  # encoding: utf-8
2
2
  module Warden
3
3
  class Manager
4
-
4
+
5
5
  class << self
6
6
  # A callback hook set to run every time after a user is set.
7
7
  # This will happen the first time the user is either authenticated, accessed or manually set
8
8
  # during a request. You can supply as many hooks as you like, and they will be run in order of decleration
9
- #
10
- # Parameters:
9
+ #
10
+ # Parameters:
11
11
  # <block> A block where you can set arbitrary logic to run every time a user is set
12
12
  # Block Parameters: |user, auth, opts|
13
13
  # user - The user object that is being set
14
- # auth - The raw authentication proxy object.
14
+ # auth - The raw authentication proxy object.
15
15
  # opts - any options passed into the set_user call includeing :scope
16
- #
16
+ #
17
17
  # Example:
18
18
  # Warden::Manager.after_set_user do |user,auth,opts|
19
19
  # scope = opts[:scope]
@@ -29,23 +29,23 @@ module Warden
29
29
  raise BlockNotGiven unless block_given?
30
30
  _after_set_user << block
31
31
  end
32
-
32
+
33
33
  # Provides access to the array of after_set_user blocks to run
34
34
  # :api: private
35
35
  def _after_set_user # :nodoc:
36
36
  @_after_set_user ||= []
37
37
  end
38
-
39
- # A callback hook set to run after the first authentiation of a session.
38
+
39
+ # A callback hook set to run after the first authentiation of a session.
40
40
  # This will only happenwhen the session is first authenticated
41
- #
41
+ #
42
42
  # Parameters:
43
43
  # <block> A block to contain logic for the callback
44
44
  # Block Parameters: |user, auth, opts|
45
45
  # user - The user object that is being set
46
- # auth - The raw authentication proxy object.
46
+ # auth - The raw authentication proxy object.
47
47
  # opts - any options passed into the authenticate call includeing :scope
48
- #
48
+ #
49
49
  # Example:
50
50
  #
51
51
  # Warden::Manager.after_authentication do |user, auth, opts|
@@ -57,18 +57,18 @@ module Warden
57
57
  raise BlockNotGiven unless block_given?
58
58
  _after_authentication << block
59
59
  end
60
-
60
+
61
61
  # Provides access to the array of after_authentication blocks
62
62
  # :api: private
63
63
  def _after_authentication
64
64
  @_after_authentication ||= []
65
65
  end
66
-
67
- # A callback that runs just prior to the failur application being called.
66
+
67
+ # A callback that runs just prior to the failur application being called.
68
68
  # This callback occurs after PATH_INFO has been modified for the failure (default /unauthenticated)
69
69
  # In this callback you can mutate the environment as required by the failure application
70
70
  # If a Rails controller were used for the failure_app for example, you would need to set request[:params][:action] = :unauthenticated
71
- #
71
+ #
72
72
  # Parameters:
73
73
  # <block> A block to contain logic for the callback
74
74
  # Block Parameters: |user, auth, opts|
@@ -81,46 +81,44 @@ module Warden
81
81
  # params[:action] = :unauthenticated
82
82
  # params[:warden_failure] = opts
83
83
  # end
84
- #
84
+ #
85
85
  # :api: public
86
86
  def before_failure(&block)
87
87
  _before_failure << block
88
88
  end
89
-
89
+
90
90
  # Provides access to the callback array for before_failure
91
91
  # :api: private
92
92
  def _before_failure
93
93
  @_before_failure ||= []
94
94
  end
95
-
96
- # A callback that runs just after to the failur application being called.
97
- # This callback is primarily included for Rails 2.3 since Rails 2.3 controllers are not pure Rack Applications
98
- # Return whatever you want to be returned for the actual rack response array
99
- #
95
+
96
+ # A callback that runs just prior to the logout of each scope.
97
+ #
100
98
  # Parameters:
101
99
  # <block> A block to contain logic for the callback
102
- # Block Parameters: |user, auth, opts|
103
- # result - The result of the rack application
104
- # opts - any options passed into the authenticate call includeing :scope
100
+ # Block Parameters: |user, auth, scope|
101
+ # user - The authenticated user for the current scope
102
+ # auth - The warden proxy object
103
+ # scope - current logout scope
105
104
  #
106
105
  # Example:
107
- # # Rails 2.3 after_failure
108
- # Warden::Manager.after_failure do |result|
109
- # result.to_a
106
+ # Warden::Manager.before_logout do |user, auth, scope|
107
+ # user.forget_me!
110
108
  # end
111
- #
109
+ #
112
110
  # :api: public
113
- def after_failure(&block)
114
- _after_failure << block
111
+ def before_logout(&block)
112
+ _before_logout << block
115
113
  end
116
-
117
- # Provides access to the callback array for after_failure
114
+
115
+ # Provides access to the callback array for before_logout
118
116
  # :api: private
119
- def _after_failure
120
- @_after_failure ||= []
117
+ def _before_logout
118
+ @_before_logout ||= []
121
119
  end
122
-
120
+
123
121
  end
124
-
122
+
125
123
  end # Manager
126
- end # Warden
124
+ end # Warden
data/lib/warden/proxy.rb CHANGED
@@ -36,9 +36,17 @@ module Warden
36
36
  # env['warden'].authenticated?(:admin)
37
37
  # :api: public
38
38
  def authenticated?(scope = :default)
39
- !raw_session["warden.user.#{scope}.key"].nil?
39
+ result = !raw_session["warden.user.#{scope}.key"].nil?
40
+ yield if block_given? && result
41
+ result
40
42
  end # authenticated?
41
43
 
44
+ def unauthenticated?(scope = :default)
45
+ result = !authenticated?(scope)
46
+ yield if block_given? && result
47
+ result
48
+ end
49
+
42
50
  # Run the authentiation strategies for the given strategies.
43
51
  # If there is already a user logged in for a given scope, the strategies are not run
44
52
  # This does not halt the flow of control and is a passive attempt to authenticate only
@@ -134,6 +142,12 @@ module Warden
134
142
  #
135
143
  # :api: public
136
144
  def logout(*scopes)
145
+ # Run before_logout hooks for each scoped user
146
+ @users.each do |scope, user|
147
+ next unless scopes.empty? || scopes.include?(scope)
148
+ Warden::Manager._before_logout.each { |hook| hook.call(user, self, scope) }
149
+ end
150
+
137
151
  if scopes.empty?
138
152
  reset_session!
139
153
  @users.clear
data/spec/spec_helper.rb CHANGED
@@ -4,13 +4,16 @@ require 'rack'
4
4
  $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
5
5
  require 'warden'
6
6
 
7
- Dir[File.join(File.dirname(__FILE__), "warden", "strategies", "**/*.rb")].each do |f|
8
- require f
9
- end
10
7
  Dir[File.join(File.dirname(__FILE__), "helpers", "**/*.rb")].each do |f|
11
8
  require f
12
9
  end
13
10
 
14
11
  Spec::Runner.configure do |config|
15
12
  config.include(Warden::Spec::Helpers)
13
+
14
+ def load_strategies
15
+ Dir[File.join(File.dirname(__FILE__), "warden", "strategies", "**/*.rb")].each do |f|
16
+ load f
17
+ end
18
+ end
16
19
  end
@@ -1,30 +1,34 @@
1
1
  require File.dirname(__FILE__) + '/../spec_helper'
2
2
 
3
3
  describe "standard authentication hooks" do
4
-
4
+
5
+ before(:all) do
6
+ load_strategies
7
+ end
8
+
5
9
  describe "after_set_user" do
6
10
  before(:each) do
7
11
  RAM = Warden::Manager unless defined?(RAM)
8
12
  RAM._after_set_user.clear
9
13
  end
10
-
14
+
11
15
  after(:each) do
12
16
  RAM._after_set_user.clear
13
17
  end
14
-
18
+
15
19
  it "should allow me to add an after_set_user hook" do
16
20
  RAM.after_set_user do |user, auth, opts|
17
21
  "boo"
18
22
  end
19
23
  RAM._after_set_user.should have(1).item
20
24
  end
21
-
25
+
22
26
  it "should allow me to add multiple after_set_user hooks" do
23
27
  RAM.after_set_user{|user, auth, opts| "foo"}
24
28
  RAM.after_set_user{|u,a| "bar"}
25
29
  RAM._after_set_user.should have(2).items
26
30
  end
27
-
31
+
28
32
  it "should run each after_set_user hook after the user is set" do
29
33
  RAM.after_set_user{|u,a,o| a.env['warden.spec.hook.foo'] = "run foo"}
30
34
  RAM.after_set_user{|u,a,o| a.env['warden.spec.hook.bar'] = "run bar"}
@@ -35,28 +39,28 @@ describe "standard authentication hooks" do
35
39
  env['warden.spec.hook.bar'].should == "run bar"
36
40
  end
37
41
  end
38
-
39
- describe "after_authenticate" do
42
+
43
+ describe "after_authentication" do
40
44
  before(:each) do
41
45
  RAM = Warden::Manager unless defined?(RAM)
42
46
  RAM._after_authentication.clear
43
47
  end
44
-
48
+
45
49
  after(:each) do
46
50
  RAM._after_authentication.clear
47
51
  end
48
-
49
- it "should allow me to add an after_authetnication hook" do
52
+
53
+ it "should allow me to add an after_authentication hook" do
50
54
  RAM.after_authentication{|user, auth, opts| "foo"}
51
55
  RAM._after_authentication.should have(1).item
52
56
  end
53
-
54
- it "should allow me to add multiple after_authetnication hooks" do
57
+
58
+ it "should allow me to add multiple after_authentication hooks" do
55
59
  RAM.after_authentication{|u,a,o| "bar"}
56
60
  RAM.after_authentication{|u,a,o| "baz"}
57
61
  RAM._after_authentication.should have(2).items
58
62
  end
59
-
63
+
60
64
  it "should run each after_authentication hook after authentication is run" do
61
65
  RAM.after_authentication{|u,a,o| a.env['warden.spec.hook.baz'] = "run baz"}
62
66
  RAM.after_authentication{|u,a,o| a.env['warden.spec.hook.paz'] = "run paz"}
@@ -67,28 +71,28 @@ describe "standard authentication hooks" do
67
71
  env['warden.spec.hook.paz'].should == 'run paz'
68
72
  end
69
73
  end
70
-
74
+
71
75
  describe "before_failure" do
72
76
  before(:each) do
73
77
  RAM = Warden::Manager unless defined?(RAM)
74
78
  RAM._before_failure.clear
75
79
  end
76
-
80
+
77
81
  after(:each) do
78
82
  RAM._before_failure.clear
79
83
  end
80
-
84
+
81
85
  it "should allow me to add a before_failure hook" do
82
86
  RAM.before_failure{|env, opts| "foo"}
83
87
  RAM._before_failure.should have(1).item
84
88
  end
85
-
89
+
86
90
  it "should allow me to add multiple before_failure hooks" do
87
91
  RAM.before_failure{|env, opts| "foo"}
88
92
  RAM.before_failure{|env, opts| "bar"}
89
93
  RAM._before_failure.should have(2).items
90
94
  end
91
-
95
+
92
96
  it "should run each before_failure hooks before failing" do
93
97
  RAM.before_failure{|e,o| e['warden.spec.before_failure.foo'] = "foo"}
94
98
  RAM.before_failure{|e,o| e['warden.spec.before_failure.bar'] = "bar"}
@@ -99,5 +103,55 @@ describe "standard authentication hooks" do
99
103
  env['warden.spec.before_failure.bar'].should == "bar"
100
104
  end
101
105
  end
102
-
103
- end
106
+
107
+ describe "before_logout" do
108
+ before(:each) do
109
+ RAM = Warden::Manager unless defined?(RAM)
110
+ RAM._before_logout.clear
111
+ end
112
+
113
+ after(:each) do
114
+ RAM._before_logout.clear
115
+ end
116
+
117
+ it "should allow me to add an before_logout hook" do
118
+ RAM.before_logout{|user, auth, scopes| "foo"}
119
+ RAM._before_logout.should have(1).item
120
+ end
121
+
122
+ it "should allow me to add multiple after_authetnication hooks" do
123
+ RAM.before_logout{|u,a,s| "bar"}
124
+ RAM.before_logout{|u,a,s| "baz"}
125
+ RAM._before_logout.should have(2).items
126
+ end
127
+
128
+ it "should run each before_logout hook before logout is run" do
129
+ RAM.before_logout{|u,a,s| a.env['warden.spec.hook.lorem'] = "run lorem"}
130
+ RAM.before_logout{|u,a,s| a.env['warden.spec.hook.ipsum'] = "run ipsum"}
131
+ app = lambda{|e| e['warden'].authenticate(:pass); valid_response}
132
+ env = env_with_params
133
+ setup_rack(app).call(env)
134
+ env['warden'].logout
135
+ env['warden.spec.hook.lorem'].should == 'run lorem'
136
+ env['warden.spec.hook.ipsum'].should == 'run ipsum'
137
+ end
138
+
139
+ it "should run before_logout hook on different scopes" do
140
+ RAM.before_logout{|u,a,s| a.env["warden.spec.hook.scope1"] = "run scope1" if s == :scope1}
141
+ RAM.before_logout{|u,a,s| a.env["warden.spec.hook.scope2"] = "run scope2" if s == :scope2}
142
+ app = lambda do |e|
143
+ e['warden'].authenticate(:pass, :scope => :scope1)
144
+ e['warden'].authenticate(:pass, :scope => :scope2)
145
+ valid_response
146
+ end
147
+ env = env_with_params
148
+ setup_rack(app).call(env)
149
+ env['warden'].logout(:scope1)
150
+ env['warden.spec.hook.scope1'].should == 'run scope1'
151
+ env['warden.spec.hook.scope2'].should == nil
152
+ env['warden'].logout(:scope2)
153
+ env['warden.spec.hook.scope2'].should == 'run scope2'
154
+ end
155
+ end
156
+
157
+ end
@@ -2,12 +2,16 @@ require File.dirname(__FILE__) + '/../spec_helper'
2
2
 
3
3
  describe Warden::Manager do
4
4
 
5
+ before(:all) do
6
+ load_strategies
7
+ end
8
+
5
9
  it "should insert a Base object into the rack env" do
6
10
  env = env_with_params
7
11
  setup_rack(success_app).call(env)
8
12
  env["warden"].should be_an_instance_of(Warden::Proxy)
9
13
  end
10
-
14
+
11
15
  describe "user storage" do
12
16
  it "should take a user and store it in the provided session" do
13
17
  session = {}
@@ -19,8 +23,8 @@ describe Warden::Manager do
19
23
  describe "thrown auth" do
20
24
  before(:each) do
21
25
  @basic_app = lambda{|env| [200,{'Content-Type' => 'text/plain'},'OK']}
22
- @authd_app = lambda do |e|
23
- if e['warden'].authenticated?
26
+ @authd_app = lambda do |e|
27
+ if e['warden'].authenticated?
24
28
  [200,{'Content-Type' => 'text/plain'},"OK"]
25
29
  else
26
30
  [401,{'Content-Type' => 'text/plain'},"Fail From The App"]
@@ -29,7 +33,7 @@ describe Warden::Manager do
29
33
  @env = Rack::MockRequest.
30
34
  env_for('/', 'HTTP_VERSION' => '1.1', 'REQUEST_METHOD' => 'GET')
31
35
  end # before(:each)
32
-
36
+
33
37
  describe "Failure" do
34
38
  it "should respond with a 401 response if the strategy fails authentication" do
35
39
  env = env_with_params("/", :foo => "bar")
@@ -40,7 +44,7 @@ describe Warden::Manager do
40
44
  result = setup_rack(app, :failure_app => @fail_app).call(env)
41
45
  result.first.should == 401
42
46
  end
43
-
47
+
44
48
  it "should use the failure message given to the failure method" do
45
49
  env = env_with_params("/", {})
46
50
  app = lambda do |env|
@@ -50,9 +54,9 @@ describe Warden::Manager do
50
54
  result = setup_rack(app, :failure_app => @fail_app).call(env)
51
55
  result.last.should == ["You Fail!"]
52
56
  end
53
-
57
+
54
58
  it "should render the failure app when there's a failure" do
55
- app = lambda do |e|
59
+ app = lambda do |e|
56
60
  throw(:warden, :action => :unauthenticated) unless e['warden'].authenticated?(:failz)
57
61
  end
58
62
  fail_app = lambda do |e|
@@ -62,21 +66,21 @@ describe Warden::Manager do
62
66
  result.last.should == ["Failure App"]
63
67
  end
64
68
  end # failure
65
-
69
+
66
70
  end
67
-
71
+
68
72
  describe "integrated strategies" do
69
73
  before(:each) do
70
74
  RAS = Warden::Strategies unless defined?(RAS)
71
75
  Warden::Strategies.clear!
72
76
  @app = setup_rack do |env|
73
77
  env['warden'].authenticate!(:foobar)
74
- [200, {"Content-Type" => "text/plain"}, ["Foo Is A Winna"]]
78
+ [200, {"Content-Type" => "text/plain"}, ["Foo Is A Winna"]]
75
79
  end
76
80
  end
77
81
 
78
82
  describe "redirecting" do
79
-
83
+
80
84
  it "should redirect with a message" do
81
85
  RAS.add(:foobar) do
82
86
  def authenticate!
@@ -88,7 +92,7 @@ describe Warden::Manager do
88
92
  result[1]["Location"].should == "/foo/bar?foo=bar"
89
93
  result[2].should == ["custom redirection message"]
90
94
  end
91
-
95
+
92
96
  it "should redirect with a default message" do
93
97
  RAS.add(:foobar) do
94
98
  def authenticate!
@@ -100,7 +104,7 @@ describe Warden::Manager do
100
104
  result[1]['Location'].should == "/foo/bar?foo=bar"
101
105
  result[2].should == ["You are being redirected to /foo/bar?foo=bar"]
102
106
  end
103
-
107
+
104
108
  it "should redirect with a permanent redirect" do
105
109
  RAS.add(:foobar) do
106
110
  def authenticate!
@@ -110,7 +114,7 @@ describe Warden::Manager do
110
114
  result = @app.call(env_with_params)
111
115
  result[0].should == 301
112
116
  end
113
-
117
+
114
118
  it "should redirect with a content type" do
115
119
  RAS.add(:foobar) do
116
120
  def authenticate!
@@ -122,7 +126,7 @@ describe Warden::Manager do
122
126
  result[1]["Location"].should == "/foo/bar?foo=bar"
123
127
  result[1]["Content-Type"].should == "text/xml"
124
128
  end
125
-
129
+
126
130
  it "should redirect with a default content type" do
127
131
  RAS.add(:foobar) do
128
132
  def authenticate!
@@ -135,7 +139,7 @@ describe Warden::Manager do
135
139
  result[1]["Content-Type"].should == "text/plain"
136
140
  end
137
141
  end
138
-
142
+
139
143
  describe "failing" do
140
144
  it "should fail according to the failure app" do
141
145
  RAS.add(:foobar) do
@@ -149,7 +153,7 @@ describe Warden::Manager do
149
153
  result[2].should == ["You Fail!"]
150
154
  env['PATH_INFO'].should == "/unauthenticated"
151
155
  end
152
-
156
+
153
157
  it "should allow you to customize the response" do
154
158
  app = lambda do |e|
155
159
  e['warden'].custom_failure!
@@ -160,7 +164,7 @@ describe Warden::Manager do
160
164
  result[0].should == 401
161
165
  result[2].should == ["Fail From The App"]
162
166
  end
163
-
167
+
164
168
  it "should render the failure application for a 401 if no custom_failure flag is set" do
165
169
  app = lambda do |e|
166
170
  [401,{'Content-Type' => 'text/plain'},["Fail From The App"]]
@@ -169,9 +173,9 @@ describe Warden::Manager do
169
173
  result[0].should == 401
170
174
  result[2].should == ["You Fail!"]
171
175
  end
172
-
176
+
173
177
  end # failing
174
-
178
+
175
179
  describe "custom rack response" do
176
180
  it "should return a custom rack response" do
177
181
  RAS.add(:foobar) do
@@ -185,7 +189,7 @@ describe Warden::Manager do
185
189
  result[2].should == ["Custom Stuff"]
186
190
  end
187
191
  end
188
-
192
+
189
193
  describe "success" do
190
194
  it "should pass through to the application when there is success" do
191
195
  RAS.add(:foobar) do
@@ -198,7 +202,7 @@ describe Warden::Manager do
198
202
  result[0].should == 200
199
203
  result[2].should == ["Foo Is A Winna"]
200
204
  end
201
- end
205
+ end
202
206
  end # integrated strategies
203
207
 
204
208
  end
@@ -3,7 +3,7 @@ require File.dirname(__FILE__) + '/../spec_helper'
3
3
  describe Warden::Proxy do
4
4
 
5
5
  before(:all) do
6
- Dir[File.join(File.dirname(__FILE__), "strategies/**/*.rb")].each{|f| load f}
6
+ load_strategies
7
7
  end
8
8
 
9
9
  before(:each) do
@@ -54,7 +54,9 @@ describe Warden::Proxy do
54
54
  env['warden'].authenticate
55
55
  env['warden'].should be_authenticated
56
56
  env['warden.spec.strategies'].should == [:password]
57
+ valid_response
57
58
  end
59
+ setup_rack(app).call(env)
58
60
  end
59
61
 
60
62
  it "should be false in my application" do
@@ -197,6 +199,7 @@ describe Warden::Proxy do
197
199
 
198
200
  before(:each) do
199
201
  @env['rack.session']['warden.user.default.key'] = "A Previous User"
202
+ @env['warden.spec.strategies'] = []
200
203
  end
201
204
 
202
205
  it "should take the user from the session when logged in" do
@@ -207,7 +210,14 @@ describe Warden::Proxy do
207
210
  setup_rack(app).call(@env)
208
211
  end
209
212
 
210
- it "should not run strategies when the user exists in the session"
213
+ it "should not run strategies when the user exists in the session" do
214
+ app = lambda do |env|
215
+ env['warden'].authenticate!(:pass)
216
+ valid_response
217
+ end
218
+ setup_rack(app).call(@env)
219
+ @env['warden.spec.strategies'].should_not include(:pass)
220
+ end
211
221
  end
212
222
  end
213
223
 
@@ -335,8 +345,148 @@ describe Warden::Proxy do
335
345
  result.first.should == 401
336
346
  end
337
347
 
338
- end
348
+ describe "authenticated?" do
349
+ describe "positive authentication" do
350
+ before do
351
+ @env['rack.session'] = {'warden.user.default.key' => 'defult_key'}
352
+ $captures = []
353
+ end
354
+
355
+ it "should return true when authenticated in the session" do
356
+ app = lambda do |e|
357
+ e['warden'].should be_authenticated
358
+ end
359
+ result = setup_rack(app).call(@env)
360
+ end
361
+
362
+ it "should yield to a block when the block is passed and authenticated" do
363
+ app = lambda do |e|
364
+ e['warden'].authenticated? do
365
+ $captures << :in_the_block
366
+ end
367
+ end
368
+ setup_rack(app).call(@env)
369
+ $captures.should == [:in_the_block]
370
+ end
371
+
372
+ it "should authenticate for a user in a different scope" do
373
+ @env['rack.session'] = {'warden.user.foo.key' => 'foo_key'}
374
+ app = lambda do |e|
375
+ e['warden'].authenticated?(:foo) do
376
+ $captures << :in_the_foo_block
377
+ end
378
+ end
379
+ setup_rack(app).call(@env)
380
+ $captures.should == [:in_the_foo_block]
381
+ end
382
+ end
383
+
384
+ describe "negative authentication" do
385
+ before do
386
+ @env['rack.session'] = {'warden.foo.default.key' => 'foo_key'}
387
+ $captures = []
388
+ end
389
+
390
+ it "should return false when authenticated in the session" do
391
+ app = lambda do |e|
392
+ e['warden'].should_not be_authenticated
393
+ end
394
+ result = setup_rack(app).call(@env)
395
+ end
396
+
397
+ it "should not yield to a block when the block is passed and authenticated" do
398
+ app = lambda do |e|
399
+ e['warden'].authenticated? do
400
+ $captures << :in_the_block
401
+ end
402
+ end
403
+ setup_rack(app).call(@env)
404
+ $captures.should == []
405
+ end
339
406
 
407
+ it "should not yield for a user in a different scope" do
408
+ app = lambda do |e|
409
+ e['warden'].authenticated?(:bar) do
410
+ $captures << :in_the_bar_block
411
+ end
412
+ end
413
+ setup_rack(app).call(@env)
414
+ $captures.should == []
415
+ end
416
+ end
417
+ end
418
+
419
+
420
+ describe "unauthenticated?" do
421
+ describe "negative unauthentication" do
422
+ before do
423
+ @env['rack.session'] = {'warden.user.default.key' => 'defult_key'}
424
+ $captures = []
425
+ end
340
426
 
427
+ it "should return false when authenticated in the session" do
428
+ app = lambda do |e|
429
+ e['warden'].should_not be_unauthenticated
430
+ end
431
+ result = setup_rack(app).call(@env)
432
+ end
433
+
434
+ it "should not yield to a block when the block is passed and authenticated" do
435
+ app = lambda do |e|
436
+ e['warden'].unauthenticated? do
437
+ $captures << :in_the_block
438
+ end
439
+ end
440
+ setup_rack(app).call(@env)
441
+ $captures.should == []
442
+ end
443
+
444
+ it "should not yield to the block for a user in a different scope" do
445
+ @env['rack.session'] = {'warden.user.foo.key' => 'foo_key'}
446
+ app = lambda do |e|
447
+ e['warden'].unauthenticated?(:foo) do
448
+ $captures << :in_the_foo_block
449
+ end
450
+ end
451
+ setup_rack(app).call(@env)
452
+ $captures.should == []
453
+ end
454
+ end
455
+
456
+ describe "positive unauthentication" do
457
+ before do
458
+ @env['rack.session'] = {'warden.foo.default.key' => 'foo_key'}
459
+ $captures = []
460
+ end
461
+
462
+ it "should return false when unauthenticated in the session" do
463
+ app = lambda do |e|
464
+ e['warden'].should be_unauthenticated
465
+ end
466
+ result = setup_rack(app).call(@env)
467
+ end
468
+
469
+ it "should yield to a block when the block is passed and authenticated" do
470
+ app = lambda do |e|
471
+ e['warden'].unauthenticated? do
472
+ $captures << :in_the_block
473
+ end
474
+ end
475
+ setup_rack(app).call(@env)
476
+ $captures.should == [:in_the_block]
477
+ end
478
+
479
+ it "should yield for a user in a different scope" do
480
+ app = lambda do |e|
481
+ e['warden'].unauthenticated?(:bar) do
482
+ $captures << :in_the_bar_block
483
+ end
484
+ end
485
+ setup_rack(app).call(@env)
486
+ $captures.should == [:in_the_bar_block]
487
+ end
488
+ end
489
+ end
490
+ end
341
491
 
342
492
  end
data/warden.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{warden}
8
- s.version = "0.4.0"
8
+ s.version = "0.5.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Daniel Neighman"]
12
- s.date = %q{2009-10-12}
12
+ s.date = %q{2009-10-21}
13
13
  s.email = %q{has.sox@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE",
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
17
17
  ]
18
18
  s.files = [
19
19
  ".gitignore",
20
+ "History.rdoc",
20
21
  "LICENSE",
21
22
  "README.textile",
22
23
  "Rakefile",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: warden
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Neighman
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-12 00:00:00 +11:00
12
+ date: 2009-10-21 00:00:00 +11:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -33,6 +33,7 @@ extra_rdoc_files:
33
33
  - README.textile
34
34
  files:
35
35
  - .gitignore
36
+ - History.rdoc
36
37
  - LICENSE
37
38
  - README.textile
38
39
  - Rakefile