summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPete Higgins <pete@peterhiggins.org>2020-10-21 16:45:24 -0700
committerPete Higgins <pete@peterhiggins.org>2020-12-01 16:05:16 -0800
commit8ecfba354ce36cedef35b97c7a278cfa452224b3 (patch)
tree847ef2d698dd86c953a4958ef2d03bce3b9d63f6
parentb8dcdf450803ab15707d8bd73acbe9a8679b136f (diff)
downloadchef-8ecfba354ce36cedef35b97c7a278cfa452224b3.tar.gz
Only stub http requests when they should happen.
Signed-off-by: Pete Higgins <pete@peterhiggins.org>
-rw-r--r--spec/unit/audit/reporter/automate_spec.rb78
-rw-r--r--spec/unit/audit/reporter/chef_server_automate_spec.rb46
2 files changed, 65 insertions, 59 deletions
diff --git a/spec/unit/audit/reporter/automate_spec.rb b/spec/unit/audit/reporter/automate_spec.rb
index a59a9e4b48..b61d822ae7 100644
--- a/spec/unit/audit/reporter/automate_spec.rb
+++ b/spec/unit/audit/reporter/automate_spec.rb
@@ -5,36 +5,11 @@ describe Chef::Audit::Reporter::Automate do
before :each do
WebMock.disable_net_connect!
- token = 'fake_token'
-
Chef::Config[:data_collector] = { token: token, server_url: 'https://automate.test/data_collector' }
-
- stub_request(:post, 'https://automate.test/compliance/profiles/metasearch')
- .with(
- body: '{"sha256": ["7bd598e369970002fc6f2d16d5b988027d58b044ac3fa30ae5fc1b8492e215cd"]}',
- headers: {
- 'Accept-Encoding' => 'identity',
- 'X-Chef-Version' => Chef::VERSION,
- 'X-Data-Collector-Auth' => 'version=1.0',
- 'X-Data-Collector-Token' => token
- }
- ).to_return(
- status: 200,
- body: '{"missing_sha256": ["7bd598e369970002fc6f2d16d5b988027d58b044ac3fa30ae5fc1b8492e215cd"]}'
- )
-
- stub_request(:post, 'https://automate.test/data_collector')
- .with(
- body: enriched_report,
- headers: {
- 'Accept-Encoding' => 'identity',
- 'X-Chef-Version' => Chef::VERSION,
- 'X-Data-Collector-Auth' => 'version=1.0',
- 'X-Data-Collector-Token' => token
- }
- ).to_return(status: 200)
end
+ let(:token) { "fake_token" }
+
let(:reporter) { Chef::Audit::Reporter::Automate.new(opts) }
let(:opts) do
@@ -181,9 +156,45 @@ describe Chef::Audit::Reporter::Automate do
}
end
- it 'sends report successfully to ChefAutomate' do
- allow(Time).to receive(:now).and_return(Time.parse('2016-07-19T19:19:19+01:00'))
- expect(reporter.send_report(inspec_report)).to eq(true)
+ describe "#send_report" do
+ it 'sends report successfully to ChefAutomate' do
+ metasearch_stub = stub_request(:post, 'https://automate.test/compliance/profiles/metasearch')
+ .with(
+ body: '{"sha256": ["7bd598e369970002fc6f2d16d5b988027d58b044ac3fa30ae5fc1b8492e215cd"]}',
+ headers: {
+ 'Accept-Encoding' => 'identity',
+ 'X-Chef-Version' => Chef::VERSION,
+ 'X-Data-Collector-Auth' => 'version=1.0',
+ 'X-Data-Collector-Token' => token
+ }
+ ).to_return(
+ status: 200,
+ body: '{"missing_sha256": ["7bd598e369970002fc6f2d16d5b988027d58b044ac3fa30ae5fc1b8492e215cd"]}'
+ )
+
+ report_stub = stub_request(:post, 'https://automate.test/data_collector')
+ .with(
+ body: enriched_report,
+ headers: {
+ 'Accept-Encoding' => 'identity',
+ 'X-Chef-Version' => Chef::VERSION,
+ 'X-Data-Collector-Auth' => 'version=1.0',
+ 'X-Data-Collector-Token' => token
+ }
+ ).to_return(status: 200)
+
+ allow(Time).to receive(:now).and_return(Time.parse('2016-07-19T19:19:19+01:00'))
+ expect(reporter.send_report(inspec_report)).to eq(true)
+
+ expect(metasearch_stub).to have_been_requested
+ expect(report_stub).to have_been_requested
+ end
+
+ it 'does not send report when entity_uuid is missing' do
+ opts.delete(:entity_uuid)
+ reporter = Chef::Audit::Reporter::Automate.new(opts)
+ expect(reporter.send_report(inspec_report)).to eq(false)
+ end
end
it 'enriches report correctly with the most test coverage' do
@@ -191,13 +202,6 @@ describe Chef::Audit::Reporter::Automate do
expect(reporter.truncate_controls_results(reporter.enriched_report(inspec_report), 2)).to eq(enriched_report)
end
- it 'does not send report when entity_uuid is missing' do
- opts.delete(:entity_uuid)
- reporter = Chef::Audit::Reporter::Automate.new(opts)
- expect(reporter.send_report(inspec_report)).to eq(false)
- pending "expect no HTTP requests"
- end
-
describe "#truncate_controls_results" do
let(:report) do
{
diff --git a/spec/unit/audit/reporter/chef_server_automate_spec.rb b/spec/unit/audit/reporter/chef_server_automate_spec.rb
index e2845b215b..aae83be1f8 100644
--- a/spec/unit/audit/reporter/chef_server_automate_spec.rb
+++ b/spec/unit/audit/reporter/chef_server_automate_spec.rb
@@ -6,27 +6,6 @@ describe Chef::Audit::Reporter::ChefServerAutomate do
Chef::Config[:client_key] = File.expand_path('../../../data/ssl/private_key.pem', __dir__)
Chef::Config[:node_name] = 'spec-node'
-
- # TODO: Had to change 'X-Ops-Server-Api-Version' from 1 to 2, is that correct?
- stub_request(:post, 'https://chef.server/data_collector')
- .with(
- body: enriched_report,
- headers: {
- 'X-Chef-Version' => Chef::VERSION,
- 'X-Ops-Authorization-1'=>'gifUT8qLYgXh8erdBeAVCX7Lz0UyU0nMDY/ONwej5ZUDDMuX6Jatp59gOI+3',
- 'X-Ops-Authorization-2'=>'/IFR9yZibVDROdNYEWuo6VXX8HynoUdFBw2aZx5APcoLzwcACQPoR9GlPejD',
- 'X-Ops-Authorization-3'=>'d/yN2VHTLhybWRB6b7FBhk60sTvQYWK03iEUiy8yy5rSr0S9+dI8vEWT+EJm',
- 'X-Ops-Authorization-4'=>'xKcSf1mhPtZ7oIjTIIBBKt8SGPAovN1lYwqD8ycdtGN5y3/qMMMYpfDhB2Y4',
- 'X-Ops-Authorization-5'=>'RctLQURtnaOPcxsTanUPuayDELFcBJzmUni+5O+bcgz8g96E5Pji+c9YqwMc',
- 'X-Ops-Authorization-6'=>'2Bm9X3ep7cwJEwtcsv3UJ2Se+JoG77TXyy2T5DUo4w==',
- 'X-Ops-Content-Hash'=>'yfck5nQDcRWta06u45Q+J463LYY=',
- 'X-Ops-Server-Api-Version' => '2',
- 'X-Ops-Sign' => 'algorithm=sha1;version=1.1;',
- 'X-Ops-Timestamp' => /.+/,
- 'X-Ops-Userid' => 'spec-node',
- 'X-Remote-Request-Id' => /.+/
- }
- ).to_return(status: 200)
end
let(:reporter) { Chef::Audit::Reporter::ChefServerAutomate.new(opts) }
@@ -168,8 +147,31 @@ describe Chef::Audit::Reporter::ChefServerAutomate do
}
end
- it 'sends report successfully to ChefServerAutomate' do
+ it 'sends report successfully' do
+ # TODO: Had to change 'X-Ops-Server-Api-Version' from 1 to 2, is that correct?
+ report_stub = stub_request(:post, 'https://chef.server/data_collector')
+ .with(
+ body: enriched_report,
+ headers: {
+ 'X-Chef-Version' => Chef::VERSION,
+ 'X-Ops-Authorization-1'=>'gifUT8qLYgXh8erdBeAVCX7Lz0UyU0nMDY/ONwej5ZUDDMuX6Jatp59gOI+3',
+ 'X-Ops-Authorization-2'=>'/IFR9yZibVDROdNYEWuo6VXX8HynoUdFBw2aZx5APcoLzwcACQPoR9GlPejD',
+ 'X-Ops-Authorization-3'=>'d/yN2VHTLhybWRB6b7FBhk60sTvQYWK03iEUiy8yy5rSr0S9+dI8vEWT+EJm',
+ 'X-Ops-Authorization-4'=>'xKcSf1mhPtZ7oIjTIIBBKt8SGPAovN1lYwqD8ycdtGN5y3/qMMMYpfDhB2Y4',
+ 'X-Ops-Authorization-5'=>'RctLQURtnaOPcxsTanUPuayDELFcBJzmUni+5O+bcgz8g96E5Pji+c9YqwMc',
+ 'X-Ops-Authorization-6'=>'2Bm9X3ep7cwJEwtcsv3UJ2Se+JoG77TXyy2T5DUo4w==',
+ 'X-Ops-Content-Hash'=>'yfck5nQDcRWta06u45Q+J463LYY=',
+ 'X-Ops-Server-Api-Version' => '2',
+ 'X-Ops-Sign' => 'algorithm=sha1;version=1.1;',
+ 'X-Ops-Timestamp' => /.+/,
+ 'X-Ops-Userid' => 'spec-node',
+ 'X-Remote-Request-Id' => /.+/
+ }
+ ).to_return(status: 200)
+
allow(Time).to receive(:now).and_return(Time.parse('2016-07-19T19:19:19+01:00'))
expect(reporter.send_report(inspec_report)).to eq(true)
+
+ expect(report_stub).to have_been_requested
end
end