Sha256: 4f034aa8da77b38e61ed07206ae0fec1353e509a1d20ba4d782514ddca1ff3e5

Contents?: true

Size: 1.73 KB

Versions: 15

Compression:

Stored size: 1.73 KB

Contents

RSpec.describe IpFormatValidator do
  subject(:ip_format_validator) do
    described_class.new(
        :attributes => attributes
    )
  end

  let(:attribute) do
    :address
  end

  let(:attributes) do
    [
        attribute
    ]
  end

  context '#validate_each' do
    subject(:validate_each) do
      ip_format_validator.validate_each(record, attribute, value)
    end

    let(:error) do
      'must be a valid IPv4 or IPv6 address'
    end

    let(:record) do
      record_class.new
    end

    let(:record_class) do
      # capture for Class.new scope
      attribute = self.attribute

      Class.new do
        include ActiveModel::Validations

        #
        # Validations
        #

        validates attribute,
                  :ip_format => true
      end
    end

    context 'with value' do
      context 'with IPv4 address' do
        let(:value) do
          '192.168.0.1'
        end

        it 'should not record any errors' do
          validate_each

          expect(record.errors).to be_empty
        end
      end

      context 'with IPv6 address' do
        let(:value) do
          '::1'
        end

        it 'should not record any errors' do
          validate_each

          expect(record.errors).to be_empty
        end
      end

      context 'without IPv4 or IPv6 address' do
        let(:value) do
          'localhost'
        end

        it 'should record error' do
          validate_each

          expect(record.errors[attribute]).to include(error)
        end
      end

    end

    context 'without value' do
      let(:value) do
        nil
      end

      it 'should record error on attribute' do
        validate_each

        expect(record.errors[attribute]).to include(error)
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
metasploit-model-5.0.2 spec/app/validators/ip_format_validator_spec.rb
metasploit-model-5.0.1 spec/app/validators/ip_format_validator_spec.rb
metasploit-model-5.0.0 spec/app/validators/ip_format_validator_spec.rb
metasploit-model-4.0.6 spec/app/validators/ip_format_validator_spec.rb
metasploit-model-4.0.5 spec/app/validators/ip_format_validator_spec.rb
metasploit-model-4.0.4 spec/app/validators/ip_format_validator_spec.rb
metasploit-model-4.0.3 spec/app/validators/ip_format_validator_spec.rb
metasploit-model-4.0.2 spec/app/validators/ip_format_validator_spec.rb
metasploit-model-4.0.1 spec/app/validators/ip_format_validator_spec.rb
metasploit-model-4.0.0 spec/app/validators/ip_format_validator_spec.rb
metasploit-model-3.1.4 spec/app/validators/ip_format_validator_spec.rb
metasploit-model-3.1.3 spec/app/validators/ip_format_validator_spec.rb
metasploit-model-3.1.2 spec/app/validators/ip_format_validator_spec.rb
metasploit-model-3.1.1 spec/app/validators/ip_format_validator_spec.rb
metasploit-model-3.1.0 spec/app/validators/ip_format_validator_spec.rb