2019-08-22 10:57:44 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-24 15:54:22 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe ::Gitlab::RepoPath do
|
2020-03-05 00:07:49 +00:00
|
|
|
include Gitlab::Routing
|
|
|
|
|
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
|
|
|
let_it_be(:personal_snippet) { create(:personal_snippet) }
|
|
|
|
let_it_be(:project_snippet) { create(:project_snippet, project: project) }
|
2020-03-17 12:09:52 +00:00
|
|
|
let_it_be(:redirect_route) { 'foo/bar/baz' }
|
|
|
|
let_it_be(:redirect) { project.route.create_redirect(redirect_route) }
|
2017-04-28 16:52:09 +00:00
|
|
|
|
2020-03-05 00:07:49 +00:00
|
|
|
describe '.parse' do
|
2017-06-16 00:03:54 +00:00
|
|
|
context 'a repository storage path' do
|
2020-03-05 00:07:49 +00:00
|
|
|
it 'parses a full repository project path' do
|
|
|
|
expect(described_class.parse(project.repository.full_path)).to eq([project, project, Gitlab::GlRepository::PROJECT, nil])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'parses a full wiki project path' do
|
2020-10-05 15:08:56 +00:00
|
|
|
expect(described_class.parse(project.wiki.repository.full_path)).to eq([project.wiki, project, Gitlab::GlRepository::WIKI, nil])
|
2020-03-05 00:07:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'parses a personal snippet repository path' do
|
|
|
|
expect(described_class.parse("snippets/#{personal_snippet.id}")).to eq([personal_snippet, nil, Gitlab::GlRepository::SNIPPET, nil])
|
2017-06-16 00:03:54 +00:00
|
|
|
end
|
2017-04-28 16:52:09 +00:00
|
|
|
|
2020-03-05 00:07:49 +00:00
|
|
|
it 'parses a project snippet repository path' do
|
|
|
|
expect(described_class.parse("#{project.full_path}/snippets/#{project_snippet.id}")).to eq([project_snippet, project, Gitlab::GlRepository::SNIPPET, nil])
|
2017-06-16 00:03:54 +00:00
|
|
|
end
|
2017-04-28 16:52:09 +00:00
|
|
|
end
|
|
|
|
|
2017-06-16 00:03:54 +00:00
|
|
|
context 'a relative path' do
|
|
|
|
it 'parses a relative repository path' do
|
2020-03-05 00:07:49 +00:00
|
|
|
expect(described_class.parse(project.full_path + '.git')).to eq([project, project, Gitlab::GlRepository::PROJECT, nil])
|
2017-06-16 00:03:54 +00:00
|
|
|
end
|
2017-04-28 16:52:09 +00:00
|
|
|
|
2017-06-16 00:03:54 +00:00
|
|
|
it 'parses a relative wiki path' do
|
2020-10-05 15:08:56 +00:00
|
|
|
expect(described_class.parse(project.full_path + '.wiki.git')).to eq([project.wiki, project, Gitlab::GlRepository::WIKI, nil])
|
2017-06-16 00:03:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'parses a relative path starting with /' do
|
2020-03-05 00:07:49 +00:00
|
|
|
expect(described_class.parse('/' + project.full_path + '.git')).to eq([project, project, Gitlab::GlRepository::PROJECT, nil])
|
2017-06-16 00:03:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'of a redirected project' do
|
|
|
|
it 'parses a relative repository path' do
|
2020-03-17 12:09:52 +00:00
|
|
|
expect(described_class.parse(redirect.path + '.git')).to eq([project, project, Gitlab::GlRepository::PROJECT, redirect_route])
|
2017-06-16 00:03:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'parses a relative wiki path' do
|
2020-10-05 15:08:56 +00:00
|
|
|
expect(described_class.parse(redirect.path + '.wiki.git')).to eq([project.wiki, project, Gitlab::GlRepository::WIKI, redirect_route])
|
2017-06-16 00:03:54 +00:00
|
|
|
end
|
2017-04-28 16:52:09 +00:00
|
|
|
|
2017-06-16 00:03:54 +00:00
|
|
|
it 'parses a relative path starting with /' do
|
2020-03-17 12:09:52 +00:00
|
|
|
expect(described_class.parse('/' + redirect.path + '.git')).to eq([project, project, Gitlab::GlRepository::PROJECT, redirect_route])
|
2020-03-05 00:07:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'parses a redirected project snippet repository path' do
|
2020-03-17 12:09:52 +00:00
|
|
|
expect(described_class.parse(redirect.path + "/snippets/#{project_snippet.id}.git")).to eq([project_snippet, project, Gitlab::GlRepository::SNIPPET, redirect_route])
|
2017-06-16 00:03:54 +00:00
|
|
|
end
|
|
|
|
end
|
2017-04-28 16:52:09 +00:00
|
|
|
end
|
2019-03-18 16:51:11 +00:00
|
|
|
|
2020-03-05 00:07:49 +00:00
|
|
|
it 'returns the default type for non existent paths' do
|
|
|
|
expect(described_class.parse('path/non-existent.git')).to eq([nil, nil, Gitlab::GlRepository.default_type, nil])
|
2019-03-18 16:51:11 +00:00
|
|
|
end
|
2017-04-28 16:52:09 +00:00
|
|
|
end
|
|
|
|
|
2020-06-17 15:08:36 +00:00
|
|
|
describe '.find_project' do
|
2017-06-16 00:03:54 +00:00
|
|
|
context 'when finding a project by its canonical path' do
|
|
|
|
context 'when the cases match' do
|
2020-03-17 12:09:52 +00:00
|
|
|
it 'returns the project and nil' do
|
2020-06-17 15:08:36 +00:00
|
|
|
expect(described_class.find_project(project.full_path)).to eq([project, nil])
|
2017-06-16 00:03:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the cases do not match' do
|
|
|
|
# This is slightly different than web behavior because on the web it is
|
|
|
|
# easy and safe to redirect someone to the correctly-cased URL. For git
|
|
|
|
# requests, we should accept wrongly-cased URLs because it is a pain to
|
|
|
|
# block people's git operations and force them to update remote URLs.
|
2020-03-17 12:09:52 +00:00
|
|
|
it 'returns the project and nil' do
|
2020-06-17 15:08:36 +00:00
|
|
|
expect(described_class.find_project(project.full_path.upcase)).to eq([project, nil])
|
2017-06-16 00:03:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when finding a project via a redirect' do
|
2020-03-17 12:09:52 +00:00
|
|
|
it 'returns the project and nil' do
|
2020-06-17 15:08:36 +00:00
|
|
|
expect(described_class.find_project(redirect.path)).to eq([project, redirect.path])
|
2017-06-16 00:03:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-03-05 00:07:49 +00:00
|
|
|
|
|
|
|
describe '.find_snippet' do
|
|
|
|
it 'extracts path and id from personal snippet route' do
|
2020-03-17 12:09:52 +00:00
|
|
|
expect(described_class.find_snippet("snippets/#{personal_snippet.id}")).to eq([personal_snippet, nil])
|
2020-03-05 00:07:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'extracts path and id from project snippet route' do
|
2020-03-17 12:09:52 +00:00
|
|
|
expect(described_class.find_snippet("#{project.full_path}/snippets/#{project_snippet.id}")).to eq([project_snippet, nil])
|
2020-03-05 00:07:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil for invalid snippet paths' do
|
|
|
|
aggregate_failures do
|
2020-03-17 12:09:52 +00:00
|
|
|
expect(described_class.find_snippet("snippets/#{project_snippet.id}")).to eq([nil, nil])
|
|
|
|
expect(described_class.find_snippet("#{project.full_path}/snippets/#{personal_snippet.id}")).to eq([nil, nil])
|
|
|
|
expect(described_class.find_snippet('')).to eq([nil, nil])
|
2020-03-05 00:07:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil for snippets not associated with the project' do
|
|
|
|
snippet = create(:project_snippet)
|
|
|
|
|
2020-03-17 12:09:52 +00:00
|
|
|
expect(described_class.find_snippet("#{project.full_path}/snippets/#{snippet.id}")).to eq([nil, nil])
|
2020-03-05 00:07:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when finding a project snippet via a redirect' do
|
|
|
|
it 'returns the project and true' do
|
2020-03-17 12:09:52 +00:00
|
|
|
expect(described_class.find_snippet("#{redirect.path}/snippets/#{project_snippet.id}")).to eq([project_snippet, redirect.path])
|
2020-03-05 00:07:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-03-24 15:54:22 +00:00
|
|
|
end
|