1
1
require 'test_helper'
2
2
3
3
class TestLDAPInstrumentation < Test ::Unit ::TestCase
4
+ # Fake Net::LDAP::Connection for testing
5
+ class FakeConnection
6
+ # It's difficult to instantiate Net::LDAP::PDU objects. Faking out what we
7
+ # need here until that object is brought under test and has it's constructor
8
+ # cleaned up.
9
+ class Result < Struct . new ( :success? , :result_code ) ; end
10
+
11
+ def initialize
12
+ @bind_success = Result . new ( true , Net ::LDAP ::ResultCodeSuccess )
13
+ @search_success = Result . new ( true , Net ::LDAP ::ResultCodeSizeLimitExceeded )
14
+ end
15
+
16
+ def bind ( args = { } )
17
+ @bind_success
18
+ end
19
+
20
+ def search ( *args )
21
+ yield @search_success if block_given?
22
+ @search_success
23
+ end
24
+ end
25
+
4
26
def setup
5
27
@connection = flexmock ( :connection , :close => true )
6
28
flexmock ( Net ::LDAP ::Connection ) . should_receive ( :new ) . and_return ( @connection )
@@ -15,8 +37,9 @@ def setup
15
37
def test_instrument_bind
16
38
events = @service . subscribe "bind.net_ldap"
17
39
18
- bind_result = flexmock ( :bind_result , :success? => true )
19
- flexmock ( @connection ) . should_receive ( :bind ) . with ( Hash ) . and_return ( bind_result )
40
+ fake_connection = FakeConnection . new
41
+ @subject . connection = fake_connection
42
+ bind_result = fake_connection . bind
20
43
21
44
assert @subject . bind
22
45
@@ -28,10 +51,9 @@ def test_instrument_bind
28
51
def test_instrument_search
29
52
events = @service . subscribe "search.net_ldap"
30
53
31
- flexmock ( @connection ) . should_receive ( :bind ) . and_return ( flexmock ( :bind_result , :result_code => Net ::LDAP ::ResultCodeSuccess ) )
32
- flexmock ( @connection ) . should_receive ( :search ) . with ( Hash , Proc ) .
33
- yields ( entry = Net ::LDAP ::Entry . new ( "uid=user1,ou=users,dc=example,dc=com" ) ) .
34
- and_return ( flexmock ( :search_result , :success? => true , :result_code => Net ::LDAP ::ResultCodeSuccess ) )
54
+ fake_connection = FakeConnection . new
55
+ @subject . connection = fake_connection
56
+ entry = fake_connection . search
35
57
36
58
refute_nil @subject . search ( :filter => "(uid=user1)" )
37
59
@@ -44,10 +66,9 @@ def test_instrument_search
44
66
def test_instrument_search_with_size
45
67
events = @service . subscribe "search.net_ldap"
46
68
47
- flexmock ( @connection ) . should_receive ( :bind ) . and_return ( flexmock ( :bind_result , :result_code => Net ::LDAP ::ResultCodeSuccess ) )
48
- flexmock ( @connection ) . should_receive ( :search ) . with ( Hash , Proc ) .
49
- yields ( entry = Net ::LDAP ::Entry . new ( "uid=user1,ou=users,dc=example,dc=com" ) ) .
50
- and_return ( flexmock ( :search_result , :success? => true , :result_code => Net ::LDAP ::ResultCodeSizeLimitExceeded ) )
69
+ fake_connection = FakeConnection . new
70
+ @subject . connection = fake_connection
71
+ entry = fake_connection . search
51
72
52
73
refute_nil @subject . search ( :filter => "(uid=user1)" , :size => 1 )
53
74
0 commit comments