test/client_test.rb in 3scale_client-2.2.7 vs test/client_test.rb in 3scale_client-2.2.8

- old
+ new

@@ -27,11 +27,11 @@ 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> @@ -52,11 +52,11 @@ 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 @@ -65,11 +65,11 @@ 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>' @@ -84,11 +84,11 @@ 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> @@ -101,40 +101,145 @@ <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> + <application> + <id>94bd2de3</id> + <key>883bdb8dbc3b6b77dbcf26845560fdbb</key> + <redirect_url>http://localhost:8080/oauth/oauth_redirect</redirect_url> + </application> + <plan>Ultimate</plan> + <usage_reports> + <usage_report metric="hits" period="week"> + <period_start>2012-01-30 00:00:00 +0000</period_start> + <period_end>2012-02-06 00:00:00 +0000</period_end> + <max_value>5000</max_value> + <current_value>1</current_value> + </usage_report> + <usage_report metric="update" period="minute"> + <period_start>2012-02-03 00:00:00 +0000</period_start> + <period_end>2012-02-03 00:00:00 +0000</period_end> + <max_value>0</max_value> + <current_value>0</current_value> + </usage_report> + </usage_reports> + </status>' + + FakeWeb.register_uri(:get, "http://#{@host}/transactions/oauth_authorize.xml?provider_key=1234abcd&app_id=foo&redirect_url=http%3A%2F%2Flocalhost%3A8080%2Foauth%2Foauth_redirect", :status => ['200', 'OK'], :body => body) + + response = @client.oauth_authorize(:app_id => 'foo', :redirect_url => "http://localhost:8080/oauth/oauth_redirect") + assert response.success? + + assert_equal '883bdb8dbc3b6b77dbcf26845560fdbb', response.app_key + assert_equal 'http://localhost:8080/oauth/oauth_redirect', response.redirect_url + + assert_equal 'Ultimate', response.plan + assert_equal 2, response.usage_reports.size + + assert_equal :week, response.usage_reports[0].period + assert_equal Time.utc(2012, 1, 30), response.usage_reports[0].period_start + assert_equal Time.utc(2012, 02, 06), response.usage_reports[0].period_end + assert_equal 1, response.usage_reports[0].current_value + assert_equal 5000, response.usage_reports[0].max_value + + assert_equal :minute, response.usage_reports[1].period + assert_equal Time.utc(2012, 2, 03), response.usage_reports[1].period_start + assert_equal Time.utc(2012, 2, 03), response.usage_reports[1].period_end + assert_equal 0, response.usage_reports[1].current_value + assert_equal 0, response.usage_reports[1].max_value + end + + def test_oauth_authorize_with_exceeded_usage_limits + body = '<status> + <authorized>false</authorized> + <reason>usage limits are exceeded</reason> + <application> + <id>94bd2de3</id> + <key>883bdb8dbc3b6b77dbcf26845560fdbb</key> + <redirect_url>http://localhost:8080/oauth/oauth_redirect</redirect_url> + </application> + <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/oauth_authorize.xml?provider_key=1234abcd&app_id=foo", :status => ['409'], :body => body) + + response = @client.oauth_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_oauth_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/oauth_authorize.xml?provider_key=1234abcd&app_id=foo", :status => ['403', 'Forbidden'], :body => body) + + response = @client.oauth_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/oauth_authorize.xml?provider_key=1234abcd&app_id=foo", :status => ['500', 'Internal Server Error'], :body => 'OMG! WTF!') + + assert_raise ThreeScale::ServerError do + @client.oauth_authorize(:app_id => 'foo') + end + end + def test_report_raises_an_exception_if_no_transactions_given assert_raise ArgumentError do @client.report end end @@ -178,11 +283,11 @@ 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') + + client = ThreeScale::Client.new(:provider_key => 'foo') response = client.report({:app_id => 'abc', :usage => {'hits' => 1}}) assert !response.success? assert_equal 'provider_key_invalid', response.error_code assert_equal 'provider key "foo" is invalid', response.error_message