Commit ef7b242c authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch '292927-refactor-response' into 'master'

Add Service Responce to SaveAutoFix service

See merge request gitlab-org/gitlab!66472
parents 020110b3 3a48b510
...@@ -47,11 +47,13 @@ module EE ...@@ -47,11 +47,13 @@ module EE
# rubocop:enable Gitlab/ModuleWithInstanceVariables # rubocop:enable Gitlab/ModuleWithInstanceVariables
def auto_fix def auto_fix
service = ::Security::Configuration::SaveAutoFixService.new(project, auto_fix_params[:feature]) service = ::Security::Configuration::SaveAutoFixService
.new(project, auto_fix_params[:feature])
.execute(enabled: auto_fix_params[:enabled])
return respond_422 unless service.execute(enabled: auto_fix_params[:enabled]) return respond_422 unless service.success?
render status: :ok, json: auto_fix_settings render status: :ok, json: service.payload
end end
private private
...@@ -77,15 +79,6 @@ module EE ...@@ -77,15 +79,6 @@ module EE
render_404 if ::Feature.disabled?(:security_auto_fix, project) render_404 if ::Feature.disabled?(:security_auto_fix, project)
end end
def auto_fix_settings
setting = project.security_setting
{
dependency_scanning: setting.auto_fix_dependency_scanning,
container_scanning: setting.auto_fix_container_scanning
}
end
def security_dashboard_feature_enabled? def security_dashboard_feature_enabled?
vulnerable.feature_available?(:security_dashboard) vulnerable.feature_available?(:security_dashboard)
end end
......
...@@ -13,15 +13,32 @@ module Security ...@@ -13,15 +13,32 @@ module Security
end end
def execute(enabled:) def execute(enabled:)
return unless valid? return error("Auto fix is not available for #{feature} feature") unless valid?
return error("Project has no security setting") unless setting
project&.security_setting&.update(toggle_params(enabled)) if setting&.update(toggle_params(enabled))
success(updated_setting)
else
error('Error during updating the auto fix param')
end
end end
private private
attr_reader :enabled, :feature, :project attr_reader :enabled, :feature, :project
def error(message)
ServiceResponse.error(message: message)
end
def setting
@setting ||= project&.security_setting
end
def success(payload)
ServiceResponse.success(payload: payload)
end
def toggle_params(enabled) def toggle_params(enabled)
if feature == 'all' if feature == 'all'
{ {
...@@ -37,6 +54,13 @@ module Security ...@@ -37,6 +54,13 @@ module Security
end end
end end
def updated_setting
{
container_scanning: setting.auto_fix_container_scanning,
dependency_scanning: setting.auto_fix_dependency_scanning
}
end
def valid? def valid?
SUPPORTED_SCANNERS.include?(feature) SUPPORTED_SCANNERS.include?(feature)
end end
......
...@@ -140,20 +140,13 @@ RSpec.describe Projects::Security::ConfigurationController do ...@@ -140,20 +140,13 @@ RSpec.describe Projects::Security::ConfigurationController do
context 'with sufficient permissions' do context 'with sufficient permissions' do
let(:user) { maintainer } let(:user) { maintainer }
it 'shows auto fix disable for dependency scanning for json format' do
get :show, params: { namespace_id: project.namespace, project_id: project, format: :json }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['auto_fix_enabled']).to include({ 'dependency_scanning' => false })
end
context 'with setup feature param' do context 'with setup feature param' do
let(:feature) { :dependency_scanning } let(:feature) { :dependency_scanning }
it 'processes request and updates setting' do it 'processes request and updates setting' do
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(project.security_setting.reload.auto_fix_dependency_scanning).to be_falsey expect(project.security_setting.reload.auto_fix_dependency_scanning).to be_falsey
expect(response[:dependency_scanning]).to be_falsey expect(json_response['dependency_scanning']).to be(false)
end end
end end
...@@ -166,7 +159,8 @@ RSpec.describe Projects::Security::ConfigurationController do ...@@ -166,7 +159,8 @@ RSpec.describe Projects::Security::ConfigurationController do
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(setting.auto_fix_dependency_scanning).to be_falsey expect(setting.auto_fix_dependency_scanning).to be_falsey
expect(setting.auto_fix_dast).to be_falsey expect(setting.auto_fix_dast).to be_falsey
expect(response[:container_scanning]).to be_falsey expect(json_response['dependency_scanning']).to be(false)
expect(json_response['container_scanning']).to be(false)
end end
end end
......
...@@ -6,16 +6,21 @@ RSpec.describe Security::Configuration::SaveAutoFixService do ...@@ -6,16 +6,21 @@ RSpec.describe Security::Configuration::SaveAutoFixService do
describe '#execute' do describe '#execute' do
let_it_be_with_reload(:project) { create(:project) } let_it_be_with_reload(:project) { create(:project) }
subject(:service) { described_class.new(project, feature) } let(:service) { described_class.new(project, feature) }
before do subject(:response) { service.execute(enabled: false) }
service.execute(enabled: false)
end
context 'with supported scanner type' do context 'with supported scanner type' do
let(:feature) { 'dependency_scanning' } let(:feature) { 'dependency_scanning' }
it 'returns success status' do
expect(response).to be_success
expect(response.payload).to eq({ container_scanning: true, dependency_scanning: false })
end
it 'changes setting' do it 'changes setting' do
response
expect(project.security_setting.auto_fix_dependency_scanning).to be_falsey expect(project.security_setting.auto_fix_dependency_scanning).to be_falsey
end end
end end
...@@ -23,7 +28,13 @@ RSpec.describe Security::Configuration::SaveAutoFixService do ...@@ -23,7 +28,13 @@ RSpec.describe Security::Configuration::SaveAutoFixService do
context 'with all scanners' do context 'with all scanners' do
let(:feature) { 'all' } let(:feature) { 'all' }
it 'returns success status' do
expect(response).to be_success
end
it 'changes setting' do it 'changes setting' do
response
expect(project.security_setting.auto_fix_dependency_scanning).to be_falsey expect(project.security_setting.auto_fix_dependency_scanning).to be_falsey
expect(project.security_setting.auto_fix_container_scanning).to be_falsey expect(project.security_setting.auto_fix_container_scanning).to be_falsey
end end
...@@ -33,7 +44,8 @@ RSpec.describe Security::Configuration::SaveAutoFixService do ...@@ -33,7 +44,8 @@ RSpec.describe Security::Configuration::SaveAutoFixService do
let(:feature) { :dep_scan } let(:feature) { :dep_scan }
it 'does not change setting' do it 'does not change setting' do
expect(project.security_setting.auto_fix_dependency_scanning).to be_truthy expect(response).to be_error
expect(response.message).to eq('Auto fix is not available for dep_scan feature')
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