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

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2023-11-08 09:07:06 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2023-11-08 09:07:06 +0300
commite808a772e795130cb3c7c1cc9673a1205f4ccade (patch)
treeb3e05491fc241038b3f7672ad8b9d8742a0e350c /spec/validators
parentefd6f06bfa01dc58f5b5dd704fde890539d8000b (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/validators')
-rw-r--r--spec/validators/ip_cidr_array_validator_spec.rb45
-rw-r--r--spec/validators/ip_cidr_validator_spec.rb45
2 files changed, 90 insertions, 0 deletions
diff --git a/spec/validators/ip_cidr_array_validator_spec.rb b/spec/validators/ip_cidr_array_validator_spec.rb
new file mode 100644
index 00000000000..4ea46d714c2
--- /dev/null
+++ b/spec/validators/ip_cidr_array_validator_spec.rb
@@ -0,0 +1,45 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe IpCidrArrayValidator, feature_category: :shared do
+ let(:model) do
+ Class.new do
+ include ActiveModel::Model
+ include ActiveModel::Validations
+
+ attr_accessor :cidr_array
+ alias_method :cidr_array_before_type_cast, :cidr_array
+
+ validates :cidr_array, ip_cidr_array: true
+ end.new
+ end
+
+ using RSpec::Parameterized::TableSyntax
+
+ # noinspection RubyMismatchedArgumentType - RubyMine is resolving `#|` from Array, instead of Rspec::Parameterized
+ where(:cidr_array, :validity, :errors) do
+ # rubocop:disable Layout/LineLength -- The RSpec table syntax often requires long lines for errors
+ nil | false | { cidr_array: ["must be an array of CIDR values"] }
+ '' | false | { cidr_array: ["must be an array of CIDR values"] }
+ ['172.0.0.1/256'] | false | { cidr_array: ["IP '172.0.0.1/256' is not a valid CIDR: Invalid netmask 256"] }
+ %w[172.0.0.1/24 invalid-CIDR] | false | { cidr_array: ["IP 'invalid-CIDR' is not a valid CIDR: IP should be followed by a slash followed by an integer subnet mask (for example: '192.168.1.0/24')"] }
+ %w[172.0.0.1/256 invalid-CIDR] | false | { cidr_array: ["IP '172.0.0.1/256' is not a valid CIDR: Invalid netmask 256", "IP 'invalid-CIDR' is not a valid CIDR: IP should be followed by a slash followed by an integer subnet mask (for example: '192.168.1.0/24')"] }
+ ['172.0.0.1/24', nil] | true | {}
+ %w[172.0.0.1/24 2001:db8::8:800:200c:417a/128] | true | {}
+ [] | true | {}
+ [nil] | true | {}
+ [''] | true | {}
+ # rubocop:enable Layout/LineLength
+ end
+
+ with_them do
+ before do
+ model.cidr_array = cidr_array
+ model.validate
+ end
+
+ it { expect(model.valid?).to eq(validity) }
+ it { expect(model.errors.messages).to eq(errors) }
+ end
+end
diff --git a/spec/validators/ip_cidr_validator_spec.rb b/spec/validators/ip_cidr_validator_spec.rb
new file mode 100644
index 00000000000..213991d9f4f
--- /dev/null
+++ b/spec/validators/ip_cidr_validator_spec.rb
@@ -0,0 +1,45 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe IpCidrValidator, feature_category: :shared do
+ let(:model) do
+ Class.new do
+ include ActiveModel::Model
+ include ActiveModel::Validations
+
+ attr_accessor :cidr
+ alias_method :cidr_before_type_cast, :cidr
+
+ validates :cidr, ip_cidr: true
+ end.new
+ end
+
+ using RSpec::Parameterized::TableSyntax
+
+ where(:cidr, :validity, :errors) do
+ # rubocop:disable Layout/LineLength -- The RSpec table syntax often requires long lines for errors'
+ 'invalid-CIDR' | false | { cidr: ["IP 'invalid-CIDR' is not a valid CIDR: IP should be followed by a slash followed by an integer subnet mask (for example: '192.168.1.0/24')"] }
+ '172.0.0.1|256' | false | { cidr: ["IP '172.0.0.1|256' is not a valid CIDR: IP should be followed by a slash followed by an integer subnet mask (for example: '192.168.1.0/24')"] }
+ '172.0.0.1' | false | { cidr: ["IP '172.0.0.1' is not a valid CIDR: IP should be followed by a slash followed by an integer subnet mask (for example: '192.168.1.0/24')"] }
+ '172.0.0.1/2/12' | false | { cidr: ["IP '172.0.0.1/2/12' is not a valid CIDR: IP should be followed by a slash followed by an integer subnet mask (for example: '192.168.1.0/24')"] }
+ '172.0.0.1/256' | false | { cidr: ["IP '172.0.0.1/256' is not a valid CIDR: Invalid netmask 256"] }
+ '2001:db8::8:800:200c:417a/129' | false | { cidr: ["IP '2001:db8::8:800:200c:417a/129' is not a valid CIDR: Prefix must be in range 0..128, got: 129"] }
+ '2001:db8::8:800:200c:417a' | false | { cidr: ["IP '2001:db8::8:800:200c:417a' is not a valid CIDR: IP should be followed by a slash followed by an integer subnet mask (for example: '192.168.1.0/24')"] }
+ '2001:db8::8:800:200c:417a/128' | true | {}
+ '172.0.0.1/32' | true | {}
+ '' | true | {}
+ nil | true | {}
+ # rubocop:enable Layout/LineLength
+ end
+
+ with_them do
+ before do
+ model.cidr = cidr
+ model.validate
+ end
+
+ it { expect(model.valid?).to eq(validity) }
+ it { expect(model.errors.messages).to eq(errors) }
+ end
+end