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