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

remote_file_validator_spec.rb « gitlab_projects « import « validators « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 27ab01c0334c8815ff0f78b8911230965c364622 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ::Import::GitlabProjects::RemoteFileValidator, :aggregate_failures, feature_category: :importers do
  let(:validated_class) do
    Class.new do
      include ActiveModel::Validations

      def self.name
        'AClass'
      end

      attr_accessor :content_type, :content_length

      def initialize(content_length:, content_type:)
        @content_type = content_type
        @content_length = content_length
      end
    end
  end

  let(:validated_object) { validated_class.new(content_length: 10.megabytes, content_type: 'application/gzip') }

  subject { described_class.new }

  before do
    stub_application_setting(max_import_remote_file_size: 100)
  end

  it 'does nothing when the object is valid' do
    subject.validate(validated_object)

    expect(validated_object.errors.full_messages).to be_empty
  end

  context 'content_length validation' do
    it 'is invalid with file too small' do
      validated_object.content_length = nil

      subject.validate(validated_object)

      expect(validated_object.errors.full_messages)
        .to include('Content length is too small (should be at least 1 B)')
    end

    it 'is invalid with file too large' do
      validated_object.content_length = 200.megabytes

      subject.validate(validated_object)

      expect(validated_object.errors.full_messages)
        .to include('Content length is too big (should be at most 100 MiB)')
    end

    context 'when max_import_remote_file_size is 0' do
      it 'does not validate file size' do
        stub_application_setting(max_import_remote_file_size: 0)

        validated_object.content_length = 200.megabytes

        subject.validate(validated_object)

        expect(validated_object.errors.full_messages).to be_empty
      end
    end
  end

  context 'content_type validation' do
    it 'only allows ALLOWED_CONTENT_TYPES as content_type' do
      described_class::ALLOWED_CONTENT_TYPES.each do |content_type|
        validated_object.content_type = content_type
        subject.validate(validated_object)

        expect(validated_object.errors.to_a).to be_empty
      end

      validated_object.content_type = 'unknown'

      subject.validate(validated_object)

      expect(validated_object.errors.full_messages)
        .to include("Content type 'unknown' not allowed. (Allowed: application/gzip, application/x-tar, application/x-gzip)")
    end
  end
end