【发布时间】:2012-11-27 00:53:53
【问题描述】:
我一直在为在 Rails 功能测试中使用 OAuth 签名请求的问题而烦恼。
我很感激帮助,或指向工作示例的指针。
我正在尝试使用 oauth gem (0.4.5) 中的内置 ActionController::TestRequest 覆盖。
我已经尝试过这个解决方案,但无济于事:http://d.hatena.ne.jp/falkenhagen/20091110/1257830144
这就是我现在正在做的……
require 'oauth/client/action_controller_request'
我创建了一种登录方法,我可以将我的一个 OauthConsumer 对象 (ActiveRecord) 和我的 URL 参数(用于查询字符串)传递给该方法。
def _do_oauth(consumer, params = {})
c=OAuth::Consumer.new(consumer.consumer_key, consumer.consumer_secret)
t=OAuth::AccessToken.new(c)
ActionController::TestRequest.use_oauth=true
@request.configure_oauth(c, t, params)
end
在我的测试用例中这样称呼它:
params = { :store => 'foo' }
_do_oauth(oauth_consumers(:one), params) # currently not working for passing params
get :index, { :format => :json }.merge(params)
但看起来请求并没有获取“参数”或正确编码它们。
我得到的错误是(发生在上面的“get”行):
ArgumentError: comparison of Array with Array failed
/home/sp/.rvm/gems/ruby-1.9.2-p180/gems/oauth-0.4.5/lib/oauth/helper.rb:37:in `sort'
/home/sp/.rvm/gems/ruby-1.9.2-p180/gems/oauth-0.4.5/lib/oauth/helper.rb:37:in `normalize'
/home/sp/.rvm/gems/ruby-1.9.2-p180/gems/oauth-0.4.5/lib/oauth/request_proxy/base.rb:98:in `normalized_parameters'
/home/sp/.rvm/gems/ruby-1.9.2-p180/gems/oauth-0.4.5/lib/oauth/request_proxy/base.rb:113:in `signature_base_string'
/home/sp/.rvm/gems/ruby-1.9.2-p180/gems/oauth-0.4.5/lib/oauth/signature/base.rb:77:in `signature_base_string'
/home/sp/.rvm/gems/ruby-1.9.2-p180/gems/oauth-0.4.5/lib/oauth/signature/hmac/base.rb:12:in `digest'
/home/sp/.rvm/gems/ruby-1.9.2-p180/gems/oauth-0.4.5/lib/oauth/signature/base.rb:65:in `signature'
/home/sp/.rvm/gems/ruby-1.9.2-p180/gems/oauth-0.4.5/lib/oauth/signature.rb:23:in `sign'
/home/sp/.rvm/gems/ruby-1.9.2-p180/gems/oauth-0.4.5/lib/oauth/client/helper.rb:45:in `signature'
/home/sp/.rvm/gems/ruby-1.9.2-p180/gems/oauth-0.4.5/lib/oauth/client/helper.rb:75:in `header'
/home/sp/.rvm/gems/ruby-1.9.2-p180/gems/oauth-0.4.5/lib/oauth/client/action_controller_request.rb:54:in `set_oauth_header'
/home/sp/.rvm/gems/ruby-1.9.2-p180/gems/oauth-0.4.5/lib/oauth/client/action_controller_request.rb:50:in `apply_oauth!'
/home/sp/.rvm/gems/ruby-1.9.2-p180/gems/oauth-0.4.5/lib/oauth/client/action_controller_request.rb:14:in `process_with_new_base_test'
/home/sp/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.7/lib/action_controller/test_case.rb:412:in `process'
/home/sp/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.7/lib/action_controller/test_case.rb:47:in `process'
/home/sp/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.7/lib/action_controller/test_case.rb:350:in `get'
test/functional/deals_controller_test.rb:56:in `block in <class:DealsControllerTest>'
我假设这与查询参数未正确编码或标头格式不正确有关。任何帮助(甚至指向有效示例的指针)都将不胜感激。
我还应该指出,我正在尝试测试的相关应用是一个 2-legged OAuth 提供程序。因此,该应用程序只是解析签名并检查消费者密钥/秘密是否签出。
【问题讨论】:
-
当参数顺序不正确时我遇到了问题。也许可以帮助。不知道。
-
是的,我相信可能是这样。我很高兴看到一个“正确”方法的例子。
-
你有没有让你的规范在一个 2-leged oauthenticated 控制器上工作?
标签: ruby-on-rails oauth