Begin cleaning up the test code to add some prune/mv cases.
authorMichael Orlitzky <michael@orlitzky.com>
Tue, 3 Nov 2015 03:54:15 +0000 (22:54 -0500)
committerMichael Orlitzky <michael@orlitzky.com>
Tue, 3 Nov 2015 03:54:15 +0000 (22:54 -0500)
bin/install-fixtures.sh
mailshears.example.conf.yml
test/mailshears.test.conf.yml
test/mailshears_test.rb [new file with mode: 0644]
test/sql/README.fixtures [deleted file]
test/test_rm.rb

index 815ba61e8f0c641dc5bc2e1193c0aaf44a1d97b7..3a7ffdc7b86a44012f147413cfbeaf9e63abb85e 100755 (executable)
@@ -1,5 +1,11 @@
 #!/bin/sh
 
 #!/bin/sh
 
+rm -r /tmp/mailshears-test
+mkdir -p /tmp/mailshears-test/example.com/alice
+mkdir -p /tmp/mailshears-test/example.com/booger
+mkdir -p /tmp/mailshears-test/example.com/jeremy
+mkdir -p /tmp/mailshears-test/example.net/adam
+
 dropdb --if-exists -U postgres agendav
 createdb -U postgres agendav
 psql -U postgres -d agendav < test/sql/agendav.sql
 dropdb --if-exists -U postgres agendav
 createdb -U postgres agendav
 psql -U postgres -d agendav < test/sql/agendav.sql
index fd703ff485183a9eb04eb277fed06058df961a34..03c3440ef04b8cf6d42fed922c7920d9245e7bad 100644 (file)
@@ -1,4 +1,24 @@
 i_mean_business: false
 i_mean_business: false
+plugins: [agendav, davical, dovecot, postfixadmin, roundcube]
+
+agendav_dbhost: localhost
+agendav_dbport: 5432
+agendav_dbopts:
+agendav_dbtty:
+agendav_dbuser: postgres
+agendav_dbpass:
+agendav_dbname: agendav
+
+davical_dbhost: localhost
+davical_dbport: 5432
+davical_dbopts:
+davical_dbtty:
+davical_dbuser: postgres
+davical_dbpass:
+davical_dbname: davical
+
+# dovecot
+mail_root: /var/spool/mail/vhosts
 
 postfixadmin_dbhost: localhost
 postfixadmin_dbport: 5432
 
 postfixadmin_dbhost: localhost
 postfixadmin_dbport: 5432
@@ -8,10 +28,6 @@ postfixadmin_dbuser: postgres
 postfixadmin_dbpass:
 postfixadmin_dbname: postfixadmin
 
 postfixadmin_dbpass:
 postfixadmin_dbname: postfixadmin
 
-plugins: [postfixadmin, dovecot, roundcube, agendav, davical]
-
-mail_root: /var/spool/mail/vhosts
-
 roundcube_dbhost: localhost
 roundcube_dbport: 5432
 roundcube_dbopts:
 roundcube_dbhost: localhost
 roundcube_dbport: 5432
 roundcube_dbopts:
@@ -20,18 +36,3 @@ roundcube_dbuser: postgres
 roundcube_dbpass:
 roundcube_dbname: roundcube
 
 roundcube_dbpass:
 roundcube_dbname: roundcube
 
-agendav_dbhost: localhost
-agendav_dbport: 5432
-agendav_dbopts:
-agendav_dbtty:
-agendav_dbuser: postgres
-agendav_dbpass:
-agendav_dbname: agendav
-
-davical_dbhost: localhost
-davical_dbport: 5432
-davical_dbopts:
-davical_dbtty:
-davical_dbuser: postgres
-davical_dbpass:
-davical_dbname: davical
index 662665a1a75978dd1103c72a1d5273e3b24fda7b..e254da57a6ac270899f40e9271b60dfdfb9c2836 100644 (file)
@@ -1,24 +1,5 @@
 i_mean_business: true
 i_mean_business: true
-
-postfixadmin_dbhost: localhost
-postfixadmin_dbport: 5432
-postfixadmin_dbopts:
-postfixadmin_dbtty:
-postfixadmin_dbuser: postgres
-postfixadmin_dbpass:
-postfixadmin_dbname: postfixadmin_test
-
-plugins: [postfixadmin, roundcube, agendav, davical]
-
-mail_root: /tmp
-
-roundcube_dbhost: localhost
-roundcube_dbport: 5432
-roundcube_dbopts:
-roundcube_dbtty:
-roundcube_dbuser: postgres
-roundcube_dbpass:
-roundcube_dbname: roundcube_test
+plugins: [agendav, davical, dovecot, postfixadmin, roundcube]
 
 agendav_dbhost: localhost
 agendav_dbport: 5432
 
 agendav_dbhost: localhost
 agendav_dbport: 5432
@@ -35,3 +16,22 @@ davical_dbtty:
 davical_dbuser: postgres
 davical_dbpass:
 davical_dbname: davical_test
 davical_dbuser: postgres
 davical_dbpass:
 davical_dbname: davical_test
+
+# dovecot
+mail_root: /tmp/mailshears-test
+
+postfixadmin_dbhost: localhost
+postfixadmin_dbport: 5432
+postfixadmin_dbopts:
+postfixadmin_dbtty:
+postfixadmin_dbuser: postgres
+postfixadmin_dbpass:
+postfixadmin_dbname: postfixadmin_test
+
+roundcube_dbhost: localhost
+roundcube_dbport: 5432
+roundcube_dbopts:
+roundcube_dbtty:
+roundcube_dbuser: postgres
+roundcube_dbpass:
+roundcube_dbname: roundcube_test
diff --git a/test/mailshears_test.rb b/test/mailshears_test.rb
new file mode 100644 (file)
index 0000000..483c587
--- /dev/null
@@ -0,0 +1,240 @@
+require 'common/configuration'
+require 'fileutils'
+require 'minitest/unit'
+require 'pg'
+
+class MailshearsTest < MiniTest::Unit::TestCase
+  # This is that class that most (if not all) of our test cases will
+  # inherit. It provides the automatic setup and teardown of the
+  # filesystem and database that the test cases will exercise.
+
+  def configuration()
+    # Return the test config object.
+    return Configuration.new('test/mailshears.test.conf.yml')
+  end
+
+  def maildir_exists(dir)
+    # Check if the given mail directory of the form "example.com/user"
+    # exists.
+    cfg = configuration()
+    mail_root = cfg.send('mail_root')
+    return File.directory?("#{mail_root}/#{dir}")
+  end
+
+  def connect_superuser()
+    # Connect to the database (specified in the test configuration) as
+    # the superuser. Your local configuration is expected to be such
+    # that this "just works."
+    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
+    # Connect to the database specified in the test configutation as
+    # the super user. Then, run all of the SQL scripts contained in
+    # test/sql. These scripts create the tables for the plugins listed
+    # in the test configuration, and then create some sample data.
+    #
+    # Here is the full list of what gets created. Every test case
+    # inheriting from this class can expect this schema and data to
+    # exist. The filesystem entries are located beneath mail_root from
+    # the configuration file.
+    #
+    # == Filesystem ==
+    #
+    #   * example.com/alice
+    #   * example.com/booger
+    #   * example.com/jeremy
+    #   * example.net/adam
+    #
+    # == Databases ==
+    #
+    # 1. agendav_test
+    #
+    #   +----------------------------+
+    #   |             prefs          |
+    #   +------------------+---------+
+    #   |  username        | options |
+    #   +------------------+---------+
+    #   | adam@example.net | herp    |
+    #   +------------------+---------+
+    #
+    #
+    #   +------------------------------------------------------+
+    #   |                        shared                        |
+    #   +-----+------------------+----------+------------------+
+    #   | sid |    user_from     | calendar |    user_which    |
+    #   +-----+------------------+----------+------------------+
+    #   |   1 | adam@example.net | derp     | beth@example.net |
+    #   +-----+------------------+----------+------------------+
+    #
+    #
+    # 2. davical_test
+    #
+    #   +-------------------------------------------------------+
+    #   |                        usr                            |
+    #   +---------+--------+----------------+-------------------+
+    #   | user_no | active |    joined      |     username      |
+    #   +---------+--------+----------------+-------------------+
+    #   |      17 | t      | 2014-01-04 ... | alice@example.com |
+    #   +---------+--------+----------------+-------------------+
+    #
+    #
+    #   +-----------------------------------------+
+    #   |                usr_setting              |
+    #   +---------+--------------+----------------+
+    #   | user_no | setting_name | setting_value  |
+    #   +---------+--------------+----------------+
+    #   |      17 | dumb setting | its dumb value |
+    #   +---------+--------------+----------------+
+    #
+    #
+    # 3. postfixadmin_test
+    #
+    #   +-------------+
+    #   |   domain    |
+    #   +-------------+
+    #   |   domain    |
+    #   +-------------+
+    #   | ALL         |
+    #   +-------------+
+    #   | example.com |
+    #   +-------------+
+    #   | example.net |
+    #   +-------------+
+    #
+    #
+    #   +----------------------------------------------+
+    #   |                    mailbox                   |
+    #   +-------------------+-------------+------------+
+    #   |     username      |   domain    | local_part |
+    #   +-------------------+-------------+------------+
+    #   | alice@example.com | example.com | alice      |
+    #   +-------------------+-------------+------------+
+    #   | bob@example.com   | example.com | bob        |
+    #   +-------------------+-------------+------------+
+    #   | adam@example.net  | example.net | adam       |
+    #   +-------------------+-------------+------------+
+    #   | beth@example.net  | example.net | beth       |
+    #   +-------------------+-------------+------------+
+    #   | carol@example.net | example.net | carol      |
+    #   +-------------------+-------------+------------+
+    #
+    #
+    #   +------------------------------------------------------+
+    #   |                          alias                       |
+    #   +-------------------+-------------------+--------------+
+    #   |     address       |       goto        |   domain     |
+    #   +-------------------+-------------------+--------------+
+    #   | alice@example.com | alice@example.com | example.com  |
+    #   +-------------------+-------------------+--------------+
+    #   | bob@example.com   | bob@example.com   | example.com  |
+    #   +-------------------+-------------------+--------------+
+    #   | adam@example.net  | adam@example.net  | example.net  |
+    #   +-------------------+-------------------+--------------+
+    #   | beth@example.net  | beth@example.net  | example.net  |
+    #   +-------------------+-------------------+--------------+
+    #   | carol@example.net | carol@example.net | example.net  |
+    #   +-------------------+-------------------+--------------+
+    #
+    #
+    #   +---------------------------------+
+    #   |          domain_admins          |
+    #   +-------------------+-------------+
+    #   |     username      |   domain    |
+    #   +-------------------+-------------+
+    #   | admin@example.com | example.com |
+    #   +-------------------+-------------+
+    #   | admin@example.com | example.net |
+    #   +-------------------+-------------+
+    #
+    # 4. roundcube_test
+    #
+    #
+    #   +---------+-------------------+
+    #   | user_id |     username      |
+    #   +---------+-------------------+
+    #   |       1 | alice@example.com |
+    #   +---------+-------------------+
+    #   |       2 | adam@example.net  |
+    #   +---------+-------------------+
+
+    cfg = configuration()
+
+    # First create the "mail directories".
+    mail_root = cfg.send('mail_root')
+    FileUtils.mkdir_p("#{mail_root}/example.com/alice")
+    FileUtils.mkdir_p("#{mail_root}/example.com/booger")
+    FileUtils.mkdir_p("#{mail_root}/example.com/jeremy")
+    FileUtils.mkdir_p("#{mail_root}/example.net/adam")
+
+    # Now the databases and their content.
+    connection = connect_superuser()
+
+    cfg.plugins.each do |plugin|
+      plugin_dbname = cfg.send("#{plugin}_dbname")
+      next if plugin_dbname.nil? # Skip the dovecot plugin
+      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 teardown
+    # Drop all of the databases that were created in setup().
+    cfg = configuration()
+    connection = connect_superuser()
+
+    cfg.plugins.each do |plugin|
+      plugin_dbname = cfg.send("#{plugin}_dbname")
+      next if plugin_dbname.nil? # Skip the dovecot plugin
+      query = "DROP DATABASE #{plugin_dbname};"
+      connection.query(query)
+    end
+
+    connection.close()
+
+    # Get rid of the maildirs.
+    mail_root = cfg.send('mail_root')
+    FileUtils.rm_r(mail_root)
+  end
+
+end
diff --git a/test/sql/README.fixtures b/test/sql/README.fixtures
deleted file mode 100644 (file)
index b4a242c..0000000
+++ /dev/null
@@ -1,113 +0,0 @@
-Here's a quick (incomplete!) overview of what winds up in your tables.
-
-
-1. agendav_test
-
-+----------------------------+
-|             prefs          |
-+------------------+---------+
-|  username        | options |
-+------------------+---------+
-| adam@example.net | herp    |
-+------------------+---------+
-
-
-+------------------------------------------------------+
-|                        shared                        |
-+-----+------------------+----------+------------------+
-| sid |    user_from     | calendar |    user_which    |
-+-----+------------------+----------+------------------+
-|   1 | adam@example.net | derp     | beth@example.net |
-+-----+------------------+----------+------------------+
-
-
-2. davical_test
-
-+-------------------------------------------------------+
-|                        usr                            |
-+---------+--------+----------------+-------------------+
-| user_no | active |    joined      |     username      |
-+---------+--------+----------------+-------------------+
-|      17 | t      | 2014-01-04 ... | alice@example.com |
-+---------+--------+----------------+-------------------+
-
-+-----------------------------------------+
-|                usr_setting              |
-+---------+--------------+----------------+
-| user_no | setting_name | setting_value  |
-+---------+--------------+----------------+
-|      17 | dumb setting | its dumb value |
-+---------+--------------+----------------+
-
-
-3. postfixadmin_test
-
-+-------------+
-|   domain    |
-+-------------+
-|   domain    |
-+-------------+
-| ALL         |
-+-------------+
-| example.com |
-+-------------+
-| example.net |
-+-------------+
-
-
-+----------------------------------------------+
-|                    mailbox                   |
-+-------------------+-------------+------------+
-|     username      |   domain    | local_part |
-+-------------------+-------------+------------+
-| alice@example.com | example.com | alice      |
-+-------------------+-------------+------------+
-| bob@example.com   | example.com | bob        |
-+-------------------+-------------+------------+
-| adam@example.net  | example.net | adam       |
-+-------------------+-------------+------------+
-| beth@example.net  | example.net | beth       |
-+-------------------+-------------+------------+
-| carol@example.net | example.net | carol      |
-+-------------------+-------------+------------+
-
-
-+------------------------------------------------------+
-|                          alias                       |
-+-------------------+-------------------+--------------+
-|     address       |       goto        |   domain     |
-+-------------------+-------------------+--------------+
-| alice@example.com | alice@example.com | example.com  |
-+-------------------+-------------------+--------------+
-| bob@example.com   | bob@example.com   | example.com  |
-+-------------------+-------------------+--------------+
-| adam@example.net  | adam@example.net  | example.net  |
-+-------------------+-------------------+--------------+
-| beth@example.net  | beth@example.net  | example.net  |
-+-------------------+-------------------+--------------+
-| carol@example.net | carol@example.net | example.net  |
-+-------------------+-------------------+--------------+
-
-
-+---------------------------------+
-|          domain_admins          |
-+-------------------+-------------+
-|     username      |   domain    |
-+-------------------+-------------+
-| admin@example.com | example.com |
-+-------------------+-------------+
-| admin@example.com | example.net |
-+-------------------+-------------+
-
-
-
-4. roundcube_test
-
-
-+---------+-------------------+
-| user_id |     username      |
-+---------+-------------------+
-|       1 | alice@example.com |
-+---------+-------------------+
-|       2 | adam@example.net  |
-+---------+-------------------+
index 3f61221e487178fbb77d4993748a9c689af05b1c..09ebe2e37c4faefcb5ff68932c92883b6ab6fad8 100644 (file)
@@ -1,82 +1,21 @@
-require 'pg'
-require 'stringio'
-require "minitest/autorun"
-require 'minitest/unit'
-
 # WARNING: Test output is dependent on the order these classes include
 # certain modules; i.e. on the 'require' order.
 # WARNING: Test output is dependent on the order these classes include
 # certain modules; i.e. on the 'require' order.
-require 'common/configuration'
 require 'common/domain'
 require 'common/user'
 require 'common/domain'
 require 'common/user'
-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 < MiniTest::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 '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
 
   def test_rm_user
-    cfg = Configuration.new(TESTCONF_PATH)
+    cfg = configuration()
     users = [ User.new("adam@example.net") ]
 
     output_buffer = StringIO.new()
     users = [ User.new("adam@example.net") ]
 
     output_buffer = StringIO.new()
@@ -87,9 +26,12 @@ class TestRm < MiniTest::Unit::TestCase
 
     actual = output_buffer.string()
 
 
     actual = output_buffer.string()
 
+    mail_root = cfg.send('mail_root')
     expected =
       "AgendavRm - Removed user adam@example.net.\n" +
       "DavicalRm - User adam@example.net not found.\n" +
     expected =
       "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"
 
       "PostfixadminRm - Removed user adam@example.net.\n" +
       "RoundcubeRm - Removed user adam@example.net (User ID: 2).\n"
 
@@ -134,11 +76,17 @@ class TestRm < MiniTest::Unit::TestCase
     actual = rrm.list_users()
     expected = [User.new('alice@example.com')]
     assert_equal(expected, actual)
     actual = rrm.list_users()
     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
   end
 
 
   def test_rm_domain
-    cfg = Configuration.new(TESTCONF_PATH)
+    cfg = configuration()
     domains = [Domain.new("example.net")]
 
     output_buffer = StringIO.new()
     domains = [Domain.new("example.net")]
 
     output_buffer = StringIO.new()
@@ -149,9 +97,12 @@ class TestRm < MiniTest::Unit::TestCase
 
     actual = output_buffer.string()
 
 
     actual = output_buffer.string()
 
+    mail_root = cfg.send('mail_root')
     expected =
       "AgendavRm - Removed domain example.net.\n" +
       "DavicalRm - Domain example.net not found.\n" +
     expected =
       "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"
 
       "PostfixadminRm - Removed domain example.net.\n" +
       "RoundcubeRm - Removed domain example.net.\n"
 
@@ -190,21 +141,13 @@ class TestRm < MiniTest::Unit::TestCase
     actual = rrm.list_users()
     expected = [User.new('alice@example.com')]
     assert_equal(expected, actual)
     actual = rrm.list_users()
     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
 
+
 end
 end