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