Commit afd0634e authored by scardoso's avatar scardoso

Fix summary_cache and summary's failing tests

- fixing typos
- change result hash format, no need to include date keys
parent 5b288074
# frozen_string_literal: true
module GitLab
module Gitlab
module Vulnerabilities
class Summary
attr_reader :group, :filters
def initialize(group, params)
@filters = params.permit(:scope, report_type: [], confidence: [], project_id: [], severity: [])
@filters = params
@group = group
end
......@@ -21,23 +21,28 @@ module GitLab
private
def cached_vulnerability_summary
summary = {} # TODO what are the keys?
summary = {
undefined: 0,
info: 0,
unknown: 0,
low: 0,
medium: 0,
high: 0,
critical: 0
}
summary_keys = ::Vulnerabilities::Occurrence::SEVERITY_LEVELS.keys.map(&:to_sym)
project_ids_to_fetch.each do |project_id|
project_summary = Gitlab::Vulnerabilities::SummaryCache
.new(group, project_id)
.fetch
summary_keys = ::Vulnerabilities::Occurrence::SEVERITY_LEVELS.keys.map(&:to_sym) # TODO sakto ba?
summary_keys << :total # TODO kinahanglan pa ba?
summary_keys.each do |key|
summary[key].merge!(project_summary[key]) do |k, aggregate, project_count|
aggregate + project_count
end
summary[key] += project_summary[key] unless project_summary[key].nil?
end
end
summary[:total] = summary[:total].sort_by { |date, count| date }.to_h # TODO what is this???
summary
end
......
......@@ -15,9 +15,9 @@ module Gitlab
findings = ::Security::VulnerabilityFindingsFinder
.new(group, params: { project_id: [project_id] })
.execute(:all)
.count_by_severity
.counted_by_severity
::Vulnerabilities::HistorySerializer.new.represent(findings)
VulnerabilitySummarySerializer.new.represent(findings)
end
end
......
......@@ -24,10 +24,9 @@ describe Gitlab::Vulnerabilities::SummaryCache do
it 'returns the proper format for uncached summary' do
Timecop.freeze do
fetched_history = described_class.new(group, project_id).fetch
fetched_history = described_class.new(group, project.id).fetch
expect(fetched_history[:total]).to eq( Date.today => 1 )
expect(fetched_history[:high]).to eq( Date.today => 1 )
expect(fetched_history[:high]).to eq(1)
end
end
......@@ -36,8 +35,7 @@ describe Gitlab::Vulnerabilities::SummaryCache do
described_class.new(group, project.id).fetch
fetched_history = described_class.new(group, project.id).fetch
expect(fetched_history[:total]).to eq( Date.today => 1 )
expect(fetched_history[:high]).to eq( Date.today => 1 )
expect(fetched_history[:high]).to eq(1)
end
end
......
......@@ -9,8 +9,8 @@ describe Gitlab::Vulnerabilities::Summary do
let(:filters) { {} }
before do
create_vulnerabilities(1, project1, { severity: :medium })
create_vulnerabilities(2, project2)
create_vulnerabilities(1, project1, { severity: :medium, report_type: :sast })
create_vulnerabilities(2, project2, { severity: :high, report_type: :sast})
end
describe '#vulnerabilities_counter', :use_clean_rails_memory_store_caching do
......@@ -18,7 +18,7 @@ describe Gitlab::Vulnerabilities::Summary do
context 'feature disabled' do
before do
stub_feature_flags(cache_vulnerability_history: false)
stub_feature_flags(cache_vulnerability_summary: false)
end
it 'does not call Gitlab::Vulnerabilities::SummaryCache' do
......@@ -29,8 +29,7 @@ describe Gitlab::Vulnerabilities::Summary do
it 'returns the proper format for the summary' do
Timecop.freeze do
expect(counter[:total]).to eq({ Date.today => 3 })
expect(counter[:high]).to eq({ Date.today => 2 })
expect(counter[:high]).to eq(2)
end
end
end
......@@ -51,15 +50,14 @@ describe Gitlab::Vulnerabilities::Summary do
end
it 'calls Gitlab::Vulnerabilities::SummaryCache' do
expect(Gitlab::Vulnerabilities::SummaryCache).to receive(:new)
expect(Gitlab::Vulnerabilities::SummaryCache).to receive(:new).twice.and_call_original
counter
end
it 'returns the proper format for the summary' do
Timecop.freeze do
expect(counter[:total]).to eq({ Date.today => 3 })
expect(counter[:high]).to eq({ Date.today => 2 })
expect(counter[:high]).to eq(2)
end
end
......@@ -73,12 +71,6 @@ describe Gitlab::Vulnerabilities::Summary do
create_vulnerabilities(1, project2)
end
end
it 'sorts by date for each key' do
Timecop.freeze do
expect(counter[:high].keys).to eq([(Date.today - 4), (Date.today - 1), Date.today])
end
end
end
end
......@@ -91,6 +83,7 @@ describe Gitlab::Vulnerabilities::Summary do
report_type: options[:report_type] || :sast,
severity: options[:severity] || :high,
pipelines: [pipeline],
project: project,
created_at: options[:created_at] || Date.today
)
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment