]>
gitweb.michael.orlitzky.com - mailshears.git/blob - test/mailshears_test.rb
1 require 'common/configuration'
3 require 'minitest/unit'
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.
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
= PGconn
.connect(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 # +-------------------+-------------------+--------------+
148 # | bob@example.com | bob@example.com | example.com |
149 # +-------------------+-------------------+--------------+
150 # | adam@example.net | adam@example.net | example.net |
151 # +-------------------+-------------------+--------------+
152 # | beth@example.net | beth@example.net | example.net |
153 # +-------------------+-------------------+--------------+
154 # | carol@example.net | carol@example.net | example.net |
155 # +-------------------+-------------------+--------------+
158 # +---------------------------------+
160 # +-------------------+-------------+
161 # | username | domain |
162 # +-------------------+-------------+
163 # | admin@example.com | example.com |
164 # +-------------------+-------------+
165 # | admin@example.com | example.net |
166 # +-------------------+-------------+
171 # +---------+--------------------+
172 # | user_id | username |
173 # +---------+--------------------+
174 # | 1 | alice@example.com |
175 # +---------+--------------------+
176 # | 2 | booger@example.com |
177 # +---------+--------------------+
178 # | 3 | adam@example.net |
179 # +---------+--------------------+
181 # First make sure we get rid of everything so we don't get random
182 # failures from databases and directories that already exist.
185 cfg
= configuration()
187 # First create the "mail directories".
188 FileUtils
.mkdir_p("#{cfg.dovecot_mail_root()}/example.com/alice")
189 FileUtils
.mkdir_p("#{cfg.dovecot_mail_root()}/example.com/booger")
190 FileUtils
.mkdir_p("#{cfg.dovecot_mail_root()}/example.com/jeremy")
191 FileUtils
.mkdir_p("#{cfg.dovecot_mail_root()}/example.net/adam")
193 # Now the databases and their content.
194 connection
= connect_superuser()
196 cfg
.plugins
.each
do |plugin
|
197 plugin_dbname
= cfg
.send("#{plugin}_dbname")
198 next if plugin_dbname
.nil? # Skip the dovecot plugin
199 query
= "CREATE DATABASE #{plugin_dbname};"
200 connection
.query(query
)
202 plugin_dbhost
= cfg
.send("#{plugin}_dbhost")
203 plugin_dbport
= cfg
.send("#{plugin}_dbport")
204 plugin_dbopts
= cfg
.send("#{plugin}_dbopts")
205 plugin_dbtty
= cfg
.send("#{plugin}_dbtty")
206 plugin_dbuser
= cfg
.send("#{plugin}_dbuser")
207 plugin_dbpass
= cfg
.send("#{plugin}_dbpass")
209 plugin_conn
= PGconn
.connect(plugin_dbhost
, plugin_dbport
, plugin_dbopts
,
210 plugin_dbtty
, plugin_dbname
, plugin_dbuser
,
213 sql
= File
.open("test/sql/#{plugin}.sql").read()
214 plugin_conn
.query(sql
)
215 sql
= File
.open("test/sql/#{plugin}-fixtures.sql").read()
216 plugin_conn
.query(sql
)
225 # Drop all of the databases that were created in setup().
226 cfg
= configuration()
227 connection
= connect_superuser()
229 # Don't emit notices about missing tables. Why this happens when I
230 # explicitly say IF EXISTS is beyond me.
231 connection
.set_notice_processor
{}
233 cfg
.plugins
.each
do |plugin
|
234 plugin_dbname
= cfg
.send("#{plugin}_dbname")
235 next if plugin_dbname
.nil? # Skip the dovecot plugin
236 query
= "DROP DATABASE IF EXISTS #{plugin_dbname};"
237 connection
.query(query
)
242 # Get rid of the maildirs.
243 FileUtils
.rm_rf(cfg
.dovecot_mail_root())