2015-12-10 12:04:40 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2016-11-23 15:14:08 -05:00
|
|
|
describe API::Triggers do
|
2015-12-10 12:04:40 -05:00
|
|
|
include ApiHelpers
|
|
|
|
|
2016-01-04 10:38:32 -05:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:user2) { create(:user) }
|
2016-01-05 06:25:16 -05:00
|
|
|
let!(:trigger_token) { 'secure_token' }
|
|
|
|
let!(:trigger_token_2) { 'secure_token_2' }
|
2016-01-04 10:38:32 -05:00
|
|
|
let!(:project) { create(:project, creator_id: user.id) }
|
2016-03-06 16:53:22 -05:00
|
|
|
let!(:master) { create(:project_member, :master, user: user, project: project) }
|
|
|
|
let!(:developer) { create(:project_member, :developer, user: user2, project: project) }
|
2016-01-04 10:38:32 -05:00
|
|
|
let!(:trigger) { create(:ci_trigger, project: project, token: trigger_token) }
|
|
|
|
let!(:trigger2) { create(:ci_trigger, project: project, token: trigger_token_2) }
|
|
|
|
let!(:trigger_request) { create(:ci_trigger_request, trigger: trigger, created_at: '2015-01-01 12:13:14') }
|
|
|
|
|
2015-12-10 12:04:40 -05:00
|
|
|
describe 'POST /projects/:project_id/trigger' do
|
2016-01-04 10:38:32 -05:00
|
|
|
let!(:project2) { create(:empty_project) }
|
2015-12-10 12:04:40 -05:00
|
|
|
let(:options) do
|
|
|
|
{
|
|
|
|
token: trigger_token
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2016-06-03 10:22:26 -04:00
|
|
|
stub_ci_pipeline_to_return_yaml_file
|
2015-12-10 12:04:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
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:04:40 -05:00
|
|
|
post api("/projects/#{project.id}/trigger/builds"), ref: 'master'
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(400)
|
2015-12-10 12:04:40 -05:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'returns not found if project is not found' do
|
2015-12-10 12:12:19 -05:00
|
|
|
post api('/projects/0/trigger/builds'), options.merge(ref: 'master')
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(404)
|
2015-12-10 12:04:40 -05:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'returns unauthorized if token is for different project' do
|
2015-12-10 12:04:40 -05:00
|
|
|
post api("/projects/#{project2.id}/trigger/builds"), options.merge(ref: 'master')
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(401)
|
2015-12-10 12:04:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'Have a commit' do
|
2016-06-03 10:22:26 -04:00
|
|
|
let(:pipeline) { project.pipelines.last }
|
2015-12-10 12:04:40 -05:00
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'creates builds' do
|
2015-12-10 12:04:40 -05:00
|
|
|
post api("/projects/#{project.id}/trigger/builds"), options.merge(ref: 'master')
|
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-12-10 12:04:40 -05:00
|
|
|
end
|
|
|
|
|
2016-11-07 12:11:54 -05:00
|
|
|
it 'creates builds on webhook from other gitlab repository and branch' do
|
|
|
|
expect do
|
|
|
|
post api("/projects/#{project.id}/ref/master/trigger/builds?token=#{trigger_token}"), { ref: 'refs/heads/other-branch' }
|
|
|
|
end.to change(project.builds, :count).by(5)
|
|
|
|
expect(response).to have_http_status(201)
|
|
|
|
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:04:40 -05:00
|
|
|
post api("/projects/#{project.id}/trigger/builds"), options.merge(ref: 'other-branch')
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(400)
|
2015-12-10 12:04:40 -05:00
|
|
|
expect(json_response['message']).to eq('No builds created')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'Validates variables' do
|
|
|
|
let(:variables) do
|
|
|
|
{ 'TRIGGER_KEY' => 'TRIGGER_VALUE' }
|
|
|
|
end
|
|
|
|
|
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 api("/projects/#{project.id}/trigger/builds"), options.merge(variables: 'value', ref: 'master')
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(400)
|
2016-11-09 10:47:39 -05:00
|
|
|
expect(json_response['error']).to eq('variables is invalid')
|
2015-12-10 12:04:40 -05: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 api("/projects/#{project.id}/trigger/builds"), options.merge(variables: { key: %w(1 2) }, ref: 'master')
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(400)
|
2015-12-10 12:04:40 -05:00
|
|
|
expect(json_response['message']).to eq('variables needs to be a map of key-valued strings')
|
|
|
|
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 api("/projects/#{project.id}/trigger/builds"), options.merge(variables: variables, ref: 'master')
|
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-12-10 12:04:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-01-04 10:38:32 -05:00
|
|
|
|
2016-01-05 06:25:16 -05:00
|
|
|
describe 'GET /projects/:id/triggers' do
|
2016-01-04 10:38:32 -05:00
|
|
|
context 'authenticated user with valid permissions' do
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'returns list of triggers' do
|
2016-01-04 10:38:32 -05:00
|
|
|
get api("/projects/#{project.id}/triggers", user)
|
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(200)
|
2016-01-04 10:38:32 -05:00
|
|
|
expect(json_response).to be_a(Array)
|
2016-01-07 12:48:33 -05:00
|
|
|
expect(json_response[0]).to have_key('token')
|
2016-01-04 10:38:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'authenticated user with invalid permissions' do
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'does not return triggers list' do
|
2016-01-04 10:38:32 -05:00
|
|
|
get api("/projects/#{project.id}/triggers", user2)
|
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(403)
|
2016-01-04 10:38:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-13 06:16:27 -05:00
|
|
|
context 'unauthenticated user' do
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'does not return triggers list' do
|
2016-01-04 10:38:32 -05:00
|
|
|
get api("/projects/#{project.id}/triggers")
|
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(401)
|
2016-01-04 10:38:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-01-05 05:27:38 -05:00
|
|
|
|
2016-01-07 12:48:33 -05:00
|
|
|
describe 'GET /projects/:id/triggers/:token' do
|
2016-01-05 06:25:16 -05:00
|
|
|
context 'authenticated user with valid permissions' do
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'returns trigger details' do
|
2016-01-07 12:48:33 -05:00
|
|
|
get api("/projects/#{project.id}/triggers/#{trigger.token}", user)
|
2016-01-05 06:25:16 -05:00
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(200)
|
2016-01-07 12:48:33 -05:00
|
|
|
expect(json_response).to be_a(Hash)
|
2016-01-05 06:25:16 -05:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'responds with 404 Not Found if requesting non-existing trigger' do
|
2016-01-07 12:48:33 -05:00
|
|
|
get api("/projects/#{project.id}/triggers/abcdef012345", user)
|
2016-01-05 06:25:16 -05:00
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(404)
|
2016-01-05 06:25:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'authenticated user with invalid permissions' do
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'does not return triggers list' do
|
2016-01-07 12:48:33 -05:00
|
|
|
get api("/projects/#{project.id}/triggers/#{trigger.token}", user2)
|
2016-01-05 06:25:16 -05:00
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(403)
|
2016-01-05 06:25:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-13 06:16:27 -05:00
|
|
|
context 'unauthenticated user' do
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'does not return triggers list' do
|
2016-01-07 12:48:33 -05:00
|
|
|
get api("/projects/#{project.id}/triggers/#{trigger.token}")
|
2016-01-05 06:25:16 -05:00
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(401)
|
2016-01-05 06:25:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST /projects/:id/triggers' do
|
2016-01-05 05:44:10 -05:00
|
|
|
context 'authenticated user with valid permissions' do
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'creates trigger' do
|
2016-01-05 05:44:10 -05:00
|
|
|
expect do
|
|
|
|
post api("/projects/#{project.id}/triggers", user)
|
|
|
|
end.to change{project.triggers.count}.by(1)
|
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(201)
|
2016-01-05 05:44:10 -05:00
|
|
|
expect(json_response).to be_a(Hash)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'authenticated user with invalid permissions' do
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'does not create trigger' do
|
2016-01-05 05:44:10 -05:00
|
|
|
post api("/projects/#{project.id}/triggers", user2)
|
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(403)
|
2016-01-05 05:44:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-13 06:16:27 -05:00
|
|
|
context 'unauthenticated user' do
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'does not create trigger' do
|
2016-01-05 05:44:10 -05:00
|
|
|
post api("/projects/#{project.id}/triggers")
|
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(401)
|
2016-01-05 05:44:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-07 12:48:33 -05:00
|
|
|
describe 'DELETE /projects/:id/triggers/:token' do
|
2016-01-05 05:27:38 -05:00
|
|
|
context 'authenticated user with valid permissions' do
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'deletes trigger' do
|
2016-01-05 05:27:38 -05:00
|
|
|
expect do
|
2016-01-07 12:48:33 -05:00
|
|
|
delete api("/projects/#{project.id}/triggers/#{trigger.token}", user)
|
2016-01-05 05:27:38 -05:00
|
|
|
end.to change{project.triggers.count}.by(-1)
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(200)
|
2016-01-05 05:27:38 -05:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'responds with 404 Not Found if requesting non-existing trigger' do
|
2016-01-07 12:48:33 -05:00
|
|
|
delete api("/projects/#{project.id}/triggers/abcdef012345", user)
|
2016-01-05 05:27:38 -05:00
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(404)
|
2016-01-05 05:27:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'authenticated user with invalid permissions' do
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'does not delete trigger' do
|
2016-01-07 12:48:33 -05:00
|
|
|
delete api("/projects/#{project.id}/triggers/#{trigger.token}", user2)
|
2016-01-05 05:27:38 -05:00
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(403)
|
2016-01-05 05:27:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-13 06:16:27 -05:00
|
|
|
context 'unauthenticated user' do
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'does not delete trigger' do
|
2016-01-07 12:48:33 -05:00
|
|
|
delete api("/projects/#{project.id}/triggers/#{trigger.token}")
|
2016-01-05 05:27:38 -05:00
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(401)
|
2016-01-05 05:27:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-12-10 12:04:40 -05:00
|
|
|
end
|