stuffinq2010
stuffinq2010

Reputation: 21

Rails Functional Tests and OAuth Parameter problem

I've been pulling my hair out over an issue with using OAuth signed requests in Rails functional tests.

I'd appreciate help, or pointers to working examples.

I'm trying to work with the built in ActionController::TestRequest overrides that are in the oauth gem (0.4.5).

I'd already tried this solution to no avail: http://d.hatena.ne.jp/falkenhagen/20091110/1257830144

This is what I'm doing now...

require 'oauth/client/action_controller_request'

I've created a method for doing the login to which I can pass one of my OauthConsumer objects (ActiveRecord), and my URL parameters (for the query string).

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

and call it like so in my test case:

params = { :store => 'foo' }
_do_oauth(oauth_consumers(:one), params) # currently not working for passing params
get :index, { :format => :json }.merge(params)

But it doesn't look like the requests are pick up the "params" or encoding them properly.

The error I'm getting is (which occurs on the "get" line above):

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>'

I'm assuming it's something to do with the query params not being encoded correctly, or the header not being formatted properly. Any help (or even pointers to examples that do work) would be greatly appreciated.

I should also point out that the app in question I am trying to test is a 2-legged OAuth provider. So, the app is just parsing the signature and checking that the consumer key/secret check out.

Upvotes: 2

Views: 481

Answers (1)

Kamal
Kamal

Reputation: 41

This probably won't help with the initial problem at this point but it might save someone a few minutes.

The problem is that the sort method on hash freaks out if a hash has a mixture of symbol and string keys. Oauth adds some entries keyed by strings into the params hash.

Upvotes: 4

Related Questions