Fix undefined method 'to_ber' for nil:NilClass. Convert unless nil.

Made modify_ops a method to allow for unit testing of it.

Added unit tests for add, delete and replace.
master
Lars Tobias Skjong-Børsting 2011-01-28 14:50:54 +01:00
parent aeda4020f2
commit 36b06f46df
2 changed files with 39 additions and 8 deletions

View File

@ -1456,6 +1456,20 @@ class Net::LDAP::Connection #:nodoc:
result_code
end
def self.modify_ops args
modify_ops = []
a = args[:operations] and a.each {|op, attr, values|
# TODO, fix the following line, which gives a bogus error
# if the opcode is invalid.
op_1 = {:add => 0, :delete => 1, :replace => 2} [op.to_sym].to_ber_enumerated
values = [values].flatten.map { |v|
v.to_ber unless v.nil?
}.to_ber_set
modify_ops << [op_1,[attr.to_s.to_ber,values].to_ber_sequence].to_ber
}
modify_ops
end
#--
# TODO: need to support a time limit, in case the server fails to respond.
# TODO: We're throwing an exception here on empty DN. Should return a
@ -1465,14 +1479,7 @@ class Net::LDAP::Connection #:nodoc:
#++
def modify(args)
modify_dn = args[:dn] or raise "Unable to modify empty DN"
modify_ops = []
a = args[:operations] and a.each { |op, attr, values|
# TODO, fix the following line, which gives a bogus error if the
# opcode is invalid.
op_1 = { :add => 0, :delete => 1, :replace => 2 }[op.to_sym].to_ber_enumerated
modify_ops << [op_1, [attr.to_s.to_ber, Array(values).map { |v| v.to_ber unless v.nil? }.to_ber_set].to_ber_sequence].to_ber_sequence
}
modify_ops = modify_ops args[:operations]
request = [modify_dn.to_ber,
modify_ops.to_ber_sequence].to_ber_appsequence(6)
pkt = [next_msgid.to_ber, request].to_ber_sequence

View File

@ -0,0 +1,24 @@
require 'common'
class TestLDAP < Test::Unit::TestCase
def test_modify_ops_delete
args = {:operations=>[[:delete, "mail"]]}
result = Net::LDAP::Connection.modify_ops(args)
expected = ["0\r\n\x01\x010\b\x04\x04mail1\x00"]
assert_equal(expected, result)
end
def test_modify_ops_add
args = {:operations=>[[:add, "mail", "testuser@example.com"]]}
result = Net::LDAP::Connection.modify_ops(args)
expected = ["0#\n\x01\x000\x1E\x04\x04mail1\x16\x04\x14testuser@example.com"]
assert_equal(expected, result)
end
def test_modify_ops_replace
args = {:operations=>[[:replace, "mail", "testuser@example.com"]]}
result = Net::LDAP::Connection.modify_ops(args)
expected = ["0#\n\x01\x020\x1E\x04\x04mail1\x16\x04\x14testuser@example.com"]
assert_equal(expected, result)
end
end