warden-rspec-rails 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 775e119dbbccddb56df09cc3e85b1596416a4566
4
- data.tar.gz: c6123ec1878a5ba17db9c7f211118cb3289ec86a
2
+ SHA256:
3
+ metadata.gz: 8101a75e25a75be2b24e44e5efb58cf3f05bda325adb59ec206817ea8494d2ee
4
+ data.tar.gz: 24d9b354d4cecb692a63975895edba65da84d687e235b2c41435f5f4295ee36b
5
5
  SHA512:
6
- metadata.gz: ee985ecdebd18d5467ec4d34ae89dc441ec666fc3a9d994fccff381ce9e7f9821ba70641cc81e5c6f6686c6790d4b6b8aac408d7a4d5d26b1ba3ee574f608b32
7
- data.tar.gz: b76c954b5f715e83041ac727045a896a94fa5fbbd332374e6935de3e7ad044df7cc4ec2ccc2eed98891c9b24fe3d4ebaacbca37a9a3fc94f4b6e51a54c075963
6
+ metadata.gz: 0533fce0ae3ccc3cb454d2211b008e644bbbf80caf1a002954dc73d604ba93792902af5cb24902342ea0224bc37a8f09ff5568a8331beace48c6854747974031
7
+ data.tar.gz: f71a6cfa1e19727ba1cfe89fdfdce7e21ee3121f8497b65030cecfa504745f3b2563d2157b0af3541d2d301f2f0e0ccfa11b138db6c2c4baa057773c3d9adc10
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Alex Sharp
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  ## What
2
2
 
3
- Rails controller spec helpers for warden. If you're using warden
4
- without devise in rails, due to how action controller sets up the test
3
+ Rails controller spec helpers for Warden. If you're using Warden
4
+ without Devise in rails, due to how ActionController sets up the test
5
5
  environment, custom test setup code is necessary.
6
6
 
7
7
  ## Usage
@@ -15,13 +15,17 @@ end
15
15
  # spec_helper.rb
16
16
  RSpec.configure do |c|
17
17
  c.include Warden::Test::ControllerHelpers, type: :controller
18
-
19
- def sign_in(user)
20
- warden.set_user(user)
21
- end
22
18
  end
23
19
  ```
24
20
 
21
+ This will define helper methods in controller tests that you can use to manage
22
+ authentication, such as:
23
+ - `warden`: Access the `Warden::Proxy`.
24
+ - `login_as`: Same as the `Warden::Test::Helpers` `login_as` method.
25
+ - `logout`: Same as the `Warden::Test::Helpers` `logout` method.
26
+ - `unlogin`: Removes the user(s) from the logged-in list, but leaves the
27
+ session value so the user can be fetched on access.
28
+
25
29
  ## Thanks
26
30
 
27
31
  * Devise: https://github.com/platformatec/devise
@@ -1,21 +1,24 @@
1
- # test/test_helpers/warden.rb
1
+ # frozen_string_literal: true
2
+
3
+ require "warden" # just in case
2
4
 
3
5
  # Based on http://stackoverflow.com/questions/13420923/configuring-warden-for-use-in-rspec-controller-specs
4
6
  module Warden
5
7
  # Warden::Test::ControllerHelpers provides a facility to test controllers in isolation
6
- # Most of the code was extracted from Devise's Devise::TestHelpers.
8
+ # Most of the code was extracted from Devise's Devise::Test::ControllerHelpers.
7
9
  module Test
8
10
  module ControllerHelpers
9
- def self.included(base)
10
- base.class_eval do
11
- setup :setup_controller_for_warden, :warden if respond_to?(:setup)
12
- end
11
+ extend ActiveSupport::Concern
12
+
13
+ included do
14
+ setup :setup_controller_for_warden, :warden # if respond_to?(:setup)
13
15
  end
14
16
 
15
17
  # Override process to consider warden.
16
- def process(*)
17
- # Make sure we always return @response, a la ActionController::TestCase::Behavior#process, even if warden interrupts
18
- _catch_warden {super} || @response
18
+ def process(*, **)
19
+ _catch_warden { super }
20
+
21
+ @response
19
22
  end
20
23
 
21
24
  # We need to setup the environment variables and the response in the controller
@@ -24,10 +27,31 @@ module Warden
24
27
  end
25
28
 
26
29
  # Quick access to Warden::Proxy.
27
- def warden
28
- @warden ||= begin
29
- manager = Warden::Manager.new(nil, &Rails.application.config.middleware.detect{|m| m.name == 'Warden::Manager'}.block)
30
- @request.env['warden'] = Warden::Proxy.new(@request.env, manager)
30
+ def warden #:nodoc:
31
+ @request.env['warden'] ||= begin
32
+ manager = Warden::Manager.new(nil, &Rails.application.config.middleware.detect { |m| m.name.include?('Warden::Manager') }.block)
33
+ Warden::Proxy.new(@request.env, manager)
34
+ end
35
+ end
36
+
37
+ # Warden::Test::Helpers style login_as for controller tests.
38
+ def login_as(user, opts = {})
39
+ opts[:event] ||= :authentication
40
+ warden.set_user(user, opts)
41
+ end
42
+
43
+ # Warden::Test::Helpers style logout for controller tests.
44
+ def logout(*scopes)
45
+ warden.logout(*scopes)
46
+ end
47
+
48
+ # Reset the logins without logging out, so the next request will fetch.
49
+ def unlogin(*scopes)
50
+ users = warden.instance_variable_get(:@users)
51
+ if scopes.empty?
52
+ users.clear
53
+ else
54
+ scopes.each { |scope| users.delete(scope) }
31
55
  end
32
56
  end
33
57
 
@@ -38,23 +62,67 @@ module Warden
38
62
  def _catch_warden(&block)
39
63
  result = catch(:warden, &block)
40
64
 
41
- if result.is_a?(Hash) && !warden.custom_failure? && !@controller.send(:performed?)
42
- result[:action] ||= :unauthenticated
65
+ env = @controller.request.env
43
66
 
44
- env = @controller.request.env
45
- env['PATH_INFO'] = "/#{result[:action]}"
46
- env['warden.options'] = result
47
- Warden::Manager._run_callbacks(:before_failure, env, result)
67
+ result ||= {}
48
68
 
49
- status, headers, body = warden.config[:failure_app].call(env).to_a
50
- @controller.send :render, :status => status, :text => body,
51
- :content_type => headers['Content-Type'], :location => headers['Location']
52
-
53
- nil
69
+ # Set the response. In production, the rack result is returned
70
+ # from Warden::Manager#call, which the following is modelled on.
71
+ case result
72
+ when Array
73
+ if result.first == 401 && intercept_401?(env) # does this happen during testing?
74
+ _process_unauthenticated(env)
75
+ else
76
+ result
77
+ end
78
+ when Hash
79
+ _process_unauthenticated(env, result)
54
80
  else
55
81
  result
56
82
  end
57
83
  end
84
+
85
+ def _process_unauthenticated(env, options = {})
86
+ proxy = request.env['warden']
87
+ options[:action] ||= begin
88
+ opts = proxy.config[:scope_defaults][proxy.config.default_scope] || {}
89
+ opts[:action] || "unauthenticated"
90
+ end
91
+ result = options[:result] || proxy.result
92
+
93
+ ret = case result
94
+ when :redirect
95
+ body = proxy.message || "You are being redirected to #{proxy.headers['Location']}"
96
+ [proxy.status, proxy.headers, [body]]
97
+ when :custom
98
+ proxy.custom_response
99
+ else
100
+ request.env["PATH_INFO"] = "/#{options[:action]}"
101
+ request.env["warden.options"] = options
102
+ Warden::Manager._run_callbacks(:before_failure, env, options)
103
+
104
+ status, headers, response = warden.config[:failure_app].call(env).to_a
105
+ @controller.response.headers.merge!(headers)
106
+ @controller.status = status
107
+ @controller.response.body = response.body
108
+ nil # causes process return @response
109
+ end
110
+
111
+ # ensure that the controller response is set up. In production, this is
112
+ # not necessary since warden returns the results to rack. However, at
113
+ # testing time, we want the response to be available to the testing
114
+ # framework to verify what would be returned to rack.
115
+ if ret.is_a?(Array)
116
+ status, headers, body = *ret
117
+ # ensure the controller response is set to our response.
118
+ @controller.response ||= @response
119
+ @response.status = status
120
+ @response.headers.merge!(headers)
121
+ @response.body = body
122
+ end
123
+
124
+ ret
125
+ end
58
126
  end
59
127
  end
60
- end
128
+ end
@@ -1,2 +1,3 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  require 'warden/test/controller_helpers'
@@ -3,7 +3,8 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "warden-rspec-rails"
6
- s.version = '0.2.0'
6
+ s.version = '0.3.0'
7
+ s.licenses = ['MIT']
7
8
  s.platform = Gem::Platform::RUBY
8
9
  s.authors = ["Alex Sharp"]
9
10
  s.email = ["ajsharp@gmail.com"]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: warden-rspec-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Sharp
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-22 00:00:00.000000000 Z
11
+ date: 2022-06-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Rails controller spec helpers for warden.
14
14
  email:
@@ -17,12 +17,14 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - LICENSE
20
21
  - README.md
21
22
  - lib/warden-rspec-rails.rb
22
23
  - lib/warden/test/controller_helpers.rb
23
24
  - warden-rspec-rails.gemspec
24
25
  homepage: https://github.com/ajsharp/warden-rspec-rails
25
- licenses: []
26
+ licenses:
27
+ - MIT
26
28
  metadata: {}
27
29
  post_install_message:
28
30
  rdoc_options: []
@@ -39,8 +41,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
39
41
  - !ruby/object:Gem::Version
40
42
  version: '0'
41
43
  requirements: []
42
- rubyforge_project:
43
- rubygems_version: 2.4.3
44
+ rubygems_version: 3.1.6
44
45
  signing_key:
45
46
  specification_version: 4
46
47
  summary: Rails controller spec helpers for warden.