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