Commit 79b829d3 authored by Mehmet Emin INAC's avatar Mehmet Emin INAC

Use InstanceSecurityDashboard as exportable instead of User

This might sound better because even the feature name is instance level
vulnerability exports so we are using here an instance of
InstanceSecurityDashboard as exportable instead of the author object.
parent 051f0c95
...@@ -349,8 +349,8 @@ module EE ...@@ -349,8 +349,8 @@ module EE
gitlab_employee? ? 'GitLab' : super gitlab_employee? ? 'GitLab' : super
end end
def vulnerabilities def security_dashboard
InstanceSecurityDashboard.new(self).vulnerabilities InstanceSecurityDashboard.new(self)
end end
protected protected
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
class InstanceSecurityDashboard class InstanceSecurityDashboard
extend ActiveModel::Naming extend ActiveModel::Naming
delegate :full_path, to: :user
def initialize(user, project_ids: []) def initialize(user, project_ids: [])
@project_ids = project_ids @project_ids = project_ids
@user = user @user = user
......
...@@ -48,16 +48,15 @@ module Vulnerabilities ...@@ -48,16 +48,15 @@ module Vulnerabilities
end end
def exportable def exportable
project || author project || author.security_dashboard
end end
def exportable=(value) def exportable=(value)
case value case value
when Project when Project
self.project = value self.project = value
when User when InstanceSecurityDashboard
self.project = nil self.project = nil
self.author = value
else else
raise "Can not assign #{value.class} as exportable" raise "Can not assign #{value.class} as exportable"
end end
......
...@@ -16,9 +16,6 @@ module EE ...@@ -16,9 +16,6 @@ module EE
updating_name_disabled_for_users & updating_name_disabled_for_users &
~admin ~admin
end.prevent :update_name end.prevent :update_name
# TODO: Check this before removing WIP from MR
rule { user_is_self }.enable :create_vulnerability_export
end end
end end
end end
# frozen_string_literal: true # frozen_string_literal: true
class InstanceSecurityDashboardPolicy < BasePolicy class InstanceSecurityDashboardPolicy < BasePolicy
rule { ~anonymous }.enable :read_instance_security_dashboard rule { ~anonymous }.policy do
enable :read_instance_security_dashboard
enable :create_vulnerability_export
end
end end
...@@ -115,4 +115,12 @@ describe InstanceSecurityDashboard do ...@@ -115,4 +115,12 @@ describe InstanceSecurityDashboard do
end end
end end
end end
describe '#full_path' do
let(:user) { create(:user) }
it 'returns the full_path of the user' do
expect(subject.full_path).to eql(user.full_path)
end
end
end end
...@@ -1144,20 +1144,13 @@ describe User do ...@@ -1144,20 +1144,13 @@ describe User do
end end
end end
describe '#vulnerabilities' do describe '#security_dashboard' do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:vulnerability_collection) { instance_double(ActiveRecord::Relation) }
let(:mock_security_dashboard) { instance_double(InstanceSecurityDashboard, vulnerabilities: vulnerability_collection) }
subject(:vulnerabilities) { user.vulnerabilities } subject(:security_dashboard) { user.security_dashboard }
before do it 'returns an instance of InstanceSecurityDashboard for the user' do
allow(InstanceSecurityDashboard).to receive(:new).and_return(mock_security_dashboard) expect(security_dashboard).to be_a(InstanceSecurityDashboard)
end
it 'delegates the call to an instance of `InstanceSecurityDashboard`' do
expect(vulnerabilities).to eql(vulnerability_collection)
expect(InstanceSecurityDashboard).to have_received(:new).with(user)
end end
end end
end end
...@@ -80,8 +80,13 @@ describe Vulnerabilities::Export do ...@@ -80,8 +80,13 @@ describe Vulnerabilities::Export do
context 'when the export does not have project assigned' do context 'when the export does not have project assigned' do
let(:author) { build(:user) } let(:author) { build(:user) }
let(:vulnerability_export) { build(:vulnerability_export, :user, author: author) } let(:vulnerability_export) { build(:vulnerability_export, :user, author: author) }
let(:mock_security_dashboard) { instance_double(InstanceSecurityDashboard) }
it { is_expected.to eql(author) } before do
allow(author).to receive(:security_dashboard).and_return(mock_security_dashboard)
end
it { is_expected.to eql(mock_security_dashboard) }
end end
end end
...@@ -98,10 +103,14 @@ describe Vulnerabilities::Export do ...@@ -98,10 +103,14 @@ describe Vulnerabilities::Export do
end end
end end
context 'when the exportable is a User' do context 'when the exportable is an InstanceSecurityDashboard' do
let(:exportable) { build(:user) } let(:exportable) { InstanceSecurityDashboard.new(vulnerability_export.author) }
before do
allow(vulnerability_export.author).to receive(:security_dashboard).and_return(exportable)
end
it 'changes the exportable of the export to given user' do it 'changes the exportable of the export to security dashboard of the author' do
expect { set_exportable }.to change { vulnerability_export.exportable }.to(exportable) expect { set_exportable }.to change { vulnerability_export.exportable }.to(exportable)
end end
end end
......
...@@ -23,4 +23,16 @@ describe InstanceSecurityDashboardPolicy do ...@@ -23,4 +23,16 @@ describe InstanceSecurityDashboardPolicy do
it { is_expected.to be_allowed(:read_instance_security_dashboard) } it { is_expected.to be_allowed(:read_instance_security_dashboard) }
end end
end end
describe 'create_vulnerability_export' do
context 'when the user is not logged in' do
let(:current_user) { nil }
it { is_expected.not_to be_allowed(:create_vulnerability_export) }
end
context 'when the user is logged in' do
it { is_expected.to be_allowed(:create_vulnerability_export) }
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