Commit e3f5ac66 authored by Dylan Griffith's avatar Dylan Griffith

Merge branch '213013_group_level_security_reports_entity_changes' into 'master'

Extend the exportable types of export with Group

See merge request gitlab-org/gitlab!31879
parents f994e6a7 e268eb55
...@@ -19,6 +19,7 @@ module Vulnerabilities ...@@ -19,6 +19,7 @@ module Vulnerabilities
validates :status, presence: true validates :status, presence: true
validates :format, presence: true validates :format, presence: true
validates :file, presence: true, if: :finished? validates :file, presence: true, if: :finished?
validate :only_one_exportable
state_machine :status, initial: :created do state_machine :status, initial: :created do
event :start do event :start do
...@@ -48,15 +49,17 @@ module Vulnerabilities ...@@ -48,15 +49,17 @@ module Vulnerabilities
end end
def exportable def exportable
project || author.security_dashboard project || group || author.security_dashboard
end end
def exportable=(value) def exportable=(value)
case value case value
when Project when Project
self.project = value make_project_level_export(value)
when Group
make_group_level_export(value)
when InstanceSecurityDashboard when InstanceSecurityDashboard
self.project = nil make_instance_level_export
else else
raise "Can not assign #{value.class} as exportable" raise "Can not assign #{value.class} as exportable"
end end
...@@ -75,5 +78,25 @@ module Vulnerabilities ...@@ -75,5 +78,25 @@ module Vulnerabilities
# which happens after object is inserted/updated # which happens after object is inserted/updated
self.update_column(:file_store, file.object_store) self.update_column(:file_store, file.object_store)
end end
private
def make_project_level_export(project)
self.project = project
self.group = nil
end
def make_group_level_export(group)
self.group = group
self.project = nil
end
def make_instance_level_export
self.project = self.group = nil
end
def only_one_exportable
errors.add(:base, _('Project & Group can not be assigned at the same time')) if project && group
end
end end
end end
...@@ -23,6 +23,49 @@ describe Vulnerabilities::Export do ...@@ -23,6 +23,49 @@ describe Vulnerabilities::Export do
it { is_expected.to validate_presence_of(:file) } it { is_expected.to validate_presence_of(:file) }
end end
describe 'presence of both project and group' do
let(:export) { build(:vulnerability_export, project: project, group: group) }
let(:expected_error) { _('Project & Group can not be assigned at the same time') }
subject { export.errors[:base] }
before do
export.validate
end
context 'when the project is present' do
let(:project) { build(:project) }
context 'when the group is present' do
let(:group) { build(:group) }
it { is_expected.to include(expected_error) }
end
context 'when the group is not present' do
let(:group) { nil }
it { is_expected.not_to include(expected_error) }
end
end
context 'when the project is not present' do
let(:project) { nil }
context 'when the group is present' do
let(:group) { build(:group) }
it { is_expected.not_to include(expected_error) }
end
context 'when the group is not present' do
let(:group) { nil }
it { is_expected.not_to include(expected_error) }
end
end
end
end end
describe '#status' do describe '#status' do
...@@ -74,19 +117,28 @@ describe Vulnerabilities::Export do ...@@ -74,19 +117,28 @@ describe Vulnerabilities::Export do
let(:project) { build(:project) } let(:project) { build(:project) }
let(:vulnerability_export) { build(:vulnerability_export, project: project) } let(:vulnerability_export) { build(:vulnerability_export, project: project) }
it { is_expected.to eql(project) } it { is_expected.to eq(project) }
end end
context 'when the export does not have project assigned' do context 'when the export does not have project assigned' do
let(:author) { build(:user) } context 'when the export has group assigned' do
let(:vulnerability_export) { build(:vulnerability_export, :user, author: author) } let(:group) { build(:group) }
let(:mock_security_dashboard) { instance_double(InstanceSecurityDashboard) } let(:vulnerability_export) { build(:vulnerability_export, :group, group: group) }
before do it { is_expected.to eq(group) }
allow(author).to receive(:security_dashboard).and_return(mock_security_dashboard)
end end
it { is_expected.to eql(mock_security_dashboard) } context 'when the export does not have group assigned' do
let(:author) { build(:user) }
let(:vulnerability_export) { build(:vulnerability_export, :user, author: author) }
let(:mock_security_dashboard) { instance_double(InstanceSecurityDashboard) }
before do
allow(author).to receive(:security_dashboard).and_return(mock_security_dashboard)
end
it { is_expected.to eq(mock_security_dashboard) }
end
end end
end end
...@@ -103,6 +155,14 @@ describe Vulnerabilities::Export do ...@@ -103,6 +155,14 @@ describe Vulnerabilities::Export do
end end
end end
context 'when the exportable is a Group' do
let(:exportable) { build(:group) }
it 'changes the exportable of the export to given group' do
expect { set_exportable }.to change { vulnerability_export.exportable }.to(exportable)
end
end
context 'when the exportable is an InstanceSecurityDashboard' do context 'when the exportable is an InstanceSecurityDashboard' do
let(:exportable) { InstanceSecurityDashboard.new(vulnerability_export.author) } let(:exportable) { InstanceSecurityDashboard.new(vulnerability_export.author) }
......
...@@ -16282,6 +16282,9 @@ msgstr "" ...@@ -16282,6 +16282,9 @@ msgstr ""
msgid "Project %{project_repo} could not be found" msgid "Project %{project_repo} could not be found"
msgstr "" msgstr ""
msgid "Project & Group can not be assigned at the same time"
msgstr ""
msgid "Project '%{project_name}' is being imported." msgid "Project '%{project_name}' is being imported."
msgstr "" msgstr ""
......
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