diff options
Diffstat (limited to 'spec/graphql/mutations/alert_management/alerts/todo/create_spec.rb')
-rw-r--r-- | spec/graphql/mutations/alert_management/alerts/todo/create_spec.rb | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/spec/graphql/mutations/alert_management/alerts/todo/create_spec.rb b/spec/graphql/mutations/alert_management/alerts/todo/create_spec.rb new file mode 100644 index 00000000000..11ee40a4c7e --- /dev/null +++ b/spec/graphql/mutations/alert_management/alerts/todo/create_spec.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Mutations::AlertManagement::Alerts::Todo::Create do + subject(:mutation) { described_class.new(object: project, context: { current_user: current_user }, field: nil) } + + let_it_be(:alert) { create(:alert_management_alert) } + let_it_be(:project) { alert.project } + let(:current_user) { project.owner } + + let(:args) { { project_path: project.full_path, iid: alert.iid } } + + specify { expect(described_class).to require_graphql_authorizations(:update_alert_management_alert) } + + describe '#resolve' do + subject(:resolve) { mutation.resolve(args) } + + context 'when user does not have permissions' do + let(:current_user) { nil } + + specify { expect { resolve }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) } + end + + context 'when project is invalid' do + let(:args) { { project_path: 'bunk/path', iid: alert.iid } } + + specify { expect { resolve }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) } + end + + context 'when alert is invalid' do + let(:args) { { project_path: project.full_path, iid: "-1" } } + + specify { expect { resolve }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) } + end + + context 'when the create service yields errors' do + let(:error_response) { double(error?: true, message: 'error', payload: { alert: {} }) } + + before do + allow_next_instance_of(::AlertManagement::Alerts::Todo::CreateService) do |service| + allow(service).to receive(:execute).and_return(error_response) + end + end + + specify { expect { resolve }.not_to change(Todo, :count) } + specify { expect(resolve[:errors]).to eq([error_response.message]) } + end + + context 'with valid inputs' do + it 'creates a new todo' do + expect { resolve }.to change { Todo.where(user: current_user, action: Todo::MARKED).count }.by(1) + end + + it { is_expected.to eq(alert: alert, todo: Todo.last, errors: []) } + end + end +end |