gitlab-org--gitlab-foss/spec/requests/ci/api/triggers_spec.rb

80 lines
2.9 KiB
Ruby
Raw Normal View History

2015-08-25 21:42:46 -04:00
require 'spec_helper'
2015-09-09 08:17:16 -04:00
describe Ci::API::API do
2015-08-25 21:42:46 -04:00
include ApiHelpers
describe 'POST /projects/:project_id/refs/:ref/trigger' do
let!(:trigger_token) { 'secure token' }
2015-09-10 10:04:06 -04:00
let!(:project) { FactoryGirl.create(:ci_project) }
2015-09-28 07:14:34 -04:00
let!(:gl_project) { FactoryGirl.create(:empty_project, gitlab_ci_project: project) }
2015-09-10 10:04:06 -04:00
let!(:project2) { FactoryGirl.create(:ci_project) }
let!(:trigger) { FactoryGirl.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
context 'Handles errors' do
it 'should return bad request if token is missing' do
2015-09-15 07:45:59 -04:00
post ci_api("/projects/#{project.id}/refs/master/trigger")
2015-09-10 10:04:06 -04:00
expect(response.status).to eq(400)
2015-08-25 21:42:46 -04:00
end
it 'should return 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
2015-09-10 10:04:06 -04:00
expect(response.status).to eq(404)
2015-08-25 21:42:46 -04:00
end
it 'should return unauthorized if token is for different project' do
2015-09-15 07:45:59 -04:00
post ci_api("/projects/#{project2.id}/refs/master/trigger"), options
2015-09-10 10:04:06 -04:00
expect(response.status).to eq(401)
2015-08-25 21:42:46 -04:00
end
end
context 'Have a commit' do
before do
2015-09-28 07:14:34 -04:00
@commit = FactoryGirl.create(:ci_commit, gl_project: gl_project)
2015-08-25 21:42:46 -04:00
end
it 'should create builds' do
2015-09-15 07:45:59 -04:00
post ci_api("/projects/#{project.id}/refs/master/trigger"), options
2015-09-10 10:04:06 -04:00
expect(response.status).to eq(201)
2015-08-25 21:42:46 -04:00
@commit.builds.reload
2015-09-10 10:04:06 -04:00
expect(@commit.builds.size).to eq(2)
2015-08-25 21:42:46 -04:00
end
it 'should return bad request with no builds created if there\'s no commit for that ref' do
2015-09-15 07:45:59 -04:00
post ci_api("/projects/#{project.id}/refs/other-branch/trigger"), options
2015-09-10 10:04:06 -04:00
expect(response.status).to eq(400)
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
it 'should validate variables to be a hash' do
2015-09-15 07:45:59 -04:00
post ci_api("/projects/#{project.id}/refs/master/trigger"), options.merge(variables: 'value')
2015-09-10 10:04:06 -04:00
expect(response.status).to eq(400)
expect(json_response['message']).to eq('variables needs to be a hash')
2015-08-25 21:42:46 -04:00
end
it 'should validate variables needs to be a map of key-valued strings' do
2015-09-15 07:45:59 -04:00
post ci_api("/projects/#{project.id}/refs/master/trigger"), options.merge(variables: { key: %w(1 2) })
2015-09-10 10:04:06 -04:00
expect(response.status).to eq(400)
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
it 'create trigger request with variables' do
2015-09-15 07:45:59 -04:00
post ci_api("/projects/#{project.id}/refs/master/trigger"), options.merge(variables: variables)
2015-09-10 10:04:06 -04:00
expect(response.status).to eq(201)
2015-08-25 21:42:46 -04:00
@commit.builds.reload
2015-09-10 10:04:06 -04:00
expect(@commit.builds.first.trigger_request.variables).to eq(variables)
2015-08-25 21:42:46 -04:00
end
end
end
end
end