diff --git a/lib/bundles/inspec-compliance/api.rb b/lib/bundles/inspec-compliance/api.rb index 3ad3aaa0d..66be940d5 100755 --- a/lib/bundles/inspec-compliance/api.rb +++ b/lib/bundles/inspec-compliance/api.rb @@ -70,13 +70,14 @@ module Compliance headers = get_headers(config) response = Compliance::HTTP.get(url+'/version', headers, insecure) return {} if response.code == '404' - data = response.body - if !data.nil? - JSON.parse(data) - else - {} - end + data = response.body + return {} if data.nil? || data.empty? + + parsed = JSON.parse(data) + return {} unless parsed.key?('version') && !parsed['version'].empty? + + parsed end # verifies that a profile @@ -203,11 +204,11 @@ module Compliance end def self.is_automate_server_pre_080?(config) - config['server_type'] == 'automate' && config['version'].empty? + config['server_type'] == 'automate' && config['version'].nil? end def self.is_automate_server_080_and_later?(config) - config['server_type'] == 'automate' && !config['version'].empty? + config['server_type'] == 'automate' && !config['version'].nil? end def self.is_automate_server?(config) diff --git a/test/unit/bundles/inspec-compliance/api_test.rb b/test/unit/bundles/inspec-compliance/api_test.rb new file mode 100644 index 000000000..6187da3f3 --- /dev/null +++ b/test/unit/bundles/inspec-compliance/api_test.rb @@ -0,0 +1,76 @@ +require 'helper' + +describe Compliance::API do + describe '.version' do + let(:headers) { 'test-headers' } + let(:config) do + { + 'server' => 'myserver', + 'insecure' => true + } + end + + before do + Compliance::API.expects(:get_headers).returns(headers) + end + + describe 'when a 404 is received' do + it 'should return an empty hash' do + response = mock + response.stubs(:code).returns('404') + Compliance::HTTP.expects(:get).with('myserver/version', 'test-headers', true).returns(response) + Compliance::API.version(config).must_equal({}) + end + end + + describe 'when the returned body is nil' do + it 'should return an empty hash' do + response = mock + response.stubs(:code).returns('200') + response.stubs(:body).returns(nil) + Compliance::HTTP.expects(:get).with('myserver/version', 'test-headers', true).returns(response) + Compliance::API.version(config).must_equal({}) + end + end + + describe 'when the returned body is an empty string' do + it 'should return an empty hash' do + response = mock + response.stubs(:code).returns('200') + response.stubs(:body).returns('') + Compliance::HTTP.expects(:get).with('myserver/version', 'test-headers', true).returns(response) + Compliance::API.version(config).must_equal({}) + end + end + + describe 'when the returned body has no version key' do + it 'should return an empty hash' do + response = mock + response.stubs(:code).returns('200') + response.stubs(:body).returns('{"api":"compliance"}') + Compliance::HTTP.expects(:get).with('myserver/version', 'test-headers', true).returns(response) + Compliance::API.version(config).must_equal({}) + end + end + + describe 'when the returned body has an empty version key' do + it 'should return an empty hash' do + response = mock + response.stubs(:code).returns('200') + response.stubs(:body).returns('{"api":"compliance","version":""}') + Compliance::HTTP.expects(:get).with('myserver/version', 'test-headers', true).returns(response) + Compliance::API.version(config).must_equal({}) + end + end + + describe 'when the returned body has a proper version' do + it 'should return an empty hash' do + response = mock + response.stubs(:code).returns('200') + response.stubs(:body).returns('{"api":"compliance","version":"1.2.3"}') + Compliance::HTTP.expects(:get).with('myserver/version', 'test-headers', true).returns(response) + Compliance::API.version(config).must_equal({'version' => '1.2.3', 'api' => 'compliance'}) + end + end + end +end