Fix warnings with newer minitest versions.
[mailshears.git] / test / test_rm.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 'rm/plugins/agendav'
7 require 'rm/plugins/davical'
8 require 'rm/plugins/dovecot'
9 require 'rm/plugins/postfixadmin'
10 require 'rm/plugins/roundcube'
11 require 'rm/rm_runner'
12
13
14 class TestRm < MailshearsTest
15
16 def test_rm_user
17 cfg = configuration()
18 users = [ User.new("adam@example.net") ]
19
20 output_buffer = StringIO.new()
21
22 $stdout = output_buffer
23 RmPlugin.run(cfg, *users)
24 $stdout = STDOUT
25
26 actual = output_buffer.string()
27
28 expected =
29 "AgendavRm - Removed user adam@example.net.\n" +
30 "DavicalRm - User adam@example.net not found.\n" +
31 "DovecotRm - Removed user adam@example.net " +
32 "(#{cfg.dovecot_mail_root}/example.net/adam).\n" +
33 "PostfixadminRm - Removed user adam@example.net.\n" +
34 "RoundcubeRm - Removed user adam@example.net (User ID: 3).\n"
35
36 assert_equal(expected, actual)
37
38 # Now make sure the database has what we expect.
39
40 arm = AgendavRm.new(cfg)
41 actual = arm.list_users()
42 expected = [User.new('booger@example.com')]
43 assert_equal(expected, actual)
44
45 drm = DavicalRm.new(cfg)
46 actual = drm.list_users()
47 expected = [User.new('alice@example.com'), User.new('booger@example.com')]
48 assert_equal(expected, actual)
49
50 pfarm = PostfixadminRm.new(cfg)
51 actual = pfarm.list_users()
52 expected = [User.new('alice@example.com'),
53 User.new('bob@example.com'),
54 User.new('beth@example.net'),
55 User.new('carol@example.net')]
56 assert_equal(expected, actual)
57
58 actual = pfarm.list_domains()
59 expected = [Domain.new('example.com'), Domain.new('example.net')]
60 assert_equal(expected, actual)
61
62 actual = pfarm.list_aliases()
63 expected = [{'address' => 'alice@example.com',
64 'goto' => 'alice@example.com,' +
65 'bob@example.com,' +
66 'carol@example.net'},
67 {'address' => 'beth@example.net',
68 'goto' => 'beth@example.net'},
69 {'address' => 'bob@example.com',
70 'goto' => 'bob@example.com'},
71 {'address' => 'carol@example.net',
72 'goto' => 'carol@example.net'}]
73 expected.each { |e| assert(actual.include?(e)) } # can't sort dicts
74 actual.each { |a| assert(expected.include?(a)) } # can't sort dicts
75
76 rrm = RoundcubeRm.new(cfg)
77 actual = rrm.list_users()
78 expected = [User.new('alice@example.com'), User.new('booger@example.com')]
79 assert_equal(expected, actual)
80
81 # Check that adam's directory is gone but that the rest remain.
82 assert(maildir_exists('example.com/alice'))
83 assert(maildir_exists('example.com/booger'))
84 assert(maildir_exists('example.com/jeremy'))
85 assert(!maildir_exists('example.net/adam'))
86 end
87
88
89 def test_rm_domain
90 cfg = configuration()
91 domains = [Domain.new("example.net")]
92
93 output_buffer = StringIO.new()
94
95 $stdout = output_buffer
96 RmPlugin.run(cfg, *domains)
97 $stdout = STDOUT
98
99 actual = output_buffer.string()
100
101 expected =
102 "AgendavRm - Removed domain example.net.\n" +
103 "DavicalRm - Domain example.net not found.\n" +
104 "DovecotRm - Removed domain example.net " +
105 "(#{cfg.dovecot_mail_root}/example.net).\n" +
106 "PostfixadminRm - Removed domain example.net.\n" +
107 "RoundcubeRm - Removed domain example.net.\n"
108
109 assert_equal(expected, actual)
110
111 # Now make sure the database has what we expect.
112
113 arm = AgendavRm.new(cfg)
114 actual = arm.list_users()
115 expected = [User.new('booger@example.com')]
116 assert_equal(expected, actual)
117
118 drm = DavicalRm.new(cfg)
119 actual = drm.list_users()
120 expected = [User.new('alice@example.com'), User.new('booger@example.com')]
121 assert_equal(expected, actual)
122
123 pfarm = PostfixadminRm.new(cfg)
124 actual = pfarm.list_users()
125 expected = [User.new('alice@example.com'),
126 User.new('bob@example.com')]
127 assert_equal(expected, actual)
128
129 actual = pfarm.list_domains()
130 expected = [Domain.new('example.com')]
131 assert_equal(expected, actual)
132
133 actual = pfarm.list_aliases()
134 expected = [{'address' => 'alice@example.com',
135 'goto' => 'alice@example.com,bob@example.com'},
136 {'address' => 'bob@example.com',
137 'goto' => 'bob@example.com'}]
138 expected.each { |e| assert(actual.include?(e)) } # can't sort dicts
139 actual.each { |a| assert(expected.include?(a)) } # can't sort dicts
140
141 rrm = RoundcubeRm.new(cfg)
142 actual = rrm.list_users()
143 expected = [User.new('alice@example.com'), User.new('booger@example.com')]
144 assert_equal(expected, actual)
145
146 # Check that example.net's directory is gone but that the rest remain.
147 assert(maildir_exists('example.com/alice'))
148 assert(maildir_exists('example.com/booger'))
149 assert(maildir_exists('example.com/jeremy'))
150 assert(!maildir_exists('example.net'))
151 end
152
153
154 end