-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathtest_ldap.rb
100 lines (78 loc) · 2.78 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
require 'test_helper'
class TestLDAPInstrumentation < Test::Unit::TestCase
# Fake Net::LDAP::Connection for testing
class FakeConnection
# It's difficult to instantiate Net::LDAP::PDU objects. Faking out what we
# need here until that object is brought under test and has it's constructor
# cleaned up.
class Result < Struct.new(:success?, :result_code); end
def initialize
@bind_success = Result.new(true, Net::LDAP::ResultCodeSuccess)
@search_success = Result.new(true, Net::LDAP::ResultCodeSizeLimitExceeded)
end
def bind(args = {})
@bind_success
end
def search(*args)
yield @search_success if block_given?
@search_success
end
end
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"
fake_connection = FakeConnection.new
@subject.connection = fake_connection
bind_result = fake_connection.bind
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"
fake_connection = FakeConnection.new
@subject.connection = fake_connection
entry = fake_connection.search
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"
fake_connection = FakeConnection.new
@subject.connection = fake_connection
entry = fake_connection.search
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_obscure_auth
password = "opensesame"
assert_include(@subject.inspect, "anonymous")
@subject.auth "joe_user", password
assert_not_include(@subject.inspect, password)
end
def test_encryption
enc = @subject.encryption('start_tls')
assert_equal enc[:method], :start_tls
end
def test_initializer_requires_hash_encryption
assert_raise ArgumentError, "encryption must be given as Hash" do
Net::LDAP.new encryption: [ :simple_tls ]
end
end
end