2015-12-29 17:12:36 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe API::API, api: true do
|
|
|
|
include ApiHelpers
|
|
|
|
|
|
|
|
let(:user) { create(:user) }
|
2016-02-19 12:06:53 -05:00
|
|
|
let(:api_user) { user }
|
2015-12-29 17:12:36 -05:00
|
|
|
let(:user2) { create(:user) }
|
|
|
|
let!(:project) { create(:project, creator_id: user.id) }
|
2016-03-06 16:53:22 -05:00
|
|
|
let!(:developer) { create(:project_member, :developer, user: user, project: project) }
|
|
|
|
let!(:reporter) { create(:project_member, :reporter, user: user2, project: project) }
|
2015-12-30 09:12:07 -05:00
|
|
|
let(:commit) { create(:ci_commit, project: project)}
|
|
|
|
let(:build) { create(:ci_build, commit: commit) }
|
2015-12-29 17:12:36 -05:00
|
|
|
|
|
|
|
describe 'GET /projects/:id/builds ' do
|
2016-02-19 12:06:53 -05:00
|
|
|
let(:query) { '' }
|
|
|
|
|
|
|
|
before { get api("/projects/#{project.id}/builds?#{query}", api_user) }
|
|
|
|
|
2015-12-29 17:12:36 -05:00
|
|
|
context 'authorized user' do
|
|
|
|
it 'should return project builds' do
|
2016-01-08 08:01:31 -05:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(json_response).to be_an Array
|
|
|
|
end
|
|
|
|
|
2016-02-19 12:06:53 -05:00
|
|
|
context 'filter project with one scope element' do
|
|
|
|
let(:query) { 'scope=pending' }
|
2016-01-08 08:01:31 -05:00
|
|
|
|
2016-02-19 12:06:53 -05:00
|
|
|
it do
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(json_response).to be_an Array
|
|
|
|
end
|
2016-01-08 08:01:31 -05:00
|
|
|
end
|
|
|
|
|
2016-02-19 12:06:53 -05:00
|
|
|
context 'filter project with array of scope elements' do
|
|
|
|
let(:query) { 'scope[0]=pending&scope[1]=running' }
|
2016-01-08 08:01:31 -05:00
|
|
|
|
2016-02-19 12:06:53 -05:00
|
|
|
it do
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(json_response).to be_an Array
|
|
|
|
end
|
2015-12-29 17:12:36 -05:00
|
|
|
end
|
2016-01-13 09:17:59 -05:00
|
|
|
|
2016-02-19 12:06:53 -05:00
|
|
|
context 'respond 400 when scope contains invalid state' do
|
|
|
|
let(:query) { 'scope[0]=pending&scope[1]=unknown_status' }
|
2016-01-13 09:17:59 -05:00
|
|
|
|
2016-02-19 12:06:53 -05:00
|
|
|
it { expect(response.status).to eq(400) }
|
2016-01-13 09:17:59 -05:00
|
|
|
end
|
2015-12-29 17:12:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'unauthorized user' do
|
2016-02-19 12:06:53 -05:00
|
|
|
let(:api_user) { nil }
|
2015-12-29 17:12:36 -05:00
|
|
|
|
2016-02-19 12:06:53 -05:00
|
|
|
it 'should not return project builds' do
|
2015-12-29 17:12:36 -05:00
|
|
|
expect(response.status).to eq(401)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-11 04:20:45 -05:00
|
|
|
describe 'GET /projects/:id/repository/commits/:sha/builds' do
|
2016-02-19 12:06:53 -05:00
|
|
|
before do
|
|
|
|
project.ensure_ci_commit(commit.sha)
|
|
|
|
get api("/projects/#{project.id}/repository/commits/#{commit.sha}/builds", api_user)
|
|
|
|
end
|
|
|
|
|
2015-12-29 17:12:36 -05:00
|
|
|
context 'authorized user' do
|
|
|
|
it 'should return project builds for specific commit' do
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(json_response).to be_an Array
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'unauthorized user' do
|
2016-02-19 12:06:53 -05:00
|
|
|
let(:api_user) { nil }
|
2015-12-29 17:12:36 -05:00
|
|
|
|
2016-02-19 12:06:53 -05:00
|
|
|
it 'should not return project builds' do
|
2015-12-29 17:12:36 -05:00
|
|
|
expect(response.status).to eq(401)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-12-30 09:12:07 -05:00
|
|
|
|
2016-01-13 12:30:49 -05:00
|
|
|
describe 'GET /projects/:id/builds/:build_id' do
|
2016-02-19 12:06:53 -05:00
|
|
|
before { get api("/projects/#{project.id}/builds/#{build.id}", api_user) }
|
|
|
|
|
2015-12-30 09:12:07 -05:00
|
|
|
context 'authorized user' do
|
|
|
|
it 'should return specific build data' do
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(json_response['name']).to eq('test')
|
|
|
|
end
|
2016-01-13 12:30:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'unauthorized user' do
|
2016-02-19 12:06:53 -05:00
|
|
|
let(:api_user) { nil }
|
2015-12-30 09:12:07 -05:00
|
|
|
|
2016-02-19 12:06:53 -05:00
|
|
|
it 'should not return specific build data' do
|
2016-01-13 12:30:49 -05:00
|
|
|
expect(response.status).to eq(401)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-08 16:32:36 -05:00
|
|
|
describe 'GET /projects/:id/builds/:build_id/artifacts' do
|
2016-02-19 12:06:53 -05:00
|
|
|
before { get api("/projects/#{project.id}/builds/#{build.id}/artifacts", api_user) }
|
2016-02-08 16:32:36 -05:00
|
|
|
|
2016-02-19 12:06:53 -05:00
|
|
|
context 'build with artifacts' do
|
|
|
|
let(:build) { create(:ci_build, :artifacts, commit: commit) }
|
2016-02-08 16:32:36 -05:00
|
|
|
|
2016-02-19 12:06:53 -05:00
|
|
|
context 'authorized user' do
|
|
|
|
let(:download_headers) do
|
|
|
|
{ 'Content-Transfer-Encoding'=>'binary',
|
|
|
|
'Content-Disposition'=>'attachment; filename=ci_build_artifacts.zip' }
|
|
|
|
end
|
2016-02-08 16:32:36 -05:00
|
|
|
|
2016-02-19 12:06:53 -05:00
|
|
|
it 'should return specific build artifacts' do
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.headers).to include(download_headers)
|
|
|
|
end
|
2016-02-08 16:32:36 -05:00
|
|
|
end
|
|
|
|
|
2016-02-19 12:06:53 -05:00
|
|
|
context 'unauthorized user' do
|
|
|
|
let(:api_user) { nil }
|
2016-02-08 16:32:36 -05:00
|
|
|
|
2016-02-19 12:06:53 -05:00
|
|
|
it 'should not return specific build artifacts' do
|
|
|
|
expect(response.status).to eq(401)
|
|
|
|
end
|
2016-02-08 16:32:36 -05:00
|
|
|
end
|
|
|
|
end
|
2016-02-19 12:06:53 -05:00
|
|
|
|
|
|
|
it 'should not return build artifacts if not uploaded' do
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
2016-02-08 16:32:36 -05:00
|
|
|
end
|
|
|
|
|
2016-01-13 12:30:49 -05:00
|
|
|
describe 'GET /projects/:id/builds/:build_id/trace' do
|
2016-02-19 12:06:53 -05:00
|
|
|
let(:build) { create(:ci_build, :trace, commit: commit) }
|
|
|
|
|
|
|
|
before { get api("/projects/#{project.id}/builds/#{build.id}/trace", api_user) }
|
|
|
|
|
2016-01-13 12:30:49 -05:00
|
|
|
context 'authorized user' do
|
2015-12-30 09:12:07 -05:00
|
|
|
it 'should return specific build trace' do
|
|
|
|
expect(response.status).to eq(200)
|
2016-02-19 12:06:53 -05:00
|
|
|
expect(response.body).to eq(build.trace)
|
2015-12-30 09:12:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'unauthorized user' do
|
2016-02-19 12:06:53 -05:00
|
|
|
let(:api_user) { nil }
|
2015-12-30 09:12:07 -05:00
|
|
|
|
2016-02-19 12:06:53 -05:00
|
|
|
it 'should not return specific build trace' do
|
2015-12-30 09:12:07 -05:00
|
|
|
expect(response.status).to eq(401)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-12-30 10:37:47 -05:00
|
|
|
|
2016-01-08 17:33:45 -05:00
|
|
|
describe 'POST /projects/:id/builds/:build_id/cancel' do
|
2016-02-19 12:06:53 -05:00
|
|
|
before { post api("/projects/#{project.id}/builds/#{build.id}/cancel", api_user) }
|
|
|
|
|
2015-12-30 10:37:47 -05:00
|
|
|
context 'authorized user' do
|
2016-02-01 17:58:04 -05:00
|
|
|
context 'user with :update_build persmission' do
|
2015-12-30 10:37:47 -05:00
|
|
|
it 'should cancel running or pending build' do
|
|
|
|
expect(response.status).to eq(201)
|
|
|
|
expect(project.builds.first.status).to eq('canceled')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-01 17:58:04 -05:00
|
|
|
context 'user without :update_build permission' do
|
2016-02-19 12:06:53 -05:00
|
|
|
let(:api_user) { user2 }
|
2015-12-30 10:37:47 -05:00
|
|
|
|
2016-02-19 12:06:53 -05:00
|
|
|
it 'should not cancel build' do
|
2015-12-30 10:37:47 -05:00
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'unauthorized user' do
|
2016-02-19 12:06:53 -05:00
|
|
|
let(:api_user) { nil }
|
2015-12-30 10:37:47 -05:00
|
|
|
|
2016-02-19 12:06:53 -05:00
|
|
|
it 'should not cancel build' do
|
2015-12-30 10:37:47 -05:00
|
|
|
expect(response.status).to eq(401)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-08 17:33:45 -05:00
|
|
|
describe 'POST /projects/:id/builds/:build_id/retry' do
|
2016-02-19 12:06:53 -05:00
|
|
|
let(:build) { create(:ci_build, :canceled, commit: commit) }
|
|
|
|
|
|
|
|
before { post api("/projects/#{project.id}/builds/#{build.id}/retry", api_user) }
|
|
|
|
|
2015-12-30 10:37:47 -05:00
|
|
|
context 'authorized user' do
|
2016-02-19 12:06:53 -05:00
|
|
|
context 'user with :update_build permission' do
|
2015-12-30 10:37:47 -05:00
|
|
|
it 'should retry non-running build' do
|
|
|
|
expect(response.status).to eq(201)
|
|
|
|
expect(project.builds.first.status).to eq('canceled')
|
|
|
|
expect(json_response['status']).to eq('pending')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-01 17:58:04 -05:00
|
|
|
context 'user without :update_build permission' do
|
2016-02-19 12:06:53 -05:00
|
|
|
let(:api_user) { user2 }
|
2015-12-30 10:37:47 -05:00
|
|
|
|
2016-02-19 12:06:53 -05:00
|
|
|
it 'should not retry build' do
|
2015-12-30 10:37:47 -05:00
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'unauthorized user' do
|
2016-02-19 12:06:53 -05:00
|
|
|
let(:api_user) { nil }
|
2015-12-30 10:37:47 -05:00
|
|
|
|
2016-02-19 12:06:53 -05:00
|
|
|
it 'should not retry build' do
|
2015-12-30 10:37:47 -05:00
|
|
|
expect(response.status).to eq(401)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-02-03 08:12:33 -05:00
|
|
|
|
2016-02-16 03:30:58 -05:00
|
|
|
describe 'POST /projects/:id/builds/:build_id/erase' do
|
2016-02-03 08:12:33 -05:00
|
|
|
before do
|
2016-02-16 03:30:58 -05:00
|
|
|
post api("/projects/#{project.id}/builds/#{build.id}/erase", user)
|
2016-02-03 08:12:33 -05:00
|
|
|
end
|
|
|
|
|
2016-02-05 02:22:29 -05:00
|
|
|
context 'build is erasable' do
|
2016-02-19 13:31:57 -05:00
|
|
|
let(:build) { create(:ci_build, :trace, :artifacts, :success, project: project, commit: commit) }
|
2016-02-03 08:12:33 -05:00
|
|
|
|
|
|
|
it 'should erase build content' do
|
2016-02-16 03:30:58 -05:00
|
|
|
expect(response.status).to eq 201
|
2016-02-03 08:12:33 -05:00
|
|
|
expect(build.trace).to be_empty
|
|
|
|
expect(build.artifacts_file.exists?).to be_falsy
|
|
|
|
expect(build.artifacts_metadata.exists?).to be_falsy
|
|
|
|
end
|
2016-02-16 07:06:52 -05:00
|
|
|
|
|
|
|
it 'should update build' do
|
|
|
|
expect(build.reload.erased_at).to be_truthy
|
|
|
|
expect(build.reload.erased_by).to eq user
|
|
|
|
end
|
2016-02-03 08:12:33 -05:00
|
|
|
end
|
|
|
|
|
2016-02-05 02:22:29 -05:00
|
|
|
context 'build is not erasable' do
|
2016-02-19 13:31:57 -05:00
|
|
|
let(:build) { create(:ci_build, :trace, project: project, commit: commit) }
|
2016-02-03 08:12:33 -05:00
|
|
|
|
|
|
|
it 'should respond with forbidden' do
|
|
|
|
expect(response.status).to eq 403
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-12-29 17:12:36 -05:00
|
|
|
end
|