From 046f4b7b1ef33f305d431f30cc710968e5bfbe44 Mon Sep 17 00:00:00 2001 From: paul Date: Thu, 29 Sep 2005 14:38:39 +0000 Subject: [PATCH] Added unit test for the command handler. git-svn-id: svn+ssh://svn.luon.net/svn/ildus/trunk@6 65a33f86-aa00-0410-91be-cd1bf5efb309 --- conf/ildusd.conf | 3 +- lib/ildus/server.rb | 8 ++---- lib/ildus/server/handler.rb | 9 ++++-- test/tc_handler.rb | 56 +++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 10 deletions(-) create mode 100644 test/tc_handler.rb diff --git a/conf/ildusd.conf b/conf/ildusd.conf index 4664ca7..7a5f52f 100644 --- a/conf/ildusd.conf +++ b/conf/ildusd.conf @@ -18,7 +18,7 @@ port: 9000 ### FILES/PATHS # # Directory for logging. -logdir = /var/log/ildusd +logdir: /var/log/ildusd # Pidfile to save the PID of the main daemon process to. pidfile: /tmp/ @@ -35,4 +35,3 @@ backend: basedn: cn=somedomain.test,ou=DNS,dc=somedomain,dc=test dnsdn: cn=admin,ou=People,dc=somedomain,dc=test dnspw: secret - diff --git a/lib/ildus/server.rb b/lib/ildus/server.rb index a116d03..9a1fefd 100644 --- a/lib/ildus/server.rb +++ b/lib/ildus/server.rb @@ -7,9 +7,10 @@ # Software Foundation; either version 2 of the License, or (at your option) # any later version. -require 'gserver' require 'yaml' +require 'ildus/server/handler' + module Ildus class Server < GServer @@ -76,8 +77,3 @@ module Ildus end # class Server end # module Ildus - -# Require the rest of the server library. -require 'ildus/server/account' -require 'ildus/server/backend' -require 'ildus/server/handler' diff --git a/lib/ildus/server/handler.rb b/lib/ildus/server/handler.rb index 9a11b46..5887d8e 100644 --- a/lib/ildus/server/handler.rb +++ b/lib/ildus/server/handler.rb @@ -7,9 +7,14 @@ # Software Foundation; either version 2 of the License, or (at your option) # any later version. +require 'gserver' + +require 'ildus/server/account' +require 'ildus/server/backend' + module Ildus - class Server < GServer + class Server class Handler @@ -107,7 +112,7 @@ module Ildus private ######### - def handle_command(cmd, args) + def handle_command(cmd, args=[]) method_name = cmd.downcase + "_cmd" meth = method(method_name) meth.call(*args) diff --git a/test/tc_handler.rb b/test/tc_handler.rb new file mode 100644 index 0000000..453edc1 --- /dev/null +++ b/test/tc_handler.rb @@ -0,0 +1,56 @@ +require 'test/unit' +require 'ildus/server/handler' + +class TC_HandlerTest < Test::Unit::TestCase + include Ildus + + def setup + # Create a handler object. + @io = StringIO.new + @hdl = Server::Handler.new(nil, @io) + assert(@hdl) + + class << @hdl + attr_reader :account + public :handle_command + end + end + + def test_auth + # "list" shouldn't be possible until authenticated. + assert_raises(Server::Handler::NotAuthError) { @hdl.handle_command("list") } + + # Set the user. + @hdl.handle_command("user", ["test"]) + assert_equal("test", @hdl.account.user) + + # "list" still shouldn't be possible. + assert_raises(Server::Handler::NotAuthError) { @hdl.handle_command("list") } + + # Set a different user. + @hdl.handle_command("user", ["test2"]) + assert_equal("test2", @hdl.account.user) + + # Nothing should be raised when setting the password, + # we should be authenticated afterwards. + assert_nothing_raised do + @hdl.handle_command("pass", ["secret"]) + end + assert_equal("secret", @hdl.account.pass) + assert(@hdl.account.authenticated?) + + # "list" should be possible now. + assert_nothing_raised do + @hdl.handle_command("list") + end + + # Setting a different user or password should result in an error. + assert_raise(Server::Handler::AlreadyAuthError) do + @hdl.handle_command("user", ["test3"]) + end + assert_raise(Server::Handler::AlreadyAuthError) do + @hdl.handle_command("pass", ["secret2"]) + end + end + +end # class TC_HandlerTest