2017-07-26 05:31:09 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-08-01 04:42:05 -04:00
|
|
|
describe Ci::PipelineTriggerService do
|
2017-07-26 05:31:09 -04:00
|
|
|
let(:project) { create(:project, :repository) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_ci_pipeline_to_return_yaml_file
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute' do
|
|
|
|
let(:user) { create(:user) }
|
2017-07-27 11:31:24 -04:00
|
|
|
let(:trigger) { create(:ci_trigger, project: project, owner: user) }
|
|
|
|
let(:result) { described_class.new(project, user, params).execute }
|
|
|
|
|
2017-07-26 05:31:09 -04:00
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
2017-07-28 05:46:04 -04:00
|
|
|
context 'when trigger belongs to a different project' do
|
|
|
|
let(:params) { { token: trigger.token, ref: 'master', variables: nil } }
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:trigger) { create(:ci_trigger, project: create(:project), owner: user) }
|
2017-07-28 05:46:04 -04:00
|
|
|
|
|
|
|
it 'does nothing' do
|
|
|
|
expect { result }.not_to change { Ci::Pipeline.count }
|
|
|
|
end
|
|
|
|
end
|
2017-07-26 05:31:09 -04:00
|
|
|
|
2017-07-28 05:46:04 -04:00
|
|
|
context 'when params have an existsed trigger token' do
|
2017-07-26 05:31:09 -04:00
|
|
|
context 'when params have an existsed ref' do
|
2017-07-28 05:46:04 -04:00
|
|
|
let(:params) { { token: trigger.token, ref: 'master', variables: nil } }
|
2017-07-26 05:31:09 -04:00
|
|
|
|
|
|
|
it 'triggers a pipeline' do
|
|
|
|
expect { result }.to change { Ci::Pipeline.count }.by(1)
|
2017-07-28 05:46:04 -04:00
|
|
|
expect(result[:pipeline].ref).to eq('master')
|
2017-07-26 05:31:09 -04:00
|
|
|
expect(result[:pipeline].project).to eq(project)
|
|
|
|
expect(result[:pipeline].user).to eq(trigger.owner)
|
|
|
|
expect(result[:status]).to eq(:success)
|
|
|
|
end
|
|
|
|
|
2017-07-28 05:46:04 -04:00
|
|
|
context 'when commit message has [ci skip]' do
|
|
|
|
before do
|
|
|
|
allow_any_instance_of(Ci::Pipeline).to receive(:git_commit_message) { '[ci skip]' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'ignores [ci skip] and create as general' do
|
|
|
|
expect { result }.to change { Ci::Pipeline.count }.by(1)
|
|
|
|
expect(result[:status]).to eq(:success)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-26 05:31:09 -04:00
|
|
|
context 'when params have a variable' do
|
2017-07-28 05:46:04 -04:00
|
|
|
let(:params) { { token: trigger.token, ref: 'master', variables: variables } }
|
2017-07-26 05:31:09 -04:00
|
|
|
let(:variables) { { 'AAA' => 'AAA123' } }
|
|
|
|
|
|
|
|
it 'has a variable' do
|
|
|
|
expect { result }.to change { Ci::PipelineVariable.count }.by(1)
|
2017-07-28 05:46:04 -04:00
|
|
|
.and change { Ci::TriggerRequest.count }.by(1)
|
|
|
|
expect(result[:pipeline].variables.map { |v| { v.key => v.value } }.first).to eq(variables)
|
|
|
|
expect(result[:pipeline].trigger_requests.last.variables).to be_nil
|
2017-07-26 05:31:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when params have a non-existsed ref' do
|
2017-07-28 05:46:04 -04:00
|
|
|
let(:params) { { token: trigger.token, ref: 'invalid-ref', variables: nil } }
|
2017-07-26 05:31:09 -04:00
|
|
|
|
|
|
|
it 'does not trigger a pipeline' do
|
|
|
|
expect { result }.not_to change { Ci::Pipeline.count }
|
|
|
|
expect(result[:http_status]).to eq(400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when params have a non-existsed trigger token' do
|
2017-07-28 05:46:04 -04:00
|
|
|
let(:params) { { token: 'invalid-token', ref: nil, variables: nil } }
|
2017-07-26 05:31:09 -04:00
|
|
|
|
|
|
|
it 'does not trigger a pipeline' do
|
|
|
|
expect { result }.not_to change { Ci::Pipeline.count }
|
|
|
|
expect(result).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|