2019-04-15 06:17:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-03-31 07:37:21 -04:00
|
|
|
require "spec_helper"
|
|
|
|
|
|
|
|
describe Projects::RepositoriesController do
|
2017-01-25 16:44:33 -05:00
|
|
|
let(:project) { create(:project, :repository) }
|
2015-03-31 07:37:21 -04:00
|
|
|
|
|
|
|
describe "GET archive" do
|
2020-02-22 07:08:58 -05:00
|
|
|
before do
|
|
|
|
allow(controller).to receive(:archive_rate_limit_reached?).and_return(false)
|
|
|
|
end
|
|
|
|
|
2016-02-15 21:17:20 -05:00
|
|
|
context 'as a guest' do
|
|
|
|
it 'responds with redirect in correct format' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :archive, params: { namespace_id: project.namespace, project_id: project, id: "master" }, format: "zip"
|
2015-03-31 07:37:21 -04:00
|
|
|
|
2016-09-27 22:52:41 -04:00
|
|
|
expect(response.header["Content-Type"]).to start_with('text/html')
|
2016-02-15 21:17:20 -05:00
|
|
|
expect(response).to be_redirect
|
|
|
|
end
|
2015-03-31 07:37:21 -04:00
|
|
|
end
|
|
|
|
|
2016-02-15 21:17:20 -05:00
|
|
|
context 'as a user' do
|
|
|
|
let(:user) { create(:user) }
|
2020-01-29 10:08:59 -05:00
|
|
|
let(:archive_name) { "#{project.path}-master" }
|
2015-03-31 07:37:21 -04:00
|
|
|
|
|
|
|
before do
|
2017-12-22 03:18:28 -05:00
|
|
|
project.add_developer(user)
|
2016-02-15 21:17:20 -05:00
|
|
|
sign_in(user)
|
2015-03-31 07:37:21 -04:00
|
|
|
end
|
|
|
|
|
2016-06-06 07:16:30 -04:00
|
|
|
it "uses Gitlab::Workhorse" do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :archive, params: { namespace_id: project.namespace, project_id: project, id: "master" }, format: "zip"
|
2016-06-06 07:16:30 -04:00
|
|
|
|
|
|
|
expect(response.header[Gitlab::Workhorse::SEND_DATA_HEADER]).to start_with("git-archive:")
|
2016-02-15 21:17:20 -05:00
|
|
|
end
|
|
|
|
|
2018-02-19 15:41:04 -05:00
|
|
|
it 'responds with redirect to the short name archive if fully qualified' do
|
2020-01-29 10:08:59 -05:00
|
|
|
get :archive, params: { namespace_id: project.namespace, project_id: project, id: "master/#{archive_name}" }, format: "zip"
|
2018-02-19 15:41:04 -05:00
|
|
|
|
|
|
|
expect(assigns(:ref)).to eq("master")
|
2020-01-29 10:08:59 -05:00
|
|
|
expect(assigns(:filename)).to eq(archive_name)
|
|
|
|
expect(response.header[Gitlab::Workhorse::SEND_DATA_HEADER]).to start_with("git-archive:")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'responds with redirect for a path with multiple slashes' do
|
|
|
|
get :archive, params: { namespace_id: project.namespace, project_id: project, id: "improve/awesome/#{archive_name}" }, format: "zip"
|
|
|
|
|
|
|
|
expect(assigns(:ref)).to eq("improve/awesome")
|
|
|
|
expect(assigns(:filename)).to eq(archive_name)
|
2018-02-19 15:41:04 -05:00
|
|
|
expect(response.header[Gitlab::Workhorse::SEND_DATA_HEADER]).to start_with("git-archive:")
|
|
|
|
end
|
|
|
|
|
2018-04-08 08:47:54 -04:00
|
|
|
it 'handles legacy queries with no ref' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :archive, params: { namespace_id: project.namespace, project_id: project }, format: "zip"
|
2018-04-08 08:47:54 -04:00
|
|
|
|
|
|
|
expect(response.header[Gitlab::Workhorse::SEND_DATA_HEADER]).to start_with("git-archive:")
|
|
|
|
end
|
|
|
|
|
2018-04-18 14:11:50 -04:00
|
|
|
it 'handles legacy queries with the ref specified as ref in params' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :archive, params: { namespace_id: project.namespace, project_id: project, ref: 'feature' }, format: 'zip'
|
2018-04-18 14:11:50 -04:00
|
|
|
|
2020-01-28 07:08:44 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-04-18 14:11:50 -04:00
|
|
|
expect(assigns(:ref)).to eq('feature')
|
|
|
|
expect(response.header[Gitlab::Workhorse::SEND_DATA_HEADER]).to start_with("git-archive:")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles legacy queries with the ref specified as id in params' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :archive, params: { namespace_id: project.namespace, project_id: project, id: 'feature' }, format: 'zip'
|
2018-04-18 14:11:50 -04:00
|
|
|
|
2020-01-28 07:08:44 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-04-18 14:11:50 -04:00
|
|
|
expect(assigns(:ref)).to eq('feature')
|
|
|
|
expect(response.header[Gitlab::Workhorse::SEND_DATA_HEADER]).to start_with("git-archive:")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'prioritizes the id param over the ref param when both are specified' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :archive, params: { namespace_id: project.namespace, project_id: project, id: 'feature', ref: 'feature_conflict' }, format: 'zip'
|
2018-04-18 14:11:50 -04:00
|
|
|
|
2020-01-28 07:08:44 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-04-18 14:11:50 -04:00
|
|
|
expect(assigns(:ref)).to eq('feature')
|
|
|
|
expect(response.header[Gitlab::Workhorse::SEND_DATA_HEADER]).to start_with("git-archive:")
|
|
|
|
end
|
|
|
|
|
2016-02-15 21:17:20 -05:00
|
|
|
context "when the service raises an error" do
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Workhorse).to receive(:send_git_archive).and_raise("Archive failed")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "renders Not Found" do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :archive, params: { namespace_id: project.namespace, project_id: project, id: "master" }, format: "zip"
|
2015-03-31 07:37:21 -04:00
|
|
|
|
2020-01-28 07:08:44 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2016-02-15 21:17:20 -05:00
|
|
|
end
|
2015-03-31 07:37:21 -04:00
|
|
|
end
|
2019-07-10 13:34:05 -04:00
|
|
|
|
2020-02-03 16:09:00 -05:00
|
|
|
context "when the request format is HTML" do
|
|
|
|
it "renders 404" do
|
|
|
|
get :archive, params: { namespace_id: project.namespace, project_id: project, id: 'master' }, format: "html"
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-22 07:08:58 -05:00
|
|
|
describe 'rate limiting' do
|
|
|
|
it 'rate limits user when thresholds hit' do
|
|
|
|
expect(controller).to receive(:archive_rate_limit_reached?).and_return(true)
|
|
|
|
|
|
|
|
get :archive, params: { namespace_id: project.namespace, project_id: project, id: 'master' }, format: "html"
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:too_many_requests)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-10 13:34:05 -04:00
|
|
|
describe 'caching' do
|
|
|
|
it 'sets appropriate caching headers' do
|
|
|
|
get_archive
|
|
|
|
|
2020-01-28 07:08:44 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-07-10 13:34:05 -04:00
|
|
|
expect(response.header['ETag']).to be_present
|
|
|
|
expect(response.header['Cache-Control']).to include('max-age=60, private')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project is public' do
|
|
|
|
let(:project) { create(:project, :repository, :public) }
|
|
|
|
|
|
|
|
it 'sets appropriate caching headers' do
|
|
|
|
get_archive
|
|
|
|
|
2020-01-28 07:08:44 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-07-10 13:34:05 -04:00
|
|
|
expect(response.header['ETag']).to be_present
|
|
|
|
expect(response.header['Cache-Control']).to include('max-age=60, public')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when ref is a commit SHA' do
|
|
|
|
it 'max-age is set to 3600 in Cache-Control header' do
|
|
|
|
get_archive('ddd0f15ae83993f5cb66a927a28673882e99100b')
|
|
|
|
|
2020-01-28 07:08:44 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-07-10 13:34:05 -04:00
|
|
|
expect(response.header['Cache-Control']).to include('max-age=3600')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when If-None-Modified header is set' do
|
|
|
|
it 'returns a 304 status' do
|
|
|
|
# Get the archive cached first
|
|
|
|
get_archive
|
|
|
|
|
|
|
|
request.headers['If-None-Match'] = response.headers['ETag']
|
|
|
|
get_archive
|
|
|
|
|
2020-01-28 07:08:44 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_modified)
|
2019-07-10 13:34:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_archive(id = 'feature')
|
|
|
|
get :archive, params: { namespace_id: project.namespace, project_id: project, id: id }, format: 'zip'
|
|
|
|
end
|
|
|
|
end
|
2015-03-31 07:37:21 -04:00
|
|
|
end
|
2019-07-22 10:56:40 -04:00
|
|
|
|
|
|
|
context 'as a sessionless user' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when no token is provided' do
|
|
|
|
it 'redirects to sign in page' do
|
|
|
|
get :archive, params: { namespace_id: project.namespace, project_id: project, id: 'master' }, format: 'zip'
|
|
|
|
|
2020-01-28 07:08:44 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:found)
|
2019-07-22 10:56:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a token param is present' do
|
|
|
|
context 'when token is correct' do
|
|
|
|
it 'calls the action normally' do
|
|
|
|
get :archive, params: { namespace_id: project.namespace, project_id: project, id: 'master', token: user.static_object_token }, format: 'zip'
|
|
|
|
|
2020-01-28 07:08:44 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-07-22 10:56:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when token is incorrect' do
|
|
|
|
it 'redirects to sign in page' do
|
|
|
|
get :archive, params: { namespace_id: project.namespace, project_id: project, id: 'master', token: 'foobar' }, format: 'zip'
|
|
|
|
|
2020-01-28 07:08:44 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:found)
|
2019-07-22 10:56:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a token header is present' do
|
|
|
|
context 'when token is correct' do
|
|
|
|
it 'calls the action normally' do
|
|
|
|
request.headers['X-Gitlab-Static-Object-Token'] = user.static_object_token
|
|
|
|
get :archive, params: { namespace_id: project.namespace, project_id: project, id: 'master' }, format: 'zip'
|
|
|
|
|
2020-01-28 07:08:44 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-07-22 10:56:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when token is incorrect' do
|
|
|
|
it 'redirects to sign in page' do
|
|
|
|
request.headers['X-Gitlab-Static-Object-Token'] = 'foobar'
|
|
|
|
get :archive, params: { namespace_id: project.namespace, project_id: project, id: 'master' }, format: 'zip'
|
|
|
|
|
2020-01-28 07:08:44 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:found)
|
2019-07-22 10:56:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-03-31 07:37:21 -04:00
|
|
|
end
|
|
|
|
end
|