cli: Downcase supermarket tool name to match URL (#3242)

* cli: Downcase supermarket tool name to match URL

This downcases the user provided tool name. Without this fetching the
profile will fail because the Supermarket API downcases in the URL.

* Add another downcase
* Add handling for `supermarket://owner_but_no_name`

Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com>
This commit is contained in:
Jerry Aldrich 2018-07-25 12:59:19 -07:00 committed by Jared Quick
parent c1d7b2cfa3
commit 2245bba021
2 changed files with 30 additions and 2 deletions

View file

@ -38,7 +38,8 @@ module Supermarket
def self.info(profile, supermarket_url = SUPERMARKET_URL)
_tool_owner, tool_name = profile_name("supermarket://#{profile}")
return if tool_name.nil? || tool_name.empty?
url = "#{supermarket_url}/api/v1/tools/#{tool_name}"
# Tool name in Supermarket URL is downcased so we need to downcase
url = "#{supermarket_url}/api/v1/tools/#{tool_name.downcase}"
_success, data = get(url, {})
JSON.parse(data) if !data.nil?
rescue JSON::ParserError
@ -48,7 +49,11 @@ module Supermarket
# compares a profile with the supermarket tool info
def self.same?(profile, supermarket_tool, supermarket_url = SUPERMARKET_URL)
tool_owner, tool_name = profile_name(profile)
tool = "#{supermarket_url}/api/v1/tools/#{tool_name}"
raise "Could not parse tool name from #{profile}" if tool_name.nil?
# Tool name in Supermarket URL is downcased so we need to downcase
tool = "#{supermarket_url}/api/v1/tools/#{tool_name.downcase}"
supermarket_tool['tool_owner'] == tool_owner && supermarket_tool['tool'] == tool
end

View file

@ -125,6 +125,29 @@ describe Supermarket::API do
profile.must_equal(profile_search_response_body['items'].first.merge({'slug' => 'test_name'}))
end
it 'downcases profile name for Supermarket API URL' do
profile_name = 'supermarket://test_owner/Test_Name'
stub_request(:get, "#{supermarket_url}/api/v1/tools-search?items=100&type=compliance_profile").
to_return(:status => 200, :body => profile_search_response_body.to_json)
profile = if default_url?(supermarket_url)
subject.find(profile_name)
else
subject.find(profile_name, supermarket_url)
end
profile.must_equal(profile_search_response_body['items'].first.merge({'slug' => 'test_name'}))
end
it 'raises an error if tool name is not present' do
profile_name = 'supermarket://owner_only'
stub_request(:get, "#{supermarket_url}/api/v1/tools-search?items=100&type=compliance_profile").
to_return(:status => 200, :body => profile_search_response_body.to_json)
e = proc { subject.find(profile_name, supermarket_url) }.must_raise
e.message.must_equal("Could not parse tool name from #{profile_name}")
end
end
end
end