Begin cleaning up the test code to add some prune/mv cases.
[mailshears.git] / test / test_rm.rb
index 01d2c1c598adc163a7a0f5d606482b211049f652..09ebe2e37c4faefcb5ff68932c92883b6ab6fad8 100644 (file)
@@ -1,98 +1,39 @@
-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
-
-
+require 'common/domain'
+require 'common/user'
+require 'mailshears_test'
+require 'minitest/autorun'
+require 'rm/plugins/agendav'
+require 'rm/plugins/davical'
+require 'rm/plugins/dovecot'
+require 'rm/plugins/postfixadmin'
+require 'rm/plugins/roundcube'
+require 'rm/rm_runner'
+require 'rm/rm_dummy_runner'
+
+class TestRm < MailshearsTest
 
   def test_rm_user
-    cfg = Configuration.new(TESTCONF_PATH)
-    argv = ["adam@example.net"]
+    cfg = configuration()
+    users = [ User.new("adam@example.net") ]
 
     output_buffer = StringIO.new()
 
     $stdout = output_buffer
-    plugin_class = RmPlugin.run(cfg, *argv)
+    RmPlugin.run(cfg, *users)
     $stdout = STDOUT
 
     actual = output_buffer.string()
 
+    mail_root = cfg.send('mail_root')
     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"
+      "AgendavRm - Removed user adam@example.net.\n" +
+      "DavicalRm - User adam@example.net not found.\n" +
+      "DovecotRm - Removed user adam@example.net " +
+        "(#{mail_root}/example.net/adam).\n" +
+      "PostfixadminRm - Removed user adam@example.net.\n" +
+      "RoundcubeRm - Removed user adam@example.net (User ID: 2).\n"
 
     assert_equal(expected, actual)
 
@@ -105,19 +46,19 @@ class TestRm < Test::Unit::TestCase
 
     drm = DavicalRm.new(cfg)
     actual = drm.list_users()
-    expected = ['alice@example.com']
+    expected = [User.new('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']
+    expected = [User.new('alice@example.com'),
+                User.new('bob@example.com'),
+                User.new('beth@example.net'),
+                User.new('carol@example.net')]
     assert_equal(expected, actual)
 
     actual = pfarm.list_domains()
-    expected = ['example.com', 'example.net']
+    expected = [Domain.new('example.com'), Domain.new('example.net')]
     assert_equal(expected, actual)
 
     actual = pfarm.list_aliases()
@@ -133,31 +74,37 @@ class TestRm < Test::Unit::TestCase
 
     rrm = RoundcubeRm.new(cfg)
     actual = rrm.list_users()
-    expected = ['alice@example.com']
+    expected = [User.new('alice@example.com')]
     assert_equal(expected, actual)
+
+    # Check that adam's directory is gone but that the rest remain.
+    assert(maildir_exists('example.com/alice'))
+    assert(maildir_exists('example.com/booger'))
+    assert(maildir_exists('example.com/jeremy'))
+    assert(!maildir_exists('example.net/adam'))
   end
 
 
   def test_rm_domain
-    #
-    # This must (and should) run after test_rm_user().
-    #
-    cfg = Configuration.new(TESTCONF_PATH)
-    argv = ["example.net"]
+    cfg = configuration()
+    domains = [Domain.new("example.net")]
 
     output_buffer = StringIO.new()
 
     $stdout = output_buffer
-    plugin_class = RmPlugin.run(cfg, *argv)
+    RmPlugin.run(cfg, *domains)
     $stdout = STDOUT
 
     actual = output_buffer.string()
 
+    mail_root = cfg.send('mail_root')
     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"
+      "AgendavRm - Removed domain example.net.\n" +
+      "DavicalRm - Domain example.net not found.\n" +
+      "DovecotRm - Removed domain example.net " +
+        "(#{mail_root}/example.net).\n" +
+      "PostfixadminRm - Removed domain example.net.\n" +
+      "RoundcubeRm - Removed domain example.net.\n"
 
     assert_equal(expected, actual)
 
@@ -170,16 +117,17 @@ class TestRm < Test::Unit::TestCase
 
     drm = DavicalRm.new(cfg)
     actual = drm.list_users()
-    expected = ['alice@example.com']
+    expected = [User.new('alice@example.com')]
     assert_equal(expected, actual)
 
     pfarm = PostfixadminRm.new(cfg)
     actual = pfarm.list_users()
-    expected = ['alice@example.com', 'bob@example.com']
+    expected = [User.new('alice@example.com'),
+                User.new('bob@example.com')]
     assert_equal(expected, actual)
 
     actual = pfarm.list_domains()
-    expected = ['example.com']
+    expected = [Domain.new('example.com')]
     assert_equal(expected, actual)
 
     actual = pfarm.list_aliases()
@@ -191,23 +139,15 @@ class TestRm < Test::Unit::TestCase
 
     rrm = RoundcubeRm.new(cfg)
     actual = rrm.list_users()
-    expected = ['alice@example.com']
+    expected = [User.new('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()
+    # Check that example.net's directory is gone but that the rest remain.
+    assert(maildir_exists('example.com/alice'))
+    assert(maildir_exists('example.com/booger'))
+    assert(maildir_exists('example.com/jeremy'))
+    assert(!maildir_exists('example.net'))
   end
 
+
 end