2015-08-25 21:42:46 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2016-11-23 15:14:08 -05:00
|
|
|
describe Ci::API::Triggers do
|
2015-08-25 21:42:46 -04:00
|
|
|
describe 'POST /projects/:project_id/refs/:ref/trigger' do
|
|
|
|
let!(:trigger_token) { 'secure token' }
|
2017-01-26 18:51:57 -05:00
|
|
|
let!(:project) { create(:project, :repository, ci_id: 10) }
|
|
|
|
let!(:project2) { create(:empty_project, ci_id: 11) }
|
|
|
|
let!(:trigger) { create(:ci_trigger, project: project, token: trigger_token) }
|
2015-09-15 08:51:03 -04:00
|
|
|
let(:options) do
|
2015-08-25 21:42:46 -04:00
|
|
|
{
|
|
|
|
token: trigger_token
|
|
|
|
}
|
2015-09-15 08:51:03 -04:00
|
|
|
end
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2015-10-05 07:51:28 -04:00
|
|
|
before do
|
2016-06-03 10:22:26 -04:00
|
|
|
stub_ci_pipeline_to_return_yaml_file
|
2015-10-05 07:51:28 -04:00
|
|
|
end
|
|
|
|
|
2015-08-25 21:42:46 -04:00
|
|
|
context 'Handles errors' do
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'returns bad request if token is missing' do
|
2015-12-10 12:12:19 -05:00
|
|
|
post ci_api("/projects/#{project.ci_id}/refs/master/trigger")
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(400)
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'returns not found if project is not found' do
|
2015-09-15 07:45:59 -04:00
|
|
|
post ci_api('/projects/0/refs/master/trigger'), options
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(404)
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'returns unauthorized if token is for different project' do
|
2015-12-10 12:12:19 -05:00
|
|
|
post ci_api("/projects/#{project2.ci_id}/refs/master/trigger"), options
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(401)
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'Have a commit' do
|
2016-06-03 10:22:26 -04:00
|
|
|
let(:pipeline) { project.pipelines.last }
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'creates builds' do
|
2015-12-10 12:12:19 -05:00
|
|
|
post ci_api("/projects/#{project.ci_id}/refs/master/trigger"), options
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(201)
|
2016-06-03 10:22:26 -04:00
|
|
|
pipeline.builds.reload
|
2016-08-11 09:22:35 -04:00
|
|
|
expect(pipeline.builds.pending.size).to eq(2)
|
|
|
|
expect(pipeline.builds.size).to eq(5)
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'returns bad request with no builds created if there\'s no commit for that ref' do
|
2015-12-10 12:12:19 -05:00
|
|
|
post ci_api("/projects/#{project.ci_id}/refs/other-branch/trigger"), options
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(400)
|
2015-09-10 10:04:06 -04:00
|
|
|
expect(json_response['message']).to eq('No builds created')
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'Validates variables' do
|
2015-09-14 07:37:18 -04:00
|
|
|
let(:variables) do
|
|
|
|
{ 'TRIGGER_KEY' => 'TRIGGER_VALUE' }
|
|
|
|
end
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'validates variables to be a hash' do
|
2015-12-10 12:12:19 -05:00
|
|
|
post ci_api("/projects/#{project.ci_id}/refs/master/trigger"), options.merge(variables: 'value')
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(400)
|
2017-02-22 04:40:58 -05:00
|
|
|
|
|
|
|
expect(json_response['error']).to eq('variables is invalid')
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'validates variables needs to be a map of key-valued strings' do
|
2015-12-10 12:12:19 -05:00
|
|
|
post ci_api("/projects/#{project.ci_id}/refs/master/trigger"), options.merge(variables: { key: %w(1 2) })
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(400)
|
2015-09-10 10:04:06 -04:00
|
|
|
expect(json_response['message']).to eq('variables needs to be a map of key-valued strings')
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'creates trigger request with variables' do
|
2015-12-10 12:12:19 -05:00
|
|
|
post ci_api("/projects/#{project.ci_id}/refs/master/trigger"), options.merge(variables: variables)
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(201)
|
2016-06-03 10:22:26 -04:00
|
|
|
pipeline.builds.reload
|
|
|
|
expect(pipeline.builds.first.trigger_request.variables).to eq(variables)
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|