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

334 lines
11 KiB
Ruby
Raw Normal View History

2015-12-23 02:04:40 +05:30
require 'spec_helper'
2017-08-17 22:00:37 +05:30
describe API::Triggers do
let(:user) { create(:user) }
let(:user2) { create(:user) }
let!(:trigger_token) { 'secure_token' }
let!(:trigger_token_2) { 'secure_token_2' }
2017-08-17 22:00:37 +05:30
let!(:project) { create(:project, :repository, creator: user) }
2016-06-02 11:05:42 +05:30
let!(:master) { create(:project_member, :master, user: user, project: project) }
let!(:developer) { create(:project_member, :developer, user: user2, project: project) }
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') }
2017-08-17 22:00:37 +05:30
describe 'POST /projects/:project_id/trigger/pipeline' do
2017-09-10 17:25:29 +05:30
let!(:project2) { create(:project, :repository) }
2015-12-23 02:04:40 +05:30
let(:options) do
{
token: trigger_token
}
end
before do
stub_ci_pipeline_to_return_yaml_file
2017-09-10 17:25:29 +05:30
trigger.update(owner: user)
2015-12-23 02:04:40 +05:30
end
context 'Handles errors' do
2016-09-13 17:45:13 +05:30
it 'returns bad request if token is missing' do
2017-08-17 22:00:37 +05:30
post api("/projects/#{project.id}/trigger/pipeline"), ref: 'master'
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(400)
2015-12-23 02:04:40 +05:30
end
2016-09-13 17:45:13 +05:30
it 'returns not found if project is not found' do
2017-08-17 22:00:37 +05:30
post api('/projects/0/trigger/pipeline'), options.merge(ref: 'master')
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(404)
2015-12-23 02:04:40 +05:30
end
end
context 'Have a commit' do
let(:pipeline) { project.pipelines.last }
2015-12-23 02:04:40 +05:30
2017-08-17 22:00:37 +05:30
it 'creates pipeline' do
post api("/projects/#{project.id}/trigger/pipeline"), options.merge(ref: 'master')
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(201)
2017-08-17 22:00:37 +05:30
expect(json_response).to include('id' => pipeline.id)
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-12-23 02:04:40 +05:30
end
2017-08-17 22:00:37 +05:30
it 'returns bad request with no pipeline created if there\'s no commit for that ref' do
post api("/projects/#{project.id}/trigger/pipeline"), options.merge(ref: 'other-branch')
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']).to eq('base' => ["Reference not found"])
2015-12-23 02:04:40 +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
2017-08-17 22:00:37 +05:30
post api("/projects/#{project.id}/trigger/pipeline"), options.merge(variables: 'value', ref: 'master')
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-12-23 02:04:40 +05:30
end
2016-09-13 17:45:13 +05:30
it 'validates variables needs to be a map of key-valued strings' do
2017-08-17 22:00:37 +05:30
post api("/projects/#{project.id}/trigger/pipeline"), options.merge(variables: { key: %w(1 2) }, ref: 'master')
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(400)
2015-12-23 02:04:40 +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
2017-08-17 22:00:37 +05:30
post api("/projects/#{project.id}/trigger/pipeline"), options.merge(variables: variables, ref: 'master')
expect(response).to have_http_status(201)
2017-09-10 17:25:29 +05:30
expect(pipeline.variables.map { |v| { v.key => v.value } }.last).to eq(variables)
end
end
context 'when legacy trigger' do
before do
trigger.update(owner: nil)
end
it 'creates pipeline' do
post api("/projects/#{project.id}/trigger/pipeline"), options.merge(ref: 'master')
expect(response).to have_http_status(201)
expect(json_response).to include('id' => pipeline.id)
pipeline.builds.reload
expect(pipeline.builds.pending.size).to eq(2)
expect(pipeline.builds.size).to eq(5)
2017-08-17 22:00:37 +05:30
end
end
end
context 'when triggering a pipeline from a trigger token' do
2017-09-10 17:25:29 +05:30
it 'does not leak the presence of project when token is for different project' do
post api("/projects/#{project2.id}/ref/master/trigger/pipeline?token=#{trigger_token}"), { ref: 'refs/heads/other-branch' }
expect(response).to have_http_status(404)
end
2017-08-17 22:00:37 +05:30
it 'creates builds from the ref given in the URL, not in the body' do
expect do
post api("/projects/#{project.id}/ref/master/trigger/pipeline?token=#{trigger_token}"), { ref: 'refs/heads/other-branch' }
end.to change(project.builds, :count).by(5)
expect(response).to have_http_status(201)
end
context 'when ref contains a dot' do
it 'creates builds from the ref given in the URL, not in the body' do
project.repository.create_file(user, '.gitlab/gitlabhq/new_feature.md', 'something valid', message: 'new_feature', branch_name: 'v.1-branch')
expect do
post api("/projects/#{project.id}/ref/v.1-branch/trigger/pipeline?token=#{trigger_token}"), { ref: 'refs/heads/other-branch' }
end.to change(project.builds, :count).by(4)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(201)
2015-12-23 02:04:40 +05:30
end
end
end
end
describe 'GET /projects/:id/triggers' do
context 'authenticated user with valid permissions' do
2016-09-13 17:45:13 +05:30
it 'returns list of triggers' do
get api("/projects/#{project.id}/triggers", user)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(200)
2017-08-17 22:00:37 +05:30
expect(response).to include_pagination_headers
expect(json_response).to be_a(Array)
expect(json_response[0]).to have_key('token')
end
end
context 'authenticated user with invalid permissions' do
2016-09-13 17:45:13 +05:30
it 'does not return triggers list' do
get api("/projects/#{project.id}/triggers", user2)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(403)
end
end
context 'unauthenticated user' do
2016-09-13 17:45:13 +05:30
it 'does not return triggers list' do
get api("/projects/#{project.id}/triggers")
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(401)
end
end
end
2017-08-17 22:00:37 +05:30
describe 'GET /projects/:id/triggers/:trigger_id' do
context 'authenticated user with valid permissions' do
2016-09-13 17:45:13 +05:30
it 'returns trigger details' do
2017-08-17 22:00:37 +05:30
get api("/projects/#{project.id}/triggers/#{trigger.id}", user)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(200)
expect(json_response).to be_a(Hash)
end
2016-09-13 17:45:13 +05:30
it 'responds with 404 Not Found if requesting non-existing trigger' do
2017-08-17 22:00:37 +05:30
get api("/projects/#{project.id}/triggers/-5", user)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(404)
end
end
context 'authenticated user with invalid permissions' do
2016-09-13 17:45:13 +05:30
it 'does not return triggers list' do
2017-08-17 22:00:37 +05:30
get api("/projects/#{project.id}/triggers/#{trigger.id}", user2)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(403)
end
end
context 'unauthenticated user' do
2016-09-13 17:45:13 +05:30
it 'does not return triggers list' do
2017-08-17 22:00:37 +05:30
get api("/projects/#{project.id}/triggers/#{trigger.id}")
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(401)
end
end
end
describe 'POST /projects/:id/triggers' do
context 'authenticated user with valid permissions' do
2017-08-17 22:00:37 +05:30
context 'with required parameters' do
it 'creates trigger' do
expect do
post api("/projects/#{project.id}/triggers", user),
description: 'trigger'
end.to change{project.triggers.count}.by(1)
expect(response).to have_http_status(201)
expect(json_response).to include('description' => 'trigger')
end
end
context 'without required parameters' do
it 'does not create trigger' do
post api("/projects/#{project.id}/triggers", user)
2017-08-17 22:00:37 +05:30
expect(response).to have_http_status(:bad_request)
end
end
end
context 'authenticated user with invalid permissions' do
2016-09-13 17:45:13 +05:30
it 'does not create trigger' do
2017-08-17 22:00:37 +05:30
post api("/projects/#{project.id}/triggers", user2),
description: 'trigger'
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(403)
end
end
context 'unauthenticated user' do
2016-09-13 17:45:13 +05:30
it 'does not create trigger' do
2017-08-17 22:00:37 +05:30
post api("/projects/#{project.id}/triggers"),
description: 'trigger'
expect(response).to have_http_status(401)
end
end
end
describe 'PUT /projects/:id/triggers/:trigger_id' do
context 'authenticated user with valid permissions' do
let(:new_description) { 'new description' }
it 'updates description' do
put api("/projects/#{project.id}/triggers/#{trigger.id}", user),
description: new_description
expect(response).to have_http_status(200)
expect(json_response).to include('description' => new_description)
expect(trigger.reload.description).to eq(new_description)
end
end
context 'authenticated user with invalid permissions' do
it 'does not update trigger' do
put api("/projects/#{project.id}/triggers/#{trigger.id}", user2)
expect(response).to have_http_status(403)
end
end
context 'unauthenticated user' do
it 'does not update trigger' do
put api("/projects/#{project.id}/triggers/#{trigger.id}")
expect(response).to have_http_status(401)
end
end
end
describe 'POST /projects/:id/triggers/:trigger_id/take_ownership' do
context 'authenticated user with valid permissions' do
it 'updates owner' do
expect(trigger.owner).to be_nil
post api("/projects/#{project.id}/triggers/#{trigger.id}/take_ownership", user)
expect(response).to have_http_status(200)
expect(json_response).to include('owner')
expect(trigger.reload.owner).to eq(user)
end
end
context 'authenticated user with invalid permissions' do
it 'does not update owner' do
post api("/projects/#{project.id}/triggers/#{trigger.id}/take_ownership", user2)
expect(response).to have_http_status(403)
end
end
context 'unauthenticated user' do
it 'does not update owner' do
post api("/projects/#{project.id}/triggers/#{trigger.id}/take_ownership")
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(401)
end
end
end
2017-08-17 22:00:37 +05:30
describe 'DELETE /projects/:id/triggers/:trigger_id' do
context 'authenticated user with valid permissions' do
2016-09-13 17:45:13 +05:30
it 'deletes trigger' do
expect do
2017-08-17 22:00:37 +05:30
delete api("/projects/#{project.id}/triggers/#{trigger.id}", user)
expect(response).to have_http_status(204)
end.to change{project.triggers.count}.by(-1)
end
2016-09-13 17:45:13 +05:30
it 'responds with 404 Not Found if requesting non-existing trigger' do
2017-08-17 22:00:37 +05:30
delete api("/projects/#{project.id}/triggers/-5", user)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(404)
end
end
context 'authenticated user with invalid permissions' do
2016-09-13 17:45:13 +05:30
it 'does not delete trigger' do
2017-08-17 22:00:37 +05:30
delete api("/projects/#{project.id}/triggers/#{trigger.id}", user2)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(403)
end
end
context 'unauthenticated user' do
2016-09-13 17:45:13 +05:30
it 'does not delete trigger' do
2017-08-17 22:00:37 +05:30
delete api("/projects/#{project.id}/triggers/#{trigger.id}")
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(401)
end
end
end
2015-12-23 02:04:40 +05:30
end