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

298 lines
10 KiB
Ruby
Raw Normal View History

2017-05-11 15:12:04 -04:00
require 'spec_helper'
describe API::PipelineSchedules do
set(:developer) { create(:user) }
set(:user) { create(:user) }
set(:project) { create(:project) }
2017-05-11 15:12:04 -04: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 08:16:54 -04:00
let(:pipeline_schedule) { create(:ci_pipeline_schedule, project: project, owner: developer) }
2017-05-11 15:12:04 -04:00
before do
2017-05-17 08:16:54 -04:00
pipeline_schedule.pipelines << build(:ci_pipeline, project: project)
2017-05-11 15:12:04 -04:00
end
it 'returns list of pipeline_schedules' do
get api("/projects/#{project.id}/pipeline_schedules", developer)
expect(response).to have_http_status(:ok)
expect(response).to include_pagination_headers
expect(response).to match_response_schema('pipeline_schedules')
end
2017-05-16 12:33:09 -04:00
it 'avoids N + 1 queries' do
control_count = ActiveRecord::QueryRecorder.new do
get api("/projects/#{project.id}/pipeline_schedules", developer)
end.count
create_list(:ci_pipeline_schedule, 10, project: project)
2017-05-17 09:58:23 -04:00
.each do |pipeline_schedule|
create(:user).tap do |user|
project.add_developer(user)
pipeline_schedule.update_attributes(owner: user)
end
2017-05-17 09:58:23 -04:00
pipeline_schedule.pipelines << build(:ci_pipeline, project: project)
end
2017-05-16 12:33:09 -04: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 15:12:04 -04: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_http_status(:not_found)
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_http_status(:unauthorized)
end
end
end
describe 'GET /projects/:id/pipeline_schedules/:pipeline_schedule_id' do
2017-05-17 08:16:54 -04: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 15:12:04 -04: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_http_status(:ok)
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_http_status(:not_found)
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_http_status(:not_found)
end
end
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_http_status(:unauthorized)
end
end
end
describe 'POST /projects/:id/pipeline_schedules' do
2017-05-17 08:16:54 -04:00
let(:params) { attributes_for(:ci_pipeline_schedule) }
2017-05-11 15:12:04 -04: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 14:38:11 -04:00
params
2017-05-12 14:41:31 -04:00
end.to change { project.pipeline_schedules.count }.by(1)
2017-05-11 15:12:04 -04:00
expect(response).to have_http_status(:created)
expect(response).to match_response_schema('pipeline_schedule')
2017-05-12 14:38:11 -04: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 08:16:54 -04:00
expect(json_response['owner']['id']).to eq(developer.id)
2017-05-11 15:12:04 -04: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_http_status(:bad_request)
end
end
2017-05-12 13:10:14 -04: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 08:16:54 -04:00
params.merge('cron' => 'invalid-cron')
2017-05-12 13:10:14 -04:00
expect(response).to have_http_status(:bad_request)
expect(json_response['message']).to have_key('cron')
end
end
2017-05-11 15:12:04 -04:00
end
context 'authenticated user with invalid permissions' do
it 'does not create pipeline_schedule' do
2017-05-12 14:38:11 -04:00
post api("/projects/#{project.id}/pipeline_schedules", user), params
2017-05-11 15:12:04 -04:00
expect(response).to have_http_status(:not_found)
end
end
context 'unauthenticated user' do
it 'does not create pipeline_schedule' do
2017-05-12 14:38:11 -04:00
post api("/projects/#{project.id}/pipeline_schedules"), params
2017-05-11 15:12:04 -04:00
expect(response).to have_http_status(:unauthorized)
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 14:38:11 -04:00
cron: '1 2 3 4 *'
2017-05-11 15:12:04 -04:00
expect(response).to have_http_status(:ok)
expect(response).to match_response_schema('pipeline_schedule')
2017-05-12 14:38:11 -04:00
expect(json_response['cron']).to eq('1 2 3 4 *')
2017-05-11 15:12:04 -04:00
end
2017-05-12 13:10:14 -04: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_http_status(:bad_request)
expect(json_response['message']).to have_key('cron')
end
end
2017-05-11 15:12:04 -04: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_http_status(:not_found)
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_http_status(:unauthorized)
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 08:16:54 -04:00
post api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}/take_ownership", developer)
2017-05-11 15:12:04 -04:00
2017-05-17 08:26:18 -04:00
expect(response).to have_http_status(:created)
2017-05-11 15:12:04 -04: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_http_status(:not_found)
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_http_status(:unauthorized)
end
end
end
describe 'DELETE /projects/:id/pipeline_schedules/:pipeline_schedule_id' do
let(:master) { create(:user) }
let!(:pipeline_schedule) do
create(:ci_pipeline_schedule, project: project, owner: developer)
end
before do
project.add_master(master)
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}", master)
2017-05-17 08:16:54 -04:00
end.to change { project.pipeline_schedules.count }.by(-1)
2017-05-11 15:12:04 -04:00
expect(response).to have_http_status(:accepted)
2017-05-11 15:12:04 -04:00
expect(response).to match_response_schema('pipeline_schedule')
end
it 'responds with 404 Not Found if requesting non-existing pipeline_schedule' do
delete api("/projects/#{project.id}/pipeline_schedules/-5", master)
expect(response).to have_http_status(:not_found)
end
end
context 'authenticated user with invalid permissions' do
it 'does not delete pipeline_schedule' do
delete api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}", developer)
2017-05-12 08:52:31 -04:00
expect(response).to have_http_status(:forbidden)
2017-05-11 15:12:04 -04:00
end
end
context 'unauthenticated user' do
it 'does not delete pipeline_schedule' do
delete api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}")
2017-05-12 08:52:31 -04:00
expect(response).to have_http_status(:unauthorized)
2017-05-11 15:12:04 -04:00
end
end
end
end