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