Skip to content

Commit

Permalink
fix #authorize with user key
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Cichra committed Jan 9, 2014
1 parent 929ca27 commit 9aca14c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
32 changes: 20 additions & 12 deletions lib/3scale/client.rb
Expand Up @@ -183,11 +183,7 @@ def report(*transactions)
# end
#
def authorize(options)
path = "/transactions/authorize.xml" +
"?provider_key=#{CGI.escape(provider_key)}" +
"&app_id=#{CGI.escape(options[:app_id].to_s)}"
path += "&app_key=#{CGI.escape(options[:app_key])}" if options[:app_key]
path += "&service_id=#{CGI.escape(options[:service_id])}" if options[:service_id]
path = "/transactions/authorize.xml" + options_to_params(options, ALL_PARAMS)

uri = URI.parse("http://#{host}#{path}")
http_response = Net::HTTP.get_response(uri)
Expand Down Expand Up @@ -234,12 +230,7 @@ def authorize(options)
# end
#
def oauth_authorize(options)
path = "/transactions/oauth_authorize.xml" +
"?provider_key=#{CGI.escape(provider_key)}" +
"&app_id=#{CGI.escape(options[:app_id].to_s)}"
path += "&app_key=#{CGI.escape(options[:app_key])}" if options[:app_key]
path += "&service_id=#{CGI.escape(options[:service_id])}" if options[:service_id]
path += "&redirect_url=#{CGI.escape(options[:redirect_url])}" if options[:redirect_url]
path = "/transactions/oauth_authorize.xml" + options_to_params(options, OAUTH_PARAMS)

uri = URI.parse("http://#{host}#{path}")
http_response = Net::HTTP.get_response(uri)
Expand All @@ -256,6 +247,23 @@ def oauth_authorize(options)

private

OAUTH_PARAMS = [:app_id, :app_key, :service_id, :redirect_url]
ALL_PARAMS = [:user_key, :app_id, :app_key, :service_id, :redirect_url]

def options_to_params(options, allowed_keys)
params = { :provider_key => provider_key }

allowed_keys.each do |key|
params[key] = options[key] if options.has_key?(key)
end

tuples = params.map do |key, value|
"#{key}=#{CGI.escape(value.to_s)}"
end

'?' + tuples.join('&')
end

def encode_transactions(transactions)
result = {}

Expand Down Expand Up @@ -320,4 +328,4 @@ def build_error_response(body)
response
end
end
end
end
2 changes: 1 addition & 1 deletion lib/3scale/client/version.rb
@@ -1,5 +1,5 @@
module ThreeScale
class Client
VERSION = '2.3.3'
VERSION = '2.3.4'
end
end
14 changes: 13 additions & 1 deletion test/client_test.rb
Expand Up @@ -121,6 +121,18 @@ def test_successful_authorize_with_app_keys
assert response.success?
end

def test_successful_authorize_with_user_key
body = '<status>
<authorized>true</authorized>
<plan>Ultimate</plan>
</status>'

FakeWeb.register_uri(:get, "http://#{@host}/transactions/authorize.xml?provider_key=1234abcd&user_key=foo", :status => ['200', 'OK'], :body => body)

response = @client.authorize(:user_key => 'foo')
assert response.success?
end

def test_authorize_with_exceeded_usage_limits
body = '<status>
<authorized>false</authorized>
Expand Down Expand Up @@ -364,4 +376,4 @@ def assert_authrep_url_with_params(str)
# this is the actual assertion, if fakeweb raises the client is submiting with wrong params
FakeWeb.register_uri(:get, parsed_authrep_url, :status => ['200', 'OK'], :body => body)
end
end
end

0 comments on commit 9aca14c

Please sign in to comment.