warden 1.2.5 → 1.2.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +5 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +7 -0
  5. data/{History.rdoc → CHANGELOG.md} +13 -3
  6. data/Gemfile +2 -1
  7. data/Gemfile.lock +40 -0
  8. data/LICENSE +1 -1
  9. data/README.md +18 -0
  10. data/Rakefile +4 -8
  11. data/lib/warden/config.rb +1 -0
  12. data/lib/warden/errors.rb +2 -1
  13. data/lib/warden/hooks.rb +1 -0
  14. data/lib/warden/manager.rb +2 -1
  15. data/lib/warden/mixins/common.rb +1 -0
  16. data/lib/warden/proxy.rb +15 -3
  17. data/lib/warden/session_serializer.rb +1 -0
  18. data/lib/warden/strategies/base.rb +2 -1
  19. data/lib/warden/strategies.rb +1 -0
  20. data/lib/warden/test/helpers.rb +2 -56
  21. data/lib/warden/test/mock.rb +69 -0
  22. data/lib/warden/test/warden_helpers.rb +1 -0
  23. data/lib/warden/version.rb +2 -1
  24. data/lib/warden.rb +2 -0
  25. data/warden.gemspec +21 -18
  26. metadata +19 -33
  27. data/README.textile +0 -9
  28. data/spec/helpers/request_helper.rb +0 -51
  29. data/spec/helpers/strategies/fail_with_user.rb +0 -10
  30. data/spec/helpers/strategies/failz.rb +0 -8
  31. data/spec/helpers/strategies/invalid.rb +0 -8
  32. data/spec/helpers/strategies/pass.rb +0 -8
  33. data/spec/helpers/strategies/pass_with_message.rb +0 -8
  34. data/spec/helpers/strategies/password.rb +0 -13
  35. data/spec/helpers/strategies/single.rb +0 -12
  36. data/spec/spec_helper.rb +0 -24
  37. data/spec/warden/authenticated_data_store_spec.rb +0 -114
  38. data/spec/warden/config_spec.rb +0 -48
  39. data/spec/warden/errors_spec.rb +0 -47
  40. data/spec/warden/hooks_spec.rb +0 -373
  41. data/spec/warden/manager_spec.rb +0 -340
  42. data/spec/warden/proxy_spec.rb +0 -1050
  43. data/spec/warden/scoped_session_serializer.rb +0 -123
  44. data/spec/warden/session_serializer_spec.rb +0 -53
  45. data/spec/warden/strategies/base_spec.rb +0 -313
  46. data/spec/warden/strategies_spec.rb +0 -94
  47. data/spec/warden/test/helpers_spec.rb +0 -101
  48. data/spec/warden/test/test_mode_spec.rb +0 -75
@@ -1,1050 +0,0 @@
1
- # encoding: utf-8
2
- require 'spec_helper'
3
-
4
- describe Warden::Proxy do
5
- before(:all) do
6
- load_strategies
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
- e['warden'].authenticate
13
- if e['warden'].authenticated?
14
- [200,{'Content-Type' => 'text/plain'},"OK"]
15
- else
16
- [401,{'Content-Type' => 'text/plain'},"You Fail"]
17
- end
18
- end
19
- @env = env_with_params("/")
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
- expect(app.call(@env).first).to eq(200)
27
- end
28
-
29
- it "should check the authentication if it is explicitly checked" do
30
- app = setup_rack(@authd_app)
31
- expect(app.call(@env).first).to eq(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
- expect(response.first).to eq(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
- expect(resp.first).to eq(200)
46
- end
47
-
48
- it "should allow authentication in my application" do
49
- env = env_with_params('/', :username => "fred", :password => "sekrit")
50
- app = lambda do |_env|
51
- _env['warden'].authenticate
52
- expect(_env['warden']).to be_authenticated
53
- expect(_env['warden.spec.strategies']).to eq([:password])
54
- valid_response
55
- end
56
- setup_rack(app).call(env)
57
- end
58
-
59
- it "should allow me to select which strategies I use in my application" do
60
- env = env_with_params("/", :foo => "bar")
61
- app = lambda do |_env|
62
- _env['warden'].authenticate(:failz)
63
- expect(_env['warden']).not_to be_authenticated
64
- expect(_env['warden.spec.strategies']).to eq([:failz])
65
- valid_response
66
- end
67
- setup_rack(app).call(env)
68
- end
69
-
70
- it "should raise error on missing strategies" do
71
- app = lambda do |env|
72
- env['warden'].authenticate(:unknown)
73
- end
74
- expect {
75
- setup_rack(app).call(@env)
76
- }.to raise_error(RuntimeError, "Invalid strategy unknown")
77
- end
78
-
79
- it "should raise error if the strategy failed" do
80
- app = lambda do |env|
81
- env['warden'].authenticate(:fail_with_user)
82
- expect(env['warden'].user).to be_nil
83
- valid_response
84
- end
85
- setup_rack(app).call(@env)
86
- end
87
-
88
- it "should not raise error on missing strategies if silencing" do
89
- app = lambda do |env|
90
- env['warden'].authenticate
91
- valid_response
92
- end
93
- expect {
94
- setup_rack(app, :silence_missing_strategies => true, :default_strategies => [:unknown]).call(@env)
95
- }.not_to raise_error
96
- end
97
-
98
- it "should allow me to get access to the user at warden.user." do
99
- app = lambda do |env|
100
- env['warden'].authenticate(:pass)
101
- expect(env['warden']).to be_authenticated
102
- expect(env['warden.spec.strategies']).to eq([:pass])
103
- valid_response
104
- end
105
- setup_rack(app).call(@env)
106
- end
107
-
108
- it "should run strategies when authenticate? is asked" do
109
- app = lambda do |env|
110
- expect(env['warden']).not_to be_authenticated
111
- env['warden'].authenticate?(:pass)
112
- expect(env['warden']).to be_authenticated
113
- expect(env['warden.spec.strategies']).to eq([:pass])
114
- valid_response
115
- end
116
- setup_rack(app).call(@env)
117
- end
118
-
119
- it "should properly send the scope to the strategy" do
120
- app = lambda do |env|
121
- env['warden'].authenticate(:pass, :scope => :failz)
122
- expect(env['warden']).not_to be_authenticated
123
- expect(env['warden.spec.strategies']).to eq([:pass])
124
- valid_response
125
- end
126
- setup_rack(app).call(@env)
127
- end
128
-
129
- it "should try multiple authentication strategies" do
130
- app = lambda do |env|
131
- env['warden'].authenticate(:password,:pass)
132
- expect(env['warden']).to be_authenticated
133
- expect(env['warden.spec.strategies']).to eq([:password, :pass])
134
- valid_response
135
- end
136
- setup_rack(app).call(@env)
137
- end
138
-
139
- it "should look for an active user in the session with authenticate" do
140
- app = lambda do |env|
141
- env['rack.session']["warden.user.default.key"] = "foo as a user"
142
- env['warden'].authenticate(:pass)
143
- valid_response
144
- end
145
- setup_rack(app).call(@env)
146
- expect(@env['warden'].user).to eq("foo as a user")
147
- end
148
-
149
- it "should look for an active user in the session with authenticate?" do
150
- app = lambda do |env|
151
- env['rack.session']['warden.user.foo_scope.key'] = "a foo user"
152
- env['warden'].authenticate?(:pass, :scope => :foo_scope)
153
- valid_response
154
- end
155
- setup_rack(app).call(@env)
156
- expect(@env['warden'].user(:foo_scope)).to eq("a foo user")
157
- end
158
-
159
- it "should look for an active user in the session with authenticate!" do
160
- app = lambda do |env|
161
- env['rack.session']['warden.user.foo_scope.key'] = "a foo user"
162
- env['warden'].authenticate!(:pass, :scope => :foo_scope)
163
- valid_response
164
- end
165
- setup_rack(app).call(@env)
166
- expect(@env['warden'].user(:foo_scope)).to eq("a foo user")
167
- end
168
-
169
- it "should throw an error when authenticate!" do
170
- app = lambda do |env|
171
- env['warden'].authenticate!(:pass, :scope => :failz)
172
- raise "OMG"
173
- end
174
- setup_rack(app).call(@env)
175
- end
176
-
177
- it "should login 2 different users from the session" do
178
- app = lambda do |env|
179
- env['rack.session']['warden.user.foo.key'] = 'foo user'
180
- env['rack.session']['warden.user.bar.key'] = 'bar user'
181
- expect(env['warden']).to be_authenticated(:foo)
182
- expect(env['warden']).to be_authenticated(:bar)
183
- expect(env['warden']).not_to be_authenticated # default scope
184
- valid_response
185
- end
186
- setup_rack(app).call(@env)
187
- expect(@env['warden'].user(:foo)).to eq('foo user')
188
- expect(@env['warden'].user(:bar)).to eq('bar user')
189
- expect(@env['warden'].user).to be_nil
190
- end
191
-
192
- it "should not authenticate other scopes just because the first is authenticated" do
193
- app = lambda do |env|
194
- env['warden'].authenticate(:pass, :scope => :foo)
195
- env['warden'].authenticate(:invalid, :scope => :bar)
196
- expect(env['warden']).to be_authenticated(:foo)
197
- expect(env['warden']).not_to be_authenticated(:bar)
198
- valid_response
199
- end
200
- setup_rack(app).call(@env)
201
- end
202
-
203
- SID_REGEXP = /rack\.session=([^;]*);/
204
-
205
- it "should renew session when user is set" do
206
- app = lambda do |env|
207
- env["rack.session"]["counter"] ||= 0
208
- env["rack.session"]["counter"] += 1
209
- if env["warden.on"]
210
- env["warden"].authenticate!(:pass)
211
- expect(env["warden"]).to be_authenticated
212
- end
213
- valid_response
214
- end
215
-
216
- # Setup a rack app with Pool session.
217
- app = setup_rack(app, :session => Rack::Session::Pool).to_app
218
- response = app.call(@env)
219
- expect(@env["rack.session"]["counter"]).to eq(1)
220
-
221
- # Ensure a cookie was given back
222
- cookie = response[1]["Set-Cookie"]
223
- expect(cookie).not_to be_nil
224
-
225
- # Ensure a session id was given
226
- sid = cookie.match(SID_REGEXP)[1]
227
- expect(sid).not_to be_nil
228
-
229
- # Do another request, giving a cookie but turning on warden authentication
230
- env = env_with_params("/", {}, 'rack.session' => @env['rack.session'], "HTTP_COOKIE" => cookie, "warden.on" => true)
231
- response = app.call(env)
232
- expect(env["rack.session"]["counter"]).to be(2)
233
-
234
- # Regardless of rack version, a cookie should be sent back
235
- new_cookie = response[1]["Set-Cookie"]
236
- expect(new_cookie).not_to be_nil
237
-
238
- # And the session id in this cookie should not be the same as the previous one
239
- new_sid = new_cookie.match(SID_REGEXP)[1]
240
- expect(new_sid).not_to be_nil
241
- expect(new_sid).not_to eq(sid)
242
- end
243
-
244
- it "should not renew session when user is fetch" do
245
- app = lambda do |env|
246
- env["rack.session"]["counter"] ||= 0
247
- env["rack.session"]["counter"] += 1
248
- env["warden"].authenticate!(:pass)
249
- expect(env["warden"]).to be_authenticated
250
- valid_response
251
- end
252
-
253
- # Setup a rack app with Pool session.
254
- app = setup_rack(app, :session => Rack::Session::Pool).to_app
255
- response = app.call(@env)
256
- expect(@env["rack.session"]["counter"]).to eq(1)
257
-
258
- # Ensure a cookie was given back
259
- cookie = response[1]["Set-Cookie"]
260
- expect(cookie).not_to be_nil
261
-
262
- # Ensure a session id was given
263
- sid = cookie.match(SID_REGEXP)[1]
264
- expect(sid).not_to be_nil
265
-
266
- # Do another request, passing the cookie. The user should be fetched from cookie.
267
- env = env_with_params("/", {}, "HTTP_COOKIE" => cookie)
268
- response = app.call(env)
269
- expect(env["rack.session"]["counter"]).to eq(2)
270
-
271
- # Depending on rack version, a cookie will be returned with the
272
- # same session id or no cookie is given back (becase it did not change).
273
- # If we don't get any of these two behaviors, raise an error.
274
- # Regardless of rack version, a cookie should be sent back
275
- new_cookie = response[1]["Set-Cookie"]
276
- if new_cookie && new_cookie.match(SID_REGEXP)[1] != sid
277
- raise "Expected a cookie to not be sent or session id to match"
278
- end
279
- end
280
- end
281
-
282
- describe "authentication cache" do
283
- it "should run strategies just once for a given scope" do
284
- app = lambda do |env|
285
- env['warden'].authenticate(:password, :pass, :scope => :failz)
286
- expect(env['warden']).not_to be_authenticated(:failz)
287
- env['warden'].authenticate(:password, :pass, :scope => :failz)
288
- expect(env['warden']).not_to be_authenticated(:failz)
289
- expect(env['warden.spec.strategies']).to eq([:password, :pass])
290
- valid_response
291
- end
292
- setup_rack(app).call(@env)
293
- end
294
-
295
- it "should run strategies for a given scope several times if cache is cleaned" do
296
- app = lambda do |env|
297
- env['warden'].authenticate(:password, :pass, :scope => :failz)
298
- env['warden'].clear_strategies_cache!(:scope => :failz)
299
- env['warden'].authenticate(:password, :pass, :scope => :failz)
300
- expect(env['warden.spec.strategies']).to eq([:password, :pass, :password, :pass])
301
- valid_response
302
- end
303
- setup_rack(app).call(@env)
304
- end
305
-
306
- it "should clear the cache for a specified strategy" do
307
- app = lambda do |env|
308
- env['warden'].authenticate(:password, :pass, :scope => :failz)
309
- env['warden'].clear_strategies_cache!(:password, :scope => :failz)
310
- env['warden'].authenticate(:password, :pass, :scope => :failz)
311
- expect(env['warden.spec.strategies']).to eq([:password, :pass, :password])
312
- valid_response
313
- end
314
- setup_rack(app).call(@env)
315
- end
316
-
317
- it "should run the strategies several times for different scopes" do
318
- app = lambda do |env|
319
- env['warden'].authenticate(:password, :pass, :scope => :failz)
320
- expect(env['warden']).not_to be_authenticated(:failz)
321
- env['warden'].authenticate(:password, :pass)
322
- expect(env['warden']).to be_authenticated
323
- expect(env['warden.spec.strategies']).to eq([:password, :pass, :password, :pass])
324
- valid_response
325
- end
326
- setup_rack(app).call(@env)
327
- end
328
-
329
- it "should not run strategies until cache is cleaned if latest winning strategy halted" do
330
- app = lambda do |env|
331
- env['warden'].authenticate(:failz)
332
- expect(env['warden']).not_to be_authenticated
333
- env['warden'].authenticate(:pass)
334
- expect(env['warden'].winning_strategy.message).to eq("The Fails Strategy Has Failed You")
335
- valid_response
336
- end
337
- setup_rack(app).call(@env)
338
- end
339
-
340
- it "should not store user if strategy isn't meant for permanent login" do
341
- session = Warden::SessionSerializer.new(@env)
342
- app = lambda do |env|
343
- env['warden'].authenticate(:single)
344
- expect(env['warden']).to be_authenticated
345
- expect(env['warden'].user).to eq("Valid User")
346
- expect(session).not_to be_stored(:default)
347
- valid_response
348
- end
349
- setup_rack(app).call(@env)
350
- end
351
- end
352
-
353
- describe "set user" do
354
- it "should store the user into the session" do
355
- app = lambda do |env|
356
- env['warden'].authenticate(:pass)
357
- expect(env['warden']).to be_authenticated
358
- expect(env['warden'].user).to eq("Valid User")
359
- expect(env['rack.session']["warden.user.default.key"]).to eq("Valid User")
360
- valid_response
361
- end
362
- setup_rack(app).call(@env)
363
- end
364
-
365
- it "should not store the user if the :store option is set to false" do
366
- app = lambda do |env|
367
- env['warden'].authenticate(:pass, :store => false)
368
- expect(env['warden']).to be_authenticated
369
- expect(env['warden'].user).to eq("Valid User")
370
- expect(env['rack.session']['warden.user.default.key']).to be_nil
371
- valid_response
372
- end
373
- setup_rack(app).call(@env)
374
- end
375
-
376
- it "should not throw error when no session is configured and store is false" do
377
- app = lambda do |env|
378
- env['rack.session'] = nil
379
- env['warden'].authenticate(:pass, :store => false)
380
- expect(env['warden']).to be_authenticated
381
- expect(env['warden'].user).to eq("Valid User")
382
- valid_response
383
- end
384
- setup_rack(app).call(@env)
385
- end
386
-
387
- it "should not run the callbacks when :run_callbacks is false" do
388
- app = lambda do |env|
389
- expect(env['warden'].manager).not_to receive(:_run_callbacks)
390
- env['warden'].authenticate(:run_callbacks => false, :scope => :pass)
391
- valid_response
392
- end
393
- setup_rack(app).call(@env)
394
- end
395
-
396
- it "should run the callbacks when :run_callbacks is true" do
397
- app = lambda do |env|
398
- expect(env['warden'].manager).to receive(:_run_callbacks).at_least(:once)
399
- env['warden'].authenticate(:pass)
400
- valid_response
401
- end
402
- setup_rack(app).call(@env)
403
- end
404
-
405
- it "should run the callbacks by default" do
406
- app = lambda do |env|
407
- expect(env['warden'].manager).to receive(:_run_callbacks).at_least(:once)
408
- env['warden'].authenticate(:pass)
409
- valid_response
410
- end
411
- setup_rack(app).call(@env)
412
- end
413
- end
414
-
415
- describe "lock" do
416
- it "should not run any strategy" do
417
- _app = lambda do |env|
418
- env['warden'].lock!
419
- env['warden'].authenticate(:pass)
420
- expect(env['warden'].user).to be_nil
421
- valid_response
422
- end
423
- end
424
-
425
- it "should keep already authenticated users" do
426
- _app = lambda do |env|
427
- env['warden'].authenticate(:pass)
428
- env['warden'].lock!
429
- expect(env['warden'].user).not_to be_nil
430
- valid_response
431
- end
432
- end
433
- end
434
-
435
- describe "get user" do
436
- before(:each) do
437
- @env['rack.session'] ||= {}
438
- @env['rack.session'].delete("warden.user.default.key")
439
- end
440
-
441
- it "should return nil when not logged in" do
442
- app = lambda do |env|
443
- expect(env['warden'].user).to be_nil
444
- valid_response
445
- end
446
- setup_rack(app).call(@env)
447
- end
448
-
449
- it "should not run strategies when not logged in" do
450
- app = lambda do |env|
451
- expect(env['warden'].user).to be_nil
452
- expect(env['warden.spec.strategies']).to be_nil
453
- valid_response
454
- end
455
- setup_rack(app).call(@env)
456
- end
457
-
458
- it "should cache unfound user" do
459
- expect_any_instance_of(Warden::SessionSerializer).to receive(:fetch).once
460
- app = lambda do |env|
461
- expect(env['warden'].user).to be_nil
462
- expect(env['warden'].user).to be_nil
463
- valid_response
464
- end
465
- setup_rack(app).call(@env)
466
- end
467
-
468
- describe "previously logged in" do
469
- before(:each) do
470
- @env['rack.session']['warden.user.default.key'] = "A Previous User"
471
- @env['warden.spec.strategies'] = []
472
- end
473
-
474
- it "should take the user from the session when logged in" do
475
- app = lambda do |env|
476
- expect(env['warden'].user).to eq("A Previous User")
477
- valid_response
478
- end
479
- setup_rack(app).call(@env)
480
- end
481
-
482
- it "should cache found user" do
483
- expect_any_instance_of(Warden::SessionSerializer).to receive(:fetch).once.and_return "A Previous User"
484
- app = lambda do |env|
485
- expect(env['warden'].user).to eq("A Previous User")
486
- expect(env['warden'].user).to eq("A Previous User")
487
- valid_response
488
- end
489
- setup_rack(app).call(@env)
490
- end
491
-
492
- it "should not run strategies when the user exists in the session" do
493
- app = lambda do |env|
494
- env['warden'].authenticate!(:pass)
495
- valid_response
496
- end
497
- setup_rack(app).call(@env)
498
- expect(@env['warden.spec.strategies']).not_to include(:pass)
499
- end
500
-
501
- describe "run callback option" do
502
- it "should not call run_callbacks when we pass a :run_callback => false" do
503
- app = lambda do |env|
504
- expect(env['warden'].manager).not_to receive(:_run_callbacks)
505
- env['warden'].user(:run_callbacks => false)
506
- valid_response
507
- end
508
- setup_rack(app).call(@env)
509
- end
510
-
511
- it "should call run_callbacks when we pass a :run_callback => true" do
512
- app = lambda do |env|
513
- expect(env['warden'].manager).to receive(:_run_callbacks).at_least(:once)
514
- env['warden'].user(:run_callbacks => true)
515
- valid_response
516
- end
517
- setup_rack(app).call(@env)
518
- end
519
-
520
- it "should call run_callbacks by default" do
521
- app = lambda do |env|
522
- expect(env['warden'].manager).to receive(:_run_callbacks).at_least(:once)
523
- env['warden'].user
524
- valid_response
525
- end
526
- setup_rack(app).call(@env)
527
- end
528
- end
529
- end
530
- end
531
-
532
- describe "logout" do
533
- before(:each) do
534
- @env['rack.session'] = {"warden.user.default.key" => "default key", "warden.user.foo.key" => "foo key", :foo => "bar"}
535
- @app = lambda do |e|
536
- e['warden'].logout(e['warden.spec.which_logout'])
537
- valid_response
538
- end
539
- end
540
-
541
- it "should logout only the scoped foo user" do
542
- @app = setup_rack(@app)
543
- @env['warden.spec.which_logout'] = :foo
544
- @app.call(@env)
545
- expect(@env['rack.session']['warden.user.default.key']).to eq("default key")
546
- expect(@env['rack.session']['warden.user.foo.key']).to be_nil
547
- expect(@env['rack.session'][:foo]).to eq("bar")
548
- end
549
-
550
- it "should logout only the scoped default user" do
551
- @app = setup_rack(@app)
552
- @env['warden.spec.which_logout'] = :default
553
- @app.call(@env)
554
- expect(@env['rack.session']['warden.user.default.key']).to be_nil
555
- expect(@env['rack.session']['warden.user.foo.key']).to eq("foo key")
556
- expect(@env['rack.session'][:foo]).to eq("bar")
557
- end
558
-
559
- it "should clear the session when no argument is given to logout" do
560
- expect(@env['rack.session']).not_to be_nil
561
- app = lambda do |e|
562
- e['warden'].logout
563
- valid_response
564
- end
565
- setup_rack(app).call(@env)
566
- expect(@env['rack.session']).to be_empty
567
- end
568
-
569
- it "should not raise exception if raw_session is nil" do
570
- @app = setup_rack(@app, { nil_session: true })
571
- @env['rack.session'] = nil
572
- @env['warden.spec.which_logout'] = :foo
573
- expect { @app.call(@env) }.to_not raise_error
574
- end
575
-
576
- it "should clear the user when logging out" do
577
- expect(@env['rack.session']).not_to be_nil
578
- app = lambda do |e|
579
- expect(e['warden'].user).not_to be_nil
580
- e['warden'].logout
581
- expect(e['warden']).not_to be_authenticated
582
- expect(e['warden'].user).to be_nil
583
- valid_response
584
- end
585
- setup_rack(app).call(@env)
586
- expect(@env['warden'].user).to be_nil
587
- end
588
-
589
- it "should clear the session data when logging out" do
590
- expect(@env['rack.session']).not_to be_nil
591
- app = lambda do |e|
592
- expect(e['warden'].user).not_to be_nil
593
- e['warden'].session[:foo] = :bar
594
- e['warden'].logout
595
- valid_response
596
- end
597
- setup_rack(app).call(@env)
598
- end
599
-
600
- it "should clear out the session by calling reset_session! so that plugins can setup their own session clearing" do
601
- expect(@env['rack.session']).not_to be_nil
602
- app = lambda do |e|
603
- expect(e['warden'].user).not_to be_nil
604
- expect(e['warden']).to receive(:reset_session!)
605
- e['warden'].logout
606
- valid_response
607
- end
608
- setup_rack(app).call(@env)
609
- end
610
- end
611
-
612
- describe "messages" do
613
- it "should allow access to the failure message" do
614
- failure = lambda do |e|
615
- [401, {"Content-Type" => "text/plain"}, [e['warden'].message]]
616
- end
617
- app = lambda do |e|
618
- e['warden'].authenticate! :failz
619
- end
620
- result = setup_rack(app, :failure_app => failure).call(@env)
621
- expect(result.last).to eq(["The Fails Strategy Has Failed You"])
622
- end
623
-
624
- it "should allow access to the success message" do
625
- success = lambda do |e|
626
- [200, {"Content-Type" => "text/plain"}, [e['warden'].message]]
627
- end
628
- app = lambda do |e|
629
- e['warden'].authenticate! :pass_with_message
630
- success.call(e)
631
- end
632
- result = setup_rack(app).call(@env)
633
- expect(result.last).to eq(["The Success Strategy Has Accepted You"])
634
- end
635
-
636
- it "should not die when accessing a message from a source where no authentication has occurred" do
637
- app = lambda do |e|
638
- [200, {"Content-Type" => "text/plain"}, [e['warden'].message]]
639
- end
640
- result = setup_rack(app).call(@env)
641
- expect(result[2]).to eq([nil])
642
- end
643
- end
644
-
645
- describe "when all strategies are not valid?" do
646
- it "should return false for authenticated? when there are no valid? strategies" do
647
- @env['rack.session'] = {}
648
- app = lambda do |e|
649
- expect(e['warden'].authenticate(:invalid)).to be_nil
650
- expect(e['warden']).not_to be_authenticated
651
- end
652
- setup_rack(app).call(@env)
653
- end
654
-
655
- it "should return nil for authenticate when there are no valid strategies" do
656
- @env['rack.session'] = {}
657
- app = lambda do |e|
658
- expect(e['warden'].authenticate(:invalid)).to be_nil
659
- end
660
- setup_rack(app).call(@env)
661
- end
662
-
663
- it "should return false for authenticate? when there are no valid strategies" do
664
- @env['rack.session'] = {}
665
- app = lambda do |e|
666
- expect(e['warden'].authenticate?(:invalid)).to eq(false)
667
- end
668
- setup_rack(app).call(@env)
669
- end
670
-
671
- it "should respond with a 401 when authenticate! cannot find any valid strategies" do
672
- @env['rack.session'] = {}
673
- app = lambda do |e|
674
- e['warden'].authenticate!(:invalid)
675
- end
676
- result = setup_rack(app).call(@env)
677
- expect(result.first).to eq(401)
678
- end
679
- end
680
-
681
- describe "authenticated?" do
682
- describe "positive authentication" do
683
- before do
684
- @env['rack.session'] = {'warden.user.default.key' => 'defult_key'}
685
- $captures = []
686
- end
687
-
688
- it "should return true when authenticated in the session" do
689
- app = lambda do |e|
690
- expect(e['warden']).to be_authenticated
691
- end
692
- setup_rack(app).call(@env)
693
- end
694
-
695
- it "should yield to a block when the block is passed and authenticated" do
696
- app = lambda do |e|
697
- e['warden'].authenticated? do
698
- $captures << :in_the_block
699
- end
700
- end
701
- setup_rack(app).call(@env)
702
- expect($captures).to eq([:in_the_block])
703
- end
704
-
705
- it "should authenticate for a user in a different scope" do
706
- @env['rack.session'] = {'warden.user.foo.key' => 'foo_key'}
707
- app = lambda do |e|
708
- e['warden'].authenticated?(:foo) do
709
- $captures << :in_the_foo_block
710
- end
711
- end
712
- setup_rack(app).call(@env)
713
- expect($captures).to eq([:in_the_foo_block])
714
- end
715
- end
716
-
717
- describe "negative authentication" do
718
- before do
719
- @env['rack.session'] = {'warden.foo.default.key' => 'foo_key'}
720
- $captures = []
721
- end
722
-
723
- it "should return false when authenticated in the session" do
724
- app = lambda do |e|
725
- expect(e['warden']).not_to be_authenticated
726
- end
727
- setup_rack(app).call(@env)
728
- end
729
-
730
- it "should return false if scope cannot be retrieved from session" do
731
- begin
732
- Warden::Manager.serialize_from_session { |k| nil }
733
- app = lambda do |env|
734
- env['rack.session']['warden.user.foo_scope.key'] = "a foo user"
735
- env['warden'].authenticated?(:foo_scope)
736
- valid_response
737
- end
738
- setup_rack(app).call(@env)
739
- expect(@env['warden'].user(:foo_scope)).to be_nil
740
- ensure
741
- Warden::Manager.serialize_from_session { |k| k }
742
- end
743
- end
744
-
745
- it "should not yield to a block when the block is passed and authenticated" do
746
- app = lambda do |e|
747
- e['warden'].authenticated? do
748
- $captures << :in_the_block
749
- end
750
- end
751
- setup_rack(app).call(@env)
752
- expect($captures).to eq([])
753
- end
754
-
755
- it "should not yield for a user in a different scope" do
756
- app = lambda do |e|
757
- e['warden'].authenticated?(:bar) do
758
- $captures << :in_the_bar_block
759
- end
760
- end
761
- setup_rack(app).call(@env)
762
- expect($captures).to eq([])
763
- end
764
- end
765
- end
766
-
767
- describe "unauthenticated?" do
768
- describe "negative unauthentication" do
769
- before do
770
- @env['rack.session'] = {'warden.user.default.key' => 'defult_key'}
771
- $captures = []
772
- end
773
-
774
- it "should return false when authenticated in the session" do
775
- app = lambda do |e|
776
- expect(e['warden']).not_to be_unauthenticated
777
- end
778
- _result = setup_rack(app).call(@env)
779
- end
780
-
781
- it "should not yield to a block when the block is passed and authenticated" do
782
- app = lambda do |e|
783
- e['warden'].unauthenticated? do
784
- $captures << :in_the_block
785
- end
786
- end
787
- setup_rack(app).call(@env)
788
- expect($captures).to eq([])
789
- end
790
-
791
- it "should not yield to the block for a user in a different scope" do
792
- @env['rack.session'] = {'warden.user.foo.key' => 'foo_key'}
793
- app = lambda do |e|
794
- e['warden'].unauthenticated?(:foo) do
795
- $captures << :in_the_foo_block
796
- end
797
- end
798
- setup_rack(app).call(@env)
799
- expect($captures).to eq([])
800
- end
801
- end
802
-
803
- describe "positive unauthentication" do
804
- before do
805
- @env['rack.session'] = {'warden.foo.default.key' => 'foo_key'}
806
- $captures = []
807
- end
808
-
809
- it "should return false when unauthenticated in the session" do
810
- app = lambda do |e|
811
- expect(e['warden']).to be_unauthenticated
812
- end
813
- setup_rack(app).call(@env)
814
- end
815
-
816
- it "should yield to a block when the block is passed and authenticated" do
817
- app = lambda do |e|
818
- e['warden'].unauthenticated? do
819
- $captures << :in_the_block
820
- end
821
- end
822
- setup_rack(app).call(@env)
823
- expect($captures).to eq([:in_the_block])
824
- end
825
-
826
- it "should yield for a user in a different scope" do
827
- app = lambda do |e|
828
- e['warden'].unauthenticated?(:bar) do
829
- $captures << :in_the_bar_block
830
- end
831
- end
832
- setup_rack(app).call(@env)
833
- expect($captures).to eq([:in_the_bar_block])
834
- end
835
- end
836
- end
837
-
838
- describe "attributes" do
839
- def def_app(&blk)
840
- @app = setup_rack(blk)
841
- end
842
-
843
- it "should have a config attribute" do
844
- app = def_app do |e|
845
- expect(e['warden'].config).to be_a_kind_of(Hash)
846
- valid_response
847
- end
848
- app.call(@env)
849
- end
850
- end
851
- end
852
-
853
- describe "dynamic default_strategies" do
854
- before(:all) do
855
- load_strategies
856
-
857
- class ::DynamicDefaultStrategies
858
- def initialize(app, &blk)
859
- @app, @blk = app, blk
860
- end
861
-
862
- def call(env)
863
- @blk.call(env)
864
- @app.call(env)
865
- end
866
- end
867
-
868
- Warden::Strategies.add(:one) do
869
- def authenticate!; $captures << :one; success!("User") end
870
- end
871
-
872
- Warden::Strategies.add(:two) do
873
- def authenticate!; $captures << :two; fail("User not found") end
874
- end
875
- end
876
-
877
- before(:each) do
878
- @app = lambda{|e| e['warden'].authenticate! }
879
- @env = env_with_params("/")
880
- $captures = []
881
- end
882
-
883
- def wrap_app(app, &blk)
884
- builder = Rack::Builder.new do
885
- use DynamicDefaultStrategies, &blk
886
- run app
887
- end
888
- builder.to_app
889
- end
890
-
891
- it "should allow me to change the default strategies on the fly" do
892
- app = wrap_app(@app) do |e|
893
- expect(e['warden'].default_strategies).to eq([:password])
894
- expect(e['warden'].config.default_strategies).to eq([:password])
895
- e['warden'].default_strategies :one
896
- e['warden'].authenticate!
897
- Rack::Response.new("OK").finish
898
- end
899
- setup_rack(app).call(@env)
900
-
901
- expect($captures).to eq([:one])
902
- end
903
-
904
- it "should allow me to append to the default strategies on the fly" do
905
- app = wrap_app(@app) do |e|
906
- e['warden'].default_strategies << :one
907
- expect(e['warden'].default_strategies).to eq([:password, :one])
908
- e['warden'].authenticate!
909
- Rack::Response.new("OK").finish
910
- end
911
- setup_rack(app).call(@env)
912
-
913
- expect($captures).to eq([:one])
914
- end
915
-
916
- it "should allow me to set the default strategies on a per scope basis" do
917
- app = wrap_app(@app) do |e|
918
- w = e['warden']
919
- w.default_strategies(:two, :one, :scope => :foo)
920
- w.default_strategies(:two, :scope => :default)
921
- expect(w.default_strategies(:scope => :foo)).to eq([:two, :one])
922
- w.authenticate(:scope => :foo)
923
- expect($captures).to eq([:two, :one])
924
- $captures.clear
925
- w.authenticate
926
- expect($captures).to eq([:two])
927
- end
928
- setup_rack(app).call(@env)
929
- expect($captures).to eq([:two])
930
- end
931
-
932
- it "should allow me to setup default strategies for each scope on the manager" do
933
- builder = Rack::Builder.new do
934
- use Warden::Spec::Helpers::Session
935
- use Warden::Manager do |config|
936
- config.default_strategies :one
937
- config.default_strategies :two, :one, :scope => :foo
938
- config.failure_app = Warden::Spec::Helpers::FAILURE_APP
939
- end
940
- run(lambda do |e|
941
- w = e['warden']
942
- w.authenticate
943
- w.authenticate(:scope => :foo)
944
- $captures << :complete
945
- end)
946
- end
947
- builder.to_app.call(@env)
948
- expect($captures).to eq([:one, :two, :one, :complete])
949
- end
950
-
951
- it "should not change the master configurations strategies when I change them" do
952
- app = wrap_app(@app) do |e|
953
- e['warden'].default_strategies << :one
954
- expect(e['warden'].default_strategies).to eq([:password, :one])
955
- expect(e['warden'].manager.config.default_strategies).to eq([:password])
956
- e['warden'].authenticate!
957
- Rack::Response.new("OK").finish
958
- end
959
- setup_rack(app).call(@env)
960
-
961
- expect($captures).to eq([:one])
962
- end
963
-
964
- describe "default scope options" do
965
-
966
- it "should allow me to set a default action for a given scope" do
967
- $captures = []
968
- builder = Rack::Builder.new do
969
- use Warden::Manager do |config|
970
- config.scope_defaults :foo, :strategies => [:two], :action => "some_bad_action"
971
- config.failure_app = Warden::Spec::Helpers::FAILURE_APP
972
- end
973
-
974
- run(lambda do |e|
975
- e['warden'].authenticate!(:scope => :foo)
976
- end)
977
- end
978
-
979
- env = env_with_params("/foo")
980
- env["rack.session"] = {}
981
- builder.to_app.call(env)
982
- request = Rack::Request.new(env)
983
- expect(request.path).to eq("/some_bad_action")
984
- end
985
-
986
- it "should allow me to set store, false on a given scope" do
987
- $captures = []
988
- warden = []
989
- builder = Rack::Builder.new do
990
- use Warden::Manager do |config|
991
- config.default_strategies :one
992
- config.default_strategies :two, :one, :scope => :foo
993
- config.default_strategies :two, :one, :scope => :bar
994
-
995
- config.scope_defaults :bar, :store => false
996
- config.scope_defaults :baz, :store => false
997
- config.failure_app = Warden::Spec::Helpers::FAILURE_APP
998
- end
999
- run(lambda do |e|
1000
- w = e['warden']
1001
- w.authenticate
1002
- w.authenticate(:scope => :foo)
1003
- w.authenticate(:one, :scope => :bar)
1004
- w.authenticate(:one, :scope => :baz, :store => true)
1005
- warden << w
1006
- $captures << :complete
1007
- Rack::Response.new("OK").finish
1008
- end)
1009
- end
1010
- session = @env["rack.session"] = {}
1011
- builder.to_app.call(@env)
1012
- expect($captures).to include(:complete)
1013
- w = warden.first
1014
- expect(w.user).to eq("User")
1015
- expect(w.user(:foo)).to eq("User")
1016
- expect(w.user(:bar)).to eq("User")
1017
- expect(w.user(:baz)).to eq("User")
1018
- expect(session['warden.user.default.key']).to eq("User")
1019
- expect(session['warden.user.foo.key']).to eq("User")
1020
- expect(session.key?('warden.user.bar.key')).to eq(false)
1021
- expect(session['warden.user.bar.key']).to be_nil
1022
- expect(session['warden.user.baz.key']).to eq("User")
1023
- end
1024
- end
1025
-
1026
- describe "#asset_request?" do
1027
- before(:each) do
1028
- @asset_regex = /^\/assets\//
1029
- ::Warden.asset_paths = @asset_regex
1030
- end
1031
-
1032
- it "should return true if PATH_INFO is in asset list" do
1033
- env = env_with_params('/assets/fun.gif')
1034
- setup_rack(success_app).call(env)
1035
- proxy = env["warden"]
1036
-
1037
- expect(proxy.env['PATH_INFO']).to match(@asset_regex)
1038
- expect(proxy).to be_asset_request
1039
- end
1040
-
1041
- it "should return false if PATH_INFO is not in asset list" do
1042
- env = env_with_params('/home')
1043
- setup_rack(success_app).call(env)
1044
- proxy = env["warden"]
1045
-
1046
- expect(proxy.env['PATH_INFO']).not_to match(@asset_regex)
1047
- expect(proxy).not_to be_asset_request
1048
- end
1049
- end
1050
- end