Skip to content

Commit

Permalink
client encodes usage values. Tests for authrep added. TODO some test …
Browse files Browse the repository at this point in the history
…cases are still wip and the test helper in use needs to be cleaned up
  • Loading branch information
joahking committed Aug 17, 2012
1 parent 9e0bfb0 commit 2275b20
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/3scale/client.rb
Expand Up @@ -68,7 +68,7 @@ def authrep(options)
usage = []
options_usage.each_pair do |metric, value|
escaped_metric = CGI.escape "[usage][#{metric}]"
usage << "#{escaped_metric}=#{value}"
usage << "#{escaped_metric}=#{CGI.escape(value.to_s)}"
end
path += "&#{usage.join('&')}"

Expand Down
49 changes: 49 additions & 0 deletions test/client_test.rb
Expand Up @@ -31,6 +31,35 @@ def test_custom_host
assert_equal 'example.com', client.host
end

def test_authrep_usage_is_encoded
assert_authrep_url_with_params "&%5Busage%5D%5Bmethod%5D=666"

@client.authrep({:usage => {:method=> 666}})
end

def test_authrep_usage_values_are_encoded
assert_authrep_url_with_params "&%5Busage%5D%5Bhits%5D=%230"

@client.authrep({:usage => {:hits => "#0"}})
end

def test_authrep_usage_defaults_to_hits_1
assert_authrep_url_with_params "&%5Busage%5D%5Bhits%5D=1"

@client.authrep({})
end

def test_authrep_supports_app_id_app_key_auth_mode
assert_authrep_url_with_params "&app_id=appid&app_key=appkey&%5Busage%5D%5Bhits%5D=1"

@client.authrep(:app_id => "appid", :app_key => "appkey")
end

#TODO these authrep tests
def test_authrep_supports_api_key_auth_mode; end
def test_authrep_log_is_encoded;end
def test_authrep_passes_all_params_to_backend;end

def test_successful_authorize
body = '<status>
<authorized>true</authorized>
Expand Down Expand Up @@ -309,4 +338,24 @@ def test_report_with_server_error
@client.report({:app_id => 'foo', :usage => {'hits' => 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
#
def assert_authrep_url_with_params(str)
authrep_url = "http://#{@host}/transactions/authrep.xml?provider_key=#{@client.provider_key}"
params = str # unless str.scan(/log/)
params << "&%5Busage%5D%5Bhits%5D=1" unless params.scan(/usage.*hits/)
parsed_authrep_url = URI.parse(authrep_url + params)
# set to have the client working
body = '<status>
<authorized>true</authorized>
<plan>Ultimate</plan>
</status>'

# this is the actual assertion, if fakeweb raises the client is submiting with wrong params
FakeWeb.register_uri(:get, parsed_authrep_url, :status => ['200', 'OK'], :body => body)
end
end

0 comments on commit 2275b20

Please sign in to comment.