Sto costruendo un provider OAuth a 2 zampe per la mia api. Tutto è collegato correttamente e posso effettuare chiamate firmate dalla console di rails. Il problema che ho è che ho problemi ad integrare OAuth nello controller_spec
.Come posso comunicare alle specifiche del controller di utilizzare la richiesta OAuth firmata
Ecco un esempio di una chiamata di lavoro sul mio server:
coneybeare $ rails c test
Loading test environment (Rails 3.2.0)
rails test: main
>> consumer = OAuth::Consumer.new("one_key", "MyString", :site => [REDACTED])
# => #<OAuth::Consumer:0x007f9d01252268 @key="one_key", @secret="MyString", @options={:signature_method=>"HMAC-SHA1", :request_token_path=>"/oauth/request_token", :authorize_path=>"/oauth/authorize", :access_token_path=>"/oauth/access_token", :proxy=>nil, :scheme=>:header, :http_method=>:post, :oauth_version=>"1.0", :site=>[REDACTED]}>
ruby: main
>> req = consumer.create_signed_request(:get, "/api/v1/client_applications.json", nil)
# => #<Net::HTTP::Get GET>
ruby: main
>> res = Net::HTTP.start([REDACTED]) {|http| http.request(req) }
# => #<Net::HTTPOK 200 OK readbody=true>
ruby: main
>> puts res.body
{"client_applications":[{"id":119059960,"name":"FooBar1","url":"http://test1.com"},{"id":504489040,"name":"FooBar2","url":"http://test2.com"}]}
# => nil
e qui è quello che sto facendo nel mio test del controller:
require 'oauth/client/action_controller_request'
describe Api::ClientApplicationsController do
include OAuthControllerSpecHelper
…
…
it "assigns all client_applications as @client_applications" do
consumer = OAuth::Consumer.new("one_key", "MyString", :site => [REDACTED])
ActionController::TestRequest.use_oauth=true
@request.configure_oauth(consumer)
@request.apply_oauth!
puts "request.env['Authorization'] = #{@request.env['Authorization']}"
get :index, {:api_version => 'v1', :format => :json}
response.should be_success # Just this for now until I can get authorization, then proper controller testing
end
end
L'uscita di questo test:
request.env['Authorization'] = OAuth oauth_consumer_key="one_key", oauth_nonce="gzAbvBSWyFtIYKfuokMAdu6VnH39EHeXvebbH2qUtE", oauth_signature="juBkJo5K0WLu9mYqHVC3Ar%2FATUs%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1328474800", oauth_version="1.0"
1) Api::ClientApplicationsController GET index assigns all client_applications as @client_applications
Failure/Error: response.should be_success
expected success? to return true, got false
E la corrispondente chiamata al server dal registro rails:
Processing by Api::ClientApplicationsController#index as JSON
Parameters: {"api_version"=>1}
Rendered text template (0.0ms)
Filter chain halted as #<OAuth::Controllers::ApplicationControllerMethods::Filter:0x007f85a51a8858 @options={:interactive=>false, :strategies=>:two_legged}, @strategies=[:two_legged]> rendered or redirected
Completed 401 Unauthorized in 15ms (Views: 14.1ms | ActiveRecord: 0.0ms)
(0.2ms) ROLLBACK
Non riesco proprio a capire perché non funziona:/Sto facendo un errore evidente?
suona come in realtà non è un controller spec. Hai provato come una richiesta specifica? – apneadiving
È una specifica del controller. L'esempio è stato semplicemente ridotto al minimo indispensabile. Se sei corretto, e questo tipo di test dovrebbe andare nella specifica della richiesta, in quale altro modo dovrei testare il mio controller quando l'ho protetto da OAuth? – coneybeare
Attualmente sto testando un'API e nelle specifiche del controller, ho semplicemente passato i valori corretti nell'intestazione. Mi dispiace, non lo so davvero per OAuth. Quello che temo nel tuo caso è che 'create_signed_request' non ottiene il risultato atteso. Hai provato a eseguirne il debug? Solo pensando: puoi bypassare alcuni 'before_filters' durante il test – apneadiving