gitlab-org--gitlab-foss/spec/lib/gitlab/chat_commands/command_spec.rb

119 lines
3.7 KiB
Ruby
Raw Normal View History

require 'spec_helper'
describe Gitlab::ChatCommands::Command, service: true do
2016-11-18 09:00:40 +00:00
let(:project) { create(:empty_project) }
2016-11-17 14:30:04 +00:00
let(:user) { create(:user) }
2016-11-17 11:57:27 +00:00
describe '#execute' do
2016-12-16 13:21:06 +00:00
subject do
described_class.new(project, user, params).execute
2016-12-16 13:21:06 +00:00
end
2016-11-18 10:38:54 +00:00
context 'when no command is available' do
2016-11-17 14:30:04 +00:00
let(:params) { { text: 'issue show 1' } }
2016-11-17 11:57:27 +00:00
let(:project) { create(:project, has_external_issue_tracker: true) }
2016-11-18 12:29:47 +00:00
it 'displays 404 messages' do
2016-11-17 11:57:27 +00:00
expect(subject[:response_type]).to be(:ephemeral)
2016-11-17 14:30:04 +00:00
expect(subject[:text]).to start_with('404 not found')
2016-11-17 11:57:27 +00:00
end
end
context 'when an unknown command is triggered' do
2016-11-18 12:29:47 +00:00
let(:params) { { command: '/gitlab', text: "unknown command 123" } }
2016-11-17 11:57:27 +00:00
it 'displays the help message' do
expect(subject[:response_type]).to be(:ephemeral)
expect(subject[:text]).to start_with('Available commands')
2016-11-18 12:29:47 +00:00
expect(subject[:text]).to match('/gitlab issue show')
end
end
2016-11-17 14:30:04 +00: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)
expect(subject[:text]).to start_with('Whoops! That action is not allowed')
end
end
2016-11-18 12:29:47 +00:00
context 'issue is successfully created' do
2016-11-17 14:30:04 +00:00
let(:params) { { text: "issue create my new issue" } }
before do
project.team << [user, :master]
end
it 'presents the issue' do
expect(subject[:text]).to match("my new issue")
end
2016-11-18 10:38:54 +00:00
it 'shows a link to the new issue' do
expect(subject[:text]).to match(/\/issues\/\d+/)
2016-11-18 10:38:54 +00:00
end
2016-11-17 14:30:04 +00:00
end
context 'when trying to do deployment' do
let(:params) { { text: 'deploy staging to production' } }
let!(:build) { create(:ci_build, project: project) }
let!(:staging) { create(:environment, name: 'staging', project: project) }
let!(:deployment) { create(:deployment, environment: staging, deployable: build) }
let!(:manual) do
create(:ci_build, :manual, project: project, pipeline: build.pipeline, name: 'first', environment: 'production')
end
context 'and user can not create deployment' do
it 'returns action' do
expect(subject[:response_type]).to be(:ephemeral)
2016-12-16 13:21:06 +00:00
expect(subject[:text]).to start_with('Whoops! That action is not allowed')
end
end
context 'and user does have deployment permission' do
before do
project.team << [user, :developer]
end
it 'returns action' do
2016-12-16 13:21:06 +00:00
expect(subject[:text]).to include('Deployment from staging to production started.')
expect(subject[:response_type]).to be(:in_channel)
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[:response_type]).to be(:ephemeral)
expect(subject[:text]).to include('Too many actions defined')
end
end
end
end
end
2016-11-30 14:51:48 +00:00
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' } }
it { is_expected.to eq(Gitlab::ChatCommands::IssueCreate) }
end
context 'IssueSearch is triggered' do
let(:params) { { text: 'issue search my query' } }
it { is_expected.to eq(Gitlab::ChatCommands::IssueSearch) }
end
end
end