test/client_test.rb in 3scale_client-2.10.0 vs test/client_test.rb in 3scale_client-2.11.0
- old
+ new
@@ -5,52 +5,72 @@
require '3scale/client'
class ThreeScale::ClientTest < MiniTest::Test
+ WARN_DEPRECATED = ENV['WARN_DEPRECATED'] == '1'
+
def client(options = {})
- ThreeScale::Client.new({:provider_key => '1234abcd'}.merge(options))
+ ThreeScale::Client.new({ provider_key: '1234abcd',
+ warn_deprecated: WARN_DEPRECATED }.merge(options))
end
def setup
FakeWeb.clean_registry
FakeWeb.allow_net_connect = false
@client = client
@host = ThreeScale::Client::DEFAULT_HOST
end
- def test_raises_exception_if_provider_key_is_missing
+ def test_raises_exception_if_no_credentials_are_specified
assert_raises ArgumentError do
ThreeScale::Client.new({})
end
+ assert_raises ArgumentError do
+ ThreeScale::Client.new(service_tokens: false)
+ end
end
+ def test_does_not_raise_if_some_credentials_are_specified
+ assert ThreeScale::Client.new(provider_key: 'some_key',
+ warn_deprecated: WARN_DEPRECATED)
+ assert ThreeScale::Client.new(service_tokens: true)
+ end
+
def test_default_host
- client = ThreeScale::Client.new(:provider_key => '1234abcd')
+ client = ThreeScale::Client.new(provider_key: '1234abcd',
+ warn_deprecated: WARN_DEPRECATED)
assert_equal 'su1.3scale.net', client.host
end
def test_custom_host
- client = ThreeScale::Client.new(:provider_key => '1234abcd', :host => "example.com")
+ client = ThreeScale::Client.new(provider_key: '1234abcd',
+ warn_deprecated: WARN_DEPRECATED,
+ host: "example.com")
assert_equal 'example.com', client.host
end
def test_default_protocol
- client = ThreeScale::Client.new(:provider_key => 'test')
+ client = ThreeScale::Client.new(provider_key: 'test',
+ warn_deprecated: WARN_DEPRECATED)
assert_equal false, client.http.use_ssl?
end
def test_insecure_protocol
- client = ThreeScale::Client.new(:provider_key => 'test', :secure => false)
+ client = ThreeScale::Client.new(provider_key: 'test',
+ warn_deprecated: WARN_DEPRECATED,
+ secure: false)
assert_equal false, client.http.use_ssl?
end
def test_secure_protocol
- client = ThreeScale::Client.new(:provider_key => 'test', :secure => true)
+ client = ThreeScale::Client.new(provider_key: 'test',
+ warn_deprecated: WARN_DEPRECATED,
+ secure: true)
assert_equal true, client.http.use_ssl?
end
def test_authrep_usage_is_encoded
assert_authrep_url_with_params "&%5Busage%5D%5Bmethod%5D=666"
@@ -629,11 +649,12 @@
FakeWeb.register_uri(:post, "http://#{@host}/transactions.xml",
:status => ['403', 'Forbidden'],
:body => error_body)
- client = ThreeScale::Client.new(:provider_key => 'foo')
+ client = ThreeScale::Client.new(provider_key: 'foo',
+ warn_deprecated: WARN_DEPRECATED)
transactions = [{ :app_id => 'abc', :usage => { 'hits' => 1 } }]
response = client.report(transactions: transactions)
assert !response.success?
assert_equal 'provider_key_invalid', response.error_code
@@ -657,11 +678,12 @@
version = ThreeScale::Client::VERSION
FakeWeb.register_uri(:get, "http://#{@host}/transactions/authorize.xml?provider_key=foo&app_id=foo",
:status => ['200', 'OK'],
:body => success_body)
- client = ThreeScale::Client.new(:provider_key => 'foo')
+ client = ThreeScale::Client.new(provider_key: 'foo',
+ warn_deprecated: WARN_DEPRECATED)
response = client.authorize(:app_id => 'foo')
assert response.success?
assert !response.limits_exceeded?
request = FakeWeb.last_request
@@ -674,11 +696,12 @@
success_body = '<?xml version="1.0" encoding="UTF-8"?><status><authorized>true</authorized><plan>Default</plan><usage_reports><usage_report metric="hits" period="minute"><period_start>2014-08-22 09:06:00 +0000</period_start><period_end>2014-08-22 09:07:00 +0000</period_end><max_value>5</max_value><current_value>0</current_value></usage_report></usage_reports></status>'
version = ThreeScale::Client::VERSION
FakeWeb.register_uri(:post, "http://#{@host}/transactions.xml",
:status => ['200', 'OK'],
:body => success_body)
- client = ThreeScale::Client.new(:provider_key => 'foo')
+ client = ThreeScale::Client.new(provider_key: 'foo',
+ warn_deprecated: WARN_DEPRECATED)
transactions = [{ :app_id => 'abc', :usage => { 'hits' => 1 } }]
client.report(transactions: transactions)
request = FakeWeb.last_request
assert_equal "plugin-ruby-v#{version}", request["X-3scale-User-Agent"]
@@ -690,11 +713,12 @@
version = ThreeScale::Client::VERSION
FakeWeb.register_uri(:get, "http://#{@host}/transactions/authrep.xml?provider_key=foo&app_id=abc&%5Busage%5D%5Bhits%5D=1",
:status => ['200', 'OK'],
:body => success_body)
- client = ThreeScale::Client.new(:provider_key => 'foo')
+ client = ThreeScale::Client.new(provider_key: 'foo',
+ warn_deprecated: WARN_DEPRECATED)
response = client.authrep(:app_id => 'abc')
assert response.success?
assert !response.limits_exceeded?
request = FakeWeb.last_request
@@ -759,10 +783,60 @@
request = FakeWeb.last_request
assert_equal EXTENSIONS_STR, request['3scale-options']
end
+ def test_client_initialized_with_sevice_tokens_uses_percall_specified_token
+ body = '<status>
+ <authorized>true</authorized>
+ <plan>Ultimate</plan>
+ </status>'
+ transactions = [{ app_id: 'foo',
+ timestamp: Time.local(2010, 4, 27, 15, 00),
+ usage: {'hits' => 1 } }]
+ usage = { 'metric1' => 1, 'metric2' => 2}
+
+ FakeWeb.register_uri(:get, "http://#{@host}/transactions/authorize.xml?user_key=foo&service_id=1&service_token=newtoken", status: ['200', 'OK'], body: body)
+ FakeWeb.register_uri(:get, "http://#{@host}/transactions/authrep.xml?service_token=newtoken&user_key=foo&service_id=1&%5Busage%5D%5Bhits%5D=1", status: ['200', 'OK'], body: body)
+ FakeWeb.register_uri(:post, "http://#{@host}/transactions.xml", parameters: {service_token: 'newtoken', service_id: '1', transactions: transactions}, status: ['200', 'OK'])
+ FakeWeb.register_uri(:get, "http://#{@host}/transactions/oauth_authorize.xml?service_id=1&%5Busage%5D%5Bmetric1%5D=1&%5Busage%5D%5Bmetric2%5D=2&service_token=newtoken",
+ status: ['200', 'OK'], body: body)
+
+ client = ThreeScale::Client.new(service_tokens: true)
+
+ response = client.authorize(user_key: 'foo', service_token: 'newtoken', service_id: 1)
+ assert response.success?
+ response = client.authrep(user_key: 'foo', service_token: 'newtoken', service_id: 1)
+ assert response.success?
+ response = client.report(transactions: transactions, service_token: 'newtoken', service_id: 1)
+ assert response.success?
+ response = client.oauth_authorize(access_token: 'oauth', usage: usage, service_token: 'newtoken', service_id: 1)
+ assert response.success?
+ end
+
+ def test_client_initialized_with_service_tokens_raises_if_unspecified_percall
+ transactions = [{ app_id: 'foo',
+ timestamp: Time.local(2010, 4, 27, 15, 00),
+ usage: {'hits' => 1 } }]
+ usage = { 'metric1' => 1, 'metric2' => 2}
+
+ client = ThreeScale::Client.new(service_tokens: true)
+
+ assert_raises ArgumentError do
+ client.authorize(user_key: 'foo', service_id: 1)
+ end
+ assert_raises ArgumentError do
+ client.authrep(user_key: 'foo', service_id: 1)
+ end
+ assert_raises ArgumentError do
+ client.report(transactions: transactions, service_id: 1)
+ end
+ assert_raises ArgumentError do
+ client.oauth_authorize(user_key: 'foo', usage: usage, service_id: 1)
+ end
+ end
+
private
#OPTIMIZE this tricky test helper relies on fakeweb catching the urls requested by the client
# it is brittle: it depends in the correct order or params in the url
#
@@ -787,10 +861,13 @@
end
class ThreeScale::NetHttpPersistentClientTest < ThreeScale::ClientTest
def client(options = {})
ThreeScale::Client::HTTPClient.persistent_backend = ThreeScale::Client::HTTPClient::NetHttpPersistent
- ThreeScale::Client.new({:provider_key => '1234abcd', :persistent => true}.merge(options))
+ ThreeScale::Client.new({ provider_key: '1234abcd',
+ warn_deprecated: WARN_DEPRECATED,
+ persistent: true,
+ }.merge(options))
end
end
class ThreeScale::NetHttpKeepAliveClientTest < ThreeScale::NetHttpPersistentClientTest
def client(options = {})