Update a comment and delete a newline.
[mailshears.git] / test / test_rm.rb
1 require 'pg'
2 require 'stringio'
3 require 'test/unit'
4
5 # WARNING: Test output is dependent on the order these classes include
6 # certain modules; i.e. on the 'require' order.
7 require 'common/configuration'
8 require "rm/plugins/agendav"
9 require "rm/plugins/davical"
10 require "rm/plugins/postfixadmin"
11 require "rm/plugins/roundcube"
12 require "rm/rm_runner"
13 require "rm/rm_dummy_runner"
14
15
16 class TestRm < Test::Unit::TestCase
17
18 TESTCONF_PATH = 'test/mailshears.test.conf.yml'
19
20 def connect_superuser()
21 db_host = 'localhost'
22 db_port = 5432
23 db_opts = nil
24 db_tty = nil
25 db_name = 'postgres'
26 db_user = 'postgres'
27 db_pass = nil
28
29 connection = PGconn.connect(db_host,
30 db_port,
31 db_opts,
32 db_tty,
33 db_name,
34 db_user,
35 db_pass)
36
37 return connection
38 end
39
40 def setup
41 # Create databases using from the test configuration file.
42 cfg = Configuration.new(TESTCONF_PATH)
43 connection = connect_superuser()
44
45 cfg.plugins.each do |plugin|
46 plugin_dbname = cfg.send("#{plugin}_dbname")
47 query = "CREATE DATABASE #{plugin_dbname};"
48 connection.query(query)
49
50 plugin_dbhost = cfg.send("#{plugin}_dbhost")
51 plugin_dbport = cfg.send("#{plugin}_dbport")
52 plugin_dbopts = cfg.send("#{plugin}_dbopts")
53 plugin_dbtty = cfg.send("#{plugin}_dbtty")
54 plugin_dbuser = cfg.send("#{plugin}_dbuser")
55 plugin_dbpass = cfg.send("#{plugin}_dbpass")
56
57 plugin_conn = PGconn.connect(plugin_dbhost,
58 plugin_dbport,
59 plugin_dbopts,
60 plugin_dbtty,
61 plugin_dbname,
62 plugin_dbuser,
63 plugin_dbpass)
64
65 sql = File.open("test/sql/#{plugin}.sql").read()
66 plugin_conn.query(sql)
67 sql = File.open("test/sql/#{plugin}-fixtures.sql").read()
68 plugin_conn.query(sql)
69 plugin_conn.close()
70 end
71
72 connection.close()
73 end
74
75
76
77 def test_rm_user
78 cfg = Configuration.new(TESTCONF_PATH)
79 argv = ["adam@example.net"]
80
81 output_buffer = StringIO.new()
82
83 $stdout = output_buffer
84 plugin_class = RmPlugin.run(cfg, *argv)
85 $stdout = STDOUT
86
87 actual = output_buffer.string()
88
89 expected =
90 "AgendavRm - Removed user: adam@example.net " +
91 "(Username: adam@example.net)\n" +
92 "DavicalRm - User not found: adam@example.net\n" +
93 "PostfixadminRm - Removed user: " +
94 "adam@example.net (adam@example.net)\n" +
95 "RoundcubeRm - Removed user: adam@example.net (User ID: 2)\n"
96
97 assert_equal(expected, actual)
98
99 # Now make sure the database has what we expect.
100
101 arm = AgendavRm.new(cfg)
102 actual = arm.list_users()
103 expected = []
104 assert_equal(expected, actual)
105
106 drm = DavicalRm.new(cfg)
107 actual = drm.list_users()
108 expected = ['alice@example.com']
109 assert_equal(expected, actual)
110
111 pfarm = PostfixadminRm.new(cfg)
112 actual = pfarm.list_users()
113 expected = ['alice@example.com',
114 'bob@example.com',
115 'beth@example.net',
116 'carol@example.net']
117 assert_equal(expected, actual)
118
119 actual = pfarm.list_domains()
120 expected = ['example.com', 'example.net']
121 assert_equal(expected, actual)
122
123 actual = pfarm.list_aliases()
124 expected = [{'address' => 'alice@example.com',
125 'goto' => 'alice@example.com'},
126 {'address' => 'bob@example.com',
127 'goto' => 'bob@example.com'},
128 {'address' => 'beth@example.net',
129 'goto' => 'beth@example.net'},
130 {'address' => 'carol@example.net',
131 'goto' => 'carol@example.net'}]
132 assert_equal(expected, actual)
133
134 rrm = RoundcubeRm.new(cfg)
135 actual = rrm.list_users()
136 expected = ['alice@example.com']
137 assert_equal(expected, actual)
138 end
139
140
141 def test_rm_domain
142 #
143 # This must (and should) run after test_rm_user().
144 #
145 cfg = Configuration.new(TESTCONF_PATH)
146 argv = ["example.net"]
147
148 output_buffer = StringIO.new()
149
150 $stdout = output_buffer
151 plugin_class = RmPlugin.run(cfg, *argv)
152 $stdout = STDOUT
153
154 actual = output_buffer.string()
155
156 expected =
157 "AgendavRm - Removed domain: example.net (example.net)\n" +
158 "DavicalRm - Domain not found: example.net\n" +
159 "PostfixadminRm - Removed domain: example.net (example.net)\n" +
160 "RoundcubeRm - Removed domain: example.net (example.net)\n"
161
162 assert_equal(expected, actual)
163
164 # Now make sure the database has what we expect.
165
166 arm = AgendavRm.new(cfg)
167 actual = arm.list_users()
168 expected = []
169 assert_equal(expected, actual)
170
171 drm = DavicalRm.new(cfg)
172 actual = drm.list_users()
173 expected = ['alice@example.com']
174 assert_equal(expected, actual)
175
176 pfarm = PostfixadminRm.new(cfg)
177 actual = pfarm.list_users()
178 expected = ['alice@example.com', 'bob@example.com']
179 assert_equal(expected, actual)
180
181 actual = pfarm.list_domains()
182 expected = ['example.com']
183 assert_equal(expected, actual)
184
185 actual = pfarm.list_aliases()
186 expected = [{'address' => 'alice@example.com',
187 'goto' => 'alice@example.com'},
188 {'address' => 'bob@example.com',
189 'goto' => 'bob@example.com'}]
190 assert_equal(expected, actual)
191
192 rrm = RoundcubeRm.new(cfg)
193 actual = rrm.list_users()
194 expected = ['alice@example.com']
195 assert_equal(expected, actual)
196 end
197
198
199
200 def teardown
201 cfg = Configuration.new(TESTCONF_PATH)
202 connection = connect_superuser()
203
204 cfg.plugins.each do |plugin|
205 plugin_dbname = cfg.send("#{plugin}_dbname")
206 query = "DROP DATABASE #{plugin_dbname};"
207 connection.query(query)
208 end
209
210 connection.close()
211 end
212
213 end