deploy_spec.rb 2.14 KB
Newer Older
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
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
      project.team << [user, :master]
    end

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

    context 'if no environment is defined' do
      it 'returns nil' do
        expect(subject).to be_nil
      end
    end

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

      context 'without actions' do
        it 'returns nil' do
          expect(subject).to be_nil
        end
      end

      context 'with action' do
        let!(:manual1) do
          create(:ci_build, :manual, project: project, pipeline: build.pipeline, name: 'first', environment: 'production')
        end

        it 'returns action' do
          expect(subject).to eq(manual1)
        end

        context 'when duplicate action exists' do
          let!(:manual2) do
            create(:ci_build, :manual, project: project, pipeline: build.pipeline, name: 'second', environment: 'production')
          end

          it 'returns error' do
            expect(subject.message).to eq('Too many actions defined')
          end
        end

        context 'when teardown action exists' do
          let!(:teardown) do
55 56
            create(:ci_build, :manual, :teardown_environment,
                   project: project, pipeline: build.pipeline,
57 58 59 60
                   name: 'teardown', environment: 'production')
          end

          it 'returns error' do
61
            expect(subject).to eq(manual1)
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
          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