-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for rspec to allow authentication on controllers
- Loading branch information
Showing
6 changed files
with
88 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,25 @@ | ||
def auth_user(user) | ||
sign_in user | ||
request.headers.merge!(user.create_new_auth_token) | ||
end | ||
module Authorization | ||
def request_with(user, http_method, action, parameters = {}, session = {}, flash = {} ) | ||
warden.set_user user | ||
process action, http_method.to_s.upcase, parameters, session, flash | ||
end | ||
|
||
[:get, :put, :post, :delete].each do |method| | ||
module_eval <<-EOV, __FILE__, __LINE__ | ||
def #{method}_with(user, *args) | ||
request_with(user, #{method.inspect}, *args) | ||
end | ||
EOV | ||
end | ||
|
||
def render_with user, options = {}, local_assigns = {}, &block | ||
allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user) | ||
allow(view).to receive(:current_user).and_return(user) | ||
allow(view).to receive(:authenticated?).and_return(true) | ||
render options, local_assigns, &block | ||
end | ||
end | ||
|
||
RSpec::configure do |c| | ||
c.include Authorization | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
module Warden | ||
# Warden::Test::ControllerHelpers provides a facility to test controllers in isolation | ||
# Most of the code was extracted from Devise's Devise::TestHelpers. | ||
module Test | ||
module ControllerHelpers | ||
def self.included(base) | ||
base.class_eval do | ||
setup :setup_controller_for_warden, :warden if respond_to?(:setup) | ||
end | ||
end | ||
|
||
# Override process to consider warden. | ||
def process(*) | ||
# Make sure we always return @response, a la ActionController::TestCase::Behavior#process, even if warden interrupts | ||
_catch_warden {super} || @response | ||
end | ||
|
||
# We need to setup the environment variables and the response in the controller | ||
def setup_controller_for_warden | ||
@request.env['action_controller.instance'] = @controller | ||
end | ||
|
||
# Quick access to Warden::Proxy. | ||
def warden | ||
@warden ||= begin | ||
manager = Warden::Manager.new(nil, &Rails.application.config.middleware.detect{|m| m.name == 'Warden::Manager'}.block) | ||
@request.env['warden'] = Warden::Proxy.new(@request.env, manager) | ||
end | ||
end | ||
|
||
protected | ||
|
||
# Catch warden continuations and handle like the middleware would. | ||
# Returns nil when interrupted, otherwise the normal result of the block. | ||
def _catch_warden(&block) | ||
result = catch(:warden, &block) | ||
|
||
if result.is_a?(Hash) && !warden.custom_failure? && !@controller.send(:performed?) | ||
result[:action] ||= :unauthenticated | ||
|
||
env = @controller.request.env | ||
env['PATH_INFO'] = "/#{result[:action]}" | ||
env['warden.options'] = result | ||
Warden::Manager._run_callbacks(:before_failure, env, result) | ||
|
||
status, headers, body = warden.config[:failure_app].call(env).to_a | ||
@controller.send :render, :status => status, :text => body, | ||
:content_type => headers['Content-Type'], :location => headers['Location'] | ||
|
||
nil | ||
else | ||
result | ||
end | ||
end | ||
end | ||
end | ||
end |