require 'common/domain'
require 'common/user'
require 'mailshears_test'
-require 'minitest/autorun'
-require 'prune/plugins/agendav'
require 'prune/plugins/davical'
require 'prune/plugins/dovecot'
require 'prune/plugins/postfixadmin'
class TestPrune < MailshearsTest
- def test_single_prune
+ def check_assertions(actual)
cfg = configuration()
- output_buffer = StringIO.new()
-
- $stdout = output_buffer
- PrunePlugin.run(cfg)
- $stdout = STDOUT
-
- actual = output_buffer.string()
-
+ # Both of our tests have the same expected output / results, so
+ # check them both using the same function.
expected =
- "AgendavPrune - Removed user booger@example.com.\n" +
"DavicalPrune - Removed user booger@example.com (Principal ID: 2).\n" +
"DovecotPrune - Removed user booger@example.com " +
"(#{cfg.dovecot_mail_root()}/example.com/booger).\n" +
# Now make sure the database has what we expect.
- arm = AgendavPrune.new(cfg)
- actual = arm.list_users()
- expected = [User.new('adam@example.net')]
- assert_equal(expected, actual)
-
- drm = DavicalPrune.new(cfg)
- actual = drm.list_users()
+ dpr = DavicalPrune.new(cfg)
+ actual = dpr.list_users()
expected = [User.new('alice@example.com')]
assert_equal(expected, actual)
- pfarm = PostfixadminPrune.new(cfg)
- actual = pfarm.list_users()
+ pfapr = PostfixadminPrune.new(cfg)
+ actual = pfapr.list_users()
expected = [User.new('alice@example.com'),
User.new('bob@example.com'),
User.new('adam@example.net'),
User.new('carol@example.net')]
assert_equal(expected, actual)
- actual = pfarm.list_domains()
+ actual = pfapr.list_domains()
expected = [Domain.new('example.com'), Domain.new('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' => 'adam@example.net',
- 'goto' => 'adam@example.net'},
+ actual = pfapr.list_aliases()
+ expected = [{'address' => 'adam@example.net',
+ 'goto' => 'adam@example.net'},
+ {'address' => 'alice@example.com',
+ 'goto' => 'alice@example.com,' +
+ 'adam@example.net,' +
+ 'bob@example.com,' +
+ 'carol@example.net'},
{'address' => 'beth@example.net',
- 'goto' => 'beth@example.net'},
+ 'goto' => 'beth@example.net'},
+ {'address' => 'bob@example.com',
+ 'goto' => 'bob@example.com'},
{'address' => 'carol@example.net',
- 'goto' => 'carol@example.net'}]
- assert_equal(expected, actual)
+ 'goto' => 'carol@example.net'}]
+ expected.each { |e| assert(actual.include?(e)) } # can't sort dicts
+ actual.each { |a| assert(expected.include?(a)) } # can't sort dicts
- rrm = RoundcubePrune.new(cfg)
- actual = rrm.list_users()
+ rpr = RoundcubePrune.new(cfg)
+ actual = rpr.list_users()
expected = [User.new('alice@example.com'), User.new('adam@example.net')]
assert_equal(expected, actual)
assert(!maildir_exists('example.com/jeremy'))
end
+
+ def test_single_prune()
+ # Run prune once and see what happens.
+ cfg = configuration()
+
+ output_buffer = StringIO.new()
+
+ $stdout = output_buffer
+ PrunePlugin.run(cfg)
+ $stdout = STDOUT
+
+ actual = output_buffer.string()
+
+ check_assertions(actual)
+ end
+
+
+ def test_double_prune
+ # Run prune twice. This should have the exact same output as
+ # running it once, since the second time around, there's nothing
+ # to prune.
+ cfg = configuration()
+
+ output_buffer = StringIO.new()
+
+ $stdout = output_buffer
+ PrunePlugin.run(cfg)
+ PrunePlugin.run(cfg)
+ $stdout = STDOUT
+
+ actual = output_buffer.string()
+
+ check_assertions(actual)
+ end
+
end