debian-mirror-gitlab/spec/lib/extracts_path_spec.rb

222 lines
6 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe ExtractsPath do
2017-09-10 17:25:29 +05:30
include described_class
2015-09-11 14:41:01 +05:30
include RepoHelpers
2017-09-10 17:25:29 +05:30
include Gitlab::Routing
2014-09-02 18:07:02 +05:30
2021-09-30 23:02:18 +05:30
# Make url_for work
def default_url_options
{ controller: 'projects/blob', action: 'show', namespace_id: @project.namespace.path, project_id: @project.path }
end
2020-06-23 00:09:42 +05:30
let_it_be(:owner) { create(:user) }
let_it_be(:container) { create(:project, :repository, creator: owner) }
2021-09-30 23:02:18 +05:30
2016-11-03 12:29:30 +05:30
let(:request) { double('request') }
2021-09-30 23:02:18 +05:30
let(:flash) { {} }
let(:redirect_renamed_default_branch?) { true }
2014-09-02 18:07:02 +05:30
before do
2020-06-23 00:09:42 +05:30
@project = container
ref_names = ['master', 'foo/bar/baz', 'v1.0.0', 'v2.0.0', 'release/app', 'release/app/v1.0.0']
2015-09-11 14:41:01 +05:30
2020-06-23 00:09:42 +05:30
allow(container.repository).to receive(:ref_names).and_return(ref_names)
2016-11-03 12:29:30 +05:30
allow(request).to receive(:format=)
2021-09-30 23:02:18 +05:30
allow(request).to receive(:get?)
allow(request).to receive(:head?)
2015-09-11 14:41:01 +05:30
end
2016-11-03 12:29:30 +05:30
describe '#assign_ref_vars' do
2015-09-11 14:41:01 +05:30
let(:ref) { sample_commit[:id] }
2021-09-30 23:02:18 +05:30
let(:path) { sample_commit[:line_code_path] }
2022-04-04 11:22:00 +05:30
let(:params) { ActionController::Parameters.new(path: path, ref: ref) }
2015-09-11 14:41:01 +05:30
2020-06-23 00:09:42 +05:30
it_behaves_like 'assigns ref vars'
2015-09-11 14:41:01 +05:30
2020-06-23 00:09:42 +05:30
it 'log tree path has no escape sequences' do
2015-09-11 14:41:01 +05:30
assign_ref_vars
2019-07-31 22:56:46 +05:30
2020-06-23 00:09:42 +05:30
expect(@logs_path).to eq("/#{@project.full_path}/-/refs/#{ref}/logs_tree/files/ruby/popen.rb")
2019-07-31 22:56:46 +05:30
end
context 'ref contains space in the middle' do
let(:ref) { 'master plan ' }
it 'returns 404' do
expect(self).to receive(:render_404)
assign_ref_vars
end
end
2016-11-03 12:29:30 +05:30
context 'ref only exists without .atom suffix' do
context 'with a path' do
2022-04-04 11:22:00 +05:30
let(:ref) { 'v1.0.0.atom' }
let(:path) { 'README.md' }
2016-11-03 12:29:30 +05:30
it 'renders a 404' do
expect(self).to receive(:render_404)
assign_ref_vars
end
end
context 'without a path' do
2022-04-04 11:22:00 +05:30
let(:ref) { 'v1.0.0.atom' }
let(:path) { nil }
2017-09-10 17:25:29 +05:30
before do
assign_ref_vars
end
2016-11-03 12:29:30 +05:30
it 'sets the un-suffixed version as @ref' do
expect(@ref).to eq('v1.0.0')
end
it 'sets the request format to Atom' do
expect(request).to have_received(:format=).with(:atom)
end
end
end
context 'ref exists with .atom suffix' do
context 'with a path' do
2022-04-04 11:22:00 +05:30
let(:ref) { 'master.atom' }
let(:path) { 'README.md' }
2016-11-03 12:29:30 +05:30
before do
repository = @project.repository
allow(repository).to receive(:commit).and_call_original
allow(repository).to receive(:commit).with('master.atom').and_return(repository.commit('master'))
assign_ref_vars
end
it 'sets the suffixed version as @ref' do
expect(@ref).to eq('master.atom')
end
it 'does not change the request format' do
expect(request).not_to have_received(:format=)
end
end
context 'without a path' do
2022-04-04 11:22:00 +05:30
let(:ref) { 'master.atom' }
let(:path) { nil }
2016-11-03 12:29:30 +05:30
before do
repository = @project.repository
allow(repository).to receive(:commit).and_call_original
allow(repository).to receive(:commit).with('master.atom').and_return(repository.commit('master'))
end
it 'sets the suffixed version as @ref' do
assign_ref_vars
expect(@ref).to eq('master.atom')
end
it 'does not change the request format' do
expect(request).not_to receive(:format=)
assign_ref_vars
end
end
end
2021-01-29 00:20:46 +05:30
context 'ref and path are nil' do
2022-04-04 11:22:00 +05:30
let(:path) { nil }
let(:ref) { nil }
2021-01-29 00:20:46 +05:30
it 'does not set commit' do
expect(container.repository).not_to receive(:commit).with('')
expect(self).to receive(:render_404)
assign_ref_vars
expect(@commit).to be_nil
end
end
2021-09-30 23:02:18 +05:30
context 'ref points to a previous default branch' do
let(:ref) { 'develop' }
before do
@project.update!(previous_default_branch: ref)
allow(@project).to receive(:default_branch).and_return('foo')
end
it 'redirects to the new default branch for a GET request' do
allow(request).to receive(:get?).and_return(true)
expect(self).to receive(:redirect_to).with("http://localhost/#{@project.full_path}/-/blob/foo/#{path}")
expect(self).not_to receive(:render_404)
assign_ref_vars
expect(@commit).to be_nil
expect(flash[:notice]).to match(/default branch/)
end
it 'redirects to the new default branch for a HEAD request' do
allow(request).to receive(:head?).and_return(true)
expect(self).to receive(:redirect_to).with("http://localhost/#{@project.full_path}/-/blob/foo/#{path}")
expect(self).not_to receive(:render_404)
assign_ref_vars
expect(@commit).to be_nil
expect(flash[:notice]).to match(/default branch/)
end
it 'returns 404 for any other request type' do
expect(self).not_to receive(:redirect_to)
expect(self).to receive(:render_404)
assign_ref_vars
expect(@commit).to be_nil
expect(flash).to be_empty
end
context 'redirect behaviour is disabled' do
let(:redirect_renamed_default_branch?) { false }
it 'returns 404 for a GET request' do
allow(request).to receive(:get?).and_return(true)
expect(self).not_to receive(:redirect_to)
expect(self).to receive(:render_404)
assign_ref_vars
expect(@commit).to be_nil
expect(flash).to be_empty
end
end
end
2014-09-02 18:07:02 +05:30
end
2020-06-23 00:09:42 +05:30
it_behaves_like 'extracts refs'
2016-11-03 12:29:30 +05:30
describe '#extract_ref_without_atom' do
it 'ignores any matching refs suffixed with atom' do
expect(extract_ref_without_atom('master.atom')).to eq('master')
end
it 'returns the longest matching ref' do
expect(extract_ref_without_atom('release/app/v1.0.0.atom')).to eq('release/app/v1.0.0')
end
2022-04-04 11:22:00 +05:30
it 'raises an error if there are no matching refs' do
expect { extract_ref_without_atom('foo.atom') }.to raise_error(ExtractsRef::InvalidPathError)
2016-11-03 12:29:30 +05:30
end
end
2014-09-02 18:07:02 +05:30
end