Commit b81bd8ff authored by Sean Arnold's avatar Sean Arnold

Test for errors additon

- Ensure we add model errors
- Simplify resolve in mutation
parent b55602e2
......@@ -14,15 +14,8 @@ module Mutations
def resolve(args)
alert = authorized_find!(project_path: args[:project_path], iid: args[:iid])
if alert
result = update_status(alert, args[:status])
prepare_response(result)
else
{
alert: alert,
errors: ['Alert could not be found']
}
end
result = update_status(alert, args[:status])
prepare_response(result)
end
private
......@@ -35,7 +28,7 @@ module Mutations
def prepare_response(result)
{
alert: result.payload[:alert],
errors: result.message.present? ? [result.message] : []
errors: result.error? ? [result.message].compact : []
}
end
end
......
......@@ -13,7 +13,7 @@ module AlertManagement
alert.status = status
return ServiceResponse.success(payload: { alert: alert }) if alert.save
error_response
error_response(alert.errors.full_messages.to_sentence)
end
private
......
......@@ -5,7 +5,7 @@ require 'spec_helper'
describe Mutations::AlertManagement::UpdateAlertStatus do
let_it_be(:current_user) { create(:user) }
let_it_be(:alert) { create(:alert_management_alert, status: 'triggered') }
let(:project) { alert.project }
let_it_be(:project) { alert.project }
let(:new_status) { 'acknowledged' }
let(:args) { { status: new_status, project_path: project.full_path, iid: alert.iid } }
......@@ -20,6 +20,36 @@ describe Mutations::AlertManagement::UpdateAlertStatus do
it 'changes the status' do
expect { resolve }.to change { alert.reload.status }.from(alert.status).to(new_status)
end
it 'returns the alert with no errors' do
expect(resolve).to eq(
alert: alert,
errors: []
)
end
context 'error occurs when updating' do
before do
# Stubbing and error on alert
allow_next_instance_of(Resolvers::AlertManagementAlertResolver) do |resolver|
allow(resolver).to receive(:resolve).and_return(alert)
end
expect(alert).to receive(:save).and_return(false)
errors = ActiveModel::Errors.new(alert)
errors.add(:status, :invalid)
expect(alert).to receive(:errors).and_return(errors)
end
it 'returns the alert with no errors' do
expect(resolve).to eq(
alert: alert,
errors: ['Status is invalid']
)
end
end
end
it 'raises an error if the resource is not accessible to the user' do
......
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