]> gitweb.michael.orlitzky.com - mailshears.git/blob - test/test_prune.rb
mailshears.gemspec: bump version to 0.1.0
[mailshears.git] / test / test_prune.rb
1 # WARNING: Test output is dependent on the order these classes include
2 # certain modules; i.e. on the 'require' order.
3 require 'common/domain'
4 require 'common/user'
5 require 'mailshears_test'
6 require 'prune/plugins/davical'
7 require 'prune/plugins/dovecot'
8 require 'prune/plugins/postfixadmin'
9 require 'prune/plugins/roundcube'
10 require 'prune/prune_runner'
11
12
13 class TestPrune < MailshearsTest
14
15 def check_assertions(actual)
16 cfg = configuration()
17
18 # Both of our tests have the same expected output / results, so
19 # check them both using the same function.
20 expected =
21 "DavicalPrune - Removed user booger@example.com (Principal ID: 2).\n" +
22 "DovecotPrune - Removed user booger@example.com " +
23 "(#{cfg.dovecot_mail_root()}/example.com/booger).\n" +
24 "DovecotPrune - Removed user jeremy@example.com " +
25 "(#{cfg.dovecot_mail_root()}/example.com/jeremy).\n" +
26 "RoundcubePrune - Removed user booger@example.com (User ID: 2).\n"
27
28 assert_equal(expected, actual)
29
30 # Now make sure the database has what we expect.
31
32 dpr = DavicalPrune.new(cfg)
33 actual = dpr.list_users()
34 expected = [User.new('alice@example.com')]
35 assert_equal(expected, actual)
36
37 pfapr = PostfixadminPrune.new(cfg)
38 actual = pfapr.list_users()
39 expected = [User.new('alice@example.com'),
40 User.new('bob@example.com'),
41 User.new('adam@example.net'),
42 User.new('beth@example.net'),
43 User.new('carol@example.net')]
44 assert_equal(expected, actual)
45
46 actual = pfapr.list_domains()
47 expected = [Domain.new('example.com'), Domain.new('example.net')]
48 assert_equal(expected, actual)
49
50 actual = pfapr.list_aliases()
51 expected = [{'address' => 'adam@example.net',
52 'goto' => 'adam@example.net'},
53 {'address' => 'alice@example.com',
54 'goto' => 'alice@example.com,' +
55 'adam@example.net,' +
56 'bob@example.com,' +
57 'carol@example.net'},
58 {'address' => 'beth@example.net',
59 'goto' => 'beth@example.net'},
60 {'address' => 'bob@example.com',
61 'goto' => 'bob@example.com'},
62 {'address' => 'carol@example.net',
63 'goto' => 'carol@example.net'}]
64 expected.each { |e| assert(actual.include?(e)) } # can't sort dicts
65 actual.each { |a| assert(expected.include?(a)) } # can't sort dicts
66
67 rpr = RoundcubePrune.new(cfg)
68 actual = rpr.list_users()
69 expected = [User.new('alice@example.com'), User.new('adam@example.net')]
70 assert_equal(expected, actual)
71
72 # Booger and Jeremy should get pruned.
73 assert(maildir_exists('example.com/alice'))
74 assert(maildir_exists('example.net/adam'))
75 assert(!maildir_exists('example.com/booger'))
76 assert(!maildir_exists('example.com/jeremy'))
77 end
78
79
80 def test_single_prune()
81 # Run prune once and see what happens.
82 cfg = configuration()
83
84 output_buffer = StringIO.new()
85
86 $stdout = output_buffer
87 PrunePlugin.run(cfg)
88 $stdout = STDOUT
89
90 actual = output_buffer.string()
91
92 check_assertions(actual)
93 end
94
95
96 def test_double_prune
97 # Run prune twice. This should have the exact same output as
98 # running it once, since the second time around, there's nothing
99 # to prune.
100 cfg = configuration()
101
102 output_buffer = StringIO.new()
103
104 $stdout = output_buffer
105 PrunePlugin.run(cfg)
106 PrunePlugin.run(cfg)
107 $stdout = STDOUT
108
109 actual = output_buffer.string()
110
111 check_assertions(actual)
112 end
113
114 end