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 ...@@ -14,15 +14,8 @@ module Mutations
def resolve(args) def resolve(args)
alert = authorized_find!(project_path: args[:project_path], iid: args[:iid]) alert = authorized_find!(project_path: args[:project_path], iid: args[:iid])
if alert
result = update_status(alert, args[:status]) result = update_status(alert, args[:status])
prepare_response(result) prepare_response(result)
else
{
alert: alert,
errors: ['Alert could not be found']
}
end
end end
private private
...@@ -35,7 +28,7 @@ module Mutations ...@@ -35,7 +28,7 @@ module Mutations
def prepare_response(result) def prepare_response(result)
{ {
alert: result.payload[:alert], alert: result.payload[:alert],
errors: result.message.present? ? [result.message] : [] errors: result.error? ? [result.message].compact : []
} }
end end
end end
......
...@@ -13,7 +13,7 @@ module AlertManagement ...@@ -13,7 +13,7 @@ module AlertManagement
alert.status = status alert.status = status
return ServiceResponse.success(payload: { alert: alert }) if alert.save return ServiceResponse.success(payload: { alert: alert }) if alert.save
error_response error_response(alert.errors.full_messages.to_sentence)
end end
private private
......
...@@ -5,7 +5,7 @@ require 'spec_helper' ...@@ -5,7 +5,7 @@ require 'spec_helper'
describe Mutations::AlertManagement::UpdateAlertStatus do describe Mutations::AlertManagement::UpdateAlertStatus do
let_it_be(:current_user) { create(:user) } let_it_be(:current_user) { create(:user) }
let_it_be(:alert) { create(:alert_management_alert, status: 'triggered') } 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(:new_status) { 'acknowledged' }
let(:args) { { status: new_status, project_path: project.full_path, iid: alert.iid } } let(:args) { { status: new_status, project_path: project.full_path, iid: alert.iid } }
...@@ -20,6 +20,36 @@ describe Mutations::AlertManagement::UpdateAlertStatus do ...@@ -20,6 +20,36 @@ describe Mutations::AlertManagement::UpdateAlertStatus do
it 'changes the status' do it 'changes the status' do
expect { resolve }.to change { alert.reload.status }.from(alert.status).to(new_status) expect { resolve }.to change { alert.reload.status }.from(alert.status).to(new_status)
end 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 end
it 'raises an error if the resource is not accessible to the user' do 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