1# frozen_string_literal: true
2
3require 'spec_helper'
4
5RSpec.describe Gitlab::Checks::PushFileCountCheck do
6  let(:snippet) { create(:personal_snippet, :repository) }
7  let(:changes) { { oldrev: oldrev, newrev: newrev, ref: ref } }
8  let(:timeout) { Gitlab::GitAccess::INTERNAL_TIMEOUT }
9  let(:logger) { Gitlab::Checks::TimedLogger.new(timeout: timeout) }
10
11  subject { described_class.new(changes, repository: snippet.repository, limit: 2, logger: logger) }
12
13  describe '#validate!' do
14    using RSpec::Parameterized::TableSyntax
15
16    before do
17      allow(snippet.repository).to receive(:new_commits).and_return(
18        snippet.repository.commits_between(oldrev, newrev)
19      )
20    end
21
22    context 'initial creation' do
23      let(:oldrev) { Gitlab::Git::EMPTY_TREE_ID }
24      let(:newrev) { TestEnv::BRANCH_SHA["snippet/single-file"] }
25      let(:ref) { "refs/heads/snippet/single-file" }
26
27      it 'allows creation' do
28        expect { subject.validate! }.not_to raise_error
29      end
30    end
31
32    where(:old, :new, :valid, :message) do
33      'single-file' | 'edit-file'            | true  | nil
34      'single-file' | 'multiple-files'       | false | 'The repository can contain at most 2 file(s).'
35      'single-file' | 'no-files'             | false | 'The repository must contain at least 1 file.'
36      'edit-file'   | 'rename-and-edit-file' | true  | nil
37    end
38
39    with_them do
40      let(:oldrev) { TestEnv::BRANCH_SHA["snippet/#{old}"] }
41      let(:newrev) { TestEnv::BRANCH_SHA["snippet/#{new}"] }
42      let(:ref) { "refs/heads/snippet/#{new}" }
43
44      it "verifies" do
45        if valid
46          expect { subject.validate! }.not_to raise_error
47        else
48          expect { subject.validate! }.to raise_error(Gitlab::GitAccess::ForbiddenError, message)
49        end
50      end
51    end
52  end
53end
54