2011-05-24 13:14:26 +04:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
2011-05-18 17:45:08 +04:00
|
|
|
require "test/unit"
|
2011-05-24 13:14:26 +04:00
|
|
|
require "pg_ldap_sync/application"
|
|
|
|
require 'yaml'
|
|
|
|
require 'test/ldap_server'
|
|
|
|
require 'fileutils'
|
2011-05-18 17:45:08 +04:00
|
|
|
|
|
|
|
class TestPgLdapSync < Test::Unit::TestCase
|
2011-05-24 13:14:26 +04:00
|
|
|
def log_and_run( *cmd )
|
|
|
|
puts cmd.join(' ')
|
|
|
|
system( *cmd )
|
|
|
|
raise "Command failed: [%s]" % [cmd.join(' ')] unless $?.success?
|
|
|
|
end
|
|
|
|
|
|
|
|
def start_ldap_server
|
|
|
|
yaml_fname = File.join(File.dirname(__FILE__), "fixtures/ldapdb.yaml")
|
2011-07-07 18:14:37 +04:00
|
|
|
@directory = File.open(yaml_fname){|f| YAML::load(f.read) }
|
2011-05-24 13:14:26 +04:00
|
|
|
|
|
|
|
# Listen for incoming LDAP connections. For each one, create a Connection
|
|
|
|
# object, which will invoke a HashOperation object for each request.
|
|
|
|
|
|
|
|
@ldap_server = LDAP::Server.new(
|
|
|
|
:port => 1389,
|
|
|
|
:nodelay => true,
|
|
|
|
:listen => 10,
|
|
|
|
# :ssl_key_file => "key.pem",
|
|
|
|
# :ssl_cert_file => "cert.pem",
|
|
|
|
# :ssl_on_connect => true,
|
|
|
|
:operation_class => HashOperation,
|
2011-07-07 18:14:37 +04:00
|
|
|
:operation_args => [@directory]
|
2011-05-24 13:14:26 +04:00
|
|
|
)
|
|
|
|
@ldap_server.run_tcpserver
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop_ldap_server
|
|
|
|
@ldap_server.stop
|
|
|
|
end
|
|
|
|
|
|
|
|
def start_pg_server
|
|
|
|
@port = 54321
|
|
|
|
ENV['PGPORT'] = @port.to_s
|
|
|
|
ENV['PGHOST'] = 'localhost'
|
|
|
|
unless File.exist?('temp/pg_data')
|
|
|
|
FileUtils.mkdir_p 'temp/pg_data'
|
|
|
|
log_and_run 'initdb', '-D', 'temp/pg_data'
|
|
|
|
end
|
2011-07-07 17:06:34 +04:00
|
|
|
log_and_run 'pg_ctl', '-w', '-o', "-k.", '-D', 'temp/pg_data', 'start'
|
2011-05-24 13:14:26 +04:00
|
|
|
log_and_run 'psql', '-e', '-c', "DROP ROLE IF EXISTS fred, wilma, \"Flintstones\", \"Wilmas\", \"All Users\"", 'postgres'
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop_pg_server
|
2011-07-07 17:06:34 +04:00
|
|
|
log_and_run 'pg_ctl', '-w', '-o', "-k.", '-D', 'temp/pg_data', 'stop'
|
2011-05-24 13:14:26 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def setup
|
|
|
|
start_ldap_server
|
|
|
|
start_pg_server
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
stop_ldap_server
|
|
|
|
stop_pg_server
|
|
|
|
end
|
2011-07-08 17:50:33 +04:00
|
|
|
|
2011-07-07 18:14:37 +04:00
|
|
|
def psqlre(*args)
|
|
|
|
/^\s*#{args[0]}[ |]*#{args[1]}[ |\{"]*#{args[2..-1].join('[", ]+')}["\}\s]*$/
|
|
|
|
end
|
2011-05-24 13:14:26 +04:00
|
|
|
|
2011-05-18 17:45:08 +04:00
|
|
|
def test_sanity
|
2011-05-24 13:14:26 +04:00
|
|
|
PgLdapSync::Application.run(%w[-c test/fixtures/config-ldapdb.yaml -vv])
|
|
|
|
|
|
|
|
ENV['LC_MESSAGES'] = 'C'
|
2011-07-07 17:07:05 +04:00
|
|
|
psql_du = `psql -c \\\\du postgres`
|
|
|
|
puts psql_du
|
2011-07-08 17:50:33 +04:00
|
|
|
|
2011-07-07 18:14:37 +04:00
|
|
|
assert_match(psqlre('All Users','Cannot login'), psql_du)
|
|
|
|
assert_match(psqlre('Flintstones','Cannot login'), psql_du)
|
|
|
|
assert_match(psqlre('Wilmas','Cannot login','All Users'), psql_du)
|
|
|
|
assert_match(psqlre('fred','','All Users','Flintstones'), psql_du)
|
|
|
|
assert_match(psqlre('wilma','','Flintstones','Wilmas'), psql_du)
|
2011-07-08 17:50:33 +04:00
|
|
|
|
|
|
|
# revoke membership of 'wilma' to 'Flintstones'
|
2011-07-07 18:14:37 +04:00
|
|
|
@directory['cn=Flintstones,dc=example,dc=com']['member'].pop
|
2011-07-08 17:50:33 +04:00
|
|
|
|
2011-07-07 18:14:37 +04:00
|
|
|
PgLdapSync::Application.run(%w[-c test/fixtures/config-ldapdb.yaml -vv])
|
|
|
|
psql_du = `psql -c \\\\du postgres`
|
|
|
|
puts psql_du
|
2011-07-08 17:50:33 +04:00
|
|
|
|
2011-07-07 18:14:37 +04:00
|
|
|
assert_match(psqlre('All Users','Cannot login'), psql_du)
|
|
|
|
assert_match(psqlre('Flintstones','Cannot login'), psql_du)
|
|
|
|
assert_match(psqlre('Wilmas','Cannot login','All Users'), psql_du)
|
|
|
|
assert_match(psqlre('fred','','All Users','Flintstones'), psql_du)
|
|
|
|
assert_match(psqlre('wilma','','Wilmas'), psql_du)
|
2011-07-08 17:50:33 +04:00
|
|
|
|
|
|
|
# rename role 'wilma'
|
|
|
|
@directory['cn=Wilma Flintstone,dc=example,dc=com']['sAMAccountName'] = ['Wilma Flintstone']
|
|
|
|
# re-add 'Wilma' to 'Flintstones'
|
|
|
|
@directory['cn=Flintstones,dc=example,dc=com']['member'] << 'cn=Wilma Flintstone,dc=example,dc=com'
|
|
|
|
|
|
|
|
PgLdapSync::Application.run(%w[-c test/fixtures/config-ldapdb.yaml -vv])
|
|
|
|
psql_du = `psql -c \\\\du postgres`
|
|
|
|
puts psql_du
|
|
|
|
|
|
|
|
assert_match(psqlre('All Users','Cannot login'), psql_du)
|
|
|
|
assert_match(psqlre('Flintstones','Cannot login'), psql_du)
|
|
|
|
assert_match(psqlre('Wilmas','Cannot login','All Users'), psql_du)
|
|
|
|
assert_match(psqlre('fred','','All Users','Flintstones'), psql_du)
|
|
|
|
assert_no_match(/wilma/, psql_du)
|
|
|
|
assert_match(psqlre('Wilma Flintstone','','Flintstones','Wilmas'), psql_du)
|
2011-05-18 17:45:08 +04:00
|
|
|
end
|
|
|
|
end
|