2016-03-12 16:05:23 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe Gitlab::Middleware::Go do
|
2016-03-12 16:05:23 -05:00
|
|
|
let(:app) { double(:app) }
|
|
|
|
let(:middleware) { described_class.new(app) }
|
|
|
|
|
|
|
|
describe '#call' do
|
|
|
|
describe 'when go-get=0' do
|
|
|
|
it 'skips go-import generation' do
|
|
|
|
env = { 'rack.input' => '',
|
|
|
|
'QUERY_STRING' => 'go-get=0' }
|
|
|
|
expect(app).to receive(:call).with(env).and_return('no-go')
|
|
|
|
middleware.call(env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when go-get=1' do
|
2017-03-07 20:40:19 -05:00
|
|
|
let(:current_user) { nil }
|
|
|
|
|
|
|
|
context 'with simple 2-segment project path' do
|
2017-08-02 15:55:11 -04:00
|
|
|
let!(:project) { create(:project, :private) }
|
2017-03-07 20:40:19 -05:00
|
|
|
|
|
|
|
context 'with subpackages' do
|
|
|
|
let(:path) { "#{project.full_path}/subpackage" }
|
|
|
|
|
|
|
|
it 'returns the full project path' do
|
|
|
|
expect_response_with_path(go, project.full_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without subpackages' do
|
|
|
|
let(:path) { project.full_path }
|
|
|
|
|
|
|
|
it 'returns the full project path' do
|
|
|
|
expect_response_with_path(go, project.full_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a nested project path' do
|
|
|
|
let(:group) { create(:group, :nested) }
|
2017-08-02 15:55:11 -04:00
|
|
|
let!(:project) { create(:project, :public, namespace: group) }
|
2017-03-07 20:40:19 -05:00
|
|
|
|
|
|
|
shared_examples 'a nested project' do
|
|
|
|
context 'when the project is public' do
|
|
|
|
it 'returns the full project path' do
|
|
|
|
expect_response_with_path(go, project.full_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the project is private' do
|
|
|
|
before do
|
|
|
|
project.update_attribute(:visibility_level, Project::PRIVATE)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with access to the project' do
|
|
|
|
let(:current_user) { project.creator }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.team.add_master(current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the full project path' do
|
|
|
|
expect_response_with_path(go, project.full_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without access to the project' do
|
|
|
|
it 'returns the 2-segment group path' do
|
|
|
|
expect_response_with_path(go, group.full_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with subpackages' do
|
|
|
|
let(:path) { "#{project.full_path}/subpackage" }
|
|
|
|
|
|
|
|
it_behaves_like 'a nested project'
|
|
|
|
end
|
|
|
|
|
2017-06-28 21:22:22 -04:00
|
|
|
context 'with a subpackage that is not a valid project path' do
|
|
|
|
let(:path) { "#{project.full_path}/---subpackage" }
|
|
|
|
|
|
|
|
it_behaves_like 'a nested project'
|
|
|
|
end
|
|
|
|
|
2017-03-07 20:40:19 -05:00
|
|
|
context 'without subpackages' do
|
|
|
|
let(:path) { project.full_path }
|
|
|
|
|
|
|
|
it_behaves_like 'a nested project'
|
|
|
|
end
|
2016-03-12 16:05:23 -05:00
|
|
|
end
|
2017-06-28 21:22:22 -04:00
|
|
|
|
|
|
|
context 'with a bogus path' do
|
|
|
|
let(:path) { "http:;url=http://www.example.com'http-equiv='refresh'x='?go-get=1" }
|
|
|
|
|
|
|
|
it 'skips go-import generation' do
|
|
|
|
expect(app).to receive(:call).and_return('no-go')
|
|
|
|
|
|
|
|
go
|
|
|
|
end
|
|
|
|
end
|
2016-03-12 16:05:23 -05:00
|
|
|
end
|
2017-03-07 20:40:19 -05:00
|
|
|
|
|
|
|
def go
|
|
|
|
env = {
|
|
|
|
'rack.input' => '',
|
|
|
|
'QUERY_STRING' => 'go-get=1',
|
|
|
|
'PATH_INFO' => "/#{path}",
|
|
|
|
'warden' => double(authenticate: current_user)
|
|
|
|
}
|
|
|
|
middleware.call(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
def expect_response_with_path(response, path)
|
|
|
|
expect(response[0]).to eq(200)
|
|
|
|
expect(response[1]['Content-Type']).to eq('text/html')
|
2017-06-28 21:22:22 -04:00
|
|
|
expected_body = %{<html><head><meta name="go-import" content="#{Gitlab.config.gitlab.host}/#{path} git http://#{Gitlab.config.gitlab.host}/#{path}.git" /></head></html>}
|
2017-03-07 20:40:19 -05:00
|
|
|
expect(response[2].body).to eq([expected_body])
|
|
|
|
end
|
2016-03-12 16:05:23 -05:00
|
|
|
end
|
|
|
|
end
|