【问题标题】:auth-hmac functional testing problemauth-hmac 功能测试问题
【发布时间】:2011-03-29 14:38:15
【问题描述】:

我想在我的 api 应用程序中使用这个 gem https://github.com/seangeo/auth-hmac/

我有一个关于为请求身份验证创建测试的问题。 我想用 hmac 签署请求,但是 rails 控制器在下一个代码之后没有 http 标头

 def setup
    #load from fixture
     @client = clients(:client_2)


 end

 def sign_valid_request(request,client)
    auth_hmac = AuthHMAC.new(client.key => client.secret )
    auth_hmac.sign!(request,client.key)
    request
  end

 def test_response_client_xml

      @request =  sign_valid_request(@request,@client)
      get :index  , :api_client_key => @client.key , :format=> "xml"
      @xml_response = @response.body

      assert_response :success
      assert_select   'id' , @client.id.to_s
end

路由有这样的配置

scope '/:token/' do
     # route only json & xml format
    constraints :format=> /(json|xml)/  do
       resources :clients, :only => [:index]
    end

  end

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 functional-testing


    【解决方案1】:

    我在功能测试方面遇到了同样的问题。要使用 AuthHMAC 正确签署每个请求,您应该将以下内容放入您的 test_helper.rb

    def with_hmac_signed_requests(access_key_id, secret, &block)
      unless ActionController::Base < ActionController::Testing
        ActionController::Base.class_eval { include ActionController::Testing }
      end
    
      @controller.instance_eval %Q(
        alias real_process_with_new_base_test process_with_new_base_test
    
        def process_with_new_base_test(request, response)
          signature = AuthHMAC.signature(request, "#{secret}")
          request.env['Authorization'] = "AuthHMAC #{access_key_id}:" + signature
          real_process_with_new_base_test(request, response)
        end
      )
    
      yield
    
      @controller.instance_eval %Q(
        undef process_with_new_base_test
        alias process_with_new_base_test real_process_with_new_base_test
      )
    end
    

    然后在您的功能测试中:

    test "secret_method should be protected by an HMAC signature" do
        with_hmac_signed_requests(key_id, secret) do
          get :protected_method
          assert_response :success
        end
    end
    

    【讨论】:

      【解决方案2】:

      你可以试试这个解决方案

        def sign_valid_request(request,client)
          auth_hmac = AuthHMAC.new(client.key => client.secret )
          auth_hmac.sign!(request,client.key)
          # because this would be deleted in request.recycle! method in test framework
          request.env.merge!(request.env['action_dispatch.request.parameters'])
          request
        end
      

      由于 Rails 3 测试单元框架删除了 action_dispatch.request 中的所有值,因此在此处添加了行 request.env.merge!(request.env['action_dispatch.request.parameters'])

      您可以在此处找到此行为:https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/test_case.rb#L404

      【讨论】:

      • 你是对的 request.recycle!但是我怎样才能根据规范进行全面测试..一些请求参数应该作为路由的一部分进入路径,其他不应该......例如我应该创建包含令牌的请求路径
      • 所有参数都应作为get 方法中的第二个参数传递:get :index, {:id =&gt; 5, :api_client_key =&gt; @client.key, :format=&gt; "xml"}。因此它们都可以通过控制器中的params 散列可见,并且格式类型将被正确应用。
      • auth_hmac.sign!(request,client.key) 方法请求必须包含预定义的路径和其他参数才能正确签署请求。如何预定义必须是 path 一部分的参数和应该是 request_uri 一部分的参数?
      • 抱歉,我不知道是否可以使用 AuthHMAC 签署 testrequest,但是使用 gem mocha: ItemsController.any_instance.expects('hmac_authenticated?').returns(true) 完全可以跳过此检查。您可以检查 AuthHMAC 是否与异地消费者正常工作。您也可以尝试集成测试。
      • 我认为现在最好不要在测试环境中使用 hmac_authentification 。它将被硬编码,但我为此浪费了很多时间。稍后我会尝试集成测试......无论如何赏金是你的!如果您有什么要补充的,我将不胜感激
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-05
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多