RickBacci
11/28/2012 - 4:04 AM

RSpec basic authentication helper module for request and controller specs

RSpec basic authentication helper module for request and controller specs

module AuthHelper
  def http_login
    user = 'username'
    pw = 'password'
    request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(user,pw)
  end
end

module AuthRequestHelper
  #
  # pass the @env along with your request, eg:
  #
  # GET '/labels', {}, @env
  #
  def http_login
    @env ||= {}
    user = 'username'
    pw = 'password'
    @env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(user,pw)
  end
end


# then in Rspec support:

RSpec.configure do |config|
  config.include AuthRequestHelper, :type => :request
  config.include AuthHelper, :type => :controller
end


# request specs need to explicitly pass the @env parameter along, eg:
describe "some request" do
  http_login # or put this in a before :all
  GET '/path', {}, @env
end