summaryrefslogtreecommitdiff
path: root/spec/graphql/mutations/alert_management/alerts/todo/create_spec.rb
blob: a10c3725ba21e1d3e162e5dac574fd2ac9b61acc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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) }

    it_behaves_like 'an incident management tracked event', :incident_management_alert_todo

    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