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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

311 lines
12 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
require "spec_helper"
2020-06-23 00:09:42 +05:30
RSpec.describe Projects::RepositoriesController do
2022-04-04 11:22:00 +05:30
let_it_be(:project) { create(:project, :repository) }
describe 'POST create' do
let_it_be(:user) { create(:user) }
let(:request) { post :create, params: { namespace_id: project.namespace, project_id: project } }
before do
project.add_maintainer(user)
sign_in(user)
end
context 'when repository does not exist' do
let!(:project) { create(:project) }
it 'creates the repository' do
expect { request }.to change { project.repository.raw_repository.exists? }.from(false).to(true)
expect(response).to be_redirect
end
end
context 'when repository already exists' do
it 'does not raise an exception' do
expect(Gitlab::ErrorTracking).not_to receive(:track_exception)
request
expect(response).to be_redirect
end
end
end
2015-04-26 12:48:37 +05:30
describe "GET archive" do
2020-04-08 14:13:33 +05:30
before do
allow(controller).to receive(:archive_rate_limit_reached?).and_return(false)
end
2016-06-02 11:05:42 +05:30
context 'as a guest' do
it 'responds with redirect in correct format' do
2019-02-15 15:39:39 +05:30
get :archive, params: { namespace_id: project.namespace, project_id: project, id: "master" }, format: "zip"
2015-04-26 12:48:37 +05:30
2016-11-03 12:29:30 +05:30
expect(response.header["Content-Type"]).to start_with('text/html')
2016-06-02 11:05:42 +05:30
expect(response).to be_redirect
end
2015-04-26 12:48:37 +05:30
end
2016-06-02 11:05:42 +05:30
context 'as a user' do
let(:user) { create(:user) }
2020-03-13 15:44:24 +05:30
let(:archive_name) { "#{project.path}-master" }
2015-04-26 12:48:37 +05:30
before do
2018-03-17 18:26:18 +05:30
project.add_developer(user)
2016-06-02 11:05:42 +05:30
sign_in(user)
2015-04-26 12:48:37 +05:30
end
2020-03-28 13:19:24 +05:30
it_behaves_like "hotlink interceptor" do
let(:http_request) do
get :archive, params: { namespace_id: project.namespace, project_id: project, id: "master" }, format: "zip"
end
end
it "uses Gitlab::Workhorse" do
2019-02-15 15:39:39 +05:30
get :archive, params: { namespace_id: project.namespace, project_id: project, id: "master" }, format: "zip"
expect(response.header[Gitlab::Workhorse::SEND_DATA_HEADER]).to start_with("git-archive:")
2016-06-02 11:05:42 +05:30
end
2018-05-09 12:01:36 +05:30
it 'responds with redirect to the short name archive if fully qualified' do
2020-03-13 15:44:24 +05:30
get :archive, params: { namespace_id: project.namespace, project_id: project, id: "master/#{archive_name}" }, format: "zip"
2018-05-09 12:01:36 +05:30
expect(assigns(:ref)).to eq("master")
2020-03-13 15:44:24 +05:30
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-05-09 12:01:36 +05:30
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
2019-02-15 15:39:39 +05:30
get :archive, params: { namespace_id: project.namespace, project_id: project, id: 'feature', ref: 'feature_conflict' }, format: 'zip'
2018-05-09 12:01:36 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2018-05-09 12:01:36 +05:30
expect(assigns(:ref)).to eq('feature')
expect(response.header[Gitlab::Workhorse::SEND_DATA_HEADER]).to start_with("git-archive:")
end
2016-06-02 11:05:42 +05:30
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
2019-02-15 15:39:39 +05:30
get :archive, params: { namespace_id: project.namespace, project_id: project, id: "master" }, format: "zip"
2015-04-26 12:48:37 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
end
end
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)
2016-06-02 11:05:42 +05:30
end
2015-04-26 12:48:37 +05:30
end
2019-10-12 21:52:04 +05:30
2020-04-08 14:13:33 +05:30
describe 'rate limiting' do
it 'rate limits user when thresholds hit' do
2022-01-26 12:08:38 +05:30
allow(Gitlab::ApplicationRateLimiter).to receive(:throttled?).and_return(true)
2020-04-08 14:13:33 +05:30
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-10-12 21:52:04 +05:30
describe 'caching' do
it 'sets appropriate caching headers' do
get_archive
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-10-12 21:52:04 +05:30
expect(response.header['ETag']).to be_present
2021-01-29 00:20:46 +05:30
expect(response.cache_control[:public]).to eq(false)
expect(response.cache_control[:max_age]).to eq(60)
expect(response.cache_control[:no_store]).to be_nil
2019-10-12 21:52:04 +05:30
end
context 'when project is public' do
let(:project) { create(:project, :repository, :public) }
it 'sets appropriate caching headers' do
get_archive
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-10-12 21:52:04 +05:30
expect(response.header['ETag']).to be_present
expect(response.header['Cache-Control']).to include('max-age=60, public')
end
2021-01-08 16:13:35 +05:30
context 'and repo is private' do
let(:project) { create(:project, :repository, :public, :repository_private) }
it 'sets appropriate caching headers' do
get_archive
expect(response).to have_gitlab_http_status(:ok)
expect(response.header['ETag']).to be_present
expect(response.header['Cache-Control']).to include('max-age=60, private')
end
end
2019-10-12 21:52:04 +05:30
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-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-10-12 21:52:04 +05:30
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-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_modified)
2019-10-12 21:52:04 +05:30
end
end
def get_archive(id = 'feature')
get :archive, params: { namespace_id: project.namespace, project_id: project, id: id }, format: 'zip'
end
end
2015-04-26 12:48:37 +05:30
end
2019-12-04 20:38:33 +05:30
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-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:found)
2019-12-04 20:38:33 +05:30
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-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-12-04 20:38:33 +05:30
end
2022-01-12 12:59:36 +05:30
context 'when user with expired password' do
let_it_be(:user) { create(:user, password_expires_at: 2.minutes.ago) }
it 'redirects to sign in page' do
get :archive, params: { namespace_id: project.namespace, project_id: project, id: 'master', token: user.static_object_token }, format: 'zip'
expect(response).to have_gitlab_http_status(:found)
expect(response.location).to end_with('/users/sign_in')
end
end
context 'when password expiration is not applicable' do
context 'when ldap user' do
let_it_be(:user) { create(:omniauth_user, provider: 'ldap', password_expires_at: 2.minutes.ago) }
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'
expect(response).to have_gitlab_http_status(:ok)
end
end
end
2019-12-04 20:38:33 +05:30
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-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:found)
2019-12-04 20:38:33 +05:30
end
end
2022-03-02 08:16:31 +05:30
context 'when token is migrated' do
let(:user) { create(:user, static_object_token: '') }
let(:token) { 'Test' }
it 'calls the action normally' do
user.update_column(:static_object_token, token)
get :archive, params: { namespace_id: project.namespace, project_id: project, id: 'master', token: token }, format: 'zip'
expect(user.static_object_token).to eq(token)
expect(response).to have_gitlab_http_status(:ok)
user.update_column(:static_object_token_encrypted, Gitlab::CryptoHelper.aes256_gcm_encrypt(token))
get :archive, params: { namespace_id: project.namespace, project_id: project, id: 'master', token: token }, format: 'zip'
expect(user.static_object_token).to eq(token)
expect(response).to have_gitlab_http_status(:ok)
end
end
2019-12-04 20:38:33 +05:30
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-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-12-04 20:38:33 +05:30
end
2022-01-12 12:59:36 +05:30
context 'when user with expired password' do
let_it_be(:user) { create(:user, password_expires_at: 2.minutes.ago) }
it 'redirects to sign in page' 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'
expect(response).to have_gitlab_http_status(:found)
expect(response.location).to end_with('/users/sign_in')
end
end
context 'when password expiration is not applicable' do
context 'when ldap user' do
let_it_be(:user) { create(:omniauth_user, provider: 'ldap', password_expires_at: 2.minutes.ago) }
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'
expect(response).to have_gitlab_http_status(:ok)
end
end
end
2019-12-04 20:38:33 +05:30
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-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:found)
2019-12-04 20:38:33 +05:30
end
end
end
end
2015-04-26 12:48:37 +05:30
end
end