+require 'pg'
+require 'stringio'
+require 'test/unit'
+
+# WARNING: Test output is dependent on the order these classes include
+# certain modules; i.e. on the 'require' order.
+require 'common/configuration'
+require "rm/plugins/agendav"
+require "rm/plugins/davical"
+require "rm/plugins/postfixadmin"
+require "rm/plugins/roundcube"
+require "rm/rm_runner"
+require "rm/rm_dummy_runner"
+
+
+class TestRm < Test::Unit::TestCase
+
+ TESTCONF_PATH = 'test/mailshears.test.conf.yml'
+
+ def connect_superuser()
+ db_host = 'localhost'
+ db_port = 5432
+ db_opts = nil
+ db_tty = nil
+ db_name = 'postgres'
+ db_user = 'postgres'
+ db_pass = nil
+
+ connection = PGconn.connect(db_host,
+ db_port,
+ db_opts,
+ db_tty,
+ db_name,
+ db_user,
+ db_pass)
+
+ return connection
+ end
+
+ def setup
+ # Create databases using from the test configuration file.
+ cfg = Configuration.new(TESTCONF_PATH)
+ connection = connect_superuser()
+
+ cfg.plugins.each do |plugin|
+ plugin_dbname = cfg.send("#{plugin}_dbname")
+ query = "CREATE DATABASE #{plugin_dbname};"
+ connection.query(query)
+
+ plugin_dbhost = cfg.send("#{plugin}_dbhost")
+ plugin_dbport = cfg.send("#{plugin}_dbport")
+ plugin_dbopts = cfg.send("#{plugin}_dbopts")
+ plugin_dbtty = cfg.send("#{plugin}_dbtty")
+ plugin_dbuser = cfg.send("#{plugin}_dbuser")
+ plugin_dbpass = cfg.send("#{plugin}_dbpass")
+
+ plugin_conn = PGconn.connect(plugin_dbhost,
+ plugin_dbport,
+ plugin_dbopts,
+ plugin_dbtty,
+ plugin_dbname,
+ plugin_dbuser,
+ plugin_dbpass)
+
+ sql = File.open("test/sql/#{plugin}.sql").read()
+ plugin_conn.query(sql)
+ sql = File.open("test/sql/#{plugin}-fixtures.sql").read()
+ plugin_conn.query(sql)
+ plugin_conn.close()
+ end
+
+ connection.close()
+ end
+
+
+
+ def test_rm_user
+ cfg = Configuration.new(TESTCONF_PATH)
+ argv = ["adam@example.net"]
+
+ output_buffer = StringIO.new()
+
+ $stdout = output_buffer
+ plugin_class = RmPlugin.run(cfg, *argv)
+ $stdout = STDOUT
+
+ actual = output_buffer.string()
+
+ expected =
+ "AgendavRm - Removed user: adam@example.net " +
+ "(Username: adam@example.net)\n" +
+ "DavicalRm - User not found: adam@example.net\n" +
+ "PostfixadminRm - Removed user: " +
+ "adam@example.net (adam@example.net)\n" +
+ "RoundcubeRm - Removed user: adam@example.net (User ID: 2)\n"
+
+ assert_equal(expected, actual)
+
+ # Now make sure the database has what we expect.
+
+ arm = AgendavRm.new(cfg)
+ actual = arm.list_users()
+ expected = []
+ assert_equal(expected, actual)
+
+ drm = DavicalRm.new(cfg)
+ actual = drm.list_users()
+ expected = ['alice@example.com']
+ assert_equal(expected, actual)
+
+ pfarm = PostfixadminRm.new(cfg)
+ actual = pfarm.list_users()
+ expected = ['alice@example.com',
+ 'bob@example.com',
+ 'beth@example.net',
+ 'carol@example.net']
+ assert_equal(expected, actual)
+
+ actual = pfarm.list_domains()
+ expected = ['example.com', 'example.net']
+ assert_equal(expected, actual)
+
+ actual = pfarm.list_aliases()
+ expected = [{'address' => 'alice@example.com',
+ 'goto' => 'alice@example.com'},
+ {'address' => 'bob@example.com',
+ 'goto' => 'bob@example.com'},
+ {'address' => 'beth@example.net',
+ 'goto' => 'beth@example.net'},
+ {'address' => 'carol@example.net',
+ 'goto' => 'carol@example.net'}]
+ assert_equal(expected, actual)
+
+ rrm = RoundcubeRm.new(cfg)
+ actual = rrm.list_users()
+ expected = ['alice@example.com']
+ assert_equal(expected, actual)
+ end
+
+
+ def test_rm_domain
+ #
+ # This must (and should) run after test_rm_user().
+ #
+ cfg = Configuration.new(TESTCONF_PATH)
+ argv = ["example.net"]
+
+ output_buffer = StringIO.new()
+
+ $stdout = output_buffer
+ plugin_class = RmPlugin.run(cfg, *argv)
+ $stdout = STDOUT
+
+ actual = output_buffer.string()
+
+ expected =
+ "AgendavRm - Removed domain: example.net (example.net)\n" +
+ "DavicalRm - Domain not found: example.net\n" +
+ "PostfixadminRm - Removed domain: example.net (example.net)\n" +
+ "RoundcubeRm - Removed domain: example.net (example.net)\n"
+
+ assert_equal(expected, actual)
+
+ # Now make sure the database has what we expect.
+
+ arm = AgendavRm.new(cfg)
+ actual = arm.list_users()
+ expected = []
+ assert_equal(expected, actual)
+
+ drm = DavicalRm.new(cfg)
+ actual = drm.list_users()
+ expected = ['alice@example.com']
+ assert_equal(expected, actual)
+
+ pfarm = PostfixadminRm.new(cfg)
+ actual = pfarm.list_users()
+ expected = ['alice@example.com', 'bob@example.com']
+ assert_equal(expected, actual)
+
+ actual = pfarm.list_domains()
+ expected = ['example.com']
+ assert_equal(expected, actual)
+
+ actual = pfarm.list_aliases()
+ expected = [{'address' => 'alice@example.com',
+ 'goto' => 'alice@example.com'},
+ {'address' => 'bob@example.com',
+ 'goto' => 'bob@example.com'}]
+ assert_equal(expected, actual)
+
+ rrm = RoundcubeRm.new(cfg)
+ actual = rrm.list_users()
+ expected = ['alice@example.com']
+ assert_equal(expected, actual)
+ end
+
+
+
+ def teardown
+ cfg = Configuration.new(TESTCONF_PATH)
+ connection = connect_superuser()
+
+ cfg.plugins.each do |plugin|
+ plugin_dbname = cfg.send("#{plugin}_dbname")
+ query = "DROP DATABASE #{plugin_dbname};"
+ connection.query(query)
+ end
+
+ connection.close()
+ end
+
+end