Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git_access_project_spec.rb « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ad593cbf005c1c2104fabd461373284709491b1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::GitAccessProject do
  let_it_be(:user) { create(:user) }
  let_it_be(:project) { create(:project, :repository) }

  let(:container) { project }
  let(:actor) { user }
  let(:project_path) { project.path }
  let(:namespace_path) { project&.namespace&.path }
  let(:repository_path) { "#{namespace_path}/#{project_path}.git" }
  let(:protocol) { 'ssh' }
  let(:authentication_abilities) { %i[read_project download_code push_code] }
  let(:changes) { Gitlab::GitAccess::ANY }
  let(:push_access_check) { access.check('git-receive-pack', changes) }
  let(:pull_access_check) { access.check('git-upload-pack', changes) }
  let(:access) do
    described_class.new(actor, container, protocol,
                        authentication_abilities: authentication_abilities,
                        repository_path: repository_path)
  end

  describe '#check_namespace!' do
    context 'when namespace is nil' do
      let(:namespace_path) { nil }

      it 'does not allow push and pull access' do
        aggregate_failures do
          expect { push_access_check }.to raise_namespace_not_found
          expect { pull_access_check }.to raise_namespace_not_found
        end
      end
    end
  end

  describe '#check_project_accessibility!' do
    context 'when the project is nil' do
      let(:container) { nil }
      let(:project_path) { "new-project" }

      context 'when user is allowed to create project in namespace' do
        let(:namespace_path) { user.namespace.path }

        it 'blocks pull access with "not found"' do
          expect { pull_access_check }.to raise_not_found
        end

        it 'allows push access' do
          expect { push_access_check }.not_to raise_error
        end
      end

      context 'when user is not allowed to create project in namespace' do
        let(:user2) { create(:user) }
        let(:namespace_path) { user2.namespace.path }

        it 'blocks push and pull with "not found"' do
          aggregate_failures do
            expect { pull_access_check }.to raise_not_found
            expect { push_access_check }.to raise_not_found
          end
        end
      end
    end
  end

  describe '#ensure_project_on_push!' do
    before do
      allow(access).to receive(:changes).and_return(changes)
    end

    shared_examples 'no project is created' do
      let(:raise_specific_error) { raise_not_found }
      let(:action) { push_access_check }

      it 'does not create a new project' do
        expect { action }
          .to raise_specific_error
          .and change { Project.count }.by(0)
      end
    end

    context 'when push' do
      let(:cmd) { 'git-receive-pack' }

      context 'when project does not exist' do
        let(:project_path) { "nonexistent" }
        let(:container) { nil }

        context 'when changes is _any' do
          let(:changes) { Gitlab::GitAccess::ANY }

          context 'when authentication abilities include push code' do
            let(:authentication_abilities) { [:push_code] }

            context 'when user can create project in namespace' do
              let(:namespace_path) { user.namespace.path }

              it 'creates a new project in the correct namespace' do
                expect { push_access_check }
                  .to change { Project.count }.by(1)
                  .and change { Project.where(namespace: user.namespace, name: project_path).count }.by(1)
              end
            end

            context 'when namespace is blank' do
              let(:repository_path) { 'project.git' }

              it_behaves_like 'no project is created' do
                let(:raise_specific_error) { raise_namespace_not_found }
              end
            end

            context 'when namespace does not exist' do
              let(:namespace_path) { 'unknown' }

              it_behaves_like 'no project is created'
            end

            context 'when user cannot create project in namespace' do
              let(:user2) { create(:user) }
              let(:namespace_path) { user2.namespace.path }

              it_behaves_like 'no project is created'
            end
          end

          context 'when authentication abilities do not include push code' do
            let(:authentication_abilities) { [] }

            context 'when user can create project in namespace' do
              let(:namespace_path) { user.namespace.path }

              it_behaves_like 'no project is created' do
                let(:raise_specific_error) { raise_forbidden }
              end
            end
          end
        end

        context 'when check contains actual changes' do
          let(:changes) { "#{Gitlab::Git::BLANK_SHA} 570e7b2abdd848b95f2f578043fc23bd6f6fd24d refs/heads/new_branch" }

          it_behaves_like 'no project is created'
        end
      end

      context 'when project exists' do
        let(:changes) { Gitlab::GitAccess::ANY }
        let!(:container) { project }

        it_behaves_like 'no project is created'
      end

      context 'when deploy key is used' do
        let(:key) { create(:deploy_key, user: user) }
        let(:actor) { key }
        let(:project_path) { "nonexistent" }
        let(:container) { nil }
        let(:namespace_path) { user.namespace.path }
        let(:changes) { Gitlab::GitAccess::ANY }

        it_behaves_like 'no project is created'
      end
    end

    context 'when pull' do
      let(:cmd) { 'git-upload-pack' }
      let(:changes) { Gitlab::GitAccess::ANY }

      context 'when project does not exist' do
        let(:project_path) { "new-project" }
        let(:namespace_path) { user.namespace.path }
        let(:container) { nil }

        it_behaves_like 'no project is created' do
          let(:action)  { pull_access_check }
        end
      end
    end
  end

  def raise_not_found
    raise_error(Gitlab::GitAccess::NotFoundError, Gitlab::GitAccess::ERROR_MESSAGES[:project_not_found])
  end

  def raise_forbidden
    raise_error(Gitlab::GitAccess::ForbiddenError)
  end

  def raise_namespace_not_found
    raise_error(Gitlab::GitAccess::NotFoundError, described_class::ERROR_MESSAGES[:namespace_not_found])
  end
end