Commit 2ee8f590 authored by James Lopez's avatar James Lopez

added ldap validator and spec

parent 734ca1eb
# LdapFilteralidator
#
# Custom validator for LDAP filters
#
# Example:
#
# class LdapGroupLink < ActiveRecord::Base
# validates :filter, ldap_filter: true
# end
#
class LdapFilterValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
Net::LDAP::Filter::FilterParser.parse(value)
rescue Net::LDAP::FilterSyntaxInvalidError
record.errors.add(attribute, 'must be a valid filter')
end
end
require 'spec_helper'
describe LdapFilterValidator do
let(:validator) { described_class.new(attributes: [:filter]) }
describe '#validates_each' do
it 'adds a message when the filter is not valid' do
link = build(:ldap_group_link, cn: nil)
validator.validate_each(link, :filter, 'wrong filter')
expect(link.errors[:filter]).to match_array(['must be a valid filter'])
end
it 'has no errors when is valid' do
link = build(:ldap_group_link, cn: nil)
validator.validate_each(link, :filter, '(cn=Babs Jensen)')
expect(link.errors[:filter]).to eq([])
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment