Fix broken nesting in lib/mv/plugins/postfixadmin.rb.
[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 arm = AgendavPrune.new(cfg)
36 actual = arm.list_users()
37 expected = [User.new('adam@example.net')]
38 assert_equal(expected, actual)
39
40 drm = DavicalPrune.new(cfg)
41 actual = drm.list_users()
42 expected = [User.new('alice@example.com')]
43 assert_equal(expected, actual)
44
45 pfarm = PostfixadminPrune.new(cfg)
46 actual = pfarm.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 = pfarm.list_domains()
55 expected = [Domain.new('example.com'), Domain.new('example.net')]
56 assert_equal(expected, actual)
57
58 actual = pfarm.list_aliases()
59 expected = [{'address' => 'alice@example.com',
60 'goto' => 'alice@example.com'},
61 {'address' => 'bob@example.com',
62 'goto' => 'bob@example.com'},
63 {'address' => 'adam@example.net',
64 'goto' => 'adam@example.net'},
65 {'address' => 'beth@example.net',
66 'goto' => 'beth@example.net'},
67 {'address' => 'carol@example.net',
68 'goto' => 'carol@example.net'}]
69 assert_equal(expected, actual)
70
71 rrm = RoundcubePrune.new(cfg)
72 actual = rrm.list_users()
73 expected = [User.new('alice@example.com'), User.new('adam@example.net')]
74 assert_equal(expected, actual)
75
76 # Booger and Jeremy should get pruned.
77 assert(maildir_exists('example.com/alice'))
78 assert(maildir_exists('example.net/adam'))
79 assert(!maildir_exists('example.com/booger'))
80 assert(!maildir_exists('example.com/jeremy'))
81 end
82
83
84 def test_single_prune()
85 # Run prune once and see what happens.
86 cfg = configuration()
87
88 output_buffer = StringIO.new()
89
90 $stdout = output_buffer
91 PrunePlugin.run(cfg)
92 $stdout = STDOUT
93
94 actual = output_buffer.string()
95
96 check_assertions(actual)
97 end
98
99
100 def test_double_prune
101 # Run prune twice. This should have the exact same output as
102 # running it once, since the second time around, there's nothing
103 # to prune.
104 cfg = configuration()
105
106 output_buffer = StringIO.new()
107
108 $stdout = output_buffer
109 PrunePlugin.run(cfg)
110 PrunePlugin.run(cfg)
111 $stdout = STDOUT
112
113 actual = output_buffer.string()
114
115 check_assertions(actual)
116 end
117
118 end