Welcome to mirror list, hosted at ThFree Co, Russian Federation.

create_service_spec.rb « todo « alerts « alert_management « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c883466cf25068a87e22bf479c2a88a85a0bfaac (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe AlertManagement::Alerts::Todo::CreateService, feature_category: :incident_management do
  let_it_be(:user) { create(:user) }
  let_it_be(:alert) { create(:alert_management_alert) }

  let(:current_user) { user }

  describe '#execute' do
    subject(:result) { described_class.new(alert, current_user).execute }

    shared_examples 'permissions error' do
      it 'returns an error', :aggregate_failures do
        expect(result.error?).to be(true)
        expect(result.message).to eq('You have insufficient permissions to create a Todo for this alert')
        expect(result.payload[:todo]).to be(nil)
        expect(result.payload[:alert]).to be(alert)
      end
    end

    context 'when the user is anonymous' do
      let(:current_user) { nil }

      it_behaves_like 'permissions error'
    end

    context 'when the user does not have permission' do
      it_behaves_like 'permissions error'
    end

    context 'when user has permission' do
      before do
        alert.project.add_developer(user)
      end

      it 'creates a todo' do
        expect { result }.to change { Todo.count }.by(1)
      end

      it 'returns the alert and todo in the payload', :aggregate_failures do
        expect(result.success?).to be(true)
        expect(result.payload[:alert][:id]).to be(alert.id)
        expect(result.payload[:todo][:id]).to be(Todo.last.id)
      end

      context 'when the user has a marked todo for the alert' do
        let_it_be(:todo_params) do
          { project: alert.project,
            target: alert,
            user: user,
            action: Todo::MARKED }
        end

        context 'when todo is pending' do
          before_all do
            create(:todo, :pending, **todo_params)
          end

          before do
            stub_feature_flags(multiple_todos: false)
          end

          it 'does not create a todo' do
            expect { result }.not_to change { Todo.count }
          end

          it 'returns an error', :aggregate_failures do
            expect(result.error?).to be(true)
            expect(result.message).to be('You already have pending todo for this alert')
            expect(result.payload[:todo]).to be(nil)
            expect(result.payload[:alert]).to be(alert)
          end
        end

        context 'when todo is done' do
          before do
            create(:todo, :done, **todo_params)
          end

          it { expect(result.success?).to be(true) }
          it { expect { result }.to change { Todo.count }.by(1) }
        end
      end
    end
  end
end