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