0
votes

I'm trying to test the authenticate_or_request_with_http_basic method using Cucumber and Capybara, but it doesn't work.

Any suggestions?

Authorization Controller:

before_filter :authorize

def show
 flash[:notice] = 'Welcome back!'
end  

private

def authorize
 authenticate_or_request_with_http_basic do |username, password|
  username == "admin" && password == "password"
 end
end

Cucumber feature:

Scenario: Successful login
  When I log in as "admin" with "password"
  Then I should see "Welcome back!"

Cucumber step:

When /^I log in as "([^\"]*)" with "([^\"]*)"$/ do |username, password|
  visit authorization_path
  authorize username, password
end

Error message:

expected there to be content "Welcome back!" in "HTTP Basic: Access denied.\n" (RSpec::Expectations::ExpectationNotMetError)

I also have tried with the following command, but it doesn't work either:

page.driver.browser.basic_authorize(username, password)
3
Can you clarify "doesn't work" please? you'll need to explain to us how it doesn't work. What error message are you getting at what point? Is there a stacktrace? what does it say in the logs? etc. - Taryn East
Taryn, the page gives a HTTP Basic: Access denied because the user cannot be authorized. I can login via browser just fine. If I skip the authorization via browser, I receive the same error message that cucumber is showing. - luizbranco

3 Answers

2
votes

Use the selenium web driver and visit the website with page.driver.visit("https://username:[email protected]")

0
votes

Ok, so - at what point is the information failing to get through. Try adding log/puts statements in appropriate locations to probe this.

When /^I log in as "([^\"]*)" with "([^\"]*)"$/ do |username, password|
  puts "got into the test-helper with: #{username.inspect} and #{password.inspect}"
  visit authorization_path
  authorize username, password
end


def authorize
  puts "in the authorize method before actually trying to authorize"
  authenticate_or_request_with_http_basic do |username, password|
    puts "inside authorize, trying to authenticate against: #{username.inspect} and #{password.inspect}"
    username == "admin" && password == "password"
  end
end

If you can see either one in the log/output then at least you'll know that the uername/password is getting to that point in the code

0
votes

I think that you are showing the welcome message on another page. If this is the case there is a step missing.

Scenario: Successful login
  When I log in as "admin" with "password"
  Then I should be on some page
  Then I should see "Welcome back!"