Commit 74c27d7c authored by Vitali Tatarintev's avatar Vitali Tatarintev

Merge branch '346247_authorize_auto_complete_action_for_vulnerabilities' into 'master'

Do not try to auto-complete vulnerabilities if the user is nil

See merge request gitlab-org/gitlab!75112
parents f51f45b9 3e0173cc
...@@ -20,7 +20,7 @@ module Autocomplete ...@@ -20,7 +20,7 @@ module Autocomplete
DEFAULT_AUTOCOMPLETE_LIMIT = 5 DEFAULT_AUTOCOMPLETE_LIMIT = 5
def execute def execute
return ::Vulnerability.none unless vulnerable.feature_available?(:security_dashboard) return ::Vulnerability.none unless current_user && vulnerable.feature_available?(:security_dashboard)
::Security::VulnerabilitiesFinder # rubocop: disable CodeReuse/Finder ::Security::VulnerabilitiesFinder # rubocop: disable CodeReuse/Finder
.new(vulnerable) .new(vulnerable)
......
...@@ -7,64 +7,73 @@ RSpec.describe Autocomplete::VulnerabilitiesAutocompleteFinder do ...@@ -7,64 +7,73 @@ RSpec.describe Autocomplete::VulnerabilitiesAutocompleteFinder do
let_it_be(:group, refind: true) { create(:group) } let_it_be(:group, refind: true) { create(:group) }
let_it_be(:project, refind: true) { create(:project, group: group) } let_it_be(:project, refind: true) { create(:project, group: group) }
let_it_be(:vulnerability) { create(:vulnerability, project: project) } let_it_be(:vulnerability) { create(:vulnerability, project: project) }
let(:params) { {} }
let_it_be(:user) { create(:user) } let(:params) { {} }
subject { described_class.new(user, vulnerable, params).execute } subject { described_class.new(user, vulnerable, params).execute }
shared_examples 'autocomplete vulnerabilities finder' do shared_examples 'autocomplete vulnerabilities finder' do
context 'when user does not have access to project' do context 'when the given user is nil' do
let(:user) { nil }
it { is_expected.to be_empty } it { is_expected.to be_empty }
end end
context 'when user has access to project' do context 'when the given user is not nil' do
before do let_it_be(:user) { create(:user) }
vulnerable.add_developer(user)
end
context 'when security dashboards are not enabled' do context 'when user does not have access to project' do
it { is_expected.to be_empty } it { is_expected.to be_empty }
end end
context 'when security dashboards are enabled' do context 'when user has access to project' do
before do before do
stub_licensed_features(security_dashboard: true) vulnerable.add_developer(user)
end end
it { is_expected.to match_array([vulnerability]) } context 'when security dashboards are not enabled' do
it { is_expected.to be_empty }
end
context 'when multiple vulnerabilities are found' do context 'when security dashboards are enabled' do
before do before do
create_list(:vulnerability, 10, project: project) stub_licensed_features(security_dashboard: true)
end end
it 'returns max 5 items' do it { is_expected.to match_array([vulnerability]) }
expect(subject.count).to eq(5)
end
it 'is sorted descending by id' do context 'when multiple vulnerabilities are found' do
expect(subject).to be_sorted(:id, :desc) before do
end create_list(:vulnerability, 10, project: project)
end end
context 'when search is provided in params' do it 'returns max 5 items' do
context 'and it matches ID of vulnerability' do expect(subject.count).to eq(5)
let(:params) { { search: vulnerability.id.to_s } } end
it { is_expected.to match_array([vulnerability]) } it 'is sorted descending by id' do
expect(subject).to be_sorted(:id, :desc)
end
end end
context 'and it matches title of vulnerability' do context 'when search is provided in params' do
let(:params) { { search: vulnerability.title } } context 'and it matches ID of vulnerability' do
let(:params) { { search: vulnerability.id.to_s } }
it { is_expected.to match_array([vulnerability]) } it { is_expected.to match_array([vulnerability]) }
end end
context 'and it matches title of vulnerability' do
let(:params) { { search: vulnerability.title } }
it { is_expected.to match_array([vulnerability]) }
end
context 'and it does not match neither title or id of vulnerability' do context 'and it does not match neither title or id of vulnerability' do
let(:params) { { search: non_existing_record_id.to_s } } let(:params) { { search: non_existing_record_id.to_s } }
it { is_expected.to be_empty } it { is_expected.to be_empty }
end
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