在Rails 3.2.3中使用Cucumber和RSpec测试HTTP基本身份验证 [英] Testing http basic authentication with Cucumber and RSpec in Rails 3.2.3

查看:89
本文介绍了在Rails 3.2.3中使用Cucumber和RSpec测试HTTP基本身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试Rails 3.2.3中的内置基本http身份验证机制.我已经尝试在RSpec和Cucumber中测试http身份验证,但是两个工具中的步骤均失败.在黄瓜中,我在运行我的功能时收到以下消息:

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"'

这是我的ApplicationController类:

Here is my ApplicationController class:

class ApplicationController < ActionController::Base
  protect_from_forgery

  http_basic_authenticate_with :name => "admin", :password => "test"
end

黄瓜中的步骤定义:

When /^I perform HTTP authentication as "([^\"]*)" with "([^\"]*)"$/ do |username, password|
  @request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("username:password")
  visit '/'
end

黄瓜功能:

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."

在RSpec中,http身份验证步骤似乎已通过,但是我从后续步骤中得到消息,因为访问被拒绝",它无法在页面上找到预期的内容:

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)>'

这是我的相关规格:

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

我也尝试过在黄瓜步骤中的http授权行之前访问根路径,但这给了我关于 @request 的NilClass的相同信息. 使用定义的"admin:test"凭据通过浏览器登录没问题,我可以看到我的应用程序的根页面.

如果有任何建议,我将不胜感激.

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.

推荐答案

这就是我如何使其工作的方式.关键是在访问任何页面之前运行身份验证步骤,否则您已经通过了身份验证.

Here's how I got it to work. The key is to run your authentication step before you visit any pages, else you've already failed the authentication.

在我的专题文章中,我输入:

In my Feature I put:

Background:
    Given I perform HTTP authentication as "<id>/<password>"
    When I go to the homepage
    Then I should see "Text-that-you-should-see-on-your-home-page"


HTTP身份验证的步骤定义如下(我在其他地方找到了此代码,并添加了puts语句以查看发生了什么事情.)


The step definition for the HTTP authentication is as follows (I found this code elsewhere and added the puts statements to see what was going on).

Given /^I perform HTTP authentication as "([^\"]*)\/([^\"]*)"$/ do |username, password|
    puts "id/pswd: #{username}/#{password}"
    ### Following works ONLY if performed first before even going to a page!!!
    if page.driver.respond_to?(:basic_auth)
        puts 'Responds to basic_auth'
        page.driver.basic_auth(username, password)
    elsif page.driver.respond_to?(:basic_authorize)
        puts 'Responds to basic_authorize'
        page.driver.basic_authorize(username, password)
    elsif page.driver.respond_to?(:browser) && page.driver.browser.respond_to?(:basic_authorize)
        puts 'Responds to browser_basic_authorize'
        page.driver.browser.basic_authorize(username, password)
    else
        raise "I don't know how to log in!"
    end
end

这篇关于在Rails 3.2.3中使用Cucumber和RSpec测试HTTP基本身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆