Commit dbb412f7 authored by Sean Arnold's avatar Sean Arnold

Update specs to use doubles

- Rename execute! to execute
- Add update_alert_management_alerts policy
parent b81bd8ff
...@@ -18,6 +18,8 @@ module Mutations ...@@ -18,6 +18,8 @@ module Mutations
null: true, null: true,
description: "The alert after mutation" description: "The alert after mutation"
authorize :update_alert_management_alerts
private private
def find_object(project_path:, iid:) def find_object(project_path:, iid:)
......
...@@ -9,8 +9,6 @@ module Mutations ...@@ -9,8 +9,6 @@ module Mutations
required: true, required: true,
description: 'The status to set the alert' description: 'The status to set the alert'
authorize :read_alert_management_alerts
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])
...@@ -22,7 +20,7 @@ module Mutations ...@@ -22,7 +20,7 @@ module Mutations
def update_status(alert, status) def update_status(alert, status)
service = ::AlertManagement::UpdateAlertStatusService.new(alert, status) service = ::AlertManagement::UpdateAlertStatusService.new(alert, status)
service.execute! service.execute
end end
def prepare_response(result) def prepare_response(result)
......
...@@ -240,6 +240,7 @@ class ProjectPolicy < BasePolicy ...@@ -240,6 +240,7 @@ class ProjectPolicy < BasePolicy
enable :read_prometheus enable :read_prometheus
enable :read_metrics_dashboard_annotation enable :read_metrics_dashboard_annotation
enable :read_alert_management_alerts enable :read_alert_management_alerts
enable :update_alert_management_alerts
enable :metrics_dashboard enable :metrics_dashboard
end end
......
...@@ -7,7 +7,7 @@ module AlertManagement ...@@ -7,7 +7,7 @@ module AlertManagement
@status = status @status = status
end end
def execute! def execute
return error_response('Invalid status') unless AlertManagement::Alert.statuses.key?(status.to_s) return error_response('Invalid status') unless AlertManagement::Alert.statuses.key?(status.to_s)
alert.status = status alert.status = status
......
...@@ -29,28 +29,34 @@ describe Mutations::AlertManagement::UpdateAlertStatus do ...@@ -29,28 +29,34 @@ describe Mutations::AlertManagement::UpdateAlertStatus do
end end
context 'error occurs when updating' do context 'error occurs when updating' do
before do it 'returns the alert with errors' do
# Stubbing and error on alert # Stub an error on the alert
allow_next_instance_of(Resolvers::AlertManagementAlertResolver) do |resolver| allow_next_instance_of(Resolvers::AlertManagementAlertResolver) do |resolver|
allow(resolver).to receive(:resolve).and_return(alert) allow(resolver).to receive(:resolve).and_return(alert)
end end
expect(alert).to receive(:save).and_return(false) expect(alert).to receive(:save).and_return(false)
expect(alert).to receive(:errors).and_return(
errors = ActiveModel::Errors.new(alert) double(full_messages: %w(foo bar))
errors.add(:status, :invalid) )
expect(resolve).to eq(
expect(alert).to receive(:errors).and_return(errors) alert: alert,
errors: ['foo and bar']
)
end end
it 'returns the alert with no errors' do context 'invalid status given' do
let(:new_status) { 'invalid_status' }
it 'returns the alert with errors' do
expect(resolve).to eq( expect(resolve).to eq(
alert: alert, alert: alert,
errors: ['Status is invalid'] errors: ['Invalid status']
) )
end end
end 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
expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
......
...@@ -5,8 +5,8 @@ require 'spec_helper' ...@@ -5,8 +5,8 @@ require 'spec_helper'
describe AlertManagement::UpdateAlertStatusService do describe AlertManagement::UpdateAlertStatusService do
let_it_be(:alert) { create(:alert_management_alert, status: 'triggered') } let_it_be(:alert) { create(:alert_management_alert, status: 'triggered') }
describe '#execute!' do describe '#execute' do
subject(:execute) { described_class.new(alert, new_status).execute! } subject(:execute) { described_class.new(alert, new_status).execute }
let(:new_status) { 'acknowledged' } let(:new_status) { 'acknowledged' }
......
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