debian-mirror-gitlab/spec/lib/gitlab/slash_commands/run_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

145 lines
4.2 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::SlashCommands::Run do
2021-04-29 21:17:54 +05:30
describe '.match' do
it 'returns true for a run command' do
expect(described_class.match('run foo')).to be_an_instance_of(MatchData)
end
it 'returns true for a run command with arguments' do
expect(described_class.match('run foo bar baz'))
.to be_an_instance_of(MatchData)
end
it 'returns true for a command containing newlines' do
expect(described_class.match("run foo\nbar\nbaz"))
.to be_an_instance_of(MatchData)
end
it 'returns false for an unrelated command' do
expect(described_class.match('foo bar')).to be_nil
end
end
2019-07-07 11:18:12 +05:30
describe '.available?' do
it 'returns true when builds are enabled for the project' do
project = double(:project, builds_enabled?: true)
allow(Gitlab::Chat)
.to receive(:available?)
.and_return(true)
expect(described_class.available?(project)).to eq(true)
end
it 'returns false when builds are disabled for the project' do
project = double(:project, builds_enabled?: false)
expect(described_class.available?(project)).to eq(false)
end
it 'returns false when chatops is not available' do
allow(Gitlab::Chat)
.to receive(:available?)
.and_return(false)
project = double(:project, builds_enabled?: true)
expect(described_class.available?(project)).to eq(false)
end
end
describe '.allowed?' do
it 'returns true when the user can create a pipeline' do
project = create(:project)
expect(described_class.allowed?(project, project.creator)).to eq(true)
end
it 'returns false when the user can not create a pipeline' do
project = create(:project)
user = create(:user)
expect(described_class.allowed?(project, user)).to eq(false)
end
end
describe '#execute' do
let(:chat_name) { create(:chat_name) }
let(:project) { create(:project) }
let(:command) do
described_class.new(project, chat_name, response_url: 'http://example.com')
end
context 'when a pipeline could not be scheduled' do
it 'returns an error' do
2020-01-01 13:55:28 +05:30
expect_next_instance_of(Gitlab::Chat::Command) do |instance|
expect(instance).to receive(:try_create_pipeline).and_return(nil)
end
2019-07-07 11:18:12 +05:30
2020-01-01 13:55:28 +05:30
expect_next_instance_of(Gitlab::SlashCommands::Presenters::Run) do |instance|
expect(instance).to receive(:failed_to_schedule).with('foo')
end
2019-07-07 11:18:12 +05:30
command.execute(command: 'foo', arguments: '')
end
end
context 'when a pipeline could be created but the chat service was not supported' do
it 'returns an error' do
build = double(:build)
pipeline = double(
:pipeline,
builds: double(:relation, take: build),
persisted?: true
)
2020-01-01 13:55:28 +05:30
expect_next_instance_of(Gitlab::Chat::Command) do |instance|
expect(instance).to receive(:try_create_pipeline).and_return(pipeline)
end
2019-07-07 11:18:12 +05:30
expect(Gitlab::Chat::Responder)
.to receive(:responder_for)
.with(build)
.and_return(nil)
2020-01-01 13:55:28 +05:30
expect_next_instance_of(Gitlab::SlashCommands::Presenters::Run) do |instance|
expect(instance).to receive(:unsupported_chat_service)
end
2019-07-07 11:18:12 +05:30
command.execute(command: 'foo', arguments: '')
end
end
context 'using a valid pipeline' do
it 'schedules the pipeline' do
responder = double(:responder, scheduled_output: 'hello')
build = double(:build)
pipeline = double(
:pipeline,
builds: double(:relation, take: build),
persisted?: true
)
2020-01-01 13:55:28 +05:30
expect_next_instance_of(Gitlab::Chat::Command) do |instance|
expect(instance).to receive(:try_create_pipeline).and_return(pipeline)
end
2019-07-07 11:18:12 +05:30
expect(Gitlab::Chat::Responder)
.to receive(:responder_for)
.with(build)
.and_return(responder)
2020-01-01 13:55:28 +05:30
expect_next_instance_of(Gitlab::SlashCommands::Presenters::Run) do |instance|
expect(instance).to receive(:in_channel_response).with(responder.scheduled_output)
end
2019-07-07 11:18:12 +05:30
command.execute(command: 'foo', arguments: '')
end
end
end
end