]>
gitweb.michael.orlitzky.com - mailshears.git/blob - test/mailshears_test.rb
1 # Without this, I get...
3 # Warning: you should require 'minitest/autorun' instead.
4 # Warning: or add 'gem "minitest"' before 'require "minitest/autorun"'
8 require 'minitest/autorun'
10 require 'common/configuration'
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.
20 # Return the test config object.
21 return Configuration
.new('test/mailshears.test.conf.yml')
24 def maildir_exists(dir
)
25 # Check if the given mail directory of the form "example.com/user"
28 return File
.directory
?("#{cfg.dovecot_mail_root()}/#{dir}")
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."
43 connection
= PG
::Connection.new(db_host
, db_port
, db_opts
, db_tty
,
44 db_name
, db_user
, db_pass
)
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.
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.
63 # * example.com/booger
64 # * example.com/jeremy
71 # +------------------------------+
73 # +--------------------+---------+
74 # | username | options |
75 # +--------------------+---------+
76 # | adam@example.net | herp |
77 # +--------------------+---------+
78 # | booger@example.com | herp |
79 # +------------------ +---------+
82 # +---------------------------------------------------------------------------------------------------------------------+
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 # +-----+-----------------------------------+---------------------------------------------------+-----------------------+
95 # +--------------------------------------------------------+
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 # +---------+--------+----------------+--------------------+
106 # +-----------------------------------------+
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 # +---------+--------------+----------------+
117 # 3. postfixadmin_test
132 # +----------------------------------------------+
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 # +-------------------+-------------+------------+
149 # +-------------------------------------------------------+
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 # +-------------------+--------------------+--------------+
169 # +---------------------------------+
171 # +-------------------+-------------+
172 # | username | domain |
173 # +-------------------+-------------+
174 # | admin@example.com | example.com |
175 # +-------------------+-------------+
176 # | admin@example.com | example.net |
177 # +-------------------+-------------+
182 # +---------+--------------------+
183 # | user_id | username |
184 # +---------+--------------------+
185 # | 1 | alice@example.com |
186 # +---------+--------------------+
187 # | 2 | booger@example.com |
188 # +---------+--------------------+
189 # | 3 | adam@example.net |
190 # +---------+--------------------+
192 # First make sure we get rid of everything so we don't get random
193 # failures from databases and directories that already exist.
196 cfg
= configuration()
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")
204 # Now the databases and their content.
205 connection
= connect_superuser()
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
)
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")
220 plugin_conn
= PG
::Connection.new(plugin_dbhost
, plugin_dbport
,
221 plugin_dbopts
, plugin_dbtty
,
222 plugin_dbname
, plugin_dbuser
,
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
)
237 # Drop all of the databases that were created in setup().
238 cfg
= configuration()
239 connection
= connect_superuser()
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
{}
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
)
254 # Get rid of the maildirs.
255 FileUtils
.rm_rf(cfg
.dovecot_mail_root())