summaryrefslogtreecommitdiff
path: root/spec/services/alert_management/update_alert_status_service_spec.rb
blob: b287d0d1614b79c33f77f5ef5b8dbbe5317c6d82 (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
61
62
63
64
65
66
# frozen_string_literal: true

require 'spec_helper'

describe AlertManagement::UpdateAlertStatusService do
  let(:project) { alert.project }
  let_it_be(:user) { build(:user) }

  let_it_be(:alert, reload: true) do
    create(:alert_management_alert, :triggered)
  end

  let(:service) { described_class.new(alert, user, new_status) }

  describe '#execute' do
    shared_examples 'update failure' do |error_message|
      it 'returns an error' do
        expect(response).to be_error
        expect(response.message).to eq(error_message)
        expect(response.payload[:alert]).to eq(alert)
      end

      it 'does not update the status' do
        expect { response }.not_to change { alert.status }
      end
    end

    let(:new_status) { Types::AlertManagement::StatusEnum.values['ACKNOWLEDGED'].value }
    let(:can_update) { true }

    subject(:response) { service.execute }

    before do
      allow(user).to receive(:can?)
        .with(:update_alert_management_alert, project)
        .and_return(can_update)
    end

    it 'returns success' do
      expect(response).to be_success
      expect(response.payload[:alert]).to eq(alert)
    end

    it 'updates the status' do
      expect { response }.to change { alert.acknowledged? }.to(true)
    end

    context 'when user has no permissions' do
      let(:can_update) { false }

      include_examples 'update failure', _('You have no permissions')
    end

    context 'with no status' do
      let(:new_status) { nil }

      include_examples 'update failure', _('Invalid status')
    end

    context 'with unknown status' do
      let(:new_status) { -1 }

      include_examples 'update failure', _('Invalid status')
    end
  end
end