Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save peterdavidhamilton/6ff6dde3f6f899e0cc35accff5336459 to your computer and use it in GitHub Desktop.
Save peterdavidhamilton/6ff6dde3f6f899e0cc35accff5336459 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment