debian-mirror-gitlab/spec/controllers/projects/artifacts_controller_spec.rb

597 lines
19 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2023-04-23 21:23:45 +05:30
RSpec.describe Projects::ArtifactsController, feature_category: :build_artifacts do
2020-05-30 21:06:31 +05:30
include RepoHelpers
2022-04-04 11:22:00 +05:30
let(:user) { project.first_owner }
2019-12-21 20:55:43 +05:30
let_it_be(:project) { create(:project, :repository, :public) }
2017-08-17 22:00:37 +05:30
2019-12-21 20:55:43 +05:30
let_it_be(:pipeline, reload: true) do
2023-05-27 22:25:52 +05:30
create(
:ci_pipeline,
project: project,
sha: project.commit.sha,
ref: project.default_branch,
status: 'success'
)
2017-08-17 22:00:37 +05:30
end
2019-12-21 20:55:43 +05:30
let!(:job) { create(:ci_build, :success, :artifacts, pipeline: pipeline) }
2017-08-17 22:00:37 +05:30
before do
sign_in(user)
end
2019-12-21 20:55:43 +05:30
describe 'GET index' do
subject { get :index, params: { namespace_id: project.namespace, project_id: project } }
2023-07-09 08:55:56 +05:30
render_views
2023-03-17 16:20:25 +05:30
2023-07-09 08:55:56 +05:30
it 'renders the page with data for the artifacts app' do
subject
2019-12-21 20:55:43 +05:30
2023-07-09 08:55:56 +05:30
expect(response).to have_gitlab_http_status(:ok)
expect(response).to render_template('projects/artifacts/index')
2019-12-21 20:55:43 +05:30
end
end
describe 'DELETE destroy' do
let!(:artifact) { job.job_artifacts.erasable.first }
subject { delete :destroy, params: { namespace_id: project.namespace, project_id: project, id: artifact } }
it 'deletes the artifact' do
expect { subject }.to change { Ci::JobArtifact.count }.by(-1)
expect(artifact).not_to exist
end
it 'redirects to artifacts index page' do
subject
expect(response).to redirect_to(project_artifacts_path(project))
end
it 'sets the notice' do
subject
2020-01-01 13:55:28 +05:30
expect(flash[:notice]).to eq(_('Artifact was successfully deleted.'))
2019-12-21 20:55:43 +05:30
end
context 'when artifact deletion fails' do
before do
allow_any_instance_of(Ci::JobArtifact).to receive(:destroy).and_return(false)
end
it 'redirects to artifacts index page' do
subject
expect(response).to redirect_to(project_artifacts_path(project))
end
it 'sets the notice' do
subject
2020-01-01 13:55:28 +05:30
expect(flash[:notice]).to eq(_('Artifact could not be deleted.'))
2019-12-21 20:55:43 +05:30
end
end
context 'when user is not authorized' do
let(:user) { create(:user) }
it 'does not delete the artifact' do
expect { subject }.not_to change { Ci::JobArtifact.count }
end
end
end
2017-08-17 22:00:37 +05:30
describe 'GET download' do
2018-12-05 23:21:45 +05:30
def download_artifact(extra_params = {})
params = { namespace_id: project.namespace, project_id: project, job_id: job }.merge(extra_params)
2017-08-17 22:00:37 +05:30
2019-02-15 15:39:39 +05:30
get :download, params: params
2018-12-05 23:21:45 +05:30
end
context 'when no file type is supplied' do
2023-04-23 21:23:45 +05:30
let(:filename) { job.job_artifacts_archive.filename }
2019-03-02 22:35:43 +05:30
2018-12-05 23:21:45 +05:30
it 'sends the artifacts file' do
2019-03-02 22:35:43 +05:30
expect(controller).to receive(:send_file)
.with(
job.artifacts_file.file.path,
2020-03-13 15:44:24 +05:30
hash_including(disposition: 'attachment', filename: filename)).and_call_original
2018-12-05 23:21:45 +05:30
download_artifact
2020-03-13 15:44:24 +05:30
expect(response.headers['Content-Disposition']).to eq(%Q(attachment; filename="#{filename}"; filename*=UTF-8''#{filename}))
2018-12-05 23:21:45 +05:30
end
end
2023-04-23 21:23:45 +05:30
context 'when artifact is set as private' do
let(:filename) { job.artifacts_file.filename }
before do
job.job_artifacts.update_all(accessibility: 'private')
end
context 'and user is not authoirized' do
let(:user) { create(:user) }
it 'returns forbidden' do
download_artifact(file_type: 'archive')
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'and user has access to project' do
it 'downloads' do
expect(controller).to receive(:send_file)
.with(
job.artifacts_file.file.path,
hash_including(disposition: 'attachment', filename: filename)).and_call_original
download_artifact(file_type: 'archive')
expect(response).to have_gitlab_http_status(:ok)
expect(response.headers['Content-Disposition']).to eq(%Q(attachment; filename="#{filename}"; filename*=UTF-8''#{filename}))
end
end
end
2018-12-05 23:21:45 +05:30
context 'when a file type is supplied' do
context 'when an invalid file type is supplied' do
let(:file_type) { 'invalid' }
it 'returns 404' do
download_artifact(file_type: file_type)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2018-12-05 23:21:45 +05:30
end
end
context 'when codequality file type is supplied' do
let(:file_type) { 'codequality' }
2019-03-02 22:35:43 +05:30
let(:filename) { job.job_artifacts_codequality.filename }
2018-12-05 23:21:45 +05:30
2018-12-13 13:39:08 +05:30
context 'when file is stored locally' do
before do
create(:ci_job_artifact, :codequality, job: job)
end
it 'sends the codequality report' do
2023-05-27 22:25:52 +05:30
expect(controller).to receive(:send_file).with(
job.job_artifacts_codequality.file.path,
hash_including(disposition: 'attachment', filename: filename)
).and_call_original
2018-12-13 13:39:08 +05:30
download_artifact(file_type: file_type)
2020-03-13 15:44:24 +05:30
expect(response.headers['Content-Disposition']).to eq(%Q(attachment; filename="#{filename}"; filename*=UTF-8''#{filename}))
2018-12-13 13:39:08 +05:30
end
2018-12-05 23:21:45 +05:30
end
2018-12-13 13:39:08 +05:30
context 'when file is stored remotely' do
2023-01-13 00:05:48 +05:30
let(:cdn_config) {}
2018-12-13 13:39:08 +05:30
before do
2023-01-13 00:05:48 +05:30
stub_artifacts_object_storage(cdn: cdn_config)
2018-12-13 13:39:08 +05:30
create(:ci_job_artifact, :remote_store, :codequality, job: job)
2023-01-13 00:05:48 +05:30
allow(Gitlab::ApplicationContext).to receive(:push).and_call_original
2018-12-13 13:39:08 +05:30
end
2018-12-05 23:21:45 +05:30
2018-12-13 13:39:08 +05:30
it 'sends the codequality report' do
2023-01-13 00:05:48 +05:30
expect(Gitlab::ApplicationContext).to receive(:push).with(artifact: an_instance_of(Ci::JobArtifact)).and_call_original
2018-12-13 13:39:08 +05:30
expect(controller).to receive(:redirect_to).and_call_original
download_artifact(file_type: file_type)
end
context 'when proxied' do
it 'sends the codequality report' do
expect(Gitlab::Workhorse).to receive(:send_url).and_call_original
download_artifact(file_type: file_type, proxy: true)
end
end
2023-01-13 00:05:48 +05:30
context 'when Google CDN is configured' do
let(:cdn_config) do
{
'provider' => 'Google',
'url' => 'https://cdn.example.org',
'key_name' => 'some-key',
'key' => Base64.urlsafe_encode64(SecureRandom.hex)
}
end
before do
request.env['action_dispatch.remote_ip'] = '18.245.0.42'
end
it 'redirects to a Google CDN request' do
expect(Gitlab::ApplicationContext).to receive(:push).with(artifact: an_instance_of(Ci::JobArtifact)).and_call_original
expect(Gitlab::ApplicationContext).to receive(:push).with(artifact_used_cdn: true).and_call_original
download_artifact(file_type: file_type)
expect(response.redirect_url).to start_with("https://cdn.example.org/")
end
end
2018-12-05 23:21:45 +05:30
end
end
2017-08-17 22:00:37 +05:30
end
2022-05-03 16:02:30 +05:30
context 'when downloading a debug trace' do
let(:file_type) { 'trace' }
let(:job) { create(:ci_build, :success, :trace_artifact, pipeline: pipeline) }
before do
2023-03-04 22:38:38 +05:30
allow_next_found_instance_of(Ci::Build) do |build|
allow(build).to receive(:debug_mode?).and_return(true)
end
2022-05-03 16:02:30 +05:30
end
context 'when the user does not have update_build permissions' do
let(:user) { create(:user) }
before do
project.add_guest(user)
end
render_views
it 'denies the user access' do
download_artifact(file_type: file_type)
expect(response).to have_gitlab_http_status(:forbidden)
expect(response.body).to include(
'You must have developer or higher permissions in the associated project to view job logs when debug trace is enabled. ' \
2023-01-13 00:05:48 +05:30
'To disable debug trace, set the 'CI_DEBUG_TRACE' and 'CI_DEBUG_SERVICES' variables to 'false' ' \
'in your pipeline configuration or CI/CD settings. If you must view this job log, a project maintainer or owner must ' \
'add you to the project with developer permissions or higher.'
2022-05-03 16:02:30 +05:30
)
end
end
context 'when the user has update_build permissions' do
2023-04-23 21:23:45 +05:30
let(:filename) { job.job_artifacts_trace.file.filename }
2022-05-03 16:02:30 +05:30
it 'sends the trace' do
download_artifact(file_type: file_type)
expect(response).to have_gitlab_http_status(:ok)
2023-04-23 21:23:45 +05:30
expect(response.headers['Content-Disposition'])
.to eq(%Q(attachment; filename="#{filename}"; filename*=UTF-8''#{filename}))
2022-05-03 16:02:30 +05:30
end
end
end
2017-08-17 22:00:37 +05:30
end
describe 'GET browse' do
context 'when the directory exists' do
it 'renders the browse view' do
2019-02-15 15:39:39 +05:30
get :browse, params: { namespace_id: project.namespace, project_id: project, job_id: job, path: 'other_artifacts_0.1.2' }
2017-08-17 22:00:37 +05:30
expect(response).to render_template('projects/artifacts/browse')
end
end
context 'when the directory does not exist' do
it 'responds Not Found' do
2019-02-15 15:39:39 +05:30
get :browse, params: { namespace_id: project.namespace, project_id: project, job_id: job, path: 'unknown' }
2017-08-17 22:00:37 +05:30
expect(response).to be_not_found
end
end
end
2022-11-25 23:54:43 +05:30
describe 'GET external_file' do
before do
allow(Gitlab.config.pages).to receive(:enabled).and_return(true)
allow(Gitlab.config.pages).to receive(:artifacts_server).and_return(true)
end
context 'when the file exists' do
it 'renders the file view' do
path = 'ci_artifacts.txt'
get :external_file, params: { namespace_id: project.namespace, project_id: project, job_id: job, path: path }
expect(response).to have_gitlab_http_status(:ok)
end
end
context 'when the file does not exist' do
it 'responds Not Found' do
get :external_file, params: { namespace_id: project.namespace, project_id: project, job_id: job, path: 'unknown' }
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
2017-08-17 22:00:37 +05:30
describe 'GET file' do
2018-03-17 18:26:18 +05:30
before do
allow(Gitlab.config.pages).to receive(:enabled).and_return(true)
end
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
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
2022-11-25 23:54:43 +05:30
path = 'ci_artifacts.txt'
2018-03-17 18:26:18 +05:30
2022-11-25 23:54:43 +05:30
get :file, params: { namespace_id: project.namespace, project_id: project, job_id: job, path: path }
2018-03-17 18:26:18 +05:30
2022-11-25 23:54:43 +05:30
expect(response).to redirect_to(external_file_project_job_artifacts_path(project, job, path: path))
2018-03-17 18:26:18 +05:30
end
2017-08-17 22:00:37 +05:30
end
end
2018-03-17 18:26:18 +05:30
context 'when the file is served through Rails' do
context 'when the file exists' do
it 'renders the file view' do
2019-02-15 15:39:39 +05:30
get :file, params: { namespace_id: project.namespace, project_id: project, job_id: job, path: 'ci_artifacts.txt' }
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
expect(response).to have_gitlab_http_status(:ok)
expect(response).to render_template('projects/artifacts/file')
end
end
context 'when the file does not exist' do
it 'responds Not Found' do
2019-02-15 15:39:39 +05:30
get :file, params: { namespace_id: project.namespace, project_id: project, job_id: job, path: 'unknown' }
2018-03-17 18:26:18 +05:30
expect(response).to be_not_found
end
end
end
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
2019-02-15 15:39:39 +05:30
get :file, params: { namespace_id: private_project.namespace, project_id: private_project, job_id: job, path: 'ci_artifacts.txt' }
2018-03-17 18:26:18 +05:30
expect(response).to have_gitlab_http_status(:ok)
expect(response).to render_template('projects/artifacts/file')
2017-08-17 22:00:37 +05:30
end
end
2019-12-21 20:55:43 +05:30
context 'when the project is private and pages access control is enabled' 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(:access_control).and_return(true)
allow(Gitlab.config.pages).to receive(:artifacts_server).and_return(true)
end
it 'renders the file view' do
get :file, params: { namespace_id: private_project.namespace, project_id: private_project, job_id: job, path: 'ci_artifacts.txt' }
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:found)
2019-12-21 20:55:43 +05:30
end
end
2017-08-17 22:00:37 +05:30
end
describe 'GET raw' do
2020-05-24 23:13:21 +05:30
let(:query_params) { { namespace_id: project.namespace, project_id: project, job_id: job, path: path } }
subject { get(:raw, params: query_params) }
2018-03-17 18:26:18 +05:30
2017-08-17 22:00:37 +05:30
context 'when the file exists' do
2018-03-17 18:26:18 +05:30
let(:path) { 'ci_artifacts.txt' }
2020-05-24 23:13:21 +05:30
let(:archive_matcher) { /build_artifacts.zip(\?[^?]+)?$/ }
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
shared_examples 'a valid file' do
it 'serves the file using workhorse' do
subject
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2022-07-16 23:28:13 +05:30
expect(response.headers['Gitlab-Workhorse-Detect-Content-Type']).to eq('true')
2018-03-17 18:26:18 +05:30
expect(send_data).to start_with('artifacts-entry:')
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
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
2020-05-24 23:13:21 +05:30
expect(params['Archive']).to match(archive_matcher)
expect(params['Entry']).to eq(Base64.encode64(path))
2018-03-17 18:26:18 +05:30
end
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
def send_data
response.headers[Gitlab::Workhorse::SEND_DATA_HEADER]
end
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
def params
@params ||= begin
2022-06-21 17:19:12 +05:30
base64_params = send_data.delete_prefix('artifacts-entry:')
2020-05-24 23:13:21 +05:30
Gitlab::Json.parse(Base64.urlsafe_decode64(base64_params))
2018-03-17 18:26:18 +05:30
end
end
end
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
context 'when using local file storage' do
it_behaves_like 'a valid file' do
let(:job) { create(:ci_build, :success, :artifacts, pipeline: pipeline) }
2018-05-09 12:01:36 +05:30
let(:store) { ObjectStorage::Store::LOCAL }
2018-03-17 18:26:18 +05:30
let(:archive_path) { JobArtifactUploader.root }
end
2017-08-17 22:00:37 +05:30
end
2018-05-09 12:01:36 +05:30
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) }
let(:store) { ObjectStorage::Store::REMOTE }
let(:archive_path) { 'https://' }
end
end
2020-05-24 23:13:21 +05:30
2023-03-17 16:20:25 +05:30
context 'when artifacts archive is missing' do
let!(:job) { create(:ci_build, :success, pipeline: pipeline) }
it 'returns 404' do
subject
expect(response).to have_gitlab_http_status(:not_found)
end
end
2020-05-24 23:13:21 +05:30
context 'fetching an artifact of different type' do
before do
job.job_artifacts.each(&:destroy)
end
context 'when the artifact is zip' do
2020-06-23 00:09:42 +05:30
let!(:artifact) { create(:ci_job_artifact, :lsif, job: job) }
2020-05-24 23:13:21 +05:30
let(:path) { 'lsif/main.go.json' }
2020-06-23 00:09:42 +05:30
let(:archive_matcher) { 'lsif.json.zip' }
2020-05-24 23:13:21 +05:30
let(:query_params) { super().merge(file_type: :lsif, path: path) }
it_behaves_like 'a valid file' do
let(:store) { ObjectStorage::Store::LOCAL }
let(:archive_path) { JobArtifactUploader.root }
end
end
context 'when the artifact is not zip' do
let(:query_params) { super().merge(file_type: :junit, path: '') }
it 'responds with not found' do
create(:ci_job_artifact, :junit, job: job)
subject
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
2017-08-17 22:00:37 +05:30
end
end
describe 'GET latest_succeeded' do
2017-09-10 17:25:29 +05:30
def params_from_ref(ref = pipeline.ref, job_name = job.name, path = 'browse')
2017-08-17 22:00:37 +05:30
{
namespace_id: project.namespace,
project_id: project,
ref_name_and_path: File.join(ref, path),
2017-09-10 17:25:29 +05:30
job: job_name
2017-08-17 22:00:37 +05:30
}
end
2017-09-10 17:25:29 +05:30
context 'cannot find the job' do
2017-08-17 22:00:37 +05:30
shared_examples 'not found' do
2018-03-17 18:26:18 +05:30
it { expect(response).to have_gitlab_http_status(:not_found) }
2017-08-17 22:00:37 +05:30
end
context 'has no such ref' do
before do
2019-02-15 15:39:39 +05:30
get :latest_succeeded, params: params_from_ref('TAIL', job.name)
2017-08-17 22:00:37 +05:30
end
it_behaves_like 'not found'
end
2017-09-10 17:25:29 +05:30
context 'has no such job' do
2017-08-17 22:00:37 +05:30
before do
2019-02-15 15:39:39 +05:30
get :latest_succeeded, params: params_from_ref(pipeline.ref, 'NOBUILD')
2017-08-17 22:00:37 +05:30
end
it_behaves_like 'not found'
end
context 'has no path' do
before do
2019-02-15 15:39:39 +05:30
get :latest_succeeded, params: params_from_ref(pipeline.sha, job.name, '')
2017-08-17 22:00:37 +05:30
end
it_behaves_like 'not found'
end
end
2017-09-10 17:25:29 +05:30
context 'found the job and redirect' do
shared_examples 'redirect to the job' do
2017-08-17 22:00:37 +05:30
it 'redirects' do
2017-09-10 17:25:29 +05:30
path = browse_project_job_artifacts_path(project, job)
2017-08-17 22:00:37 +05:30
expect(response).to redirect_to(path)
end
end
context 'with regular branch' do
before do
2023-05-27 22:25:52 +05:30
pipeline.update!(ref: 'master', sha: project.commit('master').sha)
2017-08-17 22:00:37 +05:30
2019-02-15 15:39:39 +05:30
get :latest_succeeded, params: params_from_ref('master')
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
it_behaves_like 'redirect to the job'
2017-08-17 22:00:37 +05:30
end
context 'with branch name containing slash' do
before do
2023-05-27 22:25:52 +05:30
pipeline.update!(ref: 'improve/awesome', sha: project.commit('improve/awesome').sha)
2017-08-17 22:00:37 +05:30
2019-02-15 15:39:39 +05:30
get :latest_succeeded, params: params_from_ref('improve/awesome')
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
it_behaves_like 'redirect to the job'
2017-08-17 22:00:37 +05:30
end
context 'with branch name and path containing slashes' do
before do
2023-05-27 22:25:52 +05:30
pipeline.update!(ref: 'improve/awesome', sha: project.commit('improve/awesome').sha)
2017-08-17 22:00:37 +05:30
2019-02-15 15:39:39 +05:30
get :latest_succeeded, params: params_from_ref('improve/awesome', job.name, 'file/README.md')
2017-08-17 22:00:37 +05:30
end
it 'redirects' do
2017-09-10 17:25:29 +05:30
path = file_project_job_artifacts_path(project, job, 'README.md')
2017-08-17 22:00:37 +05:30
expect(response).to redirect_to(path)
end
end
2020-05-30 21:06:31 +05:30
context 'with a failed pipeline on an updated master' do
before do
create_file_in_repo(project, 'master', 'master', 'test.txt', 'This is test')
2023-05-27 22:25:52 +05:30
create(
:ci_pipeline,
2020-05-30 21:06:31 +05:30
project: project,
sha: project.commit.sha,
ref: project.default_branch,
2023-05-27 22:25:52 +05:30
status: 'failed'
)
2020-05-30 21:06:31 +05:30
get :latest_succeeded, params: params_from_ref(project.default_branch)
end
it_behaves_like 'redirect to the job'
end
2017-08-17 22:00:37 +05:30
end
end
end