2017-05-02 18:42:37 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Projects::ArtifactsController do
|
2017-12-05 09:31:33 -05:00
|
|
|
let(:user) { project.owner }
|
2017-10-05 06:41:22 -04:00
|
|
|
set(:project) { create(:project, :repository, :public) }
|
2017-05-02 18:42:37 -04:00
|
|
|
|
|
|
|
let(:pipeline) do
|
|
|
|
create(:ci_pipeline,
|
|
|
|
project: project,
|
|
|
|
sha: project.commit.sha,
|
|
|
|
ref: project.default_branch,
|
|
|
|
status: 'success')
|
|
|
|
end
|
|
|
|
|
2017-05-16 07:41:15 -04:00
|
|
|
let(:job) { create(:ci_build, :success, :artifacts, pipeline: pipeline) }
|
2017-05-02 18:42:37 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET download' do
|
|
|
|
it 'sends the artifacts file' do
|
2017-09-07 17:27:04 -04:00
|
|
|
expect(controller).to receive(:send_file).with(job.artifacts_file.path, hash_including(disposition: 'attachment')).and_call_original
|
2017-05-02 18:42:37 -04:00
|
|
|
|
2017-05-16 07:41:15 -04:00
|
|
|
get :download, namespace_id: project.namespace, project_id: project, job_id: job
|
2017-05-02 18:42:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET browse' do
|
|
|
|
context 'when the directory exists' do
|
|
|
|
it 'renders the browse view' do
|
2017-05-16 07:41:15 -04:00
|
|
|
get :browse, namespace_id: project.namespace, project_id: project, job_id: job, path: 'other_artifacts_0.1.2'
|
2017-05-02 18:42:37 -04:00
|
|
|
|
|
|
|
expect(response).to render_template('projects/artifacts/browse')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the directory does not exist' do
|
|
|
|
it 'responds Not Found' do
|
2017-05-16 07:41:15 -04:00
|
|
|
get :browse, namespace_id: project.namespace, project_id: project, job_id: job, path: 'unknown'
|
2017-05-02 18:42:37 -04:00
|
|
|
|
|
|
|
expect(response).to be_not_found
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET file' do
|
2017-10-03 06:34:24 -04:00
|
|
|
before do
|
|
|
|
allow(Gitlab.config.pages).to receive(:enabled).and_return(true)
|
|
|
|
end
|
2017-05-02 18:42:37 -04:00
|
|
|
|
2017-10-04 15:01:27 -04:00
|
|
|
context 'when the file is served by GitLab Pages' do
|
|
|
|
before do
|
|
|
|
allow(Gitlab.config.pages).to receive(:artifacts_server).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the file exists' do
|
|
|
|
it 'renders the file view' do
|
|
|
|
get :file, namespace_id: project.namespace, project_id: project, job_id: job, path: 'ci_artifacts.txt'
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(302)
|
2017-10-04 15:01:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the file does not exist' do
|
|
|
|
it 'responds Not Found' do
|
|
|
|
get :file, namespace_id: project.namespace, project_id: project, job_id: job, path: 'unknown'
|
2017-05-02 18:42:37 -04:00
|
|
|
|
2017-10-04 15:01:27 -04:00
|
|
|
expect(response).to be_not_found
|
|
|
|
end
|
2017-05-02 18:42:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-04 15:01:27 -04:00
|
|
|
context 'when the file is served through Rails' do
|
|
|
|
context 'when the file exists' do
|
|
|
|
it 'renders the file view' do
|
|
|
|
get :file, namespace_id: project.namespace, project_id: project, job_id: job, path: 'ci_artifacts.txt'
|
2017-05-02 18:42:37 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-10-04 15:01:27 -04:00
|
|
|
expect(response).to render_template('projects/artifacts/file')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the file does not exist' do
|
|
|
|
it 'responds Not Found' do
|
|
|
|
get :file, namespace_id: project.namespace, project_id: project, job_id: job, path: 'unknown'
|
|
|
|
|
|
|
|
expect(response).to be_not_found
|
|
|
|
end
|
2017-05-02 18:42:37 -04:00
|
|
|
end
|
|
|
|
end
|
2017-10-05 06:41:22 -04:00
|
|
|
|
|
|
|
context 'when the project is private' do
|
|
|
|
let(:private_project) { create(:project, :repository, :private) }
|
|
|
|
let(:pipeline) { create(:ci_pipeline, project: private_project) }
|
|
|
|
let(:job) { create(:ci_build, :success, :artifacts, pipeline: pipeline) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
private_project.add_developer(user)
|
|
|
|
|
|
|
|
allow(Gitlab.config.pages).to receive(:artifacts_server).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not redirect the request' do
|
|
|
|
get :file, namespace_id: private_project.namespace, project_id: private_project, job_id: job, path: 'ci_artifacts.txt'
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-10-05 06:41:22 -04:00
|
|
|
expect(response).to render_template('projects/artifacts/file')
|
2017-05-02 18:42:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET raw' do
|
2017-12-26 05:27:03 -05:00
|
|
|
subject { get(:raw, namespace_id: project.namespace, project_id: project, job_id: job, path: path) }
|
|
|
|
|
2017-05-02 18:42:37 -04:00
|
|
|
context 'when the file exists' do
|
2017-09-07 17:27:04 -04:00
|
|
|
let(:path) { 'ci_artifacts.txt' }
|
|
|
|
|
|
|
|
shared_examples 'a valid file' do
|
|
|
|
it 'serves the file using workhorse' do
|
|
|
|
subject
|
|
|
|
|
2017-12-05 09:31:33 -05:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2017-09-07 17:27:04 -04:00
|
|
|
expect(send_data).to start_with('artifacts-entry:')
|
|
|
|
|
|
|
|
expect(params.keys).to eq(%w(Archive Entry))
|
|
|
|
expect(params['Archive']).to start_with(archive_path)
|
|
|
|
# On object storage, the URL can end with a query string
|
|
|
|
expect(params['Archive']).to match(/build_artifacts.zip(\?[^?]+)?$/)
|
|
|
|
expect(params['Entry']).to eq(Base64.encode64('ci_artifacts.txt'))
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_data
|
|
|
|
response.headers[Gitlab::Workhorse::SEND_DATA_HEADER]
|
|
|
|
end
|
|
|
|
|
|
|
|
def params
|
|
|
|
@params ||= begin
|
|
|
|
base64_params = send_data.sub(/\Aartifacts\-entry:/, '')
|
|
|
|
JSON.parse(Base64.urlsafe_decode64(base64_params))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-05-02 18:42:37 -04:00
|
|
|
|
2017-09-07 17:27:04 -04:00
|
|
|
context 'when using local file storage' do
|
|
|
|
it_behaves_like 'a valid file' do
|
2017-12-05 09:31:33 -05:00
|
|
|
let(:job) { create(:ci_build, :success, :artifacts, pipeline: pipeline) }
|
2018-02-02 08:59:43 -05:00
|
|
|
let(:store) { ObjectStorage::Store::LOCAL }
|
|
|
|
let(:archive_path) { JobArtifactUploader.root }
|
2017-09-07 17:27:04 -04:00
|
|
|
end
|
|
|
|
end
|
2017-05-02 18:42:37 -04:00
|
|
|
|
2017-09-07 17:27:04 -04:00
|
|
|
context 'when using remote file storage' do
|
|
|
|
before do
|
|
|
|
stub_artifacts_object_storage
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a valid file' do
|
|
|
|
let!(:artifact) { create(:ci_job_artifact, :archive, :remote_store, job: job) }
|
|
|
|
let!(:job) { create(:ci_build, :success, pipeline: pipeline) }
|
2018-02-02 08:59:43 -05:00
|
|
|
let(:store) { ObjectStorage::Store::REMOTE }
|
2017-09-07 17:27:04 -04:00
|
|
|
let(:archive_path) { 'https://' }
|
|
|
|
end
|
2017-05-02 18:42:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET latest_succeeded' do
|
2017-05-16 07:41:15 -04:00
|
|
|
def params_from_ref(ref = pipeline.ref, job_name = job.name, path = 'browse')
|
2017-05-02 18:42:37 -04:00
|
|
|
{
|
|
|
|
namespace_id: project.namespace,
|
|
|
|
project_id: project,
|
|
|
|
ref_name_and_path: File.join(ref, path),
|
2017-05-16 07:41:15 -04:00
|
|
|
job: job_name
|
2017-05-02 18:42:37 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-05-16 07:41:15 -04:00
|
|
|
context 'cannot find the job' do
|
2017-05-02 18:42:37 -04:00
|
|
|
shared_examples 'not found' do
|
2017-10-19 14:28:19 -04:00
|
|
|
it { expect(response).to have_gitlab_http_status(:not_found) }
|
2017-05-02 18:42:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'has no such ref' do
|
|
|
|
before do
|
2017-05-16 07:41:15 -04:00
|
|
|
get :latest_succeeded, params_from_ref('TAIL', job.name)
|
2017-05-02 18:42:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'not found'
|
|
|
|
end
|
|
|
|
|
2017-05-16 07:41:15 -04:00
|
|
|
context 'has no such job' do
|
2017-05-02 18:42:37 -04:00
|
|
|
before do
|
|
|
|
get :latest_succeeded, params_from_ref(pipeline.ref, 'NOBUILD')
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'not found'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'has no path' do
|
|
|
|
before do
|
2017-05-16 07:41:15 -04:00
|
|
|
get :latest_succeeded, params_from_ref(pipeline.sha, job.name, '')
|
2017-05-02 18:42:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'not found'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-16 07:41:15 -04:00
|
|
|
context 'found the job and redirect' do
|
|
|
|
shared_examples 'redirect to the job' do
|
2017-05-02 18:42:37 -04:00
|
|
|
it 'redirects' do
|
2017-06-29 13:06:35 -04:00
|
|
|
path = browse_project_job_artifacts_path(project, job)
|
2017-05-02 18:42:37 -04:00
|
|
|
|
|
|
|
expect(response).to redirect_to(path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with regular branch' do
|
|
|
|
before do
|
|
|
|
pipeline.update(ref: 'master',
|
|
|
|
sha: project.commit('master').sha)
|
|
|
|
|
|
|
|
get :latest_succeeded, params_from_ref('master')
|
|
|
|
end
|
|
|
|
|
2017-05-16 07:41:15 -04:00
|
|
|
it_behaves_like 'redirect to the job'
|
2017-05-02 18:42:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with branch name containing slash' do
|
|
|
|
before do
|
|
|
|
pipeline.update(ref: 'improve/awesome',
|
|
|
|
sha: project.commit('improve/awesome').sha)
|
|
|
|
|
|
|
|
get :latest_succeeded, params_from_ref('improve/awesome')
|
|
|
|
end
|
|
|
|
|
2017-05-16 07:41:15 -04:00
|
|
|
it_behaves_like 'redirect to the job'
|
2017-05-02 18:42:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with branch name and path containing slashes' do
|
|
|
|
before do
|
|
|
|
pipeline.update(ref: 'improve/awesome',
|
|
|
|
sha: project.commit('improve/awesome').sha)
|
|
|
|
|
2017-05-16 07:41:15 -04:00
|
|
|
get :latest_succeeded, params_from_ref('improve/awesome', job.name, 'file/README.md')
|
2017-05-02 18:42:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'redirects' do
|
2017-06-29 13:06:35 -04:00
|
|
|
path = file_project_job_artifacts_path(project, job, 'README.md')
|
2017-05-02 18:42:37 -04:00
|
|
|
|
|
|
|
expect(response).to redirect_to(path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|