【问题标题】:RoR: testing an action that uses http token authenticationRoR:测试使用 http 令牌身份验证的操作
【发布时间】:2012-08-20 15:59:52
【问题描述】:

我正在尝试测试在前置过滤器中使用 http 令牌身份验证的控制器。我的问题是当我使用 curl 传递令牌时它工作正常,但在我的测试中它总是失败(我正在使用 rspec btw)。尝试了一个简单的测试来查看令牌是否完全通过,但似乎它没有这样做。我是否缺少任何东西来让测试将令牌实际传递给控制器​​?

这是我之前的过滤器:

    def restrict_access
      authenticate_or_request_with_http_token do |token, options|
        api_key = ApiKey.find_by_access_token(token)
        @user = api_key.user unless api_key.nil?
        @token = token #set just for the sake of testing
        !api_key.nil?
      end 
    end

这是我的测试:

    it "passes the token" do
      get :new, nil,
        :authorization => ActionController::HttpAuthentication::Token.encode_credentials("test_access1")

      assigns(:token).should be "test_access1"
    end

【问题讨论】:

    标签: ruby-on-rails testing rspec2


    【解决方案1】:

    我假设 ApiKey 是一个 ActiveRecord 模型,对吗? curl 命令针对开发数据库运行,测试针对测试数据库。我看不到在您的 sn-ps 中设置 ApiKey 的任何内容。除非您在其他地方有它,否则请尝试按照以下方式添加一些内容:

    it "passes the token" do
      # use factory or just create record with AR:
      ApiKey.create!(:access_token => 'test_access1', ... rest of required attributes ...)
    
      # this part remains unchanged
      get :new, nil,
        :authorization => ActionController::HttpAuthentication::Token.encode_credentials("test_access1")
    
      assigns(:token).should be "test_access1"
    end
    

    您可以稍后将其移至before :each 块或支持模块。

    更新:

    看到您的评论后,我不得不更深入地研究。这是另一个猜测。这种形式的get

    get '/path', nil, :authorization => 'string'
    

    应该只在集成测试中起作用。对于控制器测试,身份验证准备应如下所示:

    it "passes the token" do
      request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials("test_access1")
      get :new
      assigns(:token).should be "test_access1"
    end
    

    这背后的原因来自各个测试模块的方法签名:

    # for action_controller/test_case.rb
    def get(action, parameters = nil, session = nil, flash = nil)
    
    # for action_dispatch/testing/integration.rb
    def get(path, parameters = nil, headers = nil)
    

    【讨论】:

    • 感谢您的回答,但测试数据库已经填充了该令牌。问题是测试中没有设置 http_authorization 标头。
    • 哦,我得先问一下db。添加了另一个希望更相关的建议。
    • 谢谢大佬! request.env['HTTP_AUTHORIZATION'] 位成功了。
    猜你喜欢
    • 2011-11-06
    • 1970-01-01
    • 2018-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多