warden 1.2.7 → 1.2.9

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