Created
January 22, 2013 14:36
-
-
Save divins/4595079 to your computer and use it in GitHub Desktop.
Enable digest authentication at rspec controller tests
This file contains hidden or 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 goes to: | |
# spec/support/http_digest_authentication.rb | |
require 'digest/md5' | |
module HTTPDigestAuthentication | |
def authenticate_with_http_digest(user, password, realm) | |
ActionController::Base.class_eval { include ActionController::Testing } | |
unless @controller.methods.include?(:real_process_with_new_base_test) | |
@controller.instance_eval %q{ | |
alias real_process_with_new_base_test process_with_new_base_test | |
} | |
end | |
@controller.instance_eval %Q( | |
def process_with_new_base_test(request, response) | |
credentials = { | |
:uri => request.url, | |
:realm => "#{realm}", | |
:username => "#{user}", | |
:nonce => ActionController::HttpAuthentication::Digest.nonce(request.env['action_dispatch.secret_token']), | |
:opaque => ActionController::HttpAuthentication::Digest.opaque(request.env['action_dispatch.secret_token']) | |
} | |
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Digest.encode_credentials(request.request_method, credentials, "#{password}", false) | |
real_process_with_new_base_test(request, response) | |
end | |
) | |
end | |
end |
This file contains hidden or 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 goes to: | |
# spec/spec_helper.rb | |
# This file is copied to spec/ when you run 'rails generate rspec:install' | |
ENV["RAILS_ENV"] ||= 'test' | |
require File.expand_path("../../config/environment", __FILE__) | |
require 'rspec/rails' | |
require 'rspec/autorun' | |
# require_relative 'fast_spec_helper' | |
# ... | |
# Requires supporting ruby files with custom matchers and macros, etc, | |
# in spec/support/ and its subdirectories. | |
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} | |
RSpec.configure do |config| | |
# ... | |
config.include HTTPDigestAuthentication, type: :controller | |
# ... | |
end |
This file contains hidden or 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 goes to: | |
# spec/controller/api/xxx_controller_spec.rb | |
require 'spec_helper' | |
describe Api::XxxController do | |
describe 'index' do | |
it 'is successful' do | |
authenticate_with_http_digest('name', 'password', 'realm') | |
get :index, format: :json | |
response.should be_success | |
end | |
it 'rejects the request' do | |
get :index, format: :json | |
response.should_not be_success | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment