inspec/test/unit/bundles/inspec-compliance/api_test.rb
Adam Leff dda24b9f98 Fix compliance uploads when version is not present
The Compliance::API.version method could potentially return
a hash containing no "version" key but would return an empty
hash upon any expected failure. Downstream callers of the
Compliance::API.version method were looking for a "version"
key to always be present when, in some cases, it would not be.

This change ensures that if a version is not available, there
is no "version" key in the hash, and downstream callers of this
method have been changed to check for nil instead of empty.

Signed-off-by: Adam Leff <adam@leff.co>
2017-06-06 14:49:25 +02:00

76 lines
2.7 KiB
Ruby

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