3
다음 테스트에 도움이 필요합니다.토큰 기반 인증을 테스트하려면 어떻게해야합니까?
it 'responds successfully to generic request because of key protection' do
api_key = ApiKey.create
api_key.save!
get :index
request.headers["token"] = api_key.access_token
expect(response).to be_success # test for the 200 status-code
end
:
class RequestsController < ApplicationController
include ActionController::MimeResponds
include ActionController::HttpAuthentication::Token::ControllerMethods
before_filter :restrict_access
respond_to :json
#...
def authenticate
return restrict_access
end
private
def restrict_access
authenticate_or_request_with_http_token do |token, options|
ApiKey.exists?(access_token: token)
end
end
end
내 실패 RSpec에 시험은 다음과 같습니다 내가 요청 토큰을 가지고있는 경우 before_filter
와 RequestController
확인해야 http://railscasts.com/episodes/352-securing-an-api?view=asciicast
: 나는 API를 확보 대한 RailsCast를하고있는 중이 야
결과 : expected success? to return true, got false
어떻게 유효한 api_key를 다시 삽입 할 수 있는지 이해가 안됩니다. 퀘스트를 통해 응답이 사실로 평가됩니다. 어떤 아이디어? 감사.
request.headers["HTTP_AUTHORIZATION"] = "Token token=\"#{api_key.access_token}\""
get :index
당신은 대신 encode_credentials
방법을 사용할 수 있습니다 또한
Token token="my-api-token"
, 당신은 get :index
라인 전에 헤더를 설정할 수 있습니다 :
나는 백합 이름 다른'.headers []'값? 예를 들어,'request.headers [ "username"] = "Joe"'하고 나서'request.headers [ "username"]'를 사용하여 컨트롤러에서 참조 할 수 있습니까? –
두 번째 예에서 access_token 다음에 닫기 대괄호를 추가하는 것을 잊어 버린 것 같습니다. – couchoud