2019-07-25 01:21:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2012-09-20 13:55:14 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 05:08:32 -04:00
|
|
|
RSpec.describe ExtractsPath do
|
2017-07-25 13:09:00 -04:00
|
|
|
include described_class
|
2015-04-27 00:46:27 -04:00
|
|
|
include RepoHelpers
|
2017-07-07 11:43:37 -04:00
|
|
|
include Gitlab::Routing
|
2012-09-20 13:55:14 -04:00
|
|
|
|
2021-07-14 08:09:23 -04:00
|
|
|
# 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-04 05:08:01 -04:00
|
|
|
let_it_be(:owner) { create(:user) }
|
|
|
|
let_it_be(:container) { create(:project, :repository, creator: owner) }
|
2021-06-28 23:07:32 -04:00
|
|
|
|
2016-10-07 11:49:48 -04:00
|
|
|
let(:request) { double('request') }
|
2021-07-14 08:09:23 -04:00
|
|
|
let(:flash) { {} }
|
|
|
|
let(:redirect_renamed_default_branch?) { true }
|
2012-09-20 13:55:14 -04:00
|
|
|
|
|
|
|
before do
|
2020-06-04 05:08:01 -04:00
|
|
|
@project = container
|
|
|
|
ref_names = ['master', 'foo/bar/baz', 'v1.0.0', 'v2.0.0', 'release/app', 'release/app/v1.0.0']
|
2015-05-21 17:49:06 -04:00
|
|
|
|
2020-06-04 05:08:01 -04:00
|
|
|
allow(container.repository).to receive(:ref_names).and_return(ref_names)
|
2016-10-07 11:49:48 -04:00
|
|
|
allow(request).to receive(:format=)
|
2021-07-14 08:09:23 -04:00
|
|
|
allow(request).to receive(:get?)
|
|
|
|
allow(request).to receive(:head?)
|
2012-09-20 13:55:14 -04:00
|
|
|
end
|
|
|
|
|
2016-10-07 11:49:48 -04:00
|
|
|
describe '#assign_ref_vars' do
|
2015-04-27 00:46:27 -04:00
|
|
|
let(:ref) { sample_commit[:id] }
|
2021-07-14 08:09:23 -04:00
|
|
|
let(:path) { sample_commit[:line_code_path] }
|
2022-02-09 19:15:43 -05:00
|
|
|
let(:params) { ActionController::Parameters.new(path: path, ref: ref) }
|
2015-04-27 00:46:27 -04:00
|
|
|
|
2020-06-04 05:08:01 -04:00
|
|
|
it_behaves_like 'assigns ref vars'
|
2015-04-27 00:46:27 -04:00
|
|
|
|
2020-06-04 05:08:01 -04:00
|
|
|
it 'log tree path has no escape sequences' do
|
2015-04-27 00:46:27 -04:00
|
|
|
assign_ref_vars
|
2018-12-26 02:34:47 -05:00
|
|
|
|
2020-06-04 05:08:01 -04:00
|
|
|
expect(@logs_path).to eq("/#{@project.full_path}/-/refs/#{ref}/logs_tree/files/ruby/popen.rb")
|
2018-12-26 02:34:47 -05:00
|
|
|
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-10-07 11:49:48 -04:00
|
|
|
context 'ref only exists without .atom suffix' do
|
|
|
|
context 'with a path' do
|
2022-02-09 19:15:43 -05:00
|
|
|
let(:ref) { 'v1.0.0.atom' }
|
|
|
|
let(:path) { 'README.md' }
|
2016-10-07 11:49:48 -04:00
|
|
|
|
|
|
|
it 'renders a 404' do
|
|
|
|
expect(self).to receive(:render_404)
|
|
|
|
|
|
|
|
assign_ref_vars
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without a path' do
|
2022-02-09 19:15:43 -05:00
|
|
|
let(:ref) { 'v1.0.0.atom' }
|
|
|
|
let(:path) { nil }
|
2017-06-14 14:18:56 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
assign_ref_vars
|
|
|
|
end
|
2016-10-07 11:49:48 -04:00
|
|
|
|
|
|
|
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-02-09 19:15:43 -05:00
|
|
|
let(:ref) { 'master.atom' }
|
|
|
|
let(:path) { 'README.md' }
|
2016-10-07 11:49:48 -04:00
|
|
|
|
|
|
|
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-02-09 19:15:43 -05:00
|
|
|
let(:ref) { 'master.atom' }
|
|
|
|
let(:path) { nil }
|
2016-10-07 11:49:48 -04:00
|
|
|
|
|
|
|
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
|
2020-11-16 07:09:05 -05:00
|
|
|
|
|
|
|
context 'ref and path are nil' do
|
2022-02-09 19:15:43 -05:00
|
|
|
let(:path) { nil }
|
|
|
|
let(:ref) { nil }
|
2020-11-16 07:09:05 -05:00
|
|
|
|
|
|
|
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-07-14 08:09:23 -04:00
|
|
|
|
|
|
|
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
|
2015-04-27 00:46:27 -04:00
|
|
|
end
|
|
|
|
|
2020-06-04 05:08:01 -04:00
|
|
|
it_behaves_like 'extracts refs'
|
2016-10-07 11:49:48 -04:00
|
|
|
|
|
|
|
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-02-02 10:17:50 -05:00
|
|
|
it 'raises an error if there are no matching refs' do
|
|
|
|
expect { extract_ref_without_atom('foo.atom') }.to raise_error(ExtractsRef::InvalidPathError)
|
2016-10-07 11:49:48 -04:00
|
|
|
end
|
|
|
|
end
|
2012-09-20 13:55:14 -04:00
|
|
|
end
|