]>
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."
39 :dbname => 'postgres',
43 connection
= PG
::Connection.new(db_hash
)
49 # Connect to the database specified in the test configutation as
50 # the super user. Then, run all of the SQL scripts contained in
51 # test/sql. These scripts create the tables for the plugins listed
52 # in the test configuration, and then create some sample data.
54 # Here is the full list of what gets created. Every test case
55 # inheriting from this class can expect this schema and data to
56 # exist. The filesystem entries are located beneath mail_root from
57 # the configuration file.
62 # * example.com/booger
63 # * example.com/jeremy
70 # +--------------------------------------------------------+
72 # +---------+--------+----------------+--------------------+
73 # | user_no | active | joined | username |
74 # +---------+--------+----------------+--------------------+
75 # | 17 | t | 2014-01-04 ... | alice@example.com |
76 # +---------+--------+----------------+--------------------+
77 # | 18 | t | 2014-01-04 ... | booger@example.com |
78 # +---------+--------+----------------+--------------------+
81 # +-----------------------------------------+
83 # +---------+--------------+----------------+
84 # | user_no | setting_name | setting_value |
85 # +---------+--------------+----------------+
86 # | 17 | dumb setting | its dumb value |
87 # +---------+--------------+----------------+
88 # | 18 | dumb setting | its dumb value |
89 # +---------+--------------+----------------+
92 # 2. postfixadmin_test
107 # +----------------------------------------------+
109 # +-------------------+-------------+------------+
110 # | username | domain | local_part |
111 # +-------------------+-------------+------------+
112 # | alice@example.com | example.com | alice |
113 # +-------------------+-------------+------------+
114 # | bob@example.com | example.com | bob |
115 # +-------------------+-------------+------------+
116 # | adam@example.net | example.net | adam |
117 # +-------------------+-------------+------------+
118 # | beth@example.net | example.net | beth |
119 # +-------------------+-------------+------------+
120 # | carol@example.net | example.net | carol |
121 # +-------------------+-------------+------------+
124 # +-------------------------------------------------------+
126 # +-------------------+--------------------+--------------+
127 # | address | goto | domain |
128 # +-------------------+--------------------+--------------+
129 # | alice@example.com | alice@example.com, | example.com |
130 # | | adam@example.net, | |
131 # | | bob@example.com, | |
132 # | | carol@example.net | |
133 # +-------------------+--------------------+--------------+
134 # | bob@example.com | bob@example.com | example.com |
135 # +-------------------+--------------------+--------------+
136 # | adam@example.net | adam@example.net | example.net |
137 # +-------------------+--------------------+--------------+
138 # | beth@example.net | beth@example.net | example.net |
139 # +-------------------+--------------------+--------------+
140 # | carol@example.net | carol@example.net | example.net |
141 # +-------------------+--------------------+--------------+
144 # +---------------------------------+
146 # +-------------------+-------------+
147 # | username | domain |
148 # +-------------------+-------------+
149 # | admin@example.com | example.com |
150 # +-------------------+-------------+
151 # | admin@example.com | example.net |
152 # +-------------------+-------------+
157 # +---------+--------------------+
158 # | user_id | username |
159 # +---------+--------------------+
160 # | 1 | alice@example.com |
161 # +---------+--------------------+
162 # | 2 | booger@example.com |
163 # +---------+--------------------+
164 # | 3 | adam@example.net |
165 # +---------+--------------------+
167 # First make sure we get rid of everything so we don't get random
168 # failures from databases and directories that already exist.
171 cfg
= configuration()
173 # First create the "mail directories".
174 FileUtils
.mkdir_p("#{cfg.dovecot_mail_root()}/example.com/alice")
175 FileUtils
.mkdir_p("#{cfg.dovecot_mail_root()}/example.com/booger")
176 FileUtils
.mkdir_p("#{cfg.dovecot_mail_root()}/example.com/jeremy")
177 FileUtils
.mkdir_p("#{cfg.dovecot_mail_root()}/example.net/adam")
179 # Now the databases and their content.
180 connection
= connect_superuser()
182 cfg
.plugins
.each
do |plugin
|
183 plugin_dbname
= cfg
.send("#{plugin}_dbname")
184 next if plugin_dbname
.nil? # Skip the dovecot plugin
185 query
= "CREATE DATABASE #{plugin_dbname};"
186 connection
.sync_exec(query
)
189 :host => cfg
.send("#{plugin}_dbhost"),
190 :port => cfg
.send("#{plugin}_dbport"),
191 :options => cfg
.send("#{plugin}_dbopts"),
192 :dbname => plugin_dbname
,
193 :user => cfg
.send("#{plugin}_dbuser"),
194 :password => cfg
.send("#{plugin}_dbpass")
197 plugin_conn
= PG
::Connection.new(plugin_dbhash
)
199 sql
= File
.open("test/sql/#{plugin}.sql").read()
200 plugin_conn
.sync_exec(sql
)
201 sql
= File
.open("test/sql/#{plugin}-fixtures.sql").read()
202 plugin_conn
.sync_exec(sql
)
211 # Drop all of the databases that were created in setup().
212 cfg
= configuration()
213 connection
= connect_superuser()
215 # Don't emit notices about missing tables. Why this happens when I
216 # explicitly say IF EXISTS is beyond me.
217 connection
.set_notice_processor
{}
219 cfg
.plugins
.each
do |plugin
|
220 plugin_dbname
= cfg
.send("#{plugin}_dbname")
221 next if plugin_dbname
.nil? # Skip the dovecot plugin
222 query
= "DROP DATABASE IF EXISTS #{plugin_dbname};"
223 connection
.sync_exec(query
)
228 # Get rid of the maildirs.
229 FileUtils
.rm_rf(cfg
.dovecot_mail_root())