summaryrefslogtreecommitdiff
path: root/spec/services/snippets/repository_validation_service_spec.rb
blob: 8166ce144e179a7c6b98f08942f0f5c269541518 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Snippets::RepositoryValidationService do
  describe '#execute' do
    let_it_be(:user)    { create(:user) }
    let_it_be(:snippet) { create(:personal_snippet, :empty_repo, author: user) }

    let(:repository)    { snippet.repository }
    let(:service)       { described_class.new(user, snippet) }

    subject { service.execute }

    before do
      allow(repository).to receive(:branch_count).and_return(1)
      allow(repository).to receive(:ls_files).and_return(['foo'])
      allow(repository).to receive(:branch_names).and_return(['master'])
    end

    it 'returns error when the repository has more than one branch' do
      allow(repository).to receive(:branch_count).and_return(2)

      expect(subject).to be_error
      expect(subject.message).to match /Repository has more than one branch/
    end

    it 'returns error when existing branch name is not the default one' do
      allow(repository).to receive(:branch_names).and_return(['foo'])

      expect(subject).to be_error
      expect(subject.message).to match /Repository has an invalid default branch name/
    end

    it 'returns error when the repository has tags' do
      allow(repository).to receive(:tag_count).and_return(1)

      expect(subject).to be_error
      expect(subject.message).to match /Repository has tags/
    end

    it 'returns error when the repository has more file than the limit' do
      limit = Snippet.max_file_limit + 1
      files = Array.new(limit) { FFaker::Filesystem.file_name }
      allow(repository).to receive(:ls_files).and_return(files)

      expect(subject).to be_error
      expect(subject.message).to match /Repository files count over the limit/
    end

    it 'returns error when the repository has no files' do
      allow(repository).to receive(:ls_files).and_return([])

      expect(subject).to be_error
      expect(subject.message).to match /Repository must contain at least 1 file/
    end

    it 'returns error when the repository size is over the limit' do
      expect_next_instance_of(Gitlab::RepositorySizeChecker) do |checker|
        expect(checker).to receive(:above_size_limit?).and_return(true)
      end

      expect(subject).to be_error
      expect(subject.message).to match /Repository size is above the limit/
    end

    it 'returns success when no validation errors are raised' do
      expect(subject).to be_success
    end
  end
end