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.
Files changed (42) hide show
  1. data/Gemfile +11 -0
  2. data/History.rdoc +150 -0
  3. data/LICENSE +20 -0
  4. data/README.textile +9 -0
  5. data/Rakefile +12 -0
  6. data/lib/loyal_warden.rb +2 -0
  7. data/lib/warden.rb +45 -0
  8. data/lib/warden/config.rb +112 -0
  9. data/lib/warden/errors.rb +66 -0
  10. data/lib/warden/hooks.rb +211 -0
  11. data/lib/warden/manager.rb +136 -0
  12. data/lib/warden/mixins/common.rb +44 -0
  13. data/lib/warden/proxy.rb +371 -0
  14. data/lib/warden/session_serializer.rb +52 -0
  15. data/lib/warden/strategies.rb +47 -0
  16. data/lib/warden/strategies/base.rb +175 -0
  17. data/lib/warden/test/helpers.rb +36 -0
  18. data/lib/warden/test/warden_helpers.rb +43 -0
  19. data/lib/warden/version.rb +4 -0
  20. data/loyal_warden.gemspec +26 -0
  21. data/spec/helpers/request_helper.rb +51 -0
  22. data/spec/helpers/strategies/failz.rb +8 -0
  23. data/spec/helpers/strategies/invalid.rb +8 -0
  24. data/spec/helpers/strategies/pass.rb +8 -0
  25. data/spec/helpers/strategies/pass_with_message.rb +8 -0
  26. data/spec/helpers/strategies/password.rb +13 -0
  27. data/spec/helpers/strategies/single.rb +12 -0
  28. data/spec/spec_helper.rb +24 -0
  29. data/spec/warden/authenticated_data_store_spec.rb +114 -0
  30. data/spec/warden/config_spec.rb +48 -0
  31. data/spec/warden/errors_spec.rb +47 -0
  32. data/spec/warden/hooks_spec.rb +373 -0
  33. data/spec/warden/manager_spec.rb +316 -0
  34. data/spec/warden/proxy_spec.rb +1041 -0
  35. data/spec/warden/scoped_session_serializer.rb +123 -0
  36. data/spec/warden/session_serializer_spec.rb +53 -0
  37. data/spec/warden/strategies/base_spec.rb +313 -0
  38. data/spec/warden/strategies_spec.rb +93 -0
  39. data/spec/warden/test/helpers_spec.rb +93 -0
  40. data/spec/warden/test/test_mode_spec.rb +76 -0
  41. data/warden.gemspec +24 -0
  42. metadata +105 -0
@@ -0,0 +1,123 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Warden::Manager do
5
+ before(:each) do
6
+ @env = env_with_params
7
+ @env['rack.session'] ||= {}
8
+ Warden::Manager.serialize_from_session { |k| k }
9
+ Warden::Manager.serialize_into_session { |u| u }
10
+ begin
11
+ Warden::SessionSerializer.send :remove_method, :admin_serialize
12
+ rescue
13
+ end
14
+ begin
15
+ Warden::SessionSerializer.send :remove_method, :admin_deserialize
16
+ rescue
17
+ end
18
+ end
19
+ after(:each) do
20
+ Warden::Manager.serialize_from_session { |k| k }
21
+ Warden::Manager.serialize_into_session { |u| u }
22
+ begin
23
+ Warden::SessionSerializer.send :remove_method, :admin_deserialize
24
+ Warden::SessionSerializer.send :remove_method, :admin_serialize
25
+ rescue
26
+ end
27
+ end
28
+
29
+ def serializer_respond_to?(name)
30
+ Warden::SessionSerializer.new(@env).respond_to? name
31
+ end
32
+
33
+ it "should respond to :serialize" do
34
+ serializer_respond_to?(:serialize).should == true
35
+ end
36
+
37
+ it "should respond to :deserialize" do
38
+ serializer_respond_to?(:deserialize).should == true
39
+ end
40
+
41
+ it "should respond to {scope}_deserialize if Manager.serialize_from_session is called with scope" do
42
+ Rack::Builder.new do
43
+ Warden::Manager.serialize_from_session ( :admin ) { |n| n }
44
+ end
45
+ serializer_respond_to?(:admin_deserialize).should == true
46
+ end
47
+
48
+ it "should respond to {scope}_serialize if Manager.serialize_into_session is called with scope" do
49
+ Rack::Builder.new do
50
+ Warden::Manager.serialize_into_session(:admin) { |n| n }
51
+ end
52
+ serializer_respond_to?(:admin_serialize).should == true
53
+ end
54
+
55
+ def initialize_with_scope(scope, &block)
56
+ Rack::Builder.new do
57
+ Warden::Manager.serialize_into_session(scope, &block)
58
+ end
59
+ end
60
+
61
+ it "should execute serialize if no {scope}_serialize is present" do
62
+ serialized_object = nil
63
+ initialize_with_scope(nil) do |user|
64
+ serialized_object = user
65
+ user
66
+ end
67
+ serializer = Warden::SessionSerializer.new(@env)
68
+ serializer.store("user", :admin)
69
+ serialized_object.should == "user"
70
+ end
71
+
72
+ it "should not have a {scope}_serialize by default" do
73
+ serializer_respond_to?(:admin_serialize).should == false
74
+ end
75
+
76
+ it "should execute {scope}_serialize when calling store with a scope" do
77
+ serialized_object = nil
78
+ initialize_with_scope(:admin) do |user|
79
+ serialized_object = user
80
+ user
81
+ end
82
+
83
+ serializer = Warden::SessionSerializer.new(@env)
84
+ serializer.store("user", :admin)
85
+ serialized_object.should == "user"
86
+ end
87
+
88
+
89
+ it "should execute {scope}_deserialize when calling store with a scope" do
90
+ serialized_object = nil
91
+
92
+ Rack::Builder.new do
93
+ Warden::Manager.serialize_from_session(:admin) do |key|
94
+ serialized_object = key
95
+ key
96
+ end
97
+ end
98
+
99
+ serializer = Warden::SessionSerializer.new(@env)
100
+ @env['rack.session'][serializer.key_for(:admin)] = "test"
101
+ serializer.fetch(:admin)
102
+
103
+ serialized_object.should == "test"
104
+ end
105
+
106
+ it "should execute deserialize if {scope}_deserialize is not present" do
107
+ serialized_object = nil
108
+
109
+ Rack::Builder.new do
110
+ Warden::Manager.serialize_from_session do |key|
111
+ serialized_object = key
112
+ key
113
+ end
114
+ end
115
+
116
+ serializer = Warden::SessionSerializer.new(@env)
117
+ @env['rack.session'][serializer.key_for(:admin)] = "test"
118
+ serializer.fetch(:admin)
119
+
120
+ serialized_object.should == "test"
121
+ end
122
+
123
+ end
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Warden::SessionSerializer do
5
+ before(:each) do
6
+ @env = env_with_params
7
+ @env['rack.session'] ||= {}
8
+ @session = Warden::SessionSerializer.new(@env)
9
+ end
10
+
11
+ it "should store data for the default scope" do
12
+ @session.store("user", :default)
13
+ @env['rack.session'].should == { "warden.user.default.key"=>"user" }
14
+ end
15
+
16
+ it "should check if a data is stored or not" do
17
+ @session.should_not be_stored(:default)
18
+ @session.store("user", :default)
19
+ @session.should be_stored(:default)
20
+ end
21
+
22
+ it "should load an user from store" do
23
+ @session.fetch(:default).should be_nil
24
+ @session.store("user", :default)
25
+ @session.fetch(:default).should == "user"
26
+ end
27
+
28
+ it "should store data based on the scope" do
29
+ @session.store("user", :default)
30
+ @session.fetch(:default).should == "user"
31
+ @session.fetch(:another).should be_nil
32
+ end
33
+
34
+ it "should delete data from store" do
35
+ @session.store("user", :default)
36
+ @session.fetch(:default).should == "user"
37
+ @session.delete(:default)
38
+ @session.fetch(:default).should be_nil
39
+ end
40
+
41
+ it "should delete information from store if user cannot be retrieved" do
42
+ @session.store("user", :default)
43
+ @env['rack.session'].should have_key("warden.user.default.key")
44
+ @session.instance_eval "def deserialize(key); nil; end"
45
+ @session.fetch(:default)
46
+ @env['rack.session'].should_not have_key("warden.user.default.key")
47
+ end
48
+
49
+ it "should support a nil session store" do
50
+ @env['rack.session'] = nil
51
+ @session.fetch(:default).should be_nil
52
+ end
53
+ end
@@ -0,0 +1,313 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Warden::Strategies::Base do
5
+
6
+ before(:each) do
7
+ RAS = Warden::Strategies unless defined?(RAS)
8
+ Warden::Strategies.clear!
9
+ end
10
+
11
+ describe "headers" do
12
+ it "should have headers" do
13
+ Warden::Strategies.add(:foo) do
14
+ def authenticate!
15
+ headers("foo" => "bar")
16
+ end
17
+ end
18
+ strategy = Warden::Strategies[:foo].new(env_with_params)
19
+ strategy._run!
20
+ strategy.headers["foo"].should == "bar"
21
+ end
22
+
23
+ it "should allow us to clear the headers" do
24
+ Warden::Strategies.add(:foo) do
25
+ def authenticate!
26
+ headers("foo" => "bar")
27
+ end
28
+ end
29
+ strategy = Warden::Strategies[:foo].new(env_with_params)
30
+ strategy._run!
31
+ strategy.headers["foo"].should == "bar"
32
+ strategy.headers.clear
33
+ strategy.headers.should be_empty
34
+ end
35
+ end
36
+
37
+ it "should have a user object" do
38
+ RAS.add(:foobar) do
39
+ def authenticate!
40
+ success!("foo")
41
+ end
42
+ end
43
+ strategy = RAS[:foobar].new(env_with_params)
44
+ strategy._run!
45
+ strategy.user.should == "foo"
46
+ end
47
+
48
+ it "should be performed after run" do
49
+ RAS.add(:foobar) do
50
+ def authenticate!; end
51
+ end
52
+ strategy = RAS[:foobar].new(env_with_params)
53
+ strategy.should_not be_performed
54
+ strategy._run!
55
+ strategy.should be_performed
56
+ strategy.clear!
57
+ strategy.should_not be_performed
58
+ end
59
+
60
+ it "should set the scope" do
61
+ RAS.add(:foobar) do
62
+ def authenticate!
63
+ self.scope.should == :user
64
+ end
65
+ end
66
+ strategy = RAS[:foobar].new(env_with_params, :user)
67
+ end
68
+
69
+ it "should allow you to set a message" do
70
+ RAS.add(:foobar) do
71
+ def authenticate!
72
+ self.message = "foo message"
73
+ end
74
+ end
75
+ strategy = RAS[:foobar].new(env_with_params)
76
+ strategy._run!
77
+ strategy.message.should == "foo message"
78
+ end
79
+
80
+ it "should provide access to the errors" do
81
+ RAS.add(:foobar) do
82
+ def authenticate!
83
+ errors.add(:foo, "foo has an error")
84
+ end
85
+ end
86
+ env = env_with_params
87
+ env['warden'] = Warden::Proxy.new(env, Warden::Manager.new({}))
88
+ strategy = RAS[:foobar].new(env)
89
+ strategy._run!
90
+ strategy.errors.on(:foo).should == ["foo has an error"]
91
+ end
92
+
93
+ describe "halting" do
94
+ it "should allow you to halt a strategy" do
95
+ RAS.add(:foobar) do
96
+ def authenticate!
97
+ halt!
98
+ end
99
+ end
100
+ str = RAS[:foobar].new(env_with_params)
101
+ str._run!
102
+ str.should be_halted
103
+ end
104
+
105
+ it "should not be halted if halt was not called" do
106
+ RAS.add(:foobar) do
107
+ def authenticate!
108
+ "foo"
109
+ end
110
+ end
111
+ str = RAS[:foobar].new(env_with_params)
112
+ str._run!
113
+ str.should_not be_halted
114
+ end
115
+
116
+ end
117
+
118
+ describe "pass" do
119
+ it "should allow you to pass" do
120
+ RAS.add(:foobar) do
121
+ def authenticate!
122
+ pass
123
+ end
124
+ end
125
+ str = RAS[:foobar].new(env_with_params)
126
+ str._run!
127
+ str.should_not be_halted
128
+ str.user.should be_nil
129
+ end
130
+ end
131
+
132
+ describe "redirect" do
133
+ it "should allow you to set a redirection" do
134
+ RAS.add(:foobar) do
135
+ def authenticate!
136
+ redirect!("/foo/bar")
137
+ end
138
+ end
139
+ str = RAS[:foobar].new(env_with_params)
140
+ str._run!
141
+ str.user.should be_nil
142
+ end
143
+
144
+ it "should mark the strategy as halted when redirecting" do
145
+ RAS.add(:foobar) do
146
+ def authenticate!
147
+ redirect!("/foo/bar")
148
+ end
149
+ end
150
+ str = RAS[:foobar].new(env_with_params)
151
+ str._run!
152
+ str.should be_halted
153
+ end
154
+
155
+ it "should escape redirected url parameters" do
156
+ RAS.add(:foobar) do
157
+ def authenticate!
158
+ redirect!("/foo/bar", :foo => "bar")
159
+ end
160
+ end
161
+ str = RAS[:foobar].new(env_with_params)
162
+ str._run!
163
+ str.headers["Location"].should == "/foo/bar?foo=bar"
164
+ end
165
+
166
+ it "should allow you to set a message" do
167
+ RAS.add(:foobar) do
168
+ def authenticate!
169
+ redirect!("/foo/bar", {:foo => "bar"}, :message => "You are being redirected foo")
170
+ end
171
+ end
172
+ str = RAS[:foobar].new(env_with_params)
173
+ str._run!
174
+ str.headers["Location"].should == "/foo/bar?foo=bar"
175
+ str.message.should == "You are being redirected foo"
176
+ end
177
+
178
+ it "should set the action as :redirect" do
179
+ RAS.add(:foobar) do
180
+ def authenticate!
181
+ redirect!("/foo/bar", {:foo => "bar"}, :message => "foo")
182
+ end
183
+ end
184
+ str = RAS[:foobar].new(env_with_params)
185
+ str._run!
186
+ str.result.should == :redirect
187
+ end
188
+ end
189
+
190
+ describe "failure" do
191
+
192
+ before(:each) do
193
+ RAS.add(:hard_fail) do
194
+ def authenticate!
195
+ fail!("You are not cool enough")
196
+ end
197
+ end
198
+
199
+ RAS.add(:soft_fail) do
200
+ def authenticate!
201
+ fail("You are too soft")
202
+ end
203
+ end
204
+ @hard = RAS[:hard_fail].new(env_with_params)
205
+ @soft = RAS[:soft_fail].new(env_with_params)
206
+ end
207
+
208
+ it "should allow you to fail hard" do
209
+ @hard._run!
210
+ @hard.user.should be_nil
211
+ end
212
+
213
+ it "should halt the strategies when failing hard" do
214
+ @hard._run!
215
+ @hard.should be_halted
216
+ end
217
+
218
+ it "should allow you to set a message when failing hard" do
219
+ @hard._run!
220
+ @hard.message.should == "You are not cool enough"
221
+ end
222
+
223
+ it "should set the action as :failure when failing hard" do
224
+ @hard._run!
225
+ @hard.result.should == :failure
226
+ end
227
+
228
+ it "should allow you to fail soft" do
229
+ @soft._run!
230
+ @soft.user.should be_nil
231
+ end
232
+
233
+ it "should not halt the strategies when failing soft" do
234
+ @soft._run!
235
+ @soft.should_not be_halted
236
+ end
237
+
238
+ it "should allow you to set a message when failing soft" do
239
+ @soft._run!
240
+ @soft.message.should == "You are too soft"
241
+ end
242
+
243
+ it "should set the action as :failure when failing soft" do
244
+ @soft._run!
245
+ @soft.result.should == :failure
246
+ end
247
+ end
248
+
249
+ describe "success" do
250
+ before(:each) do
251
+ RAS.add(:foobar) do
252
+ def authenticate!
253
+ success!("Foo User", "Welcome to the club!")
254
+ end
255
+ end
256
+ @str = RAS[:foobar].new(env_with_params)
257
+ end
258
+
259
+ it "should allow you to succeed" do
260
+ @str._run!
261
+ end
262
+
263
+ it "should be authenticated after success" do
264
+ @str._run!
265
+ @str.user.should_not be_nil
266
+ end
267
+
268
+ it "should allow you to set a message when succeeding" do
269
+ @str._run!
270
+ @str.message.should == "Welcome to the club!"
271
+ end
272
+
273
+ it "should store the user" do
274
+ @str._run!
275
+ @str.user.should == "Foo User"
276
+ end
277
+
278
+ it "should set the action as :success" do
279
+ @str._run!
280
+ @str.result.should == :success
281
+ end
282
+ end
283
+
284
+ describe "custom response" do
285
+ before(:each) do
286
+ RAS.add(:foobar) do
287
+ def authenticate!
288
+ custom!([521, {"foo" => "bar"}, ["BAD"]])
289
+ end
290
+ end
291
+ @str = RAS[:foobar].new(env_with_params)
292
+ @str._run!
293
+ end
294
+
295
+ it "should allow me to set a custom rack response" do
296
+ @str.user.should be_nil
297
+ end
298
+
299
+ it "should halt the strategy" do
300
+ @str.should be_halted
301
+ end
302
+
303
+ it "should provide access to the custom rack response" do
304
+ @str.custom_response.should == [521, {"foo" => "bar"}, ["BAD"]]
305
+ end
306
+
307
+ it "should set the action as :custom" do
308
+ @str._run!
309
+ @str.result.should == :custom
310
+ end
311
+ end
312
+
313
+ end