Sha256: 1b43db3dfbf516d30039ab77f0abb8799eaa00b95e70b609f321e7ab7bd4dcd8

Contents?: true

Size: 1.8 KB

Versions: 8

Compression:

Stored size: 1.8 KB

Contents

require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')

describe "IP Validation" do
  before(:each) do
    TestRecord.reset_callbacks(:validate)
  end
  
  subject { TestRecord.new }

  describe "IPv4 Validation" do
    before :each do
      TestRecord.validates :ip, :ip => { :format => :v4 }
    end

    it "accepts valid IPs" do
      subject.ip = '192.168.1.1'
      subject.should be_valid
      subject.should have(0).errors
    end

    context "for invalid IPs" do
      before :each do
        subject.ip = '267.34.56.3'
      end

      it "rejects invalid IPs" do
        subject.should_not be_valid
        subject.should have(1).error
      end

      it "generates an error message of type invalid" do
        subject.should_not be_valid
        subject.errors[:ip].should include subject.errors.generate_message(:ip, :invalid)
      end
    end
  end

  describe "IPv6 Validation" do
    before :each do
      TestRecord.validates :ip, :ip => { :format => :v6 }
    end

    it "accepts valid IPs" do
      subject.ip = '::1'
      subject.should be_valid
      subject.should have(0).errors
    end

    context "for invalid IPs" do
      before :each do
        subject.ip = '192.168.1.1'
      end

      it "rejects invalid IPs" do
        subject.should_not be_valid
        subject.should have(1).error
      end

      it "generates an error message of type invalid" do
        subject.should_not be_valid
        subject.errors[:ip].should include subject.errors.generate_message(:ip, :invalid)
      end
    end
  end

  it "checks validity of the arguments" do
    [3, "foo", 1..6].each do |wrong_argument|
      expect {
        TestRecord.validates :ip, :ip => { :format => wrong_argument }
      }.to raise_error(ArgumentError, "Unknown IP validator format #{wrong_argument.inspect}")
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
activevalidators-1.5.1 spec/validations/ip_spec.rb
activevalidators-1.5.0 spec/validations/ip_spec.rb
activevalidators-1.4.0 spec/validations/ip_spec.rb
activevalidators-1.3.0 spec/validations/ip_spec.rb
activevalidators-1.2.4 spec/validations/ip_spec.rb
activevalidators-1.2.3 spec/validations/ip_spec.rb
activevalidators-1.2.2 spec/validations/ip_spec.rb
activevalidators-1.2.1 spec/validations/ip_spec.rb