70 lines
1.9 KiB
Ruby
70 lines
1.9 KiB
Ruby
require 'stringio'
|
|
require 'test/unit'
|
|
require 'ildus/server'
|
|
|
|
class TC_HandlerTest < Test::Unit::TestCase
|
|
include Ildus
|
|
|
|
def setup
|
|
# Create a handler object.
|
|
@io = StringIO.new
|
|
serv = Server.new("conf/ildusd.conf")
|
|
@hdl = Server::Handler.new(serv, @io)
|
|
assert(@hdl)
|
|
|
|
class << @hdl
|
|
attr_reader :backend
|
|
public :handle_command
|
|
end
|
|
end
|
|
|
|
def test_auth
|
|
# "list" shouldn't be possible until authenticated.
|
|
assert_raises(Server::Handler::NotAuthError) { @hdl.handle_command("list") }
|
|
|
|
# Password is not allowed before setting a user.
|
|
assert_raises(Server::Handler::SetUserFirstError) do
|
|
@hdl.handle_command("pass", ["secret"])
|
|
end
|
|
|
|
# Set the user.
|
|
@hdl.handle_command("user", ["test"])
|
|
assert_equal("test", @hdl.backend.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.backend.user)
|
|
|
|
# Give an incorrect password.
|
|
assert_raises(Server::Handler::NotAuthError) do
|
|
@hdl.handle_command("pass", ["secret"])
|
|
end
|
|
assert_equal("secret", @hdl.backend.pass)
|
|
|
|
# Nothing should be raised when setting the password,
|
|
# we should be authenticated afterwards.
|
|
assert_nothing_raised do
|
|
@hdl.handle_command("pass", ["foo"])
|
|
end
|
|
assert_equal("foo", @hdl.backend.pass)
|
|
assert(@hdl.backend.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
|