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

468 lines
17 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) }
2017-09-04 08:53:19 -04:00
set(:project) { create(:project, :repository, public_builds: false) }
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
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 06:43:06 -04:00
pipeline_schedule.update(owner: user)
end
pipeline_schedule.pipelines << build(:ci_pipeline, project: project)
end
end
2017-05-11 15:12:04 -04: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 15:12:04 -04:00
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
# We need at least two users to trigger a preload for that relation.
create_pipeline_schedules(1)
2017-05-16 12:33:09 -04:00
control_count = ActiveRecord::QueryRecorder.new do
get api("/projects/#{project.id}/pipeline_schedules", developer)
end.count
create_pipeline_schedules(10)
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), params: { 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_gitlab_http_status(:not_found)
2017-05-11 15:12:04 -04: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 15:12:04 -04:00
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_gitlab_http_status(:ok)
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
get api("/projects/#{project.id}/pipeline_schedules/-5", developer)
expect(response).to have_gitlab_http_status(:not_found)
2017-05-11 15:12:04 -04: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 15:12:04 -04:00
end
end
2017-09-04 08:53:19 -04: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 08:53:19 -04:00
end
end
2017-05-11 15:12:04 -04: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 15:12:04 -04:00
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),
params: 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_gitlab_http_status(:created)
2017-05-11 15:12:04 -04:00
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_gitlab_http_status(:bad_request)
2017-05-11 15:12:04 -04:00
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),
params: params.merge('cron' => 'invalid-cron')
2017-05-12 13:10:14 -04:00
expect(response).to have_gitlab_http_status(:bad_request)
2017-05-12 13:10:14 -04:00
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
post api("/projects/#{project.id}/pipeline_schedules", user), params: params
2017-05-11 15:12:04 -04:00
expect(response).to have_gitlab_http_status(:not_found)
2017-05-11 15:12:04 -04:00
end
end
context 'unauthenticated user' do
it 'does not create pipeline_schedule' do
post api("/projects/#{project.id}/pipeline_schedules"), params: params
2017-05-11 15:12:04 -04:00
expect(response).to have_gitlab_http_status(:unauthorized)
2017-05-11 15:12:04 -04: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),
params: { cron: '1 2 3 4 *' }
2017-05-11 15:12:04 -04:00
expect(response).to have_gitlab_http_status(:ok)
2017-05-11 15:12:04 -04:00
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),
params: { cron: 'invalid-cron' }
2017-05-12 13:10:14 -04:00
expect(response).to have_gitlab_http_status(:bad_request)
2017-05-12 13:10:14 -04:00
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_gitlab_http_status(:not_found)
2017-05-11 15:12:04 -04: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 15:12:04 -04: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 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
expect(response).to have_gitlab_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_gitlab_http_status(:not_found)
2017-05-11 15:12:04 -04: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 15:12:04 -04:00
end
end
end
describe 'DELETE /projects/:id/pipeline_schedules/:pipeline_schedule_id' do
let(:maintainer) { create(:user) }
2017-05-11 15:12:04 -04:00
let!(:pipeline_schedule) do
create(:ci_pipeline_schedule, project: project, owner: developer)
end
before do
project.add_maintainer(maintainer)
2017-05-11 15:12:04 -04: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 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_gitlab_http_status(204)
2017-05-11 15:12:04 -04: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 15:12:04 -04:00
expect(response).to have_gitlab_http_status(:not_found)
2017-05-11 15:12:04 -04:00
end
2017-08-24 12:03:39 -04:00
it_behaves_like '412 response' do
let(:request) { api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}", maintainer) }
2017-08-24 12:03:39 -04:00
end
2017-05-11 15:12:04 -04:00
end
context 'authenticated user with invalid permissions' do
let!(:pipeline_schedule) { create(:ci_pipeline_schedule, project: project, owner: maintainer) }
2017-07-07 07:50:26 -04:00
2017-05-11 15:12:04 -04: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 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}")
expect(response).to have_gitlab_http_status(:unauthorized)
2017-05-11 15:12:04 -04:00
end
end
end
2017-08-21 13:21:37 -04:00
describe 'POST /projects/:id/pipeline_schedules/:pipeline_schedule_id/variables' do
let(:params) { attributes_for(:ci_pipeline_schedule_variable) }
2017-08-24 08:51:46 -04:00
set(:pipeline_schedule) do
2017-08-21 13:21:37 -04: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: params
2017-08-21 13:21:37 -04:00
end.to change { pipeline_schedule.variables.count }.by(1)
expect(response).to have_gitlab_http_status(:created)
2017-08-21 13:21:37 -04: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 13:21:37 -04:00
end
end
2017-08-22 08:42:17 -04:00
context 'when key has validation error' do
2017-08-21 13:21:37 -04:00
it 'does not create pipeline_schedule_variable' do
post api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}/variables", developer),
params: params.merge('key' => '!?!?')
2017-08-21 13:21:37 -04:00
expect(response).to have_gitlab_http_status(:bad_request)
2017-08-21 13:21:37 -04: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: params
2017-08-21 13:21:37 -04:00
expect(response).to have_gitlab_http_status(:not_found)
2017-08-21 13:21:37 -04: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: params
2017-08-21 13:21:37 -04:00
expect(response).to have_gitlab_http_status(:unauthorized)
2017-08-21 13:21:37 -04:00
end
end
end
describe 'PUT /projects/:id/pipeline_schedules/:pipeline_schedule_id/variables/:key' do
2017-08-24 08:51:46 -04:00
set(:pipeline_schedule) do
2017-08-21 13:21:37 -04: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 08:42:17 -04:00
it 'updates pipeline_schedule_variable' do
2017-08-21 13:21:37 -04:00
put api("/projects/#{project.id}/pipeline_schedules/#{pipeline_schedule.id}/variables/#{pipeline_schedule_variable.key}", developer),
params: { value: 'updated_value' }
2017-08-21 13:21:37 -04:00
expect(response).to have_gitlab_http_status(:ok)
2017-08-21 13:21:37 -04:00
expect(response).to match_response_schema('pipeline_schedule_variable')
2017-08-22 08:42:17 -04:00
expect(json_response['value']).to eq('updated_value')
2017-08-21 13:21:37 -04: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 13:21:37 -04: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 13:21:37 -04:00
end
end
end
describe 'DELETE /projects/:id/pipeline_schedules/:pipeline_schedule_id/variables/:key' do
let(:maintainer) { create(:user) }
2017-08-21 13:21:37 -04:00
2017-08-24 08:51:46 -04:00
set(:pipeline_schedule) do
2017-08-21 13:21:37 -04:00
create(:ci_pipeline_schedule, project: project, owner: developer)
end
2017-08-22 08:42:17 -04:00
let!(:pipeline_schedule_variable) do
2017-08-21 13:21:37 -04:00
create(:ci_pipeline_schedule_variable, pipeline_schedule: pipeline_schedule)
end
before do
project.add_maintainer(maintainer)
2017-08-21 13:21:37 -04: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 08:42:17 -04:00
end.to change { Ci::PipelineScheduleVariable.count }.by(-1)
2017-08-21 13:21:37 -04:00
expect(response).to have_gitlab_http_status(:accepted)
2017-08-21 13:21:37 -04: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 13:21:37 -04:00
expect(response).to have_gitlab_http_status(:not_found)
2017-08-21 13:21:37 -04:00
end
end
2017-08-24 08:51:46 -04:00
context 'authenticated user with invalid permissions' do
let!(:pipeline_schedule) { create(:ci_pipeline_schedule, project: project, owner: maintainer) }
2017-08-21 13:21:37 -04: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 13:21:37 -04: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 13:21:37 -04:00
end
end
end
2017-05-11 15:12:04 -04:00
end