BigW Consortium Gitlab

deploy_spec.rb 2.79 KB
Newer Older
1 2 3 4 5 6 7 8 9
require 'spec_helper'

describe Gitlab::ChatCommands::Deploy, service: true do
  describe '#execute' do
    let(:project) { create(:empty_project) }
    let(:user) { create(:user) }
    let(:regex_match) { described_class.match('deploy staging to production') }

    before do
10
      project.add_master(user)
11 12 13 14 15 16 17
    end

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

    context 'if no environment is defined' do
18 19 20
      it 'does not execute an action' do
        expect(subject[:response_type]).to be(:ephemeral)
        expect(subject[:text]).to eq("No action found to be executed")
21 22 23 24 25
      end
    end

    context 'with environment' do
      let!(:staging) { create(:environment, name: 'staging', project: project) }
26 27
      let!(:pipeline) { create(:ci_pipeline, project: project) }
      let!(:build) { create(:ci_build, pipeline: pipeline) }
28 29 30
      let!(:deployment) { create(:deployment, environment: staging, deployable: build) }

      context 'without actions' do
31 32 33
        it 'does not execute an action' do
          expect(subject[:response_type]).to be(:ephemeral)
          expect(subject[:text]).to eq("No action found to be executed")
34 35 36 37 38
        end
      end

      context 'with action' do
        let!(:manual1) do
39 40 41
          create(:ci_build, :manual, pipeline: pipeline,
                                     name: 'first',
                                     environment: 'production')
42 43
        end

44
        it 'returns success result' do
45 46
          expect(subject[:response_type]).to be(:in_channel)
          expect(subject[:text]).to start_with('Deployment started from staging to production')
47 48 49 50
        end

        context 'when duplicate action exists' do
          let!(:manual2) do
51 52 53
            create(:ci_build, :manual, pipeline: pipeline,
                                       name: 'second',
                                       environment: 'production')
54 55 56
          end

          it 'returns error' do
57 58
            expect(subject[:response_type]).to be(:ephemeral)
            expect(subject[:text]).to eq('Too many actions defined')
59 60 61 62 63
          end
        end

        context 'when teardown action exists' do
          let!(:teardown) do
64
            create(:ci_build, :manual, :teardown_environment,
65
                   pipeline: pipeline, name: 'teardown', environment: 'production')
66 67
          end

68 69 70
          it 'returns the success message' do
            expect(subject[:response_type]).to be(:in_channel)
            expect(subject[:text]).to start_with('Deployment started from staging to production')
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
          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