debian-mirror-gitlab/spec/requests/ci/api/triggers_spec.rb

81 lines
2.9 KiB
Ruby
Raw Normal View History

2015-09-25 12:07:36 +05:30
require 'spec_helper'
describe Ci::API::API do
include ApiHelpers
describe 'POST /projects/:project_id/refs/:ref/trigger' do
let!(:trigger_token) { 'secure token' }
2015-12-23 02:04:40 +05:30
let!(:project) { FactoryGirl.create(:project, ci_id: 10) }
let!(:project2) { FactoryGirl.create(:empty_project, ci_id: 11) }
2015-09-25 12:07:36 +05:30
let!(:trigger) { FactoryGirl.create(:ci_trigger, project: project, token: trigger_token) }
let(:options) do
{
token: trigger_token
}
end
2015-10-24 18:46:33 +05:30
before do
stub_ci_commit_to_return_yaml_file
end
2015-09-25 12:07:36 +05:30
context 'Handles errors' do
it 'should return 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")
2015-09-25 12:07:36 +05:30
expect(response.status).to eq(400)
end
it 'should return not found if project is not found' do
post ci_api('/projects/0/refs/master/trigger'), options
expect(response.status).to eq(404)
end
it 'should return 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
2015-09-25 12:07:36 +05:30
expect(response.status).to eq(401)
end
end
context 'Have a commit' do
2015-12-23 02:04:40 +05:30
let(:commit) { project.ci_commits.last }
2015-09-25 12:07:36 +05:30
it 'should create builds' do
2015-12-23 02:04:40 +05:30
post ci_api("/projects/#{project.ci_id}/refs/master/trigger"), options
2015-09-25 12:07:36 +05:30
expect(response.status).to eq(201)
2015-10-24 18:46:33 +05:30
commit.builds.reload
expect(commit.builds.size).to eq(2)
2015-09-25 12:07:36 +05:30
end
it 'should return 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
2015-09-25 12:07:36 +05:30
expect(response.status).to eq(400)
expect(json_response['message']).to eq('No builds created')
end
context 'Validates variables' do
let(:variables) do
{ 'TRIGGER_KEY' => 'TRIGGER_VALUE' }
end
it 'should validate 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')
2015-09-25 12:07:36 +05:30
expect(response.status).to eq(400)
expect(json_response['message']).to eq('variables needs to be a hash')
end
it 'should validate 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) })
2015-09-25 12:07:36 +05:30
expect(response.status).to eq(400)
expect(json_response['message']).to eq('variables needs to be a map of key-valued strings')
end
it 'create 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)
2015-09-25 12:07:36 +05:30
expect(response.status).to eq(201)
2015-10-24 18:46:33 +05:30
commit.builds.reload
expect(commit.builds.first.trigger_request.variables).to eq(variables)
2015-09-25 12:07:36 +05:30
end
end
end
end
end