forked from djberg96/net-ping
/
test_net_ping_ldap.rb
192 lines (156 loc) · 5.29 KB
/
test_net_ping_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#################################################################################
# test_net_ping_http.rb
#
# Test case for the Net::PingHTTP class. This should be run via the 'test' or
# 'test:http' Rake task.
#################################################################################
require 'rubygems'
gem 'test-unit'
require 'test/unit'
require 'net/ping/ldap'
require 'fakeldap'
class TC_Net_Ping_LDAP < Test::Unit::TestCase
class << self
def startup
@@host = 'localhost'
@@port = 2389
@@uri = "ldap://#{@@host}:#{@@port}"
@@timeout = 30
@@cn = 'el.Daper'
@@password = 'ldappassword'
@@ldap_server = FakeLDAP::Server.new(:port => @@port)
@@ldap_server.run_tcpserver
@@ldap_server.add_user("cn=#{@@cn},ou=USERS,dc=example,dc=com", @@password)
end
def shutdown
@@ldap_server.stop
end
end
def setup
@ldap = Net::Ping::LDAP.new(@@uri, @@timeout)
@ldap.username = @@cn
@ldap.password = @@password
@bad = Net::Ping::LDAP.new('ldap://blabfoobarurghxxxx.com') # One hopes not
end
def teardown
@ldap = nil
end
test 'ping basic functionality' do
assert_respond_to(@ldap, :ping)
assert_nothing_raised{ @ldap.ping }
end
test 'ping returns a boolean value' do
assert_boolean(@ldap.ping?)
assert_boolean(@bad.ping?)
end
test 'ping? is an alias for ping' do
assert_alias_method(@ldap, :ping?, :ping)
end
test 'pingecho is an alias for ping' do
assert_alias_method(@ldap, :pingecho, :ping)
end
test 'ping should succeed for a valid website' do
assert_true(@ldap.ping?)
end
test 'ping should fail for an invalid website' do
assert_false(@bad.ping?)
end
test 'duration basic functionality' do
assert_respond_to(@ldap, :duration)
assert_nothing_raised{ @ldap.ping }
end
test 'duration returns a float value on a successful ping' do
assert_true(@ldap.ping)
assert_kind_of(Float, @ldap.duration)
end
test 'duration is nil on an unsuccessful ping' do
assert_false(@bad.ping)
assert_nil(@ldap.duration)
end
test 'host attribute basic functionality' do
assert_respond_to(@ldap, :host)
assert_respond_to(@ldap, :host=)
assert_equal(@@host, @ldap.host)
end
test 'port attribute basic functionality' do
assert_respond_to(@ldap, :port)
assert_respond_to(@ldap, :port=)
end
test 'port attribute expected value' do
assert_equal(@@port, @ldap.port)
end
test 'timeout attribute basic functionality' do
assert_respond_to(@ldap, :timeout)
assert_respond_to(@ldap, :timeout=)
end
test 'timeout attribute expected values' do
assert_equal(@@timeout, @ldap.timeout)
assert_equal(5, @bad.timeout)
end
test 'exception attribute basic functionality' do
assert_respond_to(@ldap, :exception)
assert_nil(@ldap.exception)
end
test 'exception attribute is nil if the ping is successful' do
assert_true(@ldap.ping)
assert_nil(@ldap.exception)
end
test 'exception attribute is not nil if the ping is unsuccessful' do
assert_false(@bad.ping)
assert_not_nil(@bad.exception)
end
test 'warning attribute basic functionality' do
assert_respond_to(@ldap, :warning)
assert_nil(@ldap.warning)
end
test 'uri attribute basic functionality' do
assert_respond_to(@ldap, :uri)
assert_respond_to(@ldap, :uri=)
end
test 'username attribute basic functionality' do
assert_respond_to(@ldap, :username)
assert_respond_to(@ldap, :username=)
end
test 'password attribute basic functionality' do
assert_respond_to(@ldap, :password)
assert_respond_to(@ldap, :password=)
end
test 'encryption attribute basic functionality' do
assert_respond_to(@ldap, :encryption)
assert_respond_to(@ldap, :encryption=)
end
test 'encryption defaults to nil for ldap' do
assert_nil(Net::Ping::LDAP.new('ldap://somehost.example.net').encryption)
end
test 'encryption defaults to simple_tls for ldaps' do
assert_equal(:simple_tls, Net::Ping::LDAP.new('ldaps://somehost.example.net').encryption)
end
test 'port defaults to 389 for ldap' do
assert_equal(389, Net::Ping::LDAP.new('ldap://somehost.example.net').port)
end
test 'port defaults to 636 for ldaps' do
assert_equal(636, Net::Ping::LDAP.new('ldaps://somehost.example.net').port)
end
test 'port extracted from uri if provided' do
assert_equal(12345, Net::Ping::LDAP.new('ldap://somehost.example.net:12345').port)
assert_equal(12345, Net::Ping::LDAP.new('ldaps://somehost.example.net:12345').port)
end
test 'encryption setting is forced to symbol' do
@ldap.encryption = 'simple_tls'
assert_true( @ldap.encryption.is_a? Symbol )
assert_true( @ldap.config[:encryption].is_a? Symbol )
end
test 'username/password set in config auth section' do
@ldap.username, @ldap.password = 'fred', 'derf'
assert_equal('fred', @ldap.config[:auth][:username] )
assert_equal('derf', @ldap.config[:auth][:password] )
end
test 'auth method defaults to simple if username/password set' do
@ldap.username, @ldap.password = 'fred', 'derf'
assert_equal(:simple, @ldap.config[:auth][:method] )
end
test 'if no username/password then defaults to auth anonymous' do
@ldap.username = @ldap.password = nil
assert_equal({:method => :anonymous}, @ldap.config[:auth] )
end
end