2018-11-20 20:47:30 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Shared examples for controllers that load and send files from the git repository
|
|
|
|
# (like Projects::RawController or Projects::AvatarsController)
|
|
|
|
|
|
|
|
# These examples requires the following variables:
|
|
|
|
# - `project`
|
|
|
|
# - `filename`: filename of the file
|
|
|
|
# - `filepath`: path of the file (contains filename)
|
|
|
|
# - `subject`: the request to be made to the controller. Example:
|
|
|
|
# subject { get :show, namespace_id: project.namespace, project_id: project }
|
2019-09-04 21:01:54 +05:30
|
|
|
shared_examples 'a controller that can serve LFS files' do
|
|
|
|
let(:lfs_oid) { '91eff75a492a3ed0dfcb544d7f31326bc4014c8551849c192fd1e48d4dd2c897' }
|
|
|
|
let(:lfs_size) { '1575078' }
|
|
|
|
let!(:lfs_object) { create(:lfs_object, oid: lfs_oid, size: lfs_size) }
|
|
|
|
|
|
|
|
context 'when lfs is enabled' do
|
|
|
|
before do
|
|
|
|
allow_any_instance_of(Project).to receive(:lfs_enabled?).and_return(true)
|
|
|
|
end
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
context 'when project has access' do
|
2018-11-20 20:47:30 +05:30
|
|
|
before do
|
2019-09-04 21:01:54 +05:30
|
|
|
project.lfs_objects << lfs_object
|
|
|
|
allow_any_instance_of(LfsObjectUploader).to receive(:exists?).and_return(true)
|
|
|
|
allow(controller).to receive(:send_file) { controller.head :ok }
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
it 'serves the file' do
|
|
|
|
lfs_uploader = LfsObjectUploader.new(lfs_object)
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
# Notice the filename= is omitted from the disposition; this is because
|
|
|
|
# Rails 5 will append this header in send_file
|
|
|
|
expect(controller).to receive(:send_file)
|
|
|
|
.with(
|
|
|
|
File.join(lfs_uploader.root, lfs_uploader.store_dir, lfs_uploader.filename),
|
|
|
|
filename: filename,
|
|
|
|
disposition: %Q(attachment; filename*=UTF-8''#{filename}))
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
subject
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
end
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
context 'and lfs uses object storage' do
|
|
|
|
let(:lfs_object) { create(:lfs_object, :with_file, oid: lfs_oid, size: lfs_size) }
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
before do
|
|
|
|
stub_lfs_object_storage
|
|
|
|
lfs_object.file.migrate!(LfsObjectUploader::Store::REMOTE)
|
|
|
|
end
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
it 'responds with redirect to file' do
|
|
|
|
subject
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
expect(response).to have_gitlab_http_status(302)
|
|
|
|
expect(response.location).to include(lfs_object.reload.file.path)
|
|
|
|
end
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
it 'sets content disposition' do
|
|
|
|
subject
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
file_uri = URI.parse(response.location)
|
|
|
|
params = CGI.parse(file_uri.query)
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
expect(params["response-content-disposition"].first).to eq(%Q(attachment; filename="#{filename}"; filename*=UTF-8''#{filename}))
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
context 'when project does not have access' do
|
|
|
|
it 'does not serve the file' do
|
|
|
|
subject
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
context 'when lfs is not enabled' do
|
|
|
|
before do
|
|
|
|
allow_any_instance_of(Project).to receive(:lfs_enabled?).and_return(false)
|
|
|
|
end
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
it 'delivers ASCII file' do
|
|
|
|
subject
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
expect(response.header['Content-Type']).to eq('text/plain; charset=utf-8')
|
|
|
|
expect(response.header['Content-Disposition'])
|
|
|
|
.to eq('inline')
|
|
|
|
expect(response.header[Gitlab::Workhorse::SEND_DATA_HEADER]).to start_with('git-blob:')
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|