Permalink
Browse files

Changes to conform to the newest API version

  • Loading branch information...
1 parent ef63943 commit 15d763f46a7626d273e2d48d6981147f025deca8 @madadam madadam committed Sep 3, 2010
@@ -3,8 +3,8 @@
require 'net/http'
require 'nokogiri'
-require 'three_scale/response'
-require 'three_scale/authorize_response'
+require '3scale/response'
+require '3scale/authorize_response'
module ThreeScale
Error = Class.new(RuntimeError)
@@ -24,7 +24,7 @@ def initialize(response)
#
# client = ThreeScale::Client.new(:provider_key => "your provider key")
#
- # response = client.authorize(:user_key => "your user's key")
+ # response = client.authorize(:app_id => "an app id", :app_key => "a secret key")
#
# if response.success?
# response = client.report(:user_key => "your user's key", :usage => {"hits" => 1})
@@ -82,11 +82,11 @@ def initialize(options)
# == Examples
#
# # Report two transactions of two users.
- # client.report({:user_key => 'foo', :usage => {'hits' => 1}},
- # {:user_key => 'bar', :usage => {'hits' => 1}})
+ # client.report({:app_id => 'foo', :usage => {'hits' => 1}},
+ # {:app_id => 'bar', :usage => {'hits' => 1}})
#
# # Report one transaction with timestamp.
- # client.report({:user_key => 'foo',
+ # client.report({:app_id => 'foo',
# :timestamp => Time.local(2010, 4, 27, 15, 14),
# :usage => {'hits' => 1})
#
@@ -115,7 +115,9 @@ def report(*transactions)
#
# Hash with options:
#
- # user_key:: API key of the user to authorize. This is required.
+ # 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
#
@@ -130,7 +132,7 @@ def report(*transactions)
#
# == Examples
#
- # response = client.authorize(:user_key => 'foo')
+ # response = client.authorize(:app_id => '1234')
#
# if response.success?
# # All good. Proceed...
@@ -139,7 +141,8 @@ def report(*transactions)
def authorize(options)
path = "/transactions/authorize.xml" +
"?provider_key=#{CGI.escape(provider_key)}" +
- "&user_key=#{CGI.escape(options[:user_key].to_s)}"
+ "&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)
@@ -160,7 +163,7 @@ def encode_transactions(transactions)
result = {}
transactions.each_with_index do |transaction, index|
- append_encoded_value(result, index, [:user_key], transaction[:user_key])
+ append_encoded_value(result, index, [:app_id], transaction[:app_id])
append_encoded_value(result, index, [:timestamp], transaction[:timestamp])
append_encoded_value(result, index, [:client_ip], transaction[:client_ip])
File renamed without changes.
View
@@ -2,7 +2,7 @@
require 'fakeweb'
require 'mocha'
-require 'three_scale/client'
+require '3scale/client'
class ThreeScale::ClientTest < Test::Unit::TestCase
def setup
@@ -47,9 +47,9 @@ def test_successful_authorize
</usage_reports>
</status>'
- FakeWeb.register_uri(:get, "http://#{@host}/transactions/authorize.xml?provider_key=1234abcd&user_key=foo", :status => ['200', 'OK'], :body => body)
+ FakeWeb.register_uri(:get, "http://#{@host}/transactions/authorize.xml?provider_key=1234abcd&app_id=foo", :status => ['200', 'OK'], :body => body)
- response = @client.authorize(:user_key => 'foo')
+ response = @client.authorize(:app_id => 'foo')
assert response.success?
assert_equal 'Ultimate', response.plan
@@ -67,6 +67,18 @@ def test_successful_authorize
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>
@@ -92,32 +104,32 @@ def test_authorize_with_exceeded_usage_limits
</usage_reports>
</status>'
- FakeWeb.register_uri(:get, "http://#{@host}/transactions/authorize.xml?provider_key=1234abcd&user_key=foo", :status => ['200', 'OK'], :body => body)
+ FakeWeb.register_uri(:get, "http://#{@host}/transactions/authorize.xml?provider_key=1234abcd&app_id=foo", :status => ['200', 'OK'], :body => body)
- response = @client.authorize(:user_key => 'foo')
+ 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_user_key
- body = '<error code="user_key_invalid">user key "foo" is invalid</error>'
+ 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&user_key=foo", :status => ['403', 'Forbidden'], :body => body)
+ FakeWeb.register_uri(:get, "http://#{@host}/transactions/authorize.xml?provider_key=1234abcd&app_id=foo", :status => ['403', 'Forbidden'], :body => body)
- response = @client.authorize(:user_key => 'foo')
+ response = @client.authorize(:app_id => 'foo')
assert !response.success?
- assert_equal 'user_key_invalid', response.error_code
- assert_equal 'user key "foo" is invalid', response.error_message
+ 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&user_key=foo", :status => ['500', 'Internal Server Error'], :body => 'OMG! WTF!')
+ 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(:user_key => 'foo')
+ @client.authorize(:app_id => 'foo')
end
end
@@ -145,19 +157,19 @@ def test_report_encodes_transactions
Net::HTTP.expects(:post_form).
with(anything,
'provider_key' => '1234abcd',
- 'transactions[0][user_key]' => 'foo',
+ 'transactions[0][app_id]' => 'foo',
'transactions[0][usage][hits]' => '1',
'transactions[0][timestamp]' => CGI.escape('2010-04-27 15:42:17 0200'),
- 'transactions[1][user_key]' => 'bar',
+ 'transactions[1][app_id]' => 'bar',
'transactions[1][usage][hits]' => '1',
'transactions[1][timestamp]' => CGI.escape('2010-04-27 15:55:12 0200')).
returns(http_response)
- @client.report({:user_key => 'foo',
+ @client.report({:app_id => 'foo',
:usage => {'hits' => 1},
:timestamp => '2010-04-27 15:42:17 0200'},
- {:user_key => 'bar',
+ {:app_id => 'bar',
:usage => {'hits' => 1},
:timestamp => '2010-04-27 15:55:12 0200'})
end
View
@@ -1,11 +1,17 @@
require 'test/unit'
-require 'three_scale/client'
+require '3scale/client'
-if ENV['TEST_3SCALE_PROVIDER_KEY'] && ENV['TEST_3SCALE_USER_KEYS']
+if ENV['TEST_3SCALE_PROVIDER_KEY'] &&
+ ENV['TEST_3SCALE_APP_IDS'] &&
+ ENV['TEST_3SCALE_APP_KEYS']
class ThreeScale::RemoteTest < Test::Unit::TestCase
def setup
@provider_key = ENV['TEST_3SCALE_PROVIDER_KEY']
- @user_keys = ENV['TEST_3SCALE_USER_KEYS'].split(',').map { |key| key.strip }
+
+ stripper = lambda { |string| string.strip }
+
+ @app_ids = ENV['TEST_3SCALE_APP_IDS'].split(',').map(&stripper)
+ @app_keys = ENV['TEST_3SCALE_APP_KEYS'].split(',').map(&stripper)
@client = ThreeScale::Client.new(:provider_key => @provider_key)
@@ -16,39 +22,44 @@ def setup
end
def test_successful_authorize
- response = @client.authorize(:user_key => @user_keys[0])
- assert response.success?
+ @app_keys.each do |app_key|
+ response = @client.authorize(:app_id => @app_ids[0], :app_key => app_key)
+ assert response.success?
+ end
end
def test_failed_authorize
- response = @client.authorize(:user_key => 'invalid-user-key')
+ response = @client.authorize(:app_id => 'invalid-id')
assert !response.success?
- assert_equal 'user_key_invalid', response.error_code
- assert_equal 'user key "invalid-user-key" is invalid', response.error_message
+ 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_report
- transactions = @user_keys.map do |user_key|
- {:user_key => user_key, :usage => {'hits' => 1}}
+ transactions = @app_ids.map do |app_id|
+ {:app_id => app_id, :usage => {'hits' => 1}}
end
response = @client.report(*transactions)
assert response.success?
end
def test_failed_report
- transactions = @user_keys.map do |user_key|
- {:user_key => user_key, :usage => {'hits' => 1}}
+ transactions = @app_ids.map do |app_id|
+ {:app_id => app_id, :usage => {'hits' => 1}}
end
- client = ThreeScale::Client.new(:provider_key => 'invalid-provider-key')
+ client = ThreeScale::Client.new(:provider_key => 'invalid-key')
response = client.report(*transactions)
assert !response.success?
- assert_equal 'provider_key_invalid', response.error_code
- assert_equal 'provider key "invalid-provider-key" is invalid', response.error_message
+ assert_equal 'provider_key_invalid', response.error_code
+ assert_equal 'provider key "invalid-key" is invalid', response.error_message
end
end
else
- puts "You need to set enviroment variables TEST_3SCALE_PROVIDER_KEY and TEST_3SCALE_USER_KEYS to run this remote test."
+ puts "This test executes real requests against 3scale backend server. It needs to know provider key, application ids and application keys to use in the requests. You have to set these environment variables:"
+ puts " * TEST_3SCALE_PROVIDER_KEY - a provider key."
+ puts " * TEST_3SCALE_APP_IDS - list of application ids, separated by commas."
+ puts " * TEST_3SCALE_APP_KEYS - list of application keys corresponding to the FIRST id in the TEST_3SCALE_APP_IDS list. Also separated by commas."
end

0 comments on commit 15d763f

Please sign in to comment.