gitlab-org--gitlab-foss/spec/requests/api/pipeline_schedules_spec.rb

468 lines
16 KiB
Ruby
Raw Normal View History

2017-05-11 19:12:04 +00:00
require 'spec_helper'
describe API::PipelineSchedules do
set(:developer) { create(:user) }
set(:user) { create(:user) }
2017-09-04 12:53:19 +00:00
set(:project) { create(:project, :repository, public_builds: false) }
2017-05-11 19:12:04 +00:00
before do
project.add_developer(developer)
end
describe 'GET /projects/:id/pipeline_schedules' do
context 'authenticated user with valid permissions' do
2017-05-17 12:16:54 +00:00
let(:pipeline_schedule) { create(:ci_pipeline_schedule, project: project, owner: developer) }
2017-05-11 19:12:04 +00:00
before do
2017-05-17 12:16:54 +00:00
pipeline_schedule.pipelines << build(:ci_pipeline, project: project)
2017-05-11 19:12:04 +00:00
end
def create_pipeline_schedules(count)
create_list(:ci_pipeline_schedule, count, project: project)
.each do |pipeline_schedule|
create(:user).tap do |user|
project.add_developer(user)
2018-07-02 10:43:06 +00:00
pipeline_schedule.update(owner: user)
end
pipeline_schedule.pipelines << build(:ci_pipeline, project: project)
end
end
2017-05-11 19:12:04 +00:00
it 'returns list of pipeline_schedules' do
get api("/projects/#{project.id}/pipeline_schedules", developer)
expect(response).to have_gitlab_http_status(:ok)
2017-05-11 19:12:04 +00:00
expect(response).to include_pagination_headers
expect(response).to match_response_schema('pipeline_schedules')
end
2017-05-16 16:33:09 +00:00
it 'avoids N + 1 queries' do
# We need at least two users to trigger a preload for that relation.
create_pipeline_schedules(1)
2017-05-16 16:33:09 +00:00
control_count = ActiveRecord::QueryRecorder.new do
get api("/projects/#{project.id}/pipeline_schedules", developer)
end.count
create_pipeline_schedules(10)
2017-05-16 16:33:09 +00:00
expect do
get api("/projects/#{project.id}/pipeline_schedules", developer)
end.not_to exceed_query_limit(control_count)
end
%w[active inactive].each do |target|
context "when scope is #{target}" do
before do
create(:ci_pipeline_schedule, project: project, active: active?(target))
end
it 'returns matched pipeline schedules' do
get api("/projects/#{project.id}/pipeline_schedules", developer), scope: target
expect(json_response.map { |r| r['active'] }).to all(eq(active?(target)))
end
end
def active?(str)
(str == 'active') ? true : false
end
end
2017-05-11 19:12:04 +00:00
end
context 'authenticated user with invalid permissions' do
it 'does not return pipeline_schedules list' do
get api("/projects/#{project.id}/pipeline_schedules", user)
expect(response).to have_gitlab_http_status(:not_found)
2017-05-11 19:12:04 +00:00
end
end
context 'unauthenticated user' do
it 'does not return pipeline_schedules list' do
get api("/projects/#{project.id}/pipeline_schedules")
expect(response).to have_gitlab_http_status(:unauthorized)
2017-05-11 19:12:04 +00:00
end
end
end
describe 'GET /projects/:id/pipeline_schedules/:pipeline_schedule_id' do
2017-05-17 12:16:54 +00:00
let(:pipeline_schedule) { create(:ci_pipeline_schedule, project: project, owner: developer) }
before do
pipeline_schedule.pipelines << build(:ci_pipeline, project: project)
2017-05-11 19:12:04 +00:00
end
context 'authenticated user with valid permissions' do
it 'returns pipeline_schedule details' do
get api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}", developer)
expect(response).to have_gitlab_http_status(:ok)
2017-05-11 19:12:04 +00:00
expect(response).to match_response_schema('pipeline_schedule')
end
it 'responds with 404 Not Found if requesting non-existing pipeline_schedule' do
get api("/projects/#{project.id}/pipeline_schedules/-5", developer)
expect(response).to have_gitlab_http_status(:not_found)
2017-05-11 19:12:04 +00:00
end
end
context 'authenticated user with invalid permissions' do
it 'does not return pipeline_schedules list' do
get api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}", user)
expect(response).to have_gitlab_http_status(:not_found)
2017-05-11 19:12:04 +00:00
end
end
2017-09-04 12:53:19 +00:00
context 'authenticated user with insufficient permissions' do
before do
project.add_guest(user)
end
it 'does not return pipeline_schedules list' do
get api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}", user)
expect(response).to have_gitlab_http_status(:not_found)
2017-09-04 12:53:19 +00:00
end
end
2017-05-11 19:12:04 +00:00
context 'unauthenticated user' do
it 'does not return pipeline_schedules list' do
get api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}")
expect(response).to have_gitlab_http_status(:unauthorized)
2017-05-11 19:12:04 +00:00
end
end
end
describe 'POST /projects/:id/pipeline_schedules' do
2017-05-17 12:16:54 +00:00
let(:params) { attributes_for(:ci_pipeline_schedule) }
2017-05-11 19:12:04 +00:00
context 'authenticated user with valid permissions' do
context 'with required parameters' do
it 'creates pipeline_schedule' do
expect do
post api("/projects/#{project.id}/pipeline_schedules", developer),
2017-05-12 18:38:11 +00:00
params
2017-05-12 18:41:31 +00:00
end.to change { project.pipeline_schedules.count }.by(1)
2017-05-11 19:12:04 +00:00
expect(response).to have_gitlab_http_status(:created)
2017-05-11 19:12:04 +00:00
expect(response).to match_response_schema('pipeline_schedule')
2017-05-12 18:38:11 +00:00
expect(json_response['description']).to eq(params[:description])
expect(json_response['ref']).to eq(params[:ref])
expect(json_response['cron']).to eq(params[:cron])
expect(json_response['cron_timezone']).to eq(params[:cron_timezone])
2017-05-17 12:16:54 +00:00
expect(json_response['owner']['id']).to eq(developer.id)
2017-05-11 19:12:04 +00:00
end
end
context 'without required parameters' do
it 'does not create pipeline_schedule' do
post api("/projects/#{project.id}/pipeline_schedules", developer)
expect(response).to have_gitlab_http_status(:bad_request)
2017-05-11 19:12:04 +00:00
end
end
2017-05-12 17:10:14 +00:00
context 'when cron has validation error' do
it 'does not create pipeline_schedule' do
post api("/projects/#{project.id}/pipeline_schedules", developer),
2017-05-17 12:16:54 +00:00
params.merge('cron' => 'invalid-cron')
2017-05-12 17:10:14 +00:00
expect(response).to have_gitlab_http_status(:bad_request)
2017-05-12 17:10:14 +00:00
expect(json_response['message']).to have_key('cron')
end
end
2017-05-11 19:12:04 +00:00
end
context 'authenticated user with invalid permissions' do
it 'does not create pipeline_schedule' do
2017-05-12 18:38:11 +00:00
post api("/projects/#{project.id}/pipeline_schedules", user), params
2017-05-11 19:12:04 +00:00
expect(response).to have_gitlab_http_status(:not_found)
2017-05-11 19:12:04 +00:00
end
end
context 'unauthenticated user' do
it 'does not create pipeline_schedule' do
2017-05-12 18:38:11 +00:00
post api("/projects/#{project.id}/pipeline_schedules"), params
2017-05-11 19:12:04 +00:00
expect(response).to have_gitlab_http_status(:unauthorized)
2017-05-11 19:12:04 +00:00
end
end
end
describe 'PUT /projects/:id/pipeline_schedules/:pipeline_schedule_id' do
let(:pipeline_schedule) do
create(:ci_pipeline_schedule, project: project, owner: developer)
end
context 'authenticated user with valid permissions' do
it 'updates cron' do
put api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}", developer),
2017-05-12 18:38:11 +00:00
cron: '1 2 3 4 *'
2017-05-11 19:12:04 +00:00
expect(response).to have_gitlab_http_status(:ok)
2017-05-11 19:12:04 +00:00
expect(response).to match_response_schema('pipeline_schedule')
2017-05-12 18:38:11 +00:00
expect(json_response['cron']).to eq('1 2 3 4 *')
2017-05-11 19:12:04 +00:00
end
2017-05-12 17:10:14 +00:00
context 'when cron has validation error' do
it 'does not update pipeline_schedule' do
put api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}", developer),
cron: 'invalid-cron'
expect(response).to have_gitlab_http_status(:bad_request)
2017-05-12 17:10:14 +00:00
expect(json_response['message']).to have_key('cron')
end
end
2017-05-11 19:12:04 +00:00
end
context 'authenticated user with invalid permissions' do
it 'does not update pipeline_schedule' do
put api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}", user)
expect(response).to have_gitlab_http_status(:not_found)
2017-05-11 19:12:04 +00:00
end
end
context 'unauthenticated user' do
it 'does not update pipeline_schedule' do
put api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}")
expect(response).to have_gitlab_http_status(:unauthorized)
2017-05-11 19:12:04 +00:00
end
end
end
describe 'POST /projects/:id/pipeline_schedules/:pipeline_schedule_id/take_ownership' do
let(:pipeline_schedule) do
create(:ci_pipeline_schedule, project: project, owner: developer)
end
context 'authenticated user with valid permissions' do
it 'updates owner' do
2017-05-17 12:16:54 +00:00
post api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}/take_ownership", developer)
2017-05-11 19:12:04 +00:00
expect(response).to have_gitlab_http_status(:created)
2017-05-11 19:12:04 +00:00
expect(response).to match_response_schema('pipeline_schedule')
end
end
context 'authenticated user with invalid permissions' do
it 'does not update owner' do
post api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}/take_ownership", user)
expect(response).to have_gitlab_http_status(:not_found)
2017-05-11 19:12:04 +00:00
end
end
context 'unauthenticated user' do
it 'does not update owner' do
post api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}/take_ownership")
expect(response).to have_gitlab_http_status(:unauthorized)
2017-05-11 19:12:04 +00:00
end
end
end
describe 'DELETE /projects/:id/pipeline_schedules/:pipeline_schedule_id' do
let(:maintainer) { create(:user) }
2017-05-11 19:12:04 +00:00
let!(:pipeline_schedule) do
create(:ci_pipeline_schedule, project: project, owner: developer)
end
before do
project.add_maintainer(maintainer)
2017-05-11 19:12:04 +00:00
end
context 'authenticated user with valid permissions' do
it 'deletes pipeline_schedule' do
expect do
delete api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}", maintainer)
2017-05-17 12:16:54 +00:00
end.to change { project.pipeline_schedules.count }.by(-1)
2017-05-11 19:12:04 +00:00
expect(response).to have_gitlab_http_status(204)
2017-05-11 19:12:04 +00:00
end
it 'responds with 404 Not Found if requesting non-existing pipeline_schedule' do
delete api("/projects/#{project.id}/pipeline_schedules/-5", maintainer)
2017-05-11 19:12:04 +00:00
expect(response).to have_gitlab_http_status(:not_found)
2017-05-11 19:12:04 +00:00
end
2017-08-24 16:03:39 +00:00
it_behaves_like '412 response' do
let(:request) { api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}", maintainer) }
2017-08-24 16:03:39 +00:00
end
2017-05-11 19:12:04 +00:00
end
context 'authenticated user with invalid permissions' do
let!(:pipeline_schedule) { create(:ci_pipeline_schedule, project: project, owner: maintainer) }
2017-07-07 11:50:26 +00:00
2017-05-11 19:12:04 +00:00
it 'does not delete pipeline_schedule' do
delete api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}", developer)
expect(response).to have_gitlab_http_status(:forbidden)
2017-05-11 19:12:04 +00:00
end
end
context 'unauthenticated user' do
it 'does not delete pipeline_schedule' do
delete api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}")
expect(response).to have_gitlab_http_status(:unauthorized)
2017-05-11 19:12:04 +00:00
end
end
end
2017-08-21 17:21:37 +00:00
describe 'POST /projects/:id/pipeline_schedules/:pipeline_schedule_id/variables' do
let(:params) { attributes_for(:ci_pipeline_schedule_variable) }
2017-08-24 12:51:46 +00:00
set(:pipeline_schedule) do
2017-08-21 17:21:37 +00:00
create(:ci_pipeline_schedule, project: project, owner: developer)
end
context 'authenticated user with valid permissions' do
context 'with required parameters' do
it 'creates pipeline_schedule_variable' do
expect do
post api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}/variables", developer),
params
end.to change { pipeline_schedule.variables.count }.by(1)
expect(response).to have_gitlab_http_status(:created)
2017-08-21 17:21:37 +00:00
expect(response).to match_response_schema('pipeline_schedule_variable')
expect(json_response['key']).to eq(params[:key])
expect(json_response['value']).to eq(params[:value])
end
end
context 'without required parameters' do
it 'does not create pipeline_schedule_variable' do
post api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}/variables", developer)
expect(response).to have_gitlab_http_status(:bad_request)
2017-08-21 17:21:37 +00:00
end
end
2017-08-22 12:42:17 +00:00
context 'when key has validation error' do
2017-08-21 17:21:37 +00:00
it 'does not create pipeline_schedule_variable' do
post api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}/variables", developer),
params.merge('key' => '!?!?')
expect(response).to have_gitlab_http_status(:bad_request)
2017-08-21 17:21:37 +00:00
expect(json_response['message']).to have_key('key')
end
end
end
context 'authenticated user with invalid permissions' do
it 'does not create pipeline_schedule_variable' do
post api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}/variables", user), params
expect(response).to have_gitlab_http_status(:not_found)
2017-08-21 17:21:37 +00:00
end
end
context 'unauthenticated user' do
it 'does not create pipeline_schedule_variable' do
post api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}/variables"), params
expect(response).to have_gitlab_http_status(:unauthorized)
2017-08-21 17:21:37 +00:00
end
end
end
describe 'PUT /projects/:id/pipeline_schedules/:pipeline_schedule_id/variables/:key' do
2017-08-24 12:51:46 +00:00
set(:pipeline_schedule) do
2017-08-21 17:21:37 +00:00
create(:ci_pipeline_schedule, project: project, owner: developer)
end
let(:pipeline_schedule_variable) do
create(:ci_pipeline_schedule_variable, pipeline_schedule: pipeline_schedule)
end
context 'authenticated user with valid permissions' do
2017-08-22 12:42:17 +00:00
it 'updates pipeline_schedule_variable' do
2017-08-21 17:21:37 +00:00
put api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}/variables/#{pipeline_schedule_variable.key}", developer),
2017-08-22 12:42:17 +00:00
value: 'updated_value'
2017-08-21 17:21:37 +00:00
expect(response).to have_gitlab_http_status(:ok)
2017-08-21 17:21:37 +00:00
expect(response).to match_response_schema('pipeline_schedule_variable')
2017-08-22 12:42:17 +00:00
expect(json_response['value']).to eq('updated_value')
2017-08-21 17:21:37 +00:00
end
end
context 'authenticated user with invalid permissions' do
it 'does not update pipeline_schedule_variable' do
put api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}/variables/#{pipeline_schedule_variable.key}", user)
expect(response).to have_gitlab_http_status(:not_found)
2017-08-21 17:21:37 +00:00
end
end
context 'unauthenticated user' do
it 'does not update pipeline_schedule_variable' do
put api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}/variables/#{pipeline_schedule_variable.key}")
expect(response).to have_gitlab_http_status(:unauthorized)
2017-08-21 17:21:37 +00:00
end
end
end
describe 'DELETE /projects/:id/pipeline_schedules/:pipeline_schedule_id/variables/:key' do
let(:maintainer) { create(:user) }
2017-08-21 17:21:37 +00:00
2017-08-24 12:51:46 +00:00
set(:pipeline_schedule) do
2017-08-21 17:21:37 +00:00
create(:ci_pipeline_schedule, project: project, owner: developer)
end
2017-08-22 12:42:17 +00:00
let!(:pipeline_schedule_variable) do
2017-08-21 17:21:37 +00:00
create(:ci_pipeline_schedule_variable, pipeline_schedule: pipeline_schedule)
end
before do
project.add_maintainer(maintainer)
2017-08-21 17:21:37 +00:00
end
context 'authenticated user with valid permissions' do
it 'deletes pipeline_schedule_variable' do
expect do
delete api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}/variables/#{pipeline_schedule_variable.key}", maintainer)
2017-08-22 12:42:17 +00:00
end.to change { Ci::PipelineScheduleVariable.count }.by(-1)
2017-08-21 17:21:37 +00:00
expect(response).to have_gitlab_http_status(:accepted)
2017-08-21 17:21:37 +00:00
expect(response).to match_response_schema('pipeline_schedule_variable')
end
it 'responds with 404 Not Found if requesting non-existing pipeline_schedule_variable' do
delete api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}/variables/____", maintainer)
2017-08-21 17:21:37 +00:00
expect(response).to have_gitlab_http_status(:not_found)
2017-08-21 17:21:37 +00:00
end
end
2017-08-24 12:51:46 +00:00
context 'authenticated user with invalid permissions' do
let!(:pipeline_schedule) { create(:ci_pipeline_schedule, project: project, owner: maintainer) }
2017-08-21 17:21:37 +00:00
it 'does not delete pipeline_schedule_variable' do
delete api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}/variables/#{pipeline_schedule_variable.key}", developer)
expect(response).to have_gitlab_http_status(:forbidden)
2017-08-21 17:21:37 +00:00
end
end
context 'unauthenticated user' do
it 'does not delete pipeline_schedule_variable' do
delete api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}/variables/#{pipeline_schedule_variable.key}")
expect(response).to have_gitlab_http_status(:unauthorized)
2017-08-21 17:21:37 +00:00
end
end
end
2017-05-11 19:12:04 +00:00
end