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