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

decompressed_archive_size_validator_spec.rb « import_export « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: efb271086a017230c8f0c9b66461fb0f6f562ed0 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::ImportExport::DecompressedArchiveSizeValidator do
  let_it_be(:filepath) { File.join(Dir.tmpdir, 'decompressed_archive_size_validator_spec.gz') }

  before(:all) do
    create_compressed_file
  end

  after(:all) do
    FileUtils.rm(filepath)
  end

  subject { described_class.new(archive_path: filepath, max_bytes: max_bytes) }

  describe '#valid?' do
    let(:max_bytes) { 1 }

    context 'when file does not exceed allowed decompressed size' do
      let(:max_bytes) { 20 }

      it 'returns true' do
        expect(subject.valid?).to eq(true)
      end
    end

    context 'when file exceeds allowed decompressed size' do
      it 'returns false' do
        expect(subject.valid?).to eq(false)
      end
    end

    context 'when something goes wrong during decompression' do
      before do
        allow(subject.archive_file).to receive(:eof?).and_raise(StandardError)
      end

      it 'logs and tracks raised exception' do
        expect(Gitlab::ErrorTracking).to receive(:track_exception).with(instance_of(StandardError))
        expect(Gitlab::Import::Logger).to receive(:info).with(hash_including(message: 'Decompressed archive size validation failed.'))

        subject.valid?
      end

      it 'returns false' do
        expect(subject.valid?).to eq(false)
      end
    end
  end

  def create_compressed_file
    Zlib::GzipWriter.open(filepath) do |gz|
      gz.write('Hello World!')
    end
  end
end