lib,test: replace connection query() method with sync_exec{,_params}.
[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 # Only try to remove this guy from the agendav database, to ensure
46 # that "nonexistent" users have their shares removed.
47 arm.remove_user('stinky@example.com')
48 expected = 2
49 actual = arm.count_shares()
50 assert_equal(expected, actual)
51
52 drm = DavicalRm.new(cfg)
53 actual = drm.list_users()
54 expected = [User.new('alice@example.com'), User.new('booger@example.com')]
55 assert_equal(expected, actual)
56
57 pfarm = PostfixadminRm.new(cfg)
58 actual = pfarm.list_users()
59 expected = [User.new('alice@example.com'),
60 User.new('bob@example.com'),
61 User.new('beth@example.net'),
62 User.new('carol@example.net')]
63 assert_equal(expected, actual)
64
65 actual = pfarm.list_domains()
66 expected = [Domain.new('example.com'), Domain.new('example.net')]
67 assert_equal(expected, actual)
68
69 actual = pfarm.list_aliases()
70 expected = [{'address' => 'alice@example.com',
71 'goto' => 'alice@example.com,' +
72 'bob@example.com,' +
73 'carol@example.net'},
74 {'address' => 'beth@example.net',
75 'goto' => 'beth@example.net'},
76 {'address' => 'bob@example.com',
77 'goto' => 'bob@example.com'},
78 {'address' => 'carol@example.net',
79 'goto' => 'carol@example.net'}]
80 expected.each { |e| assert(actual.include?(e)) } # can't sort dicts
81 actual.each { |a| assert(expected.include?(a)) } # can't sort dicts
82
83 rrm = RoundcubeRm.new(cfg)
84 actual = rrm.list_users()
85 expected = [User.new('alice@example.com'), User.new('booger@example.com')]
86 assert_equal(expected, actual)
87
88 # Check that adam's directory is gone but that the rest remain.
89 assert(maildir_exists('example.com/alice'))
90 assert(maildir_exists('example.com/booger'))
91 assert(maildir_exists('example.com/jeremy'))
92 assert(!maildir_exists('example.net/adam'))
93 end
94
95
96 def test_rm_domain
97 cfg = configuration()
98 domains = [Domain.new("example.net")]
99
100 output_buffer = StringIO.new()
101
102 $stdout = output_buffer
103 RmPlugin.run(cfg, *domains)
104 $stdout = STDOUT
105
106 actual = output_buffer.string()
107
108 expected =
109 "AgendavRm - Removed domain example.net.\n" +
110 "DavicalRm - Domain example.net not found.\n" +
111 "DovecotRm - Removed domain example.net " +
112 "(#{cfg.dovecot_mail_root}/example.net).\n" +
113 "PostfixadminRm - Removed domain example.net.\n" +
114 "RoundcubeRm - Removed domain example.net.\n"
115
116 assert_equal(expected, actual)
117
118 # Now make sure the database has what we expect.
119
120 arm = AgendavRm.new(cfg)
121 actual = arm.list_users()
122 expected = [User.new('booger@example.com')]
123 assert_equal(expected, actual)
124
125 drm = DavicalRm.new(cfg)
126 actual = drm.list_users()
127 expected = [User.new('alice@example.com'), User.new('booger@example.com')]
128 assert_equal(expected, actual)
129
130 pfarm = PostfixadminRm.new(cfg)
131 actual = pfarm.list_users()
132 expected = [User.new('alice@example.com'),
133 User.new('bob@example.com')]
134 assert_equal(expected, actual)
135
136 actual = pfarm.list_domains()
137 expected = [Domain.new('example.com')]
138 assert_equal(expected, actual)
139
140 actual = pfarm.list_aliases()
141 expected = [{'address' => 'alice@example.com',
142 'goto' => 'alice@example.com,bob@example.com'},
143 {'address' => 'bob@example.com',
144 'goto' => 'bob@example.com'}]
145 expected.each { |e| assert(actual.include?(e)) } # can't sort dicts
146 actual.each { |a| assert(expected.include?(a)) } # can't sort dicts
147
148 rrm = RoundcubeRm.new(cfg)
149 actual = rrm.list_users()
150 expected = [User.new('alice@example.com'), User.new('booger@example.com')]
151 assert_equal(expected, actual)
152
153 # Check that example.net's directory is gone but that the rest remain.
154 assert(maildir_exists('example.com/alice'))
155 assert(maildir_exists('example.com/booger'))
156 assert(maildir_exists('example.com/jeremy'))
157 assert(!maildir_exists('example.net'))
158 end
159
160
161 end