2019-08-22 06:57:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-15 03:55:56 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe Gitlab::SlashCommands::Command do
|
2019-12-20 22:07:37 -05:00
|
|
|
let(:project) { create(:project, :repository) }
|
2016-11-17 09:30:04 -05:00
|
|
|
let(:user) { create(:user) }
|
2018-02-22 12:34:04 -05:00
|
|
|
let(:chat_name) { double(:chat_name, user: user) }
|
2016-11-15 03:55:56 -05:00
|
|
|
|
2016-11-17 06:57:27 -05:00
|
|
|
describe '#execute' do
|
2016-12-16 08:21:06 -05:00
|
|
|
subject do
|
2018-02-22 12:34:04 -05:00
|
|
|
described_class.new(project, chat_name, params).execute
|
2016-12-16 08:21:06 -05:00
|
|
|
end
|
2016-11-21 11:26:35 -05:00
|
|
|
|
2016-11-18 05:38:54 -05:00
|
|
|
context 'when no command is available' do
|
2016-11-17 09:30:04 -05:00
|
|
|
let(:params) { { text: 'issue show 1' } }
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project, has_external_issue_tracker: true) }
|
2016-11-15 03:55:56 -05:00
|
|
|
|
2016-11-18 07:29:47 -05:00
|
|
|
it 'displays 404 messages' do
|
2016-11-17 06:57:27 -05:00
|
|
|
expect(subject[:response_type]).to be(:ephemeral)
|
2016-11-17 09:30:04 -05:00
|
|
|
expect(subject[:text]).to start_with('404 not found')
|
2016-11-17 06:57:27 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when an unknown command is triggered' do
|
2016-11-18 07:29:47 -05:00
|
|
|
let(:params) { { command: '/gitlab', text: "unknown command 123" } }
|
2016-11-17 06:57:27 -05:00
|
|
|
|
|
|
|
it 'displays the help message' do
|
|
|
|
expect(subject[:response_type]).to be(:ephemeral)
|
2019-08-26 08:23:36 -04:00
|
|
|
expect(subject[:text]).to start_with('The specified command is not valid')
|
2016-11-18 07:29:47 -05:00
|
|
|
expect(subject[:text]).to match('/gitlab issue show')
|
2016-11-15 03:55:56 -05:00
|
|
|
end
|
|
|
|
end
|
2016-11-17 09:30:04 -05:00
|
|
|
|
2016-11-17 15:27:12 -05:00
|
|
|
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)
|
2019-08-26 08:23:36 -04:00
|
|
|
expect(subject[:text]).to start_with('You are not allowed')
|
2016-12-23 03:46:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-21 11:26:35 -05:00
|
|
|
context 'when trying to do deployment' do
|
|
|
|
let(:params) { { text: 'deploy staging to production' } }
|
2017-04-06 07:40:04 -04:00
|
|
|
let!(:build) { create(:ci_build, pipeline: pipeline) }
|
|
|
|
let!(:pipeline) { create(:ci_pipeline, project: project) }
|
2016-11-21 11:26:35 -05:00
|
|
|
let!(:staging) { create(:environment, name: 'staging', project: project) }
|
2018-11-04 19:37:40 -05:00
|
|
|
let!(:deployment) { create(:deployment, :success, environment: staging, deployable: build) }
|
2017-04-06 07:40:04 -04:00
|
|
|
|
2016-11-21 11:26:35 -05:00
|
|
|
let!(:manual) do
|
2017-04-06 07:40:04 -04:00
|
|
|
create(:ci_build, :manual, pipeline: pipeline,
|
|
|
|
name: 'first',
|
|
|
|
environment: 'production')
|
2016-11-21 11:26:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'and user can not create deployment' do
|
|
|
|
it 'returns action' do
|
|
|
|
expect(subject[:response_type]).to be(:ephemeral)
|
2019-08-26 08:23:36 -04:00
|
|
|
expect(subject[:text]).to start_with('You are not allowed')
|
2016-11-21 11:26:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-31 05:25:55 -04:00
|
|
|
context 'and user has deployment permission' do
|
2016-11-21 11:26:35 -05:00
|
|
|
before do
|
2017-05-31 05:25:55 -04:00
|
|
|
build.project.add_developer(user)
|
|
|
|
|
|
|
|
create(:protected_branch, :developers_can_merge,
|
|
|
|
name: build.ref, project: project)
|
2016-11-21 11:26:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns action' do
|
2017-01-10 13:43:58 -05:00
|
|
|
expect(subject[:text]).to include('Deployment started from staging to production')
|
2016-11-21 11:26:35 -05:00
|
|
|
expect(subject[:response_type]).to be(:in_channel)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when duplicate action exists' do
|
|
|
|
let!(:manual2) do
|
2017-04-06 07:40:04 -04:00
|
|
|
create(:ci_build, :manual, pipeline: pipeline,
|
|
|
|
name: 'second',
|
|
|
|
environment: 'production')
|
2016-11-21 11:26:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns error' do
|
|
|
|
expect(subject[:response_type]).to be(:ephemeral)
|
2017-07-28 06:16:07 -04:00
|
|
|
expect(subject[:text]).to include("Couldn't find a deployment manual action.")
|
2016-11-21 11:26:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-11-15 03:55:56 -05:00
|
|
|
end
|
2016-11-30 09:51:48 -05:00
|
|
|
|
|
|
|
describe '#match_command' do
|
2018-02-22 12:34:04 -05:00
|
|
|
subject { described_class.new(project, chat_name, params).match_command.first }
|
2016-11-30 09:51:48 -05:00
|
|
|
|
|
|
|
context 'IssueShow is triggered' do
|
|
|
|
let(:params) { { text: 'issue show 123' } }
|
|
|
|
|
2017-05-31 01:50:53 -04:00
|
|
|
it { is_expected.to eq(Gitlab::SlashCommands::IssueShow) }
|
2016-11-30 09:51:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'IssueCreate is triggered' do
|
|
|
|
let(:params) { { text: 'issue create my title' } }
|
|
|
|
|
2017-05-31 01:50:53 -04:00
|
|
|
it { is_expected.to eq(Gitlab::SlashCommands::IssueNew) }
|
2016-11-30 09:51:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'IssueSearch is triggered' do
|
|
|
|
let(:params) { { text: 'issue search my query' } }
|
|
|
|
|
2017-05-31 01:50:53 -04:00
|
|
|
it { is_expected.to eq(Gitlab::SlashCommands::IssueSearch) }
|
2016-11-30 09:51:48 -05:00
|
|
|
end
|
2018-03-12 11:29:45 -04:00
|
|
|
|
|
|
|
context 'IssueMove is triggered' do
|
|
|
|
let(:params) { { text: 'issue move #78291 to gitlab/gitlab-ci' } }
|
2019-12-12 07:07:33 -05:00
|
|
|
|
2018-03-12 11:29:45 -04:00
|
|
|
it { is_expected.to eq(Gitlab::SlashCommands::IssueMove) }
|
|
|
|
end
|
2019-11-06 04:06:23 -05:00
|
|
|
|
|
|
|
context 'IssueComment is triggered' do
|
|
|
|
let(:params) { { text: "issue comment #503\ncomment body" } }
|
2019-12-12 07:07:33 -05:00
|
|
|
|
2019-11-06 04:06:23 -05:00
|
|
|
it { is_expected.to eq(Gitlab::SlashCommands::IssueComment) }
|
|
|
|
end
|
2016-11-30 09:51:48 -05:00
|
|
|
end
|
2016-11-15 03:55:56 -05:00
|
|
|
end
|