Commit 94c29a86 authored by Michał Zając's avatar Michał Zając

Refactor VulnerabilitesHelper with new serializer

parent f8238cde
......@@ -5,7 +5,7 @@ module VulnerabilitiesHelper
return unless vulnerability
{
vulnerability_json: vulnerability.to_json,
vulnerability_json: VulnerabilitySerializer.new.represent(vulnerability).to_json,
project_fingerprint: vulnerability.finding.project_fingerprint,
create_issue_url: create_vulnerability_feedback_issue_path(vulnerability.finding.project),
pipeline_json: vulnerability_pipeline_data(pipeline).to_json,
......
......@@ -3,29 +3,65 @@
require 'spec_helper'
describe VulnerabilitiesHelper do
let_it_be(:user) { build(:user) }
let_it_be(:vulnerability) { create(:vulnerability, :with_findings, title: "My vulnerability") }
let_it_be(:finding) { vulnerability.finding }
let(:vulnerability_serializer_hash) do
vulnerability.slice(
:id,
:title,
:state,
:severity,
:confidence,
:report_type,
:resolved_on_default_branch,
:project_default_branch,
:resolved_by_id,
:dismissed_by_id,
:confirmed_by_id
)
end
let(:occurrence_serializer_hash) do
finding.slice(:description,
:identifiers,
:links,
:location,
:name,
:issue_feedback,
:project,
:solution
)
end
before do
allow(helper).to receive(:can?).and_return(true)
allow(helper).to receive(:current_user).and_return(user)
end
RSpec.shared_examples 'vulnerability properties' do
before do
vulnerability_serializer_stub = instance_double("VulnerabilitySerializer")
expect(VulnerabilitySerializer).to receive(:new).and_return(vulnerability_serializer_stub)
expect(vulnerability_serializer_stub).to receive(:represent).with(vulnerability).and_return(vulnerability_serializer_hash)
occurrence_serializer_stub = instance_double("Vulnerabilities::OccurrenceSerializer")
expect(Vulnerabilities::OccurrenceSerializer).to receive(:new).and_return(occurrence_serializer_stub)
expect(occurrence_serializer_stub).to receive(:represent).with(finding).and_return(occurrence_serializer_hash)
end
it 'has expected vulnerability properties' do
expect(subject).to include(
vulnerability_json: vulnerability.to_json,
vulnerability_json: kind_of(String),
project_fingerprint: vulnerability.finding.project_fingerprint,
create_issue_url: anything,
create_issue_url: kind_of(String),
has_mr: anything,
vulnerability_feedback_help_path: anything,
finding_json: anything
vulnerability_feedback_help_path: kind_of(String),
finding_json: kind_of(String)
)
end
end
before do
allow(helper).to receive(:can?).and_return(true)
allow(helper).to receive(:current_user).and_return(user)
end
let(:user) { build(:user) }
describe '#vulnerability_data' do
let(:vulnerability) { create(:vulnerability, :with_findings) }
subject { helper.vulnerability_data(vulnerability, pipeline) }
describe 'when pipeline exists' do
......@@ -55,9 +91,6 @@ describe VulnerabilitiesHelper do
end
describe '#vulnerability_finding_data' do
let(:vulnerability) { create(:vulnerability, :with_findings) }
let(:finding) { vulnerability.finding }
subject { helper.vulnerability_finding_data(finding) }
it "returns finding information" do
......
......@@ -3,12 +3,11 @@
require 'spec_helper'
describe VulnerabilityEntity do
let(:entity) { described_class.new(vulnerability) }
let(:vulnerability) { create(:vulnerability, project: project, author: user) }
let(:project) { create(:project) }
let(:user) { create(:user) }
subject { entity.as_json }
subject { described_class.new(vulnerability).as_json }
it 'exposes vulnerability-specific elements' do
expect(subject).to match(
......@@ -19,7 +18,10 @@ describe VulnerabilityEntity do
confidence: vulnerability.confidence,
report_type: vulnerability.report_type,
resolved_on_default_branch: vulnerability.resolved_on_default_branch,
project_default_branch: vulnerability.project_default_branch
project_default_branch: vulnerability.project_default_branch,
resolved_by_id: vulnerability.resolved_by_id,
dismissed_by_id: vulnerability.dismissed_by_id,
confirmed_by_id: vulnerability.confirmed_by_id
)
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