BigW Consortium Gitlab

command_spec.rb 3.52 KB
Newer Older
1 2
require 'spec_helper'

3
describe Gitlab::ChatCommands::Command, service: true do
4
  let(:project) { create(:empty_project) }
5
  let(:user) { create(:user) }
6

7
  describe '#execute' do
8
    subject do
9
      described_class.new(project, user, params).execute
10
    end
11

12
    context 'when no command is available' do
13
      let(:params) { { text: 'issue show 1' } }
14
      let(:project) { create(:empty_project, has_external_issue_tracker: true) }
15

Z.J. van de Weg committed
16
      it 'displays 404 messages' do
17
        expect(subject[:response_type]).to be(:ephemeral)
18
        expect(subject[:text]).to start_with('404 not found')
19 20 21 22
      end
    end

    context 'when an unknown command is triggered' do
Z.J. van de Weg committed
23
      let(:params) { { command: '/gitlab', text: "unknown command 123" } }
24 25 26

      it 'displays the help message' do
        expect(subject[:response_type]).to be(:ephemeral)
Z.J. van de Weg committed
27
        expect(subject[:text]).to start_with('Unknown command')
Z.J. van de Weg committed
28
        expect(subject[:text]).to match('/gitlab issue show')
29 30
      end
    end
31

32 33 34 35 36
    context 'the user can not create an issue' do
      let(:params) { { text: "issue create my new issue" } }

      it 'rejects the actions' do
        expect(subject[:response_type]).to be(:ephemeral)
37
        expect(subject[:text]).to start_with('Whoops! This action is not allowed')
38 39 40
      end
    end

41 42
    context 'when trying to do deployment' do
      let(:params) { { text: 'deploy staging to production' } }
43 44
      let!(:build) { create(:ci_build, pipeline: pipeline) }
      let!(:pipeline) { create(:ci_pipeline, project: project) }
45 46
      let!(:staging) { create(:environment, name: 'staging', project: project) }
      let!(:deployment) { create(:deployment, environment: staging, deployable: build) }
47

48
      let!(:manual) do
49 50 51
        create(:ci_build, :manual, pipeline: pipeline,
                                   name: 'first',
                                   environment: 'production')
52 53 54 55 56
      end

      context 'and user can not create deployment' do
        it 'returns action' do
          expect(subject[:response_type]).to be(:ephemeral)
57
          expect(subject[:text]).to start_with('Whoops! This action is not allowed')
58 59 60 61 62
        end
      end

      context 'and user does have deployment permission' do
        before do
63
          build.project.add_master(user)
64 65 66
        end

        it 'returns action' do
67
          expect(subject[:text]).to include('Deployment started from staging to production')
68 69 70 71 72
          expect(subject[:response_type]).to be(:in_channel)
        end

        context 'when duplicate action exists' do
          let!(:manual2) do
73 74 75
            create(:ci_build, :manual, pipeline: pipeline,
                                       name: 'second',
                                       environment: 'production')
76 77 78 79 80 81 82 83 84
          end

          it 'returns error' do
            expect(subject[:response_type]).to be(:ephemeral)
            expect(subject[:text]).to include('Too many actions defined')
          end
        end
      end
    end
85
  end
86 87 88 89 90 91 92 93 94 95 96 97 98

  describe '#match_command' do
    subject { described_class.new(project, user, params).match_command.first }

    context 'IssueShow is triggered' do
      let(:params) { { text: 'issue show 123' } }

      it { is_expected.to eq(Gitlab::ChatCommands::IssueShow) }
    end

    context 'IssueCreate is triggered' do
      let(:params) { { text: 'issue create my title' } }

99
      it { is_expected.to eq(Gitlab::ChatCommands::IssueNew) }
100 101 102 103 104 105 106 107
    end

    context 'IssueSearch is triggered' do
      let(:params) { { text: 'issue search my query' } }

      it { is_expected.to eq(Gitlab::ChatCommands::IssueSearch) }
    end
  end
108
end