Skip to content

Commit e7cc5ae

Browse files
committed
replace ldap tests with fake connection object
1 parent f6ad189 commit e7cc5ae

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

lib/net/ldap.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,11 @@ def inspect
12121212
inspected
12131213
end
12141214

1215+
# Internal: Set @open_connection for testing
1216+
def connection=(connection)
1217+
@open_connection = connection
1218+
end
1219+
12151220
private
12161221

12171222
# Yields an open connection if there is one, otherwise establishes a new

test/test_ldap.rb

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
require 'test_helper'
22

33
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+
426
def setup
527
@connection = flexmock(:connection, :close => true)
628
flexmock(Net::LDAP::Connection).should_receive(:new).and_return(@connection)
@@ -15,8 +37,9 @@ def setup
1537
def test_instrument_bind
1638
events = @service.subscribe "bind.net_ldap"
1739

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
2043

2144
assert @subject.bind
2245

@@ -28,10 +51,9 @@ def test_instrument_bind
2851
def test_instrument_search
2952
events = @service.subscribe "search.net_ldap"
3053

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
3557

3658
refute_nil @subject.search(:filter => "(uid=user1)")
3759

@@ -44,10 +66,9 @@ def test_instrument_search
4466
def test_instrument_search_with_size
4567
events = @service.subscribe "search.net_ldap"
4668

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
5172

5273
refute_nil @subject.search(:filter => "(uid=user1)", :size => 1)
5374

0 commit comments

Comments
 (0)