BigW Consortium Gitlab

go_spec.rb 4.93 KB
Newer Older
1 2
require 'spec_helper'

3
describe Gitlab::Middleware::Go do
4 5 6 7 8 9 10 11 12 13 14 15 16 17
  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
18 19
      let(:current_user) { nil }

20 21 22
      shared_examples 'go-get=1' do |enabled_protocol:|
        context 'with simple 2-segment project path' do
          let!(:project) { create(:project, :private) }
23

24 25
          context 'with subpackages' do
            let(:path) { "#{project.full_path}/subpackage" }
26

27 28 29
            it 'returns the full project path' do
              expect_response_with_path(go, enabled_protocol, project.full_path)
            end
30 31
          end

32 33
          context 'without subpackages' do
            let(:path) { project.full_path }
34 35

            it 'returns the full project path' do
36
              expect_response_with_path(go, enabled_protocol, project.full_path)
37 38
            end
          end
39
        end
40

41 42 43
        context 'with a nested project path' do
          let(:group) { create(:group, :nested) }
          let!(:project) { create(:project, :public, namespace: group) }
44

45 46 47 48 49 50
          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, enabled_protocol, project.full_path)
              end
            end
51

52
            context 'when the project is private' do
53
              before do
54
                project.update_attribute(:visibility_level, Project::PRIVATE)
55 56
              end

57 58 59 60 61 62 63 64 65 66
              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, enabled_protocol, project.full_path)
                end
67 68
              end

69 70 71 72
              context 'without access to the project' do
                it 'returns the 2-segment group path' do
                  expect_response_with_path(go, enabled_protocol, group.full_path)
                end
73 74 75 76
              end
            end
          end

77 78
          context 'with subpackages' do
            let(:path) { "#{project.full_path}/subpackage" }
79

80 81 82 83 84
            it_behaves_like 'a nested project'
          end

          context 'with a subpackage that is not a valid project path' do
            let(:path) { "#{project.full_path}/---subpackage" }
85

86 87 88 89 90
            it_behaves_like 'a nested project'
          end

          context 'without subpackages' do
            let(:path) { project.full_path }
91

92 93
            it_behaves_like 'a nested project'
          end
94 95
        end

96 97 98 99 100
        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')
101

102 103 104 105 106 107 108 109
            go
          end
        end
      end

      context 'with SSH disabled' do
        before do
          stub_application_setting(enabled_git_access_protocol: 'http')
110
        end
111 112

        include_examples 'go-get=1', enabled_protocol: :http
113
      end
114

115 116 117 118
      context 'with HTTP disabled' do
        before do
          stub_application_setting(enabled_git_access_protocol: 'ssh')
        end
119

120 121
        include_examples 'go-get=1', enabled_protocol: :ssh
      end
122

123 124 125
      context 'with nothing disabled' do
        before do
          stub_application_setting(enabled_git_access_protocol: nil)
126
        end
127 128

        include_examples 'go-get=1', enabled_protocol: nil
129
      end
130 131 132 133 134 135 136 137

      context 'with nothing disabled (blank string)' do
        before do
          stub_application_setting(enabled_git_access_protocol: '')
        end

        include_examples 'go-get=1', enabled_protocol: nil
      end
138
    end
139 140 141 142 143 144 145 146 147 148 149

    def go
      env = {
        'rack.input' => '',
        'QUERY_STRING' => 'go-get=1',
        'PATH_INFO' => "/#{path}",
        'warden' => double(authenticate: current_user)
      }
      middleware.call(env)
    end

150 151 152 153 154 155 156
    def expect_response_with_path(response, protocol, path)
      repository_url = case protocol
                       when :ssh
                         "ssh://git@#{Gitlab.config.gitlab.host}/#{path}.git"
                       when :http, nil
                         "http://#{Gitlab.config.gitlab.host}/#{path}.git"
                       end
157 158
      expect(response[0]).to eq(200)
      expect(response[1]['Content-Type']).to eq('text/html')
159
      expected_body = %{<html><head><meta name="go-import" content="#{Gitlab.config.gitlab.host}/#{path} git #{repository_url}" /></head></html>}
160 161
      expect(response[2].body).to eq([expected_body])
    end
162 163
  end
end