summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/checks/changes_access_spec.rb
blob: 552afcdb1806ffe99210958a84c322679cda36cd (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Checks::ChangesAccess, feature_category: :source_code_management do
  include_context 'changes access checks context'

  subject { changes_access }

  describe '#validate!' do
    before do
      allow(project).to receive(:lfs_enabled?).and_return(true)
    end

    context 'without failed checks' do
      it "doesn't raise an error" do
        expect { subject.validate! }.not_to raise_error
      end

      it 'calls lfs checks' do
        expect_next_instance_of(Gitlab::Checks::LfsCheck) do |instance|
          expect(instance).to receive(:validate!)
        end

        subject.validate!
      end
    end

    context 'when time limit was reached' do
      it 'raises a TimeoutError' do
        logger = Gitlab::Checks::TimedLogger.new(start_time: timeout.ago, timeout: timeout)
        access = described_class.new(changes,
                                     project: project,
                                     user_access: user_access,
                                     protocol: protocol,
                                     logger: logger)

        expect { access.validate! }.to raise_error(Gitlab::Checks::TimedLogger::TimeoutError)
      end
    end
  end

  describe '#commits' do
    it 'calls #new_commits' do
      expect(project.repository).to receive(:new_commits).and_call_original

      expect(subject.commits).to match_array([])
    end

    context 'when change is for notes ref' do
      let(:changes) do
        [{ oldrev: oldrev, newrev: newrev, ref: 'refs/notes/commit' }]
      end

      it 'does not return any commits' do
        expect(subject.commits).to match_array([])
      end
    end

    context 'when changes contain empty revisions' do
      let(:expected_commit) { instance_double(Commit) }

      shared_examples 'returns only commits with non empty revisions' do
        specify do
          expect(project.repository)
            .to receive(:new_commits)
            .with([newrev]) { [expected_commit] }
          expect(subject.commits).to match_array([expected_commit])
        end
      end

      context 'with oldrev' do
        let(:changes) { [{ oldrev: oldrev, newrev: newrev }, { newrev: '' }, { newrev: Gitlab::Git::BLANK_SHA }] }

        it_behaves_like 'returns only commits with non empty revisions'
      end

      context 'without oldrev' do
        let(:changes) { [{ newrev: newrev }, { newrev: '' }, { newrev: Gitlab::Git::BLANK_SHA }] }

        it_behaves_like 'returns only commits with non empty revisions'
      end
    end
  end

  describe '#commits_for' do
    let(:new_commits) { [] }
    let(:expected_commits) { [] }
    let(:oldrev) { Gitlab::Git::BLANK_SHA }

    shared_examples 'a listing of new commits' do
      it 'returns expected commits' do
        expect(subject).to receive(:commits).and_return(new_commits)

        expect(subject.commits_for(oldrev, newrev)).to eq(expected_commits)
      end
    end

    context 'with no commits' do
      it_behaves_like 'a listing of new commits'
    end

    context 'with unrelated commits' do
      let(:new_commits) { [create_commit('1234', %w[1111 2222])] }

      it_behaves_like 'a listing of new commits'
    end

    context 'with single related commit' do
      let(:new_commits) { [create_commit(newrev, %w[1111 2222])] }
      let(:expected_commits) { new_commits }

      it_behaves_like 'a listing of new commits'
    end

    context 'with single related and unrelated commit' do
      let(:new_commits) do
        [
          create_commit(newrev, %w[1111 2222]),
          create_commit('abcd', %w[1111 2222])
        ]
      end

      let(:expected_commits) do
        [create_commit(newrev, %w[1111 2222])]
      end

      it_behaves_like 'a listing of new commits'
    end

    context 'with multiple related commits' do
      let(:new_commits) do
        [
          create_commit(newrev, %w[1111]),
          create_commit('1111', %w[2222]),
          create_commit('abcd', [])
        ]
      end

      let(:expected_commits) do
        [
          create_commit(newrev, %w[1111]),
          create_commit('1111', %w[2222])
        ]
      end

      it_behaves_like 'a listing of new commits'
    end

    context 'with merge commits' do
      let(:new_commits) do
        [
          create_commit(newrev, %w[1111 2222 3333]),
          create_commit('1111', []),
          create_commit('3333', %w[4444]),
          create_commit('4444', [])
        ]
      end

      let(:expected_commits) do
        [
          create_commit(newrev, %w[1111 2222 3333]),
          create_commit('1111', []),
          create_commit('3333', %w[4444]),
          create_commit('4444', [])
        ]
      end

      it_behaves_like 'a listing of new commits'
    end

    context 'with criss-cross merges' do
      let(:new_commits) do
        [
          create_commit(newrev, %w[a1 b1]),
          create_commit('a1', %w[a2 b2]),
          create_commit('a2', %w[a3 b3]),
          create_commit('a3', %w[c]),
          create_commit('b1', %w[b2 a2]),
          create_commit('b2', %w[b3 a3]),
          create_commit('b3', %w[c]),
          create_commit('c', [])
        ]
      end

      let(:expected_commits) do
        [
          create_commit(newrev, %w[a1 b1]),
          create_commit('a1', %w[a2 b2]),
          create_commit('b1', %w[b2 a2]),
          create_commit('a2', %w[a3 b3]),
          create_commit('b2', %w[b3 a3]),
          create_commit('a3', %w[c]),
          create_commit('b3', %w[c]),
          create_commit('c', [])
        ]
      end

      it_behaves_like 'a listing of new commits'
    end

    context 'with over-push' do
      let(:newrev) { '1' }
      let(:oldrev) { '3' }

      # `#new_commits` returns too many commits, where some commits are not
      # part of the current change.
      let(:new_commits) do
        [
          create_commit('1', %w[2]),
          create_commit('2', %w[3]),
          create_commit('3', %w[4]),
          create_commit('4', %w[])
        ]
      end

      let(:expected_commits) do
        [
          create_commit('1', %w[2]),
          create_commit('2', %w[3])
        ]
      end

      it_behaves_like 'a listing of new commits'
    end
  end

  describe '#single_change_accesses' do
    let(:commits_for) { {} }
    let(:expected_accesses) { [] }

    shared_examples '#single_change_access' do
      before do
        commits_for.each do |oldrev, newrev, commits|
          expect(subject)
            .to receive(:commits_for)
            .with(oldrev, newrev)
            .and_return(commits)
        end
      end

      it 'returns an array of SingleChangeAccess' do
        # Commits are wrapped in a Gitlab::Lazy and thus need to be resolved
        # first such that we can directly compare types.
        actual_accesses = subject.single_change_accesses
          .each { |access| access.instance_variable_set(:@commits, access.commits.to_a) }

        expect(actual_accesses).to match_array(expected_accesses)
      end
    end

    context 'with no changes' do
      let(:changes) { [] }

      it_behaves_like '#single_change_access'
    end

    context 'with a single change and no new commits' do
      let(:commits_for) do
        [
          ['old', 'new', []]
        ]
      end

      let(:changes) do
        [
          { oldrev: 'old', newrev: 'new', ref: 'refs/heads/branch' }
        ]
      end

      let(:expected_accesses) do
        [
          have_attributes(oldrev: 'old', newrev: 'new', ref: 'refs/heads/branch', commits: [])
        ]
      end

      it_behaves_like '#single_change_access'
    end

    context 'with a single change and new commits' do
      let(:commits_for) do
        [
          ['old', 'new', [create_commit('new', [])]]
        ]
      end

      let(:changes) do
        [
          { oldrev: 'old', newrev: 'new', ref: 'refs/heads/branch' }
        ]
      end

      let(:expected_accesses) do
        [
          have_attributes(oldrev: 'old', newrev: 'new', ref: 'refs/heads/branch', commits: [create_commit('new', [])])
        ]
      end

      it_behaves_like '#single_change_access'
    end

    context 'with multiple changes' do
      let(:commits_for) do
        [
          [nil, 'a', [create_commit('a', [])]],
          ['a', 'c', [create_commit('c', [])]],
          [nil, 'd', []]
        ]
      end

      let(:changes) do
        [
          { newrev: 'a', ref: 'refs/heads/a' },
          { oldrev: 'b', ref: 'refs/heads/b' },
          { oldrev: 'a', newrev: 'c', ref: 'refs/heads/c' },
          { newrev: 'd', ref: 'refs/heads/d' }
        ]
      end

      let(:expected_accesses) do
        [
          have_attributes(newrev: 'a', ref: 'refs/heads/a', commits: [create_commit('a', [])]),
          have_attributes(oldrev: 'b', ref: 'refs/heads/b', commits: []),
          have_attributes(oldrev: 'a', newrev: 'c', ref: 'refs/heads/c', commits: [create_commit('c', [])]),
          have_attributes(newrev: 'd', ref: 'refs/heads/d', commits: [])
        ]
      end

      it_behaves_like '#single_change_access'
    end
  end

  def create_commit(id, parent_ids)
    Gitlab::Git::Commit.new(project.repository, {
      id: id,
      parent_ids: parent_ids
    })
  end
end