-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathtest_ldap.rb
78 lines (61 loc) · 2.92 KB
/
test_ldap.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
require 'test_helper'
class TestLDAPInstrumentation < Test::Unit::TestCase
def setup
@connection = flexmock(:connection, :close => true)
flexmock(Net::LDAP::Connection).should_receive(:new).and_return(@connection)
@service = MockInstrumentationService.new
@subject = Net::LDAP.new \
:host => "test.mocked.com", :port => 636,
:force_no_page => true, # so server capabilities are not queried
:instrumentation_service => @service
end
def test_instrument_bind
events = @service.subscribe "bind.net_ldap"
bind_result = flexmock(:bind_result, :success? => true)
flexmock(@connection).should_receive(:bind).with(Hash).and_return(bind_result)
assert @subject.bind
payload, result = events.pop
assert result
assert_equal bind_result, payload[:bind]
end
def test_instrument_search
events = @service.subscribe "search.net_ldap"
flexmock(@connection).should_receive(:bind).and_return(flexmock(:bind_result, :result_code => Net::LDAP::ResultCodeSuccess))
flexmock(@connection).should_receive(:search).with(Hash, Proc).
yields(entry = Net::LDAP::Entry.new("uid=user1,ou=users,dc=example,dc=com")).
and_return(flexmock(:search_result, :success? => true, :result_code => Net::LDAP::ResultCodeSuccess))
refute_nil @subject.search(:filter => "(uid=user1)")
payload, result = events.pop
assert_equal [entry], result
assert_equal [entry], payload[:result]
assert_equal "(uid=user1)", payload[:filter]
end
def test_instrument_search_with_size
events = @service.subscribe "search.net_ldap"
flexmock(@connection).should_receive(:bind).and_return(flexmock(:bind_result, :result_code => Net::LDAP::ResultCodeSuccess))
flexmock(@connection).should_receive(:search).with(Hash, Proc).
yields(entry = Net::LDAP::Entry.new("uid=user1,ou=users,dc=example,dc=com")).
and_return(flexmock(:search_result, :success? => true, :result_code => Net::LDAP::ResultCodeSizeLimitExceeded))
refute_nil @subject.search(:filter => "(uid=user1)", :size => 1)
payload, result = events.pop
assert_equal [entry], result
assert_equal [entry], payload[:result]
assert_equal "(uid=user1)", payload[:filter]
assert_equal result.size, payload[:size]
end
def test_connect_cb
flexmock(Net::LDAP::Connection).should_receive(:new).with(
:socket => 42,
:host => "test.mocked.com",
:port => 636,
:encryption => nil,
:instrumentation_service => @service).and_return(@connection)
flexmock(@connection).should_receive(:bind).and_return(flexmock(:bind_result, :result_code => Net::LDAP::ResultCodeSuccess))
@subject = Net::LDAP.new \
:connect_cb => lambda { |host, port| 42 },
:host => "test.mocked.com", :port => 636,
:force_no_page => true, # so server capabilities are not queried
:instrumentation_service => @service
@subject.open {}
end
end