Commit f23cdbd7 authored by Jonathan Schafer's avatar Jonathan Schafer

Reorganize test for service

Removed unneeded tests
Aligned with testing best practices
parent 2990435f
...@@ -3,14 +3,11 @@ ...@@ -3,14 +3,11 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Issues::BuildFromVulnerabilityService do RSpec.describe Issues::BuildFromVulnerabilityService do
let_it_be(:group) { create(:group) }
let_it_be(:project) { create(:project, :public, :repository, namespace: group) }
let_it_be(:user) { create(:user) }
let(:vulnerability) { create(:vulnerability, :with_finding, project: project) }
let(:params) { { vulnerability: vulnerability } }
let(:issue) { described_class.new(project, user, params).execute }
describe '#execute' do describe '#execute' do
let_it_be(:group) { create(:group) }
let_it_be(:project) { create(:project, :public, :repository, namespace: group) }
let_it_be(:user) { create(:user) }
before_all do before_all do
group.add_developer(user) group.add_developer(user)
end end
...@@ -19,92 +16,57 @@ RSpec.describe Issues::BuildFromVulnerabilityService do ...@@ -19,92 +16,57 @@ RSpec.describe Issues::BuildFromVulnerabilityService do
stub_licensed_features(security_dashboard: true) stub_licensed_features(security_dashboard: true)
end end
context 'when a vulnerability has remediations' do it 'builds the issue with the given params' do
let(:vulnerability) { create(:vulnerability, :with_remediation, project: project) } vulnerability = create(:vulnerability, :with_finding, project: project)
service = described_class.new(project, user, vulnerability: vulnerability)
context 'when raw_metadata has no remediations' do
let(:vulnerability) { create(:vulnerability, :with_finding, project: project) }
it 'does not display Remediations section' do
expect(vulnerability.remediations).to eq(nil)
expect(issue.description).not_to match(/Remediations/)
end
end
context 'when raw_metadata has empty remediations key' do issue = service.execute
before do
finding = vulnerability.finding
metadata = Gitlab::Json.parse(finding.raw_metadata)
metadata["remediations"] = [nil]
finding.raw_metadata = metadata.to_json
finding.save!
end
it 'does not display Remediations section' do
expect(vulnerability.remediations).to eq([nil])
expect(issue.description).not_to match(/Remediations/)
end
end
context 'when raw_metadata has a remediation' do expect(issue).not_to be_persisted
it 'displays Remediations section' do expect(issue).to have_attributes(
expect(vulnerability.remediations.length).to eq(1) project: project,
expect(issue.description).to match(/Remediations/) author: user,
end title: "Investigate vulnerability: #{vulnerability.title}",
description:
<<~DESC
Issue created from vulnerability <a href="http://localhost/#{group.name}/#{project.name}/-/security/vulnerabilities/#{vulnerability.id}">#{vulnerability.id}</a>
it 'attaches the diff' do ### Description:
expect(issue.description).to match(/This is a diff/)
end
end
end
context 'when an issue is built' do Description of #{vulnerability.title}
let(:expected_title) { "Investigate vulnerability: #{vulnerability.title}" }
let(:expected_description) do
<<~DESC
Issue created from vulnerability <a href="http://localhost/#{group.name}/#{project.name}/-/security/vulnerabilities/#{vulnerability.id}">#{vulnerability.id}</a>
### Description: * Severity: #{vulnerability.severity}
* Confidence: #{vulnerability.confidence}
* Location: [maven/src/main/java/com/gitlab/security_products/tests/App.java:29](http://localhost/#{project.full_path}/-/blob/master/maven/src/main/java/com/gitlab/security_products/tests/App.java#L29)
Description of #{vulnerability.title} ### Solution:
* Severity: #{vulnerability.severity} #{vulnerability.solution}
* Confidence: #{vulnerability.confidence}
* Location: [maven/src/main/java/com/gitlab/security_products/tests/App.java:29](http://localhost/#{project.full_path}/-/blob/master/maven/src/main/java/com/gitlab/security_products/tests/App.java#L29)
### Solution: ### Identifiers:
#{vulnerability.solution} * [CVE-2018-1234](http://cve.mitre.org/cgi-bin/cvename.cgi?name=2018-1234)
### Identifiers: ### Links:
* [CVE-2018-1234](http://cve.mitre.org/cgi-bin/cvename.cgi?name=2018-1234) * [Cipher does not check for integrity first?](https://crypto.stackexchange.com/questions/31428/pbewithmd5anddes-cipher-does-not-check-for-integrity-first)
### Links: ### Scanner:
* [Cipher does not check for integrity first?](https://crypto.stackexchange.com/questions/31428/pbewithmd5anddes-cipher-does-not-check-for-integrity-first) * Name: Find Security Bugs
DESC
)
end
### Scanner: context 'when a vulnerability has remediations' do
it 'displays Remediations section with attached diff' do
vulnerability = create(:vulnerability, :with_remediation, project: project)
service = described_class.new(project, user, vulnerability: vulnerability)
* Name: Find Security Bugs issue = service.execute
DESC
end
it 'builds the issue with the given params' do expect(issue.description).to match(/Remediations/)
expected_attributes = { expect(issue.description).to match(/This is a diff/)
project: project,
author: user,
title: expected_title,
description: expected_description
}
expect(issue).not_to be_persisted
expect(issue).to have_attributes(expected_attributes)
# expect(issue.project).to eq(project)
# expect(issue.author).to eq(user)
# expect(issue.title).to eq(expected_title)
# expect(issue.description.strip).to eq(expected_description)
# expect(issue).to be_confidential
end end
end end
end 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