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

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

126 lines
5.2 KiB
Ruby
Raw Permalink Normal View History

2019-12-04 20:38:33 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe ::Gitlab::RepoPath do
2020-04-08 14:13:33 +05:30
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) }
let_it_be(:redirect_route) { 'foo/bar/baz' }
let_it_be(:redirect) { project.route.create_redirect(redirect_route) }
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
describe '.parse' do
2017-09-10 17:25:29 +05:30
context 'a repository storage path' do
2021-09-30 23:02:18 +05:30
it 'parses a full project repository path' do
2020-04-08 14:13:33 +05:30
expect(described_class.parse(project.repository.full_path)).to eq([project, project, Gitlab::GlRepository::PROJECT, nil])
end
2021-09-30 23:02:18 +05:30
it 'parses a full project wiki repository path' do
2021-01-03 14:25:43 +05:30
expect(described_class.parse(project.wiki.repository.full_path)).to eq([project.wiki, project, Gitlab::GlRepository::WIKI, nil])
2020-04-08 14:13:33 +05:30
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-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
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-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
context 'a relative path' do
it 'parses a relative repository path' do
2020-04-08 14:13:33 +05:30
expect(described_class.parse(project.full_path + '.git')).to eq([project, project, Gitlab::GlRepository::PROJECT, nil])
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
it 'parses a relative wiki path' do
2021-01-03 14:25:43 +05:30
expect(described_class.parse(project.full_path + '.wiki.git')).to eq([project.wiki, project, Gitlab::GlRepository::WIKI, nil])
2017-09-10 17:25:29 +05:30
end
it 'parses a relative path starting with /' do
2020-04-08 14:13:33 +05:30
expect(described_class.parse('/' + project.full_path + '.git')).to eq([project, project, Gitlab::GlRepository::PROJECT, nil])
2017-09-10 17:25:29 +05:30
end
context 'of a redirected project' do
it 'parses a relative repository path' do
2020-04-08 14:13:33 +05:30
expect(described_class.parse(redirect.path + '.git')).to eq([project, project, Gitlab::GlRepository::PROJECT, redirect_route])
2017-09-10 17:25:29 +05:30
end
it 'parses a relative wiki path' do
2021-09-30 23:02:18 +05:30
expect(described_class.parse(redirect.path + '.wiki.git')).to eq([project.wiki, project, Gitlab::GlRepository::WIKI, "#{redirect_route}.wiki"])
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
it 'parses a relative path starting with /' do
2020-04-08 14:13:33 +05:30
expect(described_class.parse('/' + redirect.path + '.git')).to eq([project, project, Gitlab::GlRepository::PROJECT, redirect_route])
end
it 'parses a redirected project snippet repository path' do
2021-09-30 23:02:18 +05:30
expect(described_class.parse(redirect.path + "/snippets/#{project_snippet.id}.git")).to eq([project_snippet, project, Gitlab::GlRepository::SNIPPET, "#{redirect_route}/snippets/#{project_snippet.id}"])
2017-09-10 17:25:29 +05:30
end
end
2017-08-17 22:00:37 +05:30
end
2019-07-07 11:18:12 +05:30
2020-04-08 14:13:33 +05:30
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-07-07 11:18:12 +05:30
end
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
describe '.find_project' do
context 'when finding a project by its canonical path' do
context 'when the cases match' do
2021-09-30 23:02:18 +05:30
it 'returns the project' do
expect(described_class.find_project(project.full_path)).to eq(project)
2017-09-10 17:25:29 +05:30
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.
2021-09-30 23:02:18 +05:30
it 'returns the project' do
expect(described_class.find_project(project.full_path.upcase)).to eq(project)
2017-09-10 17:25:29 +05:30
end
end
end
context 'when finding a project via a redirect' do
2021-09-30 23:02:18 +05:30
it 'returns the project' do
expect(described_class.find_project(redirect.path)).to eq(project)
2020-04-08 14:13:33 +05:30
end
end
end
describe '.find_snippet' do
it 'extracts path and id from personal snippet route' do
2021-09-30 23:02:18 +05:30
expect(described_class.find_snippet("snippets/#{personal_snippet.id}")).to eq(personal_snippet)
2020-04-08 14:13:33 +05:30
end
it 'extracts path and id from project snippet route' do
2021-09-30 23:02:18 +05:30
expect(described_class.find_snippet("#{project.full_path}/snippets/#{project_snippet.id}")).to eq(project_snippet)
2020-04-08 14:13:33 +05:30
end
it 'returns nil for invalid snippet paths' do
aggregate_failures do
2021-09-30 23:02:18 +05:30
expect(described_class.find_snippet("snippets/#{project_snippet.id}")).to be_nil
expect(described_class.find_snippet("#{project.full_path}/snippets/#{personal_snippet.id}")).to be_nil
expect(described_class.find_snippet('')).to be_nil
2020-04-08 14:13:33 +05:30
end
end
it 'returns nil for snippets not associated with the project' do
snippet = create(:project_snippet)
2021-09-30 23:02:18 +05:30
expect(described_class.find_snippet("#{project.full_path}/snippets/#{snippet.id}")).to be_nil
2020-04-08 14:13:33 +05:30
end
context 'when finding a project snippet via a redirect' do
2021-09-30 23:02:18 +05:30
it 'returns the project snippet' do
expect(described_class.find_snippet("#{redirect.path}/snippets/#{project_snippet.id}")).to eq(project_snippet)
2017-09-10 17:25:29 +05:30
end
end
end
2017-08-17 22:00:37 +05:30
end