summaryrefslogtreecommitdiff
path: root/spec/validators
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2022-03-18 20:02:30 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2022-03-18 20:02:30 +0000
commit41fe97390ceddf945f3d967b8fdb3de4c66b7dea (patch)
tree9c8d89a8624828992f06d892cd2f43818ff5dcc8 /spec/validators
parent0804d2dc31052fb45a1efecedc8e06ce9bc32862 (diff)
downloadgitlab-ce-41fe97390ceddf945f3d967b8fdb3de4c66b7dea.tar.gz
Add latest changes from gitlab-org/gitlab@14-9-stable-eev14.9.0-rc42
Diffstat (limited to 'spec/validators')
-rw-r--r--spec/validators/array_members_validator_spec.rb1
-rw-r--r--spec/validators/color_validator_spec.rb24
-rw-r--r--spec/validators/cron_validator_spec.rb2
-rw-r--r--spec/validators/future_date_validator_spec.rb1
-rw-r--r--spec/validators/import/gitlab_projects/remote_file_validator_spec.rb70
5 files changed, 98 insertions, 0 deletions
diff --git a/spec/validators/array_members_validator_spec.rb b/spec/validators/array_members_validator_spec.rb
index c6960925487..0e3ca4b5b7b 100644
--- a/spec/validators/array_members_validator_spec.rb
+++ b/spec/validators/array_members_validator_spec.rb
@@ -12,6 +12,7 @@ RSpec.describe ArrayMembersValidator do
include ActiveModel::Model
include ActiveModel::Validations
attr_accessor :children
+
validates :children, array_members: { member_class: child_class }
end
end
diff --git a/spec/validators/color_validator_spec.rb b/spec/validators/color_validator_spec.rb
index bd77b3df182..9c1339caffb 100644
--- a/spec/validators/color_validator_spec.rb
+++ b/spec/validators/color_validator_spec.rb
@@ -10,6 +10,7 @@ RSpec.describe ColorValidator do
include ActiveModel::Model
include ActiveModel::Validations
attr_accessor :color
+
validates :color, color: true
end.new
end
@@ -22,7 +23,12 @@ RSpec.describe ColorValidator do
'#ffff' | false
'#000111222' | false
'invalid' | false
+ 'red' | false
'000' | false
+ nil | true # use presence to validate non-nil
+ '' | false
+ Time.current | false
+ ::Gitlab::Color.of(:red) | true
end
with_them do
@@ -40,4 +46,22 @@ RSpec.describe ColorValidator do
Timeout.timeout(5.seconds) { subject.valid? }
end.not_to raise_error
end
+
+ context 'when color must be present' do
+ subject do
+ Class.new do
+ include ActiveModel::Model
+ include ActiveModel::Validations
+ attr_accessor :color
+
+ validates :color, color: true, presence: true
+ end.new
+ end
+
+ it 'rejects nil' do
+ subject.color = nil
+
+ expect(subject).not_to be_valid
+ end
+ end
end
diff --git a/spec/validators/cron_validator_spec.rb b/spec/validators/cron_validator_spec.rb
index dff3b506b89..bd7fe242957 100644
--- a/spec/validators/cron_validator_spec.rb
+++ b/spec/validators/cron_validator_spec.rb
@@ -8,6 +8,7 @@ RSpec.describe CronValidator do
include ActiveModel::Model
include ActiveModel::Validations
attr_accessor :cron
+
validates :cron, cron: true
def cron_timezone
@@ -34,6 +35,7 @@ RSpec.describe CronValidator do
include ActiveModel::Model
include ActiveModel::Validations
attr_accessor :cron_partytime
+
validates :cron_partytime, cron: true
end.new
end
diff --git a/spec/validators/future_date_validator_spec.rb b/spec/validators/future_date_validator_spec.rb
index 6814ba7c820..7af3d473bd9 100644
--- a/spec/validators/future_date_validator_spec.rb
+++ b/spec/validators/future_date_validator_spec.rb
@@ -8,6 +8,7 @@ RSpec.describe FutureDateValidator do
include ActiveModel::Model
include ActiveModel::Validations
attr_accessor :expires_at
+
validates :expires_at, future_date: true
end.new
end
diff --git a/spec/validators/import/gitlab_projects/remote_file_validator_spec.rb b/spec/validators/import/gitlab_projects/remote_file_validator_spec.rb
new file mode 100644
index 00000000000..428e0279821
--- /dev/null
+++ b/spec/validators/import/gitlab_projects/remote_file_validator_spec.rb
@@ -0,0 +1,70 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe ::Import::GitlabProjects::RemoteFileValidator, :aggregate_failures 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: 1.gigabytes, content_type: 'application/gzip') }
+
+ subject { described_class.new }
+
+ it 'does nothing when the oject 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 Byte)')
+ end
+
+ it 'is invalid with file too large' do
+ validated_object.content_length = (described_class::FILE_SIZE_LIMIT + 1).gigabytes
+
+ subject.validate(validated_object)
+
+ expect(validated_object.errors.full_messages)
+ .to include('Content length is too big (should be at most 10 GB)')
+ 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