]>
gitweb.michael.orlitzky.com - mailshears.git/blob - test/mailshears_test.rb
1 require 'common/configuration'
3 require 'minitest/autorun'
6 class MailshearsTest
< MiniTest
::Test
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.
12 # Return the test config object.
13 return Configuration
.new('test/mailshears.test.conf.yml')
16 def maildir_exists(dir
)
17 # Check if the given mail directory of the form "example.com/user"
20 return File
.directory
?("#{cfg.dovecot_mail_root()}/#{dir}")
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."
35 connection
= PG
::Connection.new(db_host
, db_port
, db_opts
, db_tty
,
36 db_name
, db_user
, db_pass
)
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.
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.
55 # * example.com/booger
56 # * example.com/jeremy
63 # +------------------------------+
65 # +--------------------+---------+
66 # | username | options |
67 # +--------------------+---------+
68 # | adam@example.net | herp |
69 # +--------------------+---------+
70 # | booger@example.com | herp |
71 # +------------------ +---------+
74 # +---------------------------------------------------------+
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 # +-----+--------------------+----------+-------------------+
87 # +--------------------------------------------------------+
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 # +---------+--------+----------------+--------------------+
98 # +-----------------------------------------+
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 # +---------+--------------+----------------+
109 # 3. postfixadmin_test
124 # +----------------------------------------------+
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 # +-------------------+-------------+------------+
141 # +-------------------------------------------------------+
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 # +-------------------+--------------------+--------------+
161 # +---------------------------------+
163 # +-------------------+-------------+
164 # | username | domain |
165 # +-------------------+-------------+
166 # | admin@example.com | example.com |
167 # +-------------------+-------------+
168 # | admin@example.com | example.net |
169 # +-------------------+-------------+
174 # +---------+--------------------+
175 # | user_id | username |
176 # +---------+--------------------+
177 # | 1 | alice@example.com |
178 # +---------+--------------------+
179 # | 2 | booger@example.com |
180 # +---------+--------------------+
181 # | 3 | adam@example.net |
182 # +---------+--------------------+
184 # First make sure we get rid of everything so we don't get random
185 # failures from databases and directories that already exist.
188 cfg
= configuration()
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")
196 # Now the databases and their content.
197 connection
= connect_superuser()
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
)
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")
212 plugin_conn
= PG
::Connection.new(plugin_dbhost
, plugin_dbport
,
213 plugin_dbopts
, plugin_dbtty
,
214 plugin_dbname
, plugin_dbuser
,
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
)
229 # Drop all of the databases that were created in setup().
230 cfg
= configuration()
231 connection
= connect_superuser()
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
{}
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
)
246 # Get rid of the maildirs.
247 FileUtils
.rm_rf(cfg
.dovecot_mail_root())