@@ -25,6 +25,24 @@ def ssl_test(server_opts, opts = {})
2525 test_server . stop
2626end
2727
28+ def basic_auth_test ( server_opts , opts = { } )
29+ server_opts [ :rsa_key ] ||= File . read ( OctocatalogDiff ::Spec . fixture_path ( 'ssl/generated/server.key' ) )
30+ server_opts [ :cert ] ||= File . read ( OctocatalogDiff ::Spec . fixture_path ( 'ssl/generated/server.crt' ) )
31+ server_opts [ :require_header ] ||= { }
32+ server_opts [ :require_header ] [ 'Authorization' ] = 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=' # username:password
33+ test_server = nil
34+ 3 . times do
35+ test_server = SSLTestServer . new ( server_opts )
36+ test_server . start
37+ break if test_server . port > 0
38+ end
39+ raise OctocatalogDiff ::Spec ::FixtureError , 'Unable to instantiate SSLTestServer' unless test_server . port > 0
40+ testobj = OctocatalogDiff ::PuppetDB . new ( opts . merge ( puppetdb_url : "https://username:password@localhost:#{ test_server . port } " ) )
41+ return testobj . get ( '/foo' )
42+ ensure
43+ test_server . stop
44+ end
45+
2846describe OctocatalogDiff ::PuppetDB do
2947 # Test constructor's ability to create @connections
3048 describe '#initialize' do
@@ -272,6 +290,53 @@ def ssl_test(server_opts, opts = {})
272290 expect { testobj . send ( :parse_url , test_url ) } . to raise_error ( URI ::InvalidURIError )
273291 end
274292 end
293+
294+ context 'basic auth' do
295+ it 'should detect a username:password combination' do
296+ test_url = 'https://username:password@foo.bar.host:8090'
297+ testobj = OctocatalogDiff ::PuppetDB . new
298+ result = testobj . send ( :parse_url , test_url )
299+ expect ( result [ :username ] ) . to eq ( 'username' )
300+ expect ( result [ :password ] ) . to eq ( 'password' )
301+ end
302+
303+ it 'should allow usernames without passwords' do
304+ test_url = 'https://username:@foo.bar.host:8090'
305+ testobj = OctocatalogDiff ::PuppetDB . new
306+ result = testobj . send ( :parse_url , test_url )
307+ expect ( result [ :username ] ) . to eq ( 'username' )
308+ expect ( result [ :password ] ) . to eq ( '' )
309+ end
310+
311+ it 'should allow passwords without usernames' do
312+ test_url = 'https://:password@foo.bar.host:8090'
313+ testobj = OctocatalogDiff ::PuppetDB . new
314+ result = testobj . send ( :parse_url , test_url )
315+ expect ( result [ :username ] ) . to eq ( '' )
316+ expect ( result [ :password ] ) . to eq ( 'password' )
317+ end
318+
319+ it 'should not parse a username or password when none are provided' do
320+ test_url = 'https://foo.bar.host:8090'
321+ testobj = OctocatalogDiff ::PuppetDB . new
322+ result = testobj . send ( :parse_url , test_url )
323+ expect ( result [ :username ] ) . to eq ( nil )
324+ expect ( result [ :password ] ) . to eq ( nil )
325+ end
326+ end
327+ end
328+
329+ context 'basic auth connection options' do
330+ context 'with basic auth on' do
331+ let ( :server_opts ) { { } }
332+ let ( :client_opts ) { { } }
333+ describe '#get' do
334+ it 'should not fail with basic auth on' do
335+ result = basic_auth_test ( server_opts , client_opts )
336+ expect ( result . key? ( 'success' ) ) . to eq ( true )
337+ end
338+ end
339+ end
275340 end
276341
277342 context 'puppetdb ssl connection options' do
0 commit comments