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