2012-02-05 11 views
7

मैं अपने एपीआई के लिए 2-लीगड ओएथ प्रदाता का निर्माण कर रहा हूं। सब कुछ ठीक से लगाया गया है और मैं रेल कंसोल से हस्ताक्षरित कॉल कर सकता हूं। मेरी समस्या यह है कि मुझे OAuth को controller_spec में एकीकृत करने में समस्या हो रही है।मैं हस्ताक्षर किए गए OAuth अनुरोध का उपयोग करने के लिए नियंत्रक चश्मा कैसे कह सकता हूं

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 

और यहाँ है मैं अपने नियंत्रक परीक्षण में क्या कर रहा हूँ:

यहाँ मेरी सर्वर पर काम कर रहे कॉल का एक उदाहरण है

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 

है कि परीक्षण के उत्पादन:

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 

और रेल लॉग से संबंधित सर्वर कॉल:

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 

मैं यह नहीं समझ सकता कि यह क्यों काम नहीं कर रहा है:/क्या मैं एक स्पष्ट गलती कर रहा हूं?

+0

लगता है यह वास्तव में एक नियंत्रक नहीं है की तरह कल्पना। क्या आपने इसे एक अनुरोध के रूप में प्रयोग किया था? – apneadiving

+0

यह एक नियंत्रक कल्पना है। उदाहरण सिर्फ अपने बेयर जरूरी चीजों को तोड़ दिया गया था। यदि आप सही हैं, और इस प्रकार के परीक्षण को अनुरोध spec में जाना चाहिए, तो जब मैं इसे OAuth द्वारा संरक्षित करता हूं तो मुझे अपने नियंत्रक का परीक्षण कैसे करना चाहिए? – coneybeare

+0

मैं इन दिनों और नियंत्रक चश्मे में एक एपीआई का परीक्षण कर रहा हूं, मैं बस हेडर में उचित मान पास करता हूं। मुझे खेद है, मैं वास्तव में ओथ के लिए नहीं जानता। मुझे आपके मामले में क्या डर है कि 'create_signed_request' को अपेक्षित परिणाम नहीं मिलते हैं। क्या आपने इसे डीबग करने का प्रयास किया था? बस सोच रहा है: – apneadiving

उत्तर

0

यह पता चला है कि मेरे नियंत्रक का परीक्षण करने का सबसे अच्छा तरीका भी सबसे सरल था। प्रत्येक परीक्षण पर हस्ताक्षर करने की कोशिश करने के बजाय नियंत्रक को सही जानकारी मिलती है (कुछ जो वास्तव में एक अनुरोध में संबंधित होता है, एक नियंत्रक spec नहीं है), मुझे लगा कि मैं नियंत्रक को मैन्युअल रूप से आवश्यक जानकारी दे सकता हूं।

ऐसा करने के लिए, मैं बस 2 तरीकों ठूंठ था:

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 

allow? विधि छोटा करते वजह से रैक प्रमाणन सोच यह प्रमाणीकृत किया गया में मूर्ख बनाया जा करने के लिए। allow? ने क्रेडेंशियल्स के आधार पर client_application भी सेट किया है, इसलिए मुझे इसे भी रोकना पड़ा। अब जब ऑथ रास्ते से बाहर है, तो मैं अपने नियंत्रक का सही ढंग से परीक्षण कर सकता हूं।

0

मैं यह देखता हूं कि Omniauth परीक्षण सहायक कैसे काम करते हैं, विशेष रूप से इन फ़ाइलों: https://github.com/intridea/omniauth/tree/master/lib/omniauth/test। यह कैसे स्थापित किया गया है इसके विचारों के लिए उनके wiki page on integration testing देखें। मुझे एहसास है कि आप एक प्रदाता का निर्माण कर रहे हैं, ग्राहक नहीं, लेकिन यह एक अच्छा प्रारंभिक बिंदु हो सकता है। साथ ही, जैसा कि कुछ टिप्पणीकार पहले ही कह चुके हैं, मुझे नहीं पता कि आप इसे नियंत्रक परीक्षण के साथ कर सकते हैं या नहीं; रैक पर्यावरण को पूरी तरह अनुकरण करने के लिए आपको अनुरोध या एकीकरण परीक्षण की आवश्यकता हो सकती है।

1

आप एक अनुरोध कल्पना में यह परीक्षण करना चाहते हैं और वास्तव में छोटा करते बिना परीक्षण करने के लिए की जरूरत है, तो आप एक ता का निर्माण और इस तरह के अनुरोध पर हस्ताक्षर कर सकते हैं:

@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 } 
संबंधित मुद्दे