Commit 6acc1ff3 authored by Sean Arnold's avatar Sean Arnold

Update service and response codes

- Simplify checks in service
parent 06638bf5
......@@ -23,9 +23,7 @@ module EE
end
def metric_images_available?
return false unless ::AlertManagement::MetricImage.available_for?(project)
true
::AlertManagement::MetricImage.available_for?(project)
end
end
end
......
......@@ -15,7 +15,7 @@ module AlertManagement
end
def execute
return ServiceResponse.error(message: "Not allowed!") unless alert.metric_images_available? && can_upload_metrics?
return ServiceResponse.error(message: _("You are not authorized to upload metric images"), http_status: :forbidden) unless can_upload_metrics?
metric = AlertManagement::MetricImage.new(
alert: alert,
......@@ -27,14 +27,14 @@ module AlertManagement
if metric.save
ServiceResponse.success(payload: { metric: metric, alert: alert })
else
ServiceResponse.error(message: metric.errors.full_messages.join(', '))
ServiceResponse.error(message: metric.errors.full_messages.join(', '), http_status: :bad_request)
end
end
private
def can_upload_metrics?
current_user&.can?(:upload_alert_management_metric_image, alert)
alert.metric_images_available? && current_user&.can?(:upload_alert_management_metric_image, alert)
end
end
end
......
......@@ -52,7 +52,7 @@ module API
if upload.success?
present upload.payload[:metric], with: Entities::MetricImage, current_user: current_user, project: user_project
else
render_api_error!(upload.message, 400)
render_api_error!(upload.message, upload.http_status)
end
end
......@@ -80,7 +80,7 @@ module API
authorize!(:update_alert_management_metric_image, alert)
render_api_error!('Not allowed!', 400) unless alert.metric_images_available?
render_api_error!('Feature not available', 400) unless alert.metric_images_available?
metric_image = alert.metric_images.find_by_id(params[:metric_image_id])
......@@ -89,7 +89,7 @@ module API
if metric_image&.update(params.slice(:url, :url_text))
present metric_image, with: Entities::MetricImage, current_user: current_user, project: user_project
else
render_api_error!('Metric image could not be updated', 400)
render_api_error!('Metric image could not be updated', 422)
end
end
end
......
......@@ -161,7 +161,7 @@ RSpec.describe API::AlertManagementAlerts do
project.add_developer(user)
allow_next_instance_of(::AlertManagement::MetricImages::UploadService) do |service|
error = double(success?: false, message: 'some error')
error = double(success?: false, message: 'some error', http_status: :bad_request)
allow(service).to receive(:execute).and_return(error)
end
end
......@@ -321,7 +321,7 @@ RSpec.describe API::AlertManagementAlerts do
it 'returns an error' do
subject
expect(response).to have_gitlab_http_status(:bad_request)
expect(response).to have_gitlab_http_status(:unprocessable_entity)
expect(json_response['message']).to eq('Metric image could not be updated')
end
end
......@@ -336,7 +336,7 @@ RSpec.describe API::AlertManagementAlerts do
subject
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['message']).to eq('Not allowed!')
expect(json_response['message']).to eq('Feature not available')
end
end
end
......
......@@ -36,7 +36,7 @@ RSpec.describe AlertManagement::MetricImages::UploadService do
end
context 'user does not have permissions' do
it_behaves_like 'no metric saved, an error given', 'Not allowed!'
it_behaves_like 'no metric saved, an error given', 'You are not authorized to upload metric images'
end
context 'user has permissions' do
......@@ -44,7 +44,7 @@ RSpec.describe AlertManagement::MetricImages::UploadService do
project.add_developer(current_user)
end
it_behaves_like 'no metric saved, an error given', 'Not allowed!'
it_behaves_like 'no metric saved, an error given', 'You are not authorized to upload metric images'
context 'with license' do
before do
......@@ -79,7 +79,7 @@ RSpec.describe AlertManagement::MetricImages::UploadService do
project.add_guest(current_user)
end
it_behaves_like 'no metric saved, an error given', 'Not allowed!'
it_behaves_like 'no metric saved, an error given', 'You are not authorized to upload metric images'
end
end
end
......
......@@ -41693,6 +41693,9 @@ msgstr ""
msgid "You are not authorized to update this scanner profile"
msgstr ""
msgid "You are not authorized to upload metric images"
msgstr ""
msgid "You are now impersonating %{username}"
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