Permalink
Browse files

Bring back authorize method, now we have authorize and oauth_authorize

  • Loading branch information...
1 parent da8c940 commit 061409789a8a781fa47c98816d937fab034e63cb @jaimeiniesta jaimeiniesta committed Mar 1, 2012
Showing with 174 additions and 2 deletions.
  1. +52 −2 lib/3scale/client.rb
  2. +108 −0 test/client_test.rb
  3. +14 −0 test/remote_test.rb
View
@@ -109,6 +109,54 @@ def report(*transactions)
end
end
+ # Authorize an application.
+ #
+ # == Parameters
+ #
+ # Hash with options:
+ #
+ # app_id:: id of the application to authorize. This is required.
+ # app_key:: secret key assigned to the application. Required only if application has
+ # a key defined.
+ #
+ # == Return
+ #
+ # An ThreeScale::AuthorizeResponse object. It's +success?+ method returns true if
+ # the authorization is successful, false otherwise. It contains additional information
+ # about the status of the usage. See the ThreeScale::AuthorizeResponse for more information.
+ # In case of error, the +error_code+ returns code of the error and +error_message+
+ # human readable error description.
+ #
+ # In case of unexpected internal server error, this method raises a ThreeScale::ServerError
+ # exception.
+ #
+ # == Examples
+ #
+ # response = client.authorize(:app_id => '1234')
+ #
+ # if response.success?
+ # # All good. Proceed...
+ # 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]
+
+ uri = URI.parse("http://#{host}#{path}")
+ http_response = Net::HTTP.get_response(uri)
+
+ case http_response
+ when Net::HTTPSuccess,Net::HTTPConflict
+ build_authorize_response(http_response.body)
+ when Net::HTTPClientError
+ build_error_response(http_response.body)
+ else
+ raise ServerError.new(http_response)
+ end
+ end
+
# Authorize an application with OAuth.
#
# == Parameters
@@ -195,8 +243,10 @@ def build_authorize_response(body)
response.error!(doc.at_css('reason').content)
end
- response.app_key = doc.at_css('application key').content.to_s.strip
- response.redirect_url = doc.at_css('application redirect_url').content.to_s.strip
+ if doc.at_css('application')
+ response.app_key = doc.at_css('application key').content.to_s.strip
+ response.redirect_url = doc.at_css('application redirect_url').content.to_s.strip
+ end
response.plan = doc.at_css('plan').content.to_s.strip
View
@@ -25,6 +25,114 @@ def test_default_host
assert_equal 'su1.3scale.net', client.host
end
+ def test_successful_authorize
+ body = '<status>
+ <authorized>true</authorized>
+ <plan>Ultimate</plan>
+
+ <usage_reports>
+ <usage_report metric="hits" period="day">
+ <period_start>2010-04-26 00:00:00 +0000</period_start>
+ <period_end>2010-04-27 00:00:00 +0000</period_end>
+ <current_value>10023</current_value>
+ <max_value>50000</max_value>
+ </usage_report>
+
+ <usage_report metric="hits" period="month">
+ <period_start>2010-04-01 00:00:00 +0000</period_start>
+ <period_end>2010-05-01 00:00:00 +0000</period_end>
+ <current_value>999872</current_value>
+ <max_value>150000</max_value>
+ </usage_report>
+ </usage_reports>
+ </status>'
+
+ FakeWeb.register_uri(:get, "http://#{@host}/transactions/authorize.xml?provider_key=1234abcd&app_id=foo", :status => ['200', 'OK'], :body => body)
+
+ response = @client.authorize(:app_id => 'foo')
+
+ assert response.success?
+ assert_equal 'Ultimate', response.plan
+ assert_equal 2, response.usage_reports.size
+
+ assert_equal :day, response.usage_reports[0].period
+ assert_equal Time.utc(2010, 4, 26), response.usage_reports[0].period_start
+ assert_equal Time.utc(2010, 4, 27), response.usage_reports[0].period_end
+ assert_equal 10023, response.usage_reports[0].current_value
+ assert_equal 50000, response.usage_reports[0].max_value
+
+ assert_equal :month, response.usage_reports[1].period
+ assert_equal Time.utc(2010, 4, 1), response.usage_reports[1].period_start
+ assert_equal Time.utc(2010, 5, 1), response.usage_reports[1].period_end
+ assert_equal 999872, response.usage_reports[1].current_value
+ assert_equal 150000, response.usage_reports[1].max_value
+ end
+
+ def test_successful_authorize_with_app_keys
+ body = '<status>
+ <authorized>true</authorized>
+ <plan>Ultimate</plan>
+ </status>'
+
+ FakeWeb.register_uri(:get, "http://#{@host}/transactions/authorize.xml?provider_key=1234abcd&app_id=foo&app_key=toosecret", :status => ['200', 'OK'], :body => body)
+
+ response = @client.authorize(:app_id => 'foo', :app_key => 'toosecret')
+ assert response.success?
+ end
+
+ def test_authorize_with_exceeded_usage_limits
+ body = '<status>
+ <authorized>false</authorized>
+ <reason>usage limits are exceeded</reason>
+
+ <plan>Ultimate</plan>
+
+ <usage_reports>
+ <usage_report metric="hits" period="day" exceeded="true">
+ <period_start>2010-04-26 00:00:00 +0000</period_start>
+ <period_end>2010-04-27 00:00:00 +0000</period_end>
+ <current_value>50002</current_value>
+ <max_value>50000</max_value>
+ </usage_report>
+
+ <usage_report metric="hits" period="month">
+ <period_start>2010-04-01 00:00:00 +0000</period_start>
+ <period_end>2010-05-01 00:00:00 +0000</period_end>
+ <current_value>999872</current_value>
+ <max_value>150000</max_value>
+ </usage_report>
+ </usage_reports>
+ </status>'
+
+ FakeWeb.register_uri(:get, "http://#{@host}/transactions/authorize.xml?provider_key=1234abcd&app_id=foo", :status => ['409'], :body => body)
+
+ response = @client.authorize(:app_id => 'foo')
+
+ assert !response.success?
+ assert_equal 'usage limits are exceeded', response.error_message
+ assert response.usage_reports[0].exceeded?
+ end
+
+ def test_authorize_with_invalid_app_id
+ body = '<error code="application_not_found">application with id="foo" was not found</error>'
+
+ FakeWeb.register_uri(:get, "http://#{@host}/transactions/authorize.xml?provider_key=1234abcd&app_id=foo", :status => ['403', 'Forbidden'], :body => body)
+
+ response = @client.authorize(:app_id => 'foo')
+
+ assert !response.success?
+ assert_equal 'application_not_found', response.error_code
+ assert_equal 'application with id="foo" was not found', response.error_message
+ end
+
+ def test_authorize_with_server_error
+ FakeWeb.register_uri(:get, "http://#{@host}/transactions/authorize.xml?provider_key=1234abcd&app_id=foo", :status => ['500', 'Internal Server Error'], :body => 'OMG! WTF!')
+
+ assert_raise ThreeScale::ServerError do
+ @client.authorize(:app_id => 'foo')
+ end
+ end
+
def test_successful_oauth_authorize
body = '<status>
<authorized>true</authorized>
View
@@ -21,6 +21,20 @@ def setup
end
end
+ def test_successful_authorize
+ @app_keys.each do |app_key|
+ response = @client.authorize(:app_id => @app_ids[0], :app_key => app_key)
+ assert response.success?, "Authorize should succeed for app_id=#{@app_ids[0]} and app_key=#{app_key}, but it failed with: '#{response.error_message}'"
+ end
+ end
+
+ def test_failed_authorize
+ response = @client.authorize(:app_id => 'invalid-id')
+ assert !response.success?
+ assert_equal 'application_not_found', response.error_code
+ assert_equal 'application with id="invalid-id" was not found', response.error_message
+ end
+
def test_successful_oauth_authorize
@app_keys.each do |app_key|
response = @client.oauth_authorize(:app_id => @app_ids[0])

0 comments on commit 0614097

Please sign in to comment.