warden-rspec-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,24 @@
1
+ ## What
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
5
+ environment, custom test setup code is necessary.
6
+
7
+ ## Usage
8
+
9
+ ```ruby
10
+ # Gemfile
11
+ group :test do
12
+ gem 'warden-rspec-rails'
13
+ end
14
+
15
+ # spec_helper.rb
16
+ RSpec.configure do |c|
17
+ c.include Warden::Test::ControllerHelpers
18
+ end
19
+ ```
20
+
21
+ ## Thanks
22
+
23
+ * Devise: https://github.com/platformatec/devise
24
+ * Kentaro Imai: http://kentaroimai.com/articles/1-controller-test-helpers-for-warden
@@ -0,0 +1,65 @@
1
+ # test/test_helpers/warden.rb
2
+
3
+ module Warden
4
+ # Warden::Test::ControllerHelpers provides a facility to test controllers in isolation
5
+ # Most of the code was extracted from Devise's Devise::TestHelpers.
6
+ module Test
7
+ module ControllerHelpers
8
+ def self.included(base)
9
+ base.class_eval do
10
+ before(:each) do
11
+ setup_controller_for_warden
12
+ end
13
+
14
+ before(:each) do
15
+ warden
16
+ end
17
+ end
18
+ end
19
+
20
+ # Override process to consider warden.
21
+ def process(*)
22
+ # Make sure we always return @response, a la ActionController::TestCase::Behavior#process, even if warden interrupts
23
+ _catch_warden {super} || @response
24
+ end
25
+
26
+ # We need to setup the environment variables and the response in the controller
27
+ def setup_controller_for_warden
28
+ @request.env['action_controller.instance'] = @controller
29
+ end
30
+
31
+ # Quick access to Warden::Proxy.
32
+ def warden
33
+ @warden ||= begin
34
+ manager = Warden::Manager.new(nil, &Rails.application.config.middleware.detect{|m| m.name == 'RailsWarden::Manager'}.block)
35
+ @request.env['warden'] = Warden::Proxy.new(@request.env, manager)
36
+ end
37
+ end
38
+
39
+ protected
40
+
41
+ # Catch warden continuations and handle like the middleware would.
42
+ # Returns nil when interrupted, otherwise the normal result of the block.
43
+ def _catch_warden(&block)
44
+ result = catch(:warden, &block)
45
+
46
+ if result.is_a?(Hash) && !warden.custom_failure? && !@controller.send(:performed?)
47
+ result[:action] ||= :unauthenticated
48
+
49
+ env = @controller.request.env
50
+ env['PATH_INFO'] = "/#{result[:action]}"
51
+ env['warden.options'] = result
52
+ Warden::Manager._run_callbacks(:before_failure, env, result)
53
+
54
+ status, headers, body = warden.config[:failure_app].call(env).to_a
55
+ @controller.send :render, :status => status, :text => body,
56
+ :content_type => headers['Content-Type'], :location => headers['Location']
57
+
58
+ nil
59
+ else
60
+ result
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,2 @@
1
+
2
+ require 'warden/test/controller_helpers'
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "warden-rspec-rails"
6
+ s.version = '0.1.0'
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Alex Sharp"]
9
+ s.email = ["ajsharp@gmail.com"]
10
+ s.homepage = "https://github.com/ajsharp/warden-rspec-rails"
11
+ s.summary = %q{Rails controller spec helpers for warden.}
12
+ s.description = %q{Rails controller spec helpers for warden.}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: warden-rspec-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alex Sharp
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-06 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Rails controller spec helpers for warden.
15
+ email:
16
+ - ajsharp@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - README.md
22
+ - lib/warden-rspec-rails.rb
23
+ - lib/warden/test/controller_helpers.rb
24
+ - warden-rspec-rails.gemspec
25
+ homepage: https://github.com/ajsharp/warden-rspec-rails
26
+ licenses: []
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 1.8.25
46
+ signing_key:
47
+ specification_version: 3
48
+ summary: Rails controller spec helpers for warden.
49
+ test_files: []