test/client_test.rb in 3scale_client-2.7.0 vs test/client_test.rb in 3scale_client-2.8.0

- old
+ new

@@ -210,10 +210,55 @@ assert_raises ThreeScale::ServerError do @client.authorize(:app_id => 'foo') end end + def test_authorize_with_usage_within_limits + url = "http://#{@host}/transactions/authorize.xml?provider_key=1234abcd"\ + "&app_id=foo&%5Busage%5D%5Bmetric1%5D=1&%5Busage%5D%5Bmetric2%5D=2" + + body = '<status> + <authorized>true</authorized> + <plan>Ultimate</plan> + </status>' + + FakeWeb.register_uri(:get, url, :status => ['200', 'OK'], :body => body) + + response = @client.authorize(:app_id => 'foo', + :usage => { 'metric1' => 1, 'metric2' => 2 }) + + assert response.success? + end + + def test_authorize_with_usage_and_limits_exceeded + url = "http://#{@host}/transactions/authorize.xml?provider_key=1234abcd"\ + "&app_id=foo&%5Busage%5D%5Bhits%5D=1" + + 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>10</current_value> + <max_value>5</max_value> + </usage_report> + </usage_reports> + </status>' + + FakeWeb.register_uri(:get, url, :status => ['409'], :body => body) + + response = @client.authorize(:app_id => 'foo', :usage => { 'hits' => 1 }) + + assert !response.success? + assert_equal 'usage limits are exceeded', response.error_message + end + def test_successful_oauth_authorize body = '<status> <authorized>true</authorized> <application> <id>94bd2de3</id> @@ -315,24 +360,79 @@ assert_raises ThreeScale::ServerError do @client.oauth_authorize(:app_id => 'foo') end end + def test_oauth_authorize_with_usage_within_limits + url = "http://#{@host}/transactions/oauth_authorize.xml"\ + "?provider_key=1234abcd&app_id=foo&%5Busage%5D%5Bmetric1%5D=1"\ + "&%5Busage%5D%5Bmetric2%5D=2" + + body = '<status> + <authorized>true</authorized> + <plan>Ultimate</plan> + </status>' + + FakeWeb.register_uri(:get, url, :status => ['200', 'OK'], :body => body) + + response = @client.oauth_authorize( + :app_id => 'foo', :usage => { 'metric1' => 1, 'metric2' => 2 }) + + assert response.success? + end + + def test_oauth_authorize_with_usage_and_limits_exceeded + url = "http://#{@host}/transactions/oauth_authorize.xml"\ + "?provider_key=1234abcd&app_id=foo&%5Busage%5D%5Bhits%5D=1" + + 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>10</current_value> + <max_value>5</max_value> + </usage_report> + </usage_reports> + </status>' + + FakeWeb.register_uri(:get, url, :status => ['409'], :body => body) + + response = @client.oauth_authorize(:app_id => 'foo', + :usage => { 'hits' => 1 }) + + assert !response.success? + assert_equal 'usage limits are exceeded', response.error_message + end + def test_report_raises_an_exception_if_no_transactions_given assert_raises ArgumentError do @client.report end + + [nil, []].each do |invalid_transactions| + assert_raises ArgumentError do + @client.report(transactions: invalid_transactions) + end + end end def test_successful_report FakeWeb.register_uri(:post, "http://#{@host}/transactions.xml", :status => ['200', 'OK']) - response = @client.report({:app_id => 'foo', - :timestamp => Time.local(2010, 4, 27, 15, 00), - :usage => {'hits' => 1}}) + transactions = [{ :app_id => 'foo', + :timestamp => Time.local(2010, 4, 27, 15, 00), + :usage => {'hits' => 1 } }] + response = @client.report(transactions: transactions) + assert response.success? end def test_report_encodes_transactions payload = { @@ -349,23 +449,24 @@ } FakeWeb.register_uri(:post, "http://#{@host}/transactions.xml", :status => ['200', 'OK']) - @client.report({:app_id => 'foo', - :usage => {'hits' => 1}, - :timestamp => '2010-04-27 15:42:17 0200', - :log => { - 'request' => 'foo', - 'response' => 'bar', - 'code' => 200, - } - }, + transactions = [{ :app_id => 'foo', + :usage => { 'hits' => 1 }, + :timestamp => '2010-04-27 15:42:17 0200', + :log => { + 'request' => 'foo', + 'response' => 'bar', + 'code' => 200 + } + }, + { :app_id => 'bar', + :usage => { 'hits' => 1 }, + :timestamp => Time.local(2010, 4, 27, 15, 00) }] - {:app_id => 'bar', - :usage => {'hits' => 1}, - :timestamp => Time.local(2010, 4, 27, 15, 00)}) + @client.report(transactions: transactions) request = FakeWeb.last_request assert_equal URI.encode_www_form(payload), request.body end @@ -379,28 +480,91 @@ } FakeWeb.register_uri(:post, "http://#{@host}/transactions.xml", :status => ['200', 'OK']) - @client.report({:user_key => 'foo', - :usage => {'hits' => 1}, - :timestamp => '2016-07-18 15:42:17 0200'}) + transactions = [{ :user_key => 'foo', + :usage => { 'hits' => 1 }, + :timestamp => '2016-07-18 15:42:17 0200' }] + @client.report(transactions: transactions) + request = FakeWeb.last_request assert_equal URI.encode_www_form(payload), request.body end + def test_report_with_service_id + FakeWeb.register_uri(:post, "http://#{@host}/transactions.xml", + :status => ['200', 'OK']) + + transactions = [{ :app_id => 'an_app_id', + :usage => { 'hits' => 1 }, + :timestamp => '2016-07-18 15:42:17 0200' }] + + @client.report(transactions: transactions, service_id: 'a_service_id') + + request = FakeWeb.last_request + + payload = { + 'transactions[0][app_id]' => 'an_app_id', + 'transactions[0][timestamp]' => '2016-07-18 15:42:17 0200', + 'transactions[0][usage][hits]' => '1', + 'provider_key' => '1234abcd', + 'service_id' => 'a_service_id' + } + + assert_equal URI.encode_www_form(payload), request.body + end + + # We changed the signature of the report method but we keep the compatibility + # with the old one: def report(*transactions).This tests only checks that + # backwards compatibility. + def test_report_compatibility_with_old_report_format + FakeWeb.register_uri(:post, "http://#{@host}/transactions.xml", + :status => ['200', 'OK']) + + transactions = [{ :app_id => 'app_id_1', + :usage => { 'hits' => 1 }, + :timestamp => '2016-07-18 15:42:17 0200' }, + { :app_id => 'app_id_2', + :usage => { 'hits' => 2 }, + :timestamp => '2016-07-19 15:42:17 0200' }, + { :app_id => 'app_id_3', + :usage => { 'hits' => 3 }, + :timestamp => '2016-07-20 15:42:17 0200' }] + + @client.report(*transactions) + + request = FakeWeb.last_request + + payload = { + 'transactions[0][app_id]' => 'app_id_1', + 'transactions[0][timestamp]' => '2016-07-18 15:42:17 0200', + 'transactions[0][usage][hits]' => '1', + 'transactions[1][app_id]' => 'app_id_2', + 'transactions[1][timestamp]' => '2016-07-19 15:42:17 0200', + 'transactions[1][usage][hits]' => '2', + 'transactions[2][app_id]' => 'app_id_3', + 'transactions[2][timestamp]' => '2016-07-20 15:42:17 0200', + 'transactions[2][usage][hits]' => '3', + 'provider_key' => '1234abcd' + } + + assert_equal URI.encode_www_form(payload), request.body + end + def test_failed_report error_body = '<error code="provider_key_invalid">provider key "foo" is invalid</error>' FakeWeb.register_uri(:post, "http://#{@host}/transactions.xml", :status => ['403', 'Forbidden'], :body => error_body) client = ThreeScale::Client.new(:provider_key => 'foo') - response = client.report({:app_id => 'abc', :usage => {'hits' => 1}}) + transactions = [{ :app_id => 'abc', :usage => { 'hits' => 1 } }] + response = client.report(transactions: transactions) assert !response.success? assert_equal 'provider_key_invalid', response.error_code assert_equal 'provider key "foo" is invalid', response.error_message end @@ -408,12 +572,14 @@ def test_report_with_server_error FakeWeb.register_uri(:post, "http://#{@host}/transactions.xml", :status => ['500', 'Internal Server Error'], :body => 'OMG! WTF!') + transactions = [{ :app_id => 'foo', :usage => { 'hits' => 1 } }] + assert_raises ThreeScale::ServerError do - @client.report({:app_id => 'foo', :usage => {'hits' => 1}}) + @client.report(transactions: transactions) end end def test_authorize_client_header_sent 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>' @@ -437,10 +603,11 @@ 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') - response = client.report({:app_id => 'abc', :usage => {'hits' => 1}}) + 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"] assert_equal "su1.3scale.net", request["host"] end