summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/slash_commands/deploy_spec.rb
blob: 93a724d8e12d70458ebc3edf5de90c4dd8e0f090 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::SlashCommands::Deploy do
  describe '#execute' do
    let(:project) { create(:project) }
    let(:user) { create(:user) }
    let(:chat_name) { double(:chat_name, user: user) }
    let(:regex_match) { described_class.match('deploy staging to production') }

    before do
      # Make it possible to trigger protected manual actions for developers.
      #
      project.add_developer(user)

      create(:protected_branch, :developers_can_merge,
             name: 'master', project: project)
    end

    subject do
      described_class.new(project, chat_name).execute(regex_match)
    end

    context 'if no environment is defined' do
      it 'does not execute an action' do
        expect(subject[:response_type]).to be(:ephemeral)
        expect(subject[:text]).to eq "Couldn't find a deployment manual action."
      end
    end

    context 'with environment' do
      let!(:staging) { create(:environment, name: 'staging', project: project) }
      let!(:pipeline) { create(:ci_pipeline, project: project) }
      let!(:build) { create(:ci_build, pipeline: pipeline) }
      let!(:deployment) { create(:deployment, :success, environment: staging, deployable: build) }

      context 'without actions' do
        it 'does not execute an action' do
          expect(subject[:response_type]).to be(:ephemeral)
          expect(subject[:text]).to eq "Couldn't find a deployment manual action."
        end
      end

      context 'when single action has been matched' do
        before do
          create(:ci_build, :manual, pipeline: pipeline,
                                     name: 'first',
                                     environment: 'production')
        end

        it 'returns success result' do
          expect(subject[:response_type]).to be(:in_channel)
          expect(subject[:text])
            .to start_with('Deployment started from staging to production')
        end
      end

      context 'when more than one action has been matched' do
        context 'when there is no specific actions with a environment name' do
          before do
            create(:ci_build, :manual, pipeline: pipeline,
                                       name: 'first',
                                       environment: 'production')

            create(:ci_build, :manual, pipeline: pipeline,
                                       name: 'second',
                                       environment: 'production')
          end

          it 'returns error about too many actions defined' do
            expect(subject[:text]).to eq("Couldn't find a deployment manual action.")
            expect(subject[:response_type]).to be(:ephemeral)
          end
        end

        context 'when one of the actions is environement specific action' do
          before do
            create(:ci_build, :manual, pipeline: pipeline,
                                       name: 'first',
                                       environment: 'production')

            create(:ci_build, :manual, pipeline: pipeline,
                                       name: 'production',
                                       environment: 'production')
          end

          it 'deploys to production' do
            expect(subject[:text])
              .to start_with('Deployment started from staging to production')
            expect(subject[:response_type]).to be(:in_channel)
          end
        end

        context 'when one of the actions is a teardown action' do
          before do
            create(:ci_build, :manual, pipeline: pipeline,
                                       name: 'first',
                                       environment: 'production')

            create(:ci_build, :manual, :teardown_environment,
                   pipeline: pipeline, name: 'teardown', environment: 'production')
          end

          it 'deploys to production' do
            expect(subject[:text])
              .to start_with('Deployment started from staging to production')
            expect(subject[:response_type]).to be(:in_channel)
          end
        end
      end
    end
  end

  describe 'self.match' do
    it 'matches the environment' do
      match = described_class.match('deploy staging to production')

      expect(match[:from]).to eq('staging')
      expect(match[:to]).to eq('production')
    end
  end
end