2012-02-05 20 views
7

Api için 2 Bacaklı bir OAuth sağlayıcısı yapıyorum. Her şey doğru şekilde bağlanmış ve ben de konsol konsolundan imzalı aramalar yapabilirim. Sahip olduğum sorun, OAuth'ı controller_spec'a entegre etmede sorun yaşıyorum. Benim denetleyicisi testlerinde ne yapıyorum buradaİmzalanmış OAuth İsteğini Kullanmak için Denetçi Özelliklerine Nasıl Anlatabilirim

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 

Ve edilir: Burada

benim sunucuda çalışan bir çağrının bir örnektir

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 

o testin çıkışı:

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 

Ve karşılık gelen sunucu çağrıları raylardan günlüğe kaydet:

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 

Neden çalışmadığını anlayamıyorum:/Açık bir hata mı yapıyorum?

+0

sesleri spec. Bir istek şartı olarak denedin mi? – apneadiving

+0

Bir denetleyici spek. Örnek, çıplak temellerine kadar soyuldu. Doğru iseniz ve bu tür bir sınama istek türüne giriyorsa, denetleyicimi OAuth tarafından korunduğumda nasıl test etmem gerekiyor? – coneybeare

+0

Bu günlerde bir API'yi test ediyorum ve denetleyici spesifikasyonlarında, sadece başlıktaki uygun değerleri iletiyorum. Üzgünüm, OAuth için gerçekten bilmiyorum. Durumunuzda korktuğum şey, 'create_signed_request' beklenen sonucu almıyor olmasıdır. Hata ayıklamaya çalıştın mı? Sadece düşünüyorum: – apneadiving

cevap

0

Denetleyicimi test etmenin en iyi yolunun en kolay olduğu ortaya çıktı. Her bir testi imzalamaya çalışmak yerine, denetleyici doğru bilgileri alır (gerçekte bir denetleyici tarafından değil, bir istek isteğine aittir), denetleyiciye el ile gereken bilgileri verebildiğimi anladım.

Bunu yapmak için, ben sadece 2 yöntemlerini saplama zorunda: allow? yöntemi stubbing

fixtures :client_applications 
before(:each) do 
    @client_application1 = client_applications(:client_application1) 
    Api::ClientApplicationsController::Authenticator.any_instance.stub(:allow?).and_return(true) 
    controller.stub(:client_application).and_return(@client_application1) 
end 

raf auth o doğrulanmış oldu aptal düşünce neden oldu. allow? ayrıca kimlik bilgilerine dayanarak client_application'u da ayarladı; Artık, auth yoldan çıktı, kontrol cihazımı düzgün bir şekilde test edebilirim. Eğer bir istek spec bunu test etmek ve aslında stubbing olmadan test etmek gerekiyordu olursa

0

Omniauth sınama yardımcılarının nasıl çalıştığını, özellikle şu dosyaları nasıl izleyeceğimi bir göz atalım: https://github.com/intridea/omniauth/tree/master/lib/omniauth/test. Bunun nasıl kurulacağına dair fikir edinmek için wiki page on integration testing numaralarına bakın. Bir sağlayıcı değil, bir müşteri oluşturduğunuzu biliyorum, ancak bu iyi bir başlangıç ​​noktası olabilir. Ayrıca, bazı yorumcuların söyledikleri gibi, bunu bir denetleyici testi ile yapıp yapamayacağınızı bilmiyorum; Raf ortamını tam olarak simüle etmek için bir istek veya entegrasyon testine ihtiyacınız olabilir.

1

, OAuth tüketici oluşturabilir ve bunun gibi bir istek oturum: gerçekten bir kontrolör yokmuş gibi

@access_token = FactoryGirl.create :access_token 
    @consumer = OAuth::Consumer.new(@access_token.app.key, @access_token.app.secret, :site => "http://www.example.com/") 
    @path = "/path/to/request" 
    @request = @consumer.create_signed_request(:get, @path, OAuth::AccessToken.new(@consumer, @access_token.token, @access_token.secret)) 
    get @path, nil, { 'HTTP_AUTHORIZATION' => @request.get_fields('authorization').first } 
İlgili konular