Skip to content

Commit

Permalink
Merge pull request #264 from ryanshow/master
Browse files Browse the repository at this point in the history
Normalize the encryption parameter passed to the LDAP constructor
  • Loading branch information
jch committed Feb 3, 2016
2 parents 771e11c + 8aaa96b commit 5f7d795
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
23 changes: 15 additions & 8 deletions lib/net/ldap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ def initialize(args = {})
@auth = args[:auth] || DefaultAuth
@base = args[:base] || DefaultTreebase
@force_no_page = args[:force_no_page] || DefaultForceNoPage
@encryption = args[:encryption] # may be nil
@encryption = normalize_encryption(args[:encryption]) # may be nil
@connect_timeout = args[:connect_timeout]

if pr = @auth[:password] and pr.respond_to?(:call)
Expand Down Expand Up @@ -609,13 +609,7 @@ def authenticate(username, password)
def encryption(args)
warn "Deprecation warning: please give :encryption option as a Hash to Net::LDAP.new"
return if args.nil?
return @encryption = args if args.is_a? Hash

case method = args.to_sym
when :simple_tls, :start_tls
args = { :method => method, :tls_options => {} }
end
@encryption = args
@encryption = normalize_encryption(args)
end

# #open takes the same parameters as #new. #open makes a network
Expand Down Expand Up @@ -1323,4 +1317,17 @@ def new_connection
}
raise e
end

# Normalize encryption parameter the constructor accepts, expands a few
# convenience symbols into recognizable hashes
def normalize_encryption(args)
return if args.nil?
return args if args.is_a? Hash

case method = args.to_sym
when :simple_tls, :start_tls
{ :method => method, :tls_options => {} }
end
end

end # class LDAP
20 changes: 20 additions & 0 deletions test/test_ldap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,24 @@ def test_encryption

assert_equal enc[:method], :start_tls
end

def test_normalize_encryption_symbol
enc = @subject.send(:normalize_encryption, :start_tls)
assert_equal enc, {:method => :start_tls, :tls_options => {}}
end

def test_normalize_encryption_nil
enc = @subject.send(:normalize_encryption, nil)
assert_equal enc, nil
end

def test_normalize_encryption_string
enc = @subject.send(:normalize_encryption, 'start_tls')
assert_equal enc, {:method => :start_tls, :tls_options => {}}
end

def test_normalize_encryption_hash
enc = @subject.send(:normalize_encryption, {:method => :start_tls, :tls_options => {:foo => :bar}})
assert_equal enc, {:method => :start_tls, :tls_options => {:foo => :bar}}
end
end

0 comments on commit 5f7d795

Please sign in to comment.