I want to test the build-in basic http authentication mechanism in Rails 3.2.3. I have tried to test the http authentication in both RSpec and Cucumber but with a failing step in both tools. In Cucumber I get the following message on running my Feature:
When I perform HTTP authentication as "admin" with "test" # features/step_definitions/web_steps.rb:1
undefined method `env' for nil:NilClass (NoMethodError)
./features/step_definitions/web_steps.rb:3:in `/^I perform HTTP authentication as "([^\"]*)" with "([^\"]*)"$/'
features/sign_in_http.feature:4:in `When I perform HTTP authentication as "admin" with "test"'
Here is my ApplicationController class:
class ApplicationController < ActionController::Base
protect_from_forgery
http_basic_authenticate_with :name => "admin", :password => "test"
end
The step definition in Cucumber:
When /^I perform HTTP authentication as "([^\"]*)" with "([^\"]*)"$/ do |username, password|
@request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("username:password")
visit '/'
end
The cucumber feature:
Feature: Signing in via http
Scenario: HTTP sign-in
When I perform HTTP authentication as "admin" with "test"
Then I should see "Welcome to the app."
In RSpec the http authentication step seems to pass but I get the message from the subsequent step, that it couldn't find the expected content on the page because "Access was denied":
1) ApplicationController sign in via http should be successful
Failure/Error: page.should have_content("Welcome to the app.")
expected there to be content "Welcome to the app." in "HTTP Basic: Access denied.\n"
# ./spec/controllers/application_controller_spec.rb:12:in `block (3 levels) in <top (required)>'
Here is my associated spec:
require 'spec_helper'
describe ApplicationController do
describe "sign in via http" do
it "should be successful" do
@request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("admin:test")
visit '/'
response.should be_success
page.should have_content("Welcome to the app.")
end
end
end
I have also tried to visit the root path before the http authorization line in the cucumber step but this gave me the same message about the NilClass for @request.
Login via the browser with the defined "admin:test" credentials is no problem and I can see the root page of my app.
I would be thankful for any suggestions.