|
1 |
| -require 'spec_helper' |
2 |
| - |
3 |
| -describe Net::LDAP do |
4 |
| - describe "initialize" do |
5 |
| - context "when instrumentation is configured" do |
6 |
| - before do |
7 |
| - @connection = flexmock(:connection, :close => true) |
8 |
| - flexmock(Net::LDAP::Connection).should_receive(:new).and_return(@connection) |
9 |
| - |
10 |
| - @service = MockInstrumentationService.new |
11 |
| - end |
12 |
| - |
13 |
| - subject do |
14 |
| - Net::LDAP.new \ |
15 |
| - :server => "test.mocked.com", :port => 636, |
16 |
| - :force_no_page => true, # so server capabilities are not queried |
17 |
| - :instrumentation_service => @service |
18 |
| - end |
19 |
| - |
20 |
| - it "should instrument bind" do |
21 |
| - events = @service.subscribe "bind.net_ldap" |
22 |
| - |
23 |
| - bind_result = flexmock(:bind_result, :success? => true) |
24 |
| - @connection.should_receive(:bind).with(Hash).and_return(bind_result) |
25 |
| - |
26 |
| - subject.bind.should == true |
27 |
| - |
28 |
| - payload, result = events.pop |
29 |
| - result.should == true |
30 |
| - payload[:bind].should == bind_result |
31 |
| - end |
32 |
| - |
33 |
| - it "should instrument search" do |
34 |
| - events = @service.subscribe "search.net_ldap" |
35 |
| - |
36 |
| - @connection.should_receive(:bind).and_return(flexmock(:bind_result, :result_code => 0)) |
37 |
| - @connection.should_receive(:search).with(Hash, Proc). |
38 |
| - yields(entry = Net::LDAP::Entry.new("uid=user1,ou=users,dc=example,dc=com")). |
39 |
| - and_return(flexmock(:search_result, :success? => true, :result_code => 0)) |
40 |
| - |
41 |
| - subject.search(:filter => "(uid=user1)").should_not be_nil |
42 |
| - |
43 |
| - payload, result = events.pop |
44 |
| - result.should == [entry] |
45 |
| - payload[:result].should == [entry] |
46 |
| - payload[:filter].should == "(uid=user1)" |
47 |
| - end |
48 |
| - end |
49 |
| - end |
50 |
| -end |
51 |
| - |
52 |
| -describe Net::LDAP::Connection do |
53 |
| - describe "initialize" do |
54 |
| - context "when host is not responding" do |
55 |
| - before(:each) do |
56 |
| - flexmock(TCPSocket). |
57 |
| - should_receive(:new).and_raise(Errno::ECONNREFUSED) |
58 |
| - end |
59 |
| - |
60 |
| - it "should raise LdapError" do |
61 |
| - lambda { |
62 |
| - Net::LDAP::Connection.new( |
63 |
| - :server => 'test.mocked.com', |
64 |
| - :port => 636) |
65 |
| - }.should raise_error(Net::LDAP::LdapError) |
66 |
| - end |
67 |
| - end |
68 |
| - context "when host is blocking the port" do |
69 |
| - before(:each) do |
70 |
| - flexmock(TCPSocket). |
71 |
| - should_receive(:new).and_raise(SocketError) |
72 |
| - end |
73 |
| - |
74 |
| - it "should raise LdapError" do |
75 |
| - lambda { |
76 |
| - Net::LDAP::Connection.new( |
77 |
| - :server => 'test.mocked.com', |
78 |
| - :port => 636) |
79 |
| - }.should raise_error(Net::LDAP::LdapError) |
80 |
| - end |
81 |
| - end |
82 |
| - context "on other exceptions" do |
83 |
| - before(:each) do |
84 |
| - flexmock(TCPSocket). |
85 |
| - should_receive(:new).and_raise(NameError) |
86 |
| - end |
87 |
| - |
88 |
| - it "should rethrow the exception" do |
89 |
| - lambda { |
90 |
| - Net::LDAP::Connection.new( |
91 |
| - :server => 'test.mocked.com', |
92 |
| - :port => 636) |
93 |
| - }.should raise_error(NameError) |
94 |
| - end |
95 |
| - end |
| 1 | +require 'common' |
| 2 | + |
| 3 | +class TestLDAPInstrumentation < Test::Unit::TestCase |
| 4 | + def setup |
| 5 | + @connection = flexmock(:connection, :close => true) |
| 6 | + flexmock(Net::LDAP::Connection).should_receive(:new).and_return(@connection) |
| 7 | + |
| 8 | + @service = MockInstrumentationService.new |
| 9 | + @ldap = Net::LDAP.new \ |
| 10 | + :server => "test.mocked.com", :port => 636, |
| 11 | + :force_no_page => true, # so server capabilities are not queried |
| 12 | + :instrumentation_service => @service |
96 | 13 | end
|
97 | 14 |
|
98 |
| - context "populate error messages" do |
99 |
| - before do |
100 |
| - @tcp_socket = flexmock(:connection) |
101 |
| - @tcp_socket.should_receive(:write) |
102 |
| - flexmock(TCPSocket).should_receive(:new).and_return(@tcp_socket) |
103 |
| - end |
| 15 | + def test_instrument_bind |
| 16 | + events = @service.subscribe "bind.net_ldap" |
104 | 17 |
|
105 |
| - subject { Net::LDAP::Connection.new(:server => 'test.mocked.com', :port => 636) } |
| 18 | + bind_result = flexmock(:bind_result, :success? => true) |
| 19 | + @connection.should_receive(:bind).with(Hash).and_return(bind_result) |
106 | 20 |
|
107 |
| - it "should get back error messages if operation fails" do |
108 |
| - ber = Net::BER::BerIdentifiedArray.new([53, "", "The provided password value was rejected by a password validator: The provided password did not contain enough characters from the character set 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'. The minimum number of characters from that set that must be present in user passwords is 1"]) |
109 |
| - ber.ber_identifier = Net::LDAP::PDU::ModifyResponse |
110 |
| - @tcp_socket.should_receive(:read_ber).and_return([2, ber]) |
| 21 | + assert @ldap.bind |
111 | 22 |
|
112 |
| - result = subject.modify(:dn => "1", :operations => [[:replace, "mail", "something@sothsdkf.com"]]) |
113 |
| - result.should be_failure |
114 |
| - result.error_message.should == "The provided password value was rejected by a password validator: The provided password did not contain enough characters from the character set 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'. The minimum number of characters from that set that must be present in user passwords is 1" |
115 |
| - end |
116 |
| - |
117 |
| - it "shouldn't get back error messages if operation succeeds" do |
118 |
| - ber = Net::BER::BerIdentifiedArray.new([0, "", ""]) |
119 |
| - ber.ber_identifier = Net::LDAP::PDU::ModifyResponse |
120 |
| - @tcp_socket.should_receive(:read_ber).and_return([2, ber]) |
121 |
| - |
122 |
| - result = subject.modify(:dn => "1", :operations => [[:replace, "mail", "something@sothsdkf.com"]]) |
123 |
| - result.should be_success |
124 |
| - result.error_message.should == "" |
125 |
| - end |
| 23 | + payload, result = events.pop |
| 24 | + assert result |
| 25 | + assert_equal bind_result, payload[:bind] |
126 | 26 | end
|
127 | 27 |
|
128 |
| - context "instrumentation" do |
129 |
| - before do |
130 |
| - @tcp_socket = flexmock(:connection) |
131 |
| - # handle write |
132 |
| - @tcp_socket.should_receive(:write) |
133 |
| - # return this mock |
134 |
| - flexmock(TCPSocket).should_receive(:new).and_return(@tcp_socket) |
135 |
| - |
136 |
| - @service = MockInstrumentationService.new |
137 |
| - end |
138 |
| - |
139 |
| - subject do |
140 |
| - Net::LDAP::Connection.new(:server => 'test.mocked.com', :port => 636, |
141 |
| - :instrumentation_service => @service) |
142 |
| - end |
143 |
| - |
144 |
| - it "should publish a write.net_ldap_connection event" do |
145 |
| - ber = Net::BER::BerIdentifiedArray.new([0, "", ""]) |
146 |
| - ber.ber_identifier = Net::LDAP::PDU::BindResult |
147 |
| - read_result = [2, ber] |
148 |
| - @tcp_socket.should_receive(:read_ber).and_return(read_result) |
149 |
| - |
150 |
| - events = @service.subscribe "write.net_ldap_connection" |
151 |
| - |
152 |
| - result = subject.bind(method: :anon) |
153 |
| - result.should be_success |
154 |
| - |
155 |
| - # a write event |
156 |
| - payload, result = events.pop |
157 |
| - payload.should have_key(:result) |
158 |
| - payload.should have_key(:content_length) |
159 |
| - end |
160 |
| - |
161 |
| - it "should publish a read.net_ldap_connection event" do |
162 |
| - ber = Net::BER::BerIdentifiedArray.new([0, "", ""]) |
163 |
| - ber.ber_identifier = Net::LDAP::PDU::BindResult |
164 |
| - read_result = [2, ber] |
165 |
| - @tcp_socket.should_receive(:read_ber).and_return(read_result) |
166 |
| - |
167 |
| - events = @service.subscribe "read.net_ldap_connection" |
168 |
| - |
169 |
| - result = subject.bind(method: :anon) |
170 |
| - result.should be_success |
171 |
| - |
172 |
| - # a read event |
173 |
| - payload, result = events.pop |
174 |
| - payload.should have_key(:result) |
175 |
| - result.should == read_result |
176 |
| - end |
177 |
| - |
178 |
| - it "should publish a bind.net_ldap_connection event" do |
179 |
| - ber = Net::BER::BerIdentifiedArray.new([0, "", ""]) |
180 |
| - ber.ber_identifier = Net::LDAP::PDU::BindResult |
181 |
| - bind_result = [2, ber] |
182 |
| - @tcp_socket.should_receive(:read_ber).and_return(bind_result) |
183 |
| - |
184 |
| - events = @service.subscribe "bind.net_ldap_connection" |
185 |
| - |
186 |
| - result = subject.bind(method: :anon) |
187 |
| - result.should be_success |
188 |
| - |
189 |
| - # a read event |
190 |
| - payload, result = events.pop |
191 |
| - payload.should have_key(:result) |
192 |
| - result.should be_success |
193 |
| - end |
194 |
| - |
195 |
| - it "should publish a search.net_ldap_connection event" do |
196 |
| - # search data |
197 |
| - search_data_ber = Net::BER::BerIdentifiedArray.new([2, [ |
198 |
| - "uid=user1,ou=OrgUnit2,ou=OrgUnitTop,dc=openldap,dc=ghe,dc=local", |
199 |
| - [ ["uid", ["user1"]] ] |
200 |
| - ]]) |
201 |
| - search_data_ber.ber_identifier = Net::LDAP::PDU::SearchReturnedData |
202 |
| - search_data = [2, search_data_ber] |
203 |
| - # search result (end of results) |
204 |
| - search_result_ber = Net::BER::BerIdentifiedArray.new([0, "", ""]) |
205 |
| - search_result_ber.ber_identifier = Net::LDAP::PDU::SearchResult |
206 |
| - search_result = [2, search_result_ber] |
207 |
| - @tcp_socket.should_receive(:read_ber).and_return(search_data). |
208 |
| - and_return(search_result) |
| 28 | + def test_instrument_search |
| 29 | + events = @service.subscribe "search.net_ldap" |
209 | 30 |
|
210 |
| - events = @service.subscribe "search.net_ldap_connection" |
| 31 | + @connection.should_receive(:bind).and_return(flexmock(:bind_result, :result_code => 0)) |
| 32 | + @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 => 0)) |
211 | 35 |
|
212 |
| - result = subject.search(filter: "(uid=user1)") |
213 |
| - result.should be_success |
| 36 | + refute_nil @ldap.search(:filter => "(uid=user1)") |
214 | 37 |
|
215 |
| - # a search event |
216 |
| - payload, result = events.pop |
217 |
| - payload.should have_key(:result) |
218 |
| - payload.should have_key(:filter) |
219 |
| - payload[:filter].to_s.should == "(uid=user1)" |
220 |
| - result.should be_truthy |
221 |
| - end |
| 38 | + payload, result = events.pop |
| 39 | + assert_equal [entry], result |
| 40 | + assert_equal [entry], payload[:result] |
| 41 | + assert_equal "(uid=user1)", payload[:filter] |
222 | 42 | end
|
223 | 43 | end
|
0 commit comments