簡體   English   中英

Rspec Rails 4.2.5使用基本的HTTP身份驗證請求測試通過

[英]Rspec Rails 4.2.5 Request test pass with basic http auth

設置如下。 對於每個http請求,管理器都會在標頭(name,pw)中發送其憑據。 將對照db中的條目檢查這些條目,如果它們成功返回了所需的用戶對象。 在請求測試中如何實現基本的http_auth? 我只想添加密碼和用戶名並測試返回值? 請求測試的目標是什么? 我嘗試了以下方法,但沒有獲得很大的成功:

我在spec/support/auth_helper.rb使用以下方法創建了AuthHelper模塊:

module AuthHelper
  def http_login
    user = 'test'
    pw = 'test'
    request.ENV['HTTP_AUTHORIZATION'] =ActionController::HttpAuthentication::Basic.encode_credentials(user,pw)
   end  
end

並按如下所示在requests/api/user_spec.rb使用它

include AuthHelper
 describe "User API get 1 user object" do     
      before(:each) do
               http_login 
       end

但我收到此錯誤消息。 如何解決此問題並使我的測試通過http_auth? 我在這里也閱讀了很多類似的topis和問題,但它們主要適用於rspec和rails的較舊版本,不適用於我的情況

提前致謝!

Failure/Error: request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(user,pw)

 NoMethodError:
   undefined method `env' for nil:NilClass# ./spec/support

 # ./spec/support/auth_helper.rb:5:in `http_login'
 # ./spec/requests/api/user_spec.rb:8:in `block (2 levels) in <top (required)>'

更新

我在請求中移動了標頭生成。 我查了一下Auth動詞,所以我認為該分配應該有效。 我在Rails控制台中測試了ActionController調用,並收到了一個字符串。 我想這也是正確的,我的整個測試現在看起來像這樣:

describe "User API get 1 user object", type: :request do  
      it 'Get sends back one User object ' do             
              headers = {   
                            "AUTHORIZATION" =>ActionController::HttpAuthentication::Basic.encode_credentials("test","test")
   #                        "AUTHORIZATION" =>ActionController::HttpAuthentication::Token.encode_credentials("test","test")
              }
              FactoryGirl.create(:user)
              get "/api/1/user", headers
              #json = JSON.parse(response.body)
              expect(response).to be_success
      #       expect(response.content_type).to eq("application/json")
      end     
  end     

收到以下錯誤:

包含@buf @buf=["HTTP Basic: Access denied.\\n"]因此仍然拒絕訪問。

Failure/Error: expect(response).to be_success
   expected `#<ActionDispatch::TestResponse:0x000000070d1d38 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mutex:0x000000070d1c98>, @stream=#<ActionDispatch::Response::Buffer:0x000000070d1c48 @response=#<ActionDispatch::TestResponse:0x000000070d1d38 ...>, 
 @buf=["HTTP Basic: Access denied.\n"], @closed=false>, @header={"X-Frame-Options"=>"SAMEORIGIN", "X-XSS-Protection"=>"1; mode=block", "X-Content-Type-Options"=>"nosniff", "WWW-Authenticate"=>"Basic realm=\"Application\"", "Content-Type"=>"text/html; charset=utf-8", "Cache-Control"=>"no-cache", "X-Request-Id"=>"9c27d4e9-84c0-4ef3-82ed-cccfb19876a0", "X-Runtime"=>"0.134230", "Content-Length"=>"27"}, @status=401, @sending_file=false, @blank=false, 
@cv=#<MonitorMixin::ConditionVariable:0x000000070d1bf8 @monitor=#<ActionDispatch::TestResponse:0x000000070d1d38 ...>, @cond=#<Thread::ConditionVariable:0x000000070d1bd0>>, @committed=false, @sending=false, @sent=false, @content_type=#<Mime::Type:0x00000002af78f8 @synonyms=["application/xhtml+xml"], @symbol=:html, @string="text/html">, @charset="utf-8", @cache_control={:no_cache=>true}, @etag=nil>.success?` 
to return true, got false

解決方案:該測試尚未打磨(但尚未完成),但至少現在可以通過。

describe "User API get 1 user object", type: :request do  
    it 'Get sends back one User object ' do             
        @env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(user,pw)
              FactoryGirl.create(:user)

        get "/api/1/user", {}, @env

        JSON.parse(response.body)
        expect(response).to be_success
        expect(response.status).to eq 200
    end     
end     

請仔細閱讀錯誤: undefined method `env' for nil:NilClass表示request為nil。 在稍后測試中定義請求時是否要嘗試測試設置標頭?

您可能需要查看文檔,以獲取有關如何設置標題的示例

如果仍然遇到問題,請同時發布其中一項測試。

這行看起來很可疑:

request.ENV['HTTP_AUTHORIZATION'] =ActionController::HttpAuthentication::Basic.encode_credentials(user,pw)

您確定"ENV"應大寫嗎? 我認為應該將其寫為"env"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM