Fix Postfixadmin alias updating (don't leave commas at the start/end).
[mailshears.git] / test / mailshears_test.rb
1 require 'common/configuration'
2 require 'fileutils'
3 require 'minitest/unit'
4 require 'pg'
5
6 class MailshearsTest < MiniTest::Unit::TestCase
7 # This is that class that most (if not all) of our test cases will
8 # inherit. It provides the automatic setup and teardown of the
9 # filesystem and database that the test cases will exercise.
10
11 def configuration()
12 # Return the test config object.
13 return Configuration.new('test/mailshears.test.conf.yml')
14 end
15
16 def maildir_exists(dir)
17 # Check if the given mail directory of the form "example.com/user"
18 # exists.
19 cfg = configuration()
20 return File.directory?("#{cfg.dovecot_mail_root()}/#{dir}")
21 end
22
23 def connect_superuser()
24 # Connect to the database (specified in the test configuration) as
25 # the superuser. Your local configuration is expected to be such
26 # that this "just works."
27 db_host = 'localhost'
28 db_port = 5432
29 db_opts = nil
30 db_tty = nil
31 db_name = 'postgres'
32 db_user = 'postgres'
33 db_pass = nil
34
35 connection = PG::Connection.new(db_host, db_port, db_opts, db_tty,
36 db_name, db_user, db_pass)
37
38 return connection
39 end
40
41 def setup
42 # Connect to the database specified in the test configutation as
43 # the super user. Then, run all of the SQL scripts contained in
44 # test/sql. These scripts create the tables for the plugins listed
45 # in the test configuration, and then create some sample data.
46 #
47 # Here is the full list of what gets created. Every test case
48 # inheriting from this class can expect this schema and data to
49 # exist. The filesystem entries are located beneath mail_root from
50 # the configuration file.
51 #
52 # == Filesystem ==
53 #
54 # * example.com/alice
55 # * example.com/booger
56 # * example.com/jeremy
57 # * example.net/adam
58 #
59 # == Databases ==
60 #
61 # 1. agendav_test
62 #
63 # +------------------------------+
64 # | prefs |
65 # +--------------------+---------+
66 # | username | options |
67 # +--------------------+---------+
68 # | adam@example.net | herp |
69 # +--------------------+---------+
70 # | booger@example.com | herp |
71 # +------------------ +---------+
72 #
73 #
74 # +---------------------------------------------------------+
75 # | shared |
76 # +-----+--------------------+----------+-------------------+
77 # | sid | user_from | calendar | user_which |
78 # +-----+--------------------+----------+-------------------+
79 # | 1 | adam@example.net | derp | beth@example.net |
80 # +-----+--------------------+----------+-------------------+
81 # | 2 | booger@example.com | derp | carol@example.net |
82 # +-----+--------------------+----------+-------------------+
83 #
84 #
85 # 2. davical_test
86 #
87 # +--------------------------------------------------------+
88 # | usr |
89 # +---------+--------+----------------+--------------------+
90 # | user_no | active | joined | username |
91 # +---------+--------+----------------+--------------------+
92 # | 17 | t | 2014-01-04 ... | alice@example.com |
93 # +---------+--------+----------------+--------------------+
94 # | 18 | t | 2014-01-04 ... | booger@example.com |
95 # +---------+--------+----------------+--------------------+
96 #
97 #
98 # +-----------------------------------------+
99 # | usr_setting |
100 # +---------+--------------+----------------+
101 # | user_no | setting_name | setting_value |
102 # +---------+--------------+----------------+
103 # | 17 | dumb setting | its dumb value |
104 # +---------+--------------+----------------+
105 # | 18 | dumb setting | its dumb value |
106 # +---------+--------------+----------------+
107 #
108 #
109 # 3. postfixadmin_test
110 #
111 # +-------------+
112 # | domain |
113 # +-------------+
114 # | domain |
115 # +-------------+
116 # | ALL |
117 # +-------------+
118 # | example.com |
119 # +-------------+
120 # | example.net |
121 # +-------------+
122 #
123 #
124 # +----------------------------------------------+
125 # | mailbox |
126 # +-------------------+-------------+------------+
127 # | username | domain | local_part |
128 # +-------------------+-------------+------------+
129 # | alice@example.com | example.com | alice |
130 # +-------------------+-------------+------------+
131 # | bob@example.com | example.com | bob |
132 # +-------------------+-------------+------------+
133 # | adam@example.net | example.net | adam |
134 # +-------------------+-------------+------------+
135 # | beth@example.net | example.net | beth |
136 # +-------------------+-------------+------------+
137 # | carol@example.net | example.net | carol |
138 # +-------------------+-------------+------------+
139 #
140 #
141 # +-------------------------------------------------------+
142 # | alias |
143 # +-------------------+--------------------+--------------+
144 # | address | goto | domain |
145 # +-------------------+--------------------+--------------+
146 # | alice@example.com | alice@example.com, | example.com |
147 # | | adam@example.net, | |
148 # | | bob@example.com, | |
149 # | | carol@example.net | |
150 # +-------------------+--------------------+--------------+
151 # | bob@example.com | bob@example.com | example.com |
152 # +-------------------+--------------------+--------------+
153 # | adam@example.net | adam@example.net | example.net |
154 # +-------------------+--------------------+--------------+
155 # | beth@example.net | beth@example.net | example.net |
156 # +-------------------+--------------------+--------------+
157 # | carol@example.net | carol@example.net | example.net |
158 # +-------------------+--------------------+--------------+
159 #
160 #
161 # +---------------------------------+
162 # | domain_admins |
163 # +-------------------+-------------+
164 # | username | domain |
165 # +-------------------+-------------+
166 # | admin@example.com | example.com |
167 # +-------------------+-------------+
168 # | admin@example.com | example.net |
169 # +-------------------+-------------+
170 #
171 # 4. roundcube_test
172 #
173 #
174 # +---------+--------------------+
175 # | user_id | username |
176 # +---------+--------------------+
177 # | 1 | alice@example.com |
178 # +---------+--------------------+
179 # | 2 | booger@example.com |
180 # +---------+--------------------+
181 # | 3 | adam@example.net |
182 # +---------+--------------------+
183
184 # First make sure we get rid of everything so we don't get random
185 # failures from databases and directories that already exist.
186 teardown()
187
188 cfg = configuration()
189
190 # First create the "mail directories".
191 FileUtils.mkdir_p("#{cfg.dovecot_mail_root()}/example.com/alice")
192 FileUtils.mkdir_p("#{cfg.dovecot_mail_root()}/example.com/booger")
193 FileUtils.mkdir_p("#{cfg.dovecot_mail_root()}/example.com/jeremy")
194 FileUtils.mkdir_p("#{cfg.dovecot_mail_root()}/example.net/adam")
195
196 # Now the databases and their content.
197 connection = connect_superuser()
198
199 cfg.plugins.each do |plugin|
200 plugin_dbname = cfg.send("#{plugin}_dbname")
201 next if plugin_dbname.nil? # Skip the dovecot plugin
202 query = "CREATE DATABASE #{plugin_dbname};"
203 connection.query(query)
204
205 plugin_dbhost = cfg.send("#{plugin}_dbhost")
206 plugin_dbport = cfg.send("#{plugin}_dbport")
207 plugin_dbopts = cfg.send("#{plugin}_dbopts")
208 plugin_dbtty = cfg.send("#{plugin}_dbtty")
209 plugin_dbuser = cfg.send("#{plugin}_dbuser")
210 plugin_dbpass = cfg.send("#{plugin}_dbpass")
211
212 plugin_conn = PG::Connection.new(plugin_dbhost, plugin_dbport,
213 plugin_dbopts, plugin_dbtty,
214 plugin_dbname, plugin_dbuser,
215 plugin_dbpass)
216
217 sql = File.open("test/sql/#{plugin}.sql").read()
218 plugin_conn.query(sql)
219 sql = File.open("test/sql/#{plugin}-fixtures.sql").read()
220 plugin_conn.query(sql)
221 plugin_conn.close()
222 end
223
224 connection.close()
225 end
226
227
228 def teardown
229 # Drop all of the databases that were created in setup().
230 cfg = configuration()
231 connection = connect_superuser()
232
233 # Don't emit notices about missing tables. Why this happens when I
234 # explicitly say IF EXISTS is beyond me.
235 connection.set_notice_processor{}
236
237 cfg.plugins.each do |plugin|
238 plugin_dbname = cfg.send("#{plugin}_dbname")
239 next if plugin_dbname.nil? # Skip the dovecot plugin
240 query = "DROP DATABASE IF EXISTS #{plugin_dbname};"
241 connection.query(query)
242 end
243
244 connection.close()
245
246 # Get rid of the maildirs.
247 FileUtils.rm_rf(cfg.dovecot_mail_root())
248 end
249
250 end