Class: TestLdapClient

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
vendor/plugins/ruby-net-ldap-0.0.4/tests/testldap.rb

Instance Method Summary

Instance Method Details

- (Object) internal_test_search_attributes(attrs_to_search)

This is a helper routine for test_search_attributes.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'vendor/plugins/ruby-net-ldap-0.0.4/tests/testldap.rb', line 107

def internal_test_search_attributes attrs_to_search
  ldap = Net::LDAP.new :host => @host, :port => @port, :auth => @auth
  assert( ldap.bind )

  search = {
    :base => "dc=bayshorenetworks,dc=com",
    :attributes => attrs_to_search
  }

  ldif = @ldif
  ldif.each {|dn,entry|
    entry.delete_if {|attr,value|
      ! attrs_to_search.include?(attr)
    }
  }

  assert_equal( true, ldap.search( search ))
  ldap.search( search ) {|res|
    res_keys = res.keys.sort
    ldif_keys = ldif.keys.sort
    assert( res_keys, ldif_keys )
    res.keys.each {|rk|
      assert( res[rk], ldif[rk] )
    }
  }
end

- (Object) load_test_data

Get some test data which will be used to validate the responses from the test LDAP server we will connect to. TODO, Bogus: we are HARDCODING the location of the file for now.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'vendor/plugins/ruby-net-ldap-0.0.4/tests/testldap.rb', line 43

def load_test_data
  ary = File.readlines( "tests/testdata.ldif" )
  hash = {}
  while line = ary.shift and line.chomp!
    if line =~ /^dn:[\s]*/i
      dn = $'
      hash[dn] = {}
      while attr = ary.shift and attr.chomp! and attr =~ /^([\w]+)[\s]*:[\s]*/
        hash[dn][$1.downcase.intern] ||= []
        hash[dn][$1.downcase.intern] << $'
      end
    end
  end
  hash
end

- (Object) setup

TODO: these tests crash and burn if the associated LDAP testserver isn’t up and running. We rely on being able to read a file with test data in LDIF format. TODO, WARNING: for the moment, this data is in a file whose name and location are HARDCODED into the instance method load_test_data.



24
25
26
27
28
29
30
31
32
33
34
# File 'vendor/plugins/ruby-net-ldap-0.0.4/tests/testldap.rb', line 24

def setup
  @host = "127.0.0.1"
  @port = 3890
  @auth = {
    :method => :simple,
    :username => "cn=bigshot,dc=bayshorenetworks,dc=com",
    :password => "opensesame"
  }

  @ldif = load_test_data
end

- (Object) test_bind

Binding tests. Need tests for all kinds of network failures and incorrect auth. TODO: Implement a class-level timeout for operations like bind. Search has a timeout defined at the protocol level, other ops do not. TODO, use constants for the LDAP result codes, rather than hardcoding them.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'vendor/plugins/ruby-net-ldap-0.0.4/tests/testldap.rb', line 66

def test_bind
  ldap = Net::LDAP.new :host => @host, :port => @port, :auth => @auth
  assert_equal( true, ldap.bind )
  assert_equal( 0, ldap.get_operation_result.code )
  assert_equal( "Success", ldap.get_operation_result.message )

  bad_username = @auth.merge( {:username => "cn=badguy,dc=imposters,dc=com"} )
  ldap = Net::LDAP.new :host => @host, :port => @port, :auth => bad_username
  assert_equal( false, ldap.bind )
  assert_equal( 48, ldap.get_operation_result.code )
  assert_equal( "Inappropriate Authentication", ldap.get_operation_result.message )

  bad_password = @auth.merge( {:password => "cornhusk"} )
  ldap = Net::LDAP.new :host => @host, :port => @port, :auth => bad_password
  assert_equal( false, ldap.bind )
  assert_equal( 49, ldap.get_operation_result.code )
  assert_equal( "Invalid Credentials", ldap.get_operation_result.message )
end

- (Object) test_ldap_open



175
176
177
178
179
180
181
182
# File 'vendor/plugins/ruby-net-ldap-0.0.4/tests/testldap.rb', line 175

def test_ldap_open
  Net::LDAP.open( :host => @host, :port => @port, :auth => @auth ) {|ldap|
    10.times {
      rc = ldap.search( :base => "dc=bayshorenetworks,dc=com" )
      assert_equal( true, rc )
    }
  }
end

- (Object) test_open



164
165
166
167
168
169
170
171
172
# File 'vendor/plugins/ruby-net-ldap-0.0.4/tests/testldap.rb', line 164

def test_open
  ldap = Net::LDAP.new :host => @host, :port => @port, :auth => @auth
  ldap.open {|ldap|
    10.times {
      rc = ldap.search( :base => "dc=bayshorenetworks,dc=com" )
      assert_equal( true, rc )
    }
  }
end

- (Object) test_search



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'vendor/plugins/ruby-net-ldap-0.0.4/tests/testldap.rb', line 87

def test_search
  ldap = Net::LDAP.new :host => @host, :port => @port, :auth => @auth

  search = {:base => "dc=smalldomain,dc=com"}
  assert_equal( false, ldap.search( search ))
  assert_equal( 32, ldap.get_operation_result.code )
  
  search = {:base => "dc=bayshorenetworks,dc=com"}
  assert_equal( true, ldap.search( search ))
  assert_equal( 0, ldap.get_operation_result.code )
  
  ldap.search( search ) {|res|
    assert_equal( res, @ldif )
  }
end

- (Object) test_search_attributes



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'vendor/plugins/ruby-net-ldap-0.0.4/tests/testldap.rb', line 135

def test_search_attributes
  internal_test_search_attributes [:mail]
  internal_test_search_attributes [:cn]
  internal_test_search_attributes [:ou]
  internal_test_search_attributes [:hasaccessprivilege]
  internal_test_search_attributes ["mail"]
  internal_test_search_attributes ["cn"]
  internal_test_search_attributes ["ou"]
  internal_test_search_attributes ["hasaccessrole"]

  internal_test_search_attributes [:mail, :cn, :ou, :hasaccessrole]
  internal_test_search_attributes [:mail, "cn", :ou, "hasaccessrole"]
end

- (Object) test_search_filters



150
151
152
153
154
155
156
157
158
159
160
# File 'vendor/plugins/ruby-net-ldap-0.0.4/tests/testldap.rb', line 150

def test_search_filters
  ldap = Net::LDAP.new :host => @host, :port => @port, :auth => @auth
  search = {
    :base => "dc=bayshorenetworks,dc=com",
    :filter => Net::LDAP::Filter.eq( "sn", "Fosse" )
  }

  ldap.search( search ) {|res|
    p res
  }
end