]> gitweb.michael.orlitzky.com - mailshears.git/blob - test/mailshears_test.rb
8e80c063f0a3a07092996d2eab49f82d50db5d8d
[mailshears.git] / test / mailshears_test.rb
1 require 'common/configuration'
2 require 'fileutils'
3 require 'minitest/unit'
4 require 'pg'
5
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.
10
11 def configuration()
12 # Return the test config object.
13 return Configuration.new('test/mailshears.test.conf.yml')
14 end
15
16 def maildir_exists(dir)
17 # Check if the given mail directory of the form "example.com/user"
18 # exists.
19 cfg = configuration()
20 return File.directory?("#{cfg.dovecot_mail_root()}/#{dir}")
21 end
22
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."
27 db_host = 'localhost'
28 db_port = 5432
29 db_opts = nil
30 db_tty = nil
31 db_name = 'postgres'
32 db_user = 'postgres'
33 db_pass = nil
34
35 connection = PGconn.connect(db_host, db_port, db_opts, db_tty,
36 db_name, db_user, db_pass)
37
38 return connection
39 end
40
41 def setup
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.
46 #
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.
51 #
52 # == Filesystem ==
53 #
54 # * example.com/alice
55 # * example.com/booger
56 # * example.com/jeremy
57 # * example.net/adam
58 #
59 # == Databases ==
60 #
61 # 1. agendav_test
62 #
63 # +------------------------------+
64 # | prefs |
65 # +--------------------+---------+
66 # | username | options |
67 # +--------------------+---------+
68 # | adam@example.net | herp |
69 # +--------------------+---------+
70 # | booger@example.com | herp |
71 # +------------------ +---------+
72 #
73 #
74 # +---------------------------------------------------------+
75 # | shared |
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 # +-----+--------------------+----------+-------------------+
83 #
84 #
85 # 2. davical_test
86 #
87 # +--------------------------------------------------------+
88 # | usr |
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 # +---------+--------+----------------+--------------------+
96 #
97 #
98 # +-----------------------------------------+
99 # | usr_setting |
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 # +---------+--------------+----------------+
107 #
108 #
109 # 3. postfixadmin_test
110 #
111 # +-------------+
112 # | domain |
113 # +-------------+
114 # | domain |
115 # +-------------+
116 # | ALL |
117 # +-------------+
118 # | example.com |
119 # +-------------+
120 # | example.net |
121 # +-------------+
122 #
123 #
124 # +----------------------------------------------+
125 # | mailbox |
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 # +-------------------+-------------+------------+
139 #
140 #
141 # +------------------------------------------------------+
142 # | alias |
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 # +-------------------+-------------------+--------------+
156 #
157 #
158 # +---------------------------------+
159 # | domain_admins |
160 # +-------------------+-------------+
161 # | username | domain |
162 # +-------------------+-------------+
163 # | admin@example.com | example.com |
164 # +-------------------+-------------+
165 # | admin@example.com | example.net |
166 # +-------------------+-------------+
167 #
168 # 4. roundcube_test
169 #
170 #
171 # +---------+--------------------+
172 # | user_id | username |
173 # +---------+--------------------+
174 # | 1 | alice@example.com |
175 # +---------+--------------------+
176 # | 2 | booger@example.com |
177 # +---------+--------------------+
178 # | 3 | adam@example.net |
179 # +---------+--------------------+
180
181 # First make sure we get rid of everything so we don't get random
182 # failures from databases and directories that already exist.
183 teardown()
184
185 cfg = configuration()
186
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")
192
193 # Now the databases and their content.
194 connection = connect_superuser()
195
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)
201
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")
208
209 plugin_conn = PGconn.connect(plugin_dbhost, plugin_dbport, plugin_dbopts,
210 plugin_dbtty, plugin_dbname, plugin_dbuser,
211 plugin_dbpass)
212
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)
217 plugin_conn.close()
218 end
219
220 connection.close()
221 end
222
223
224 def teardown
225 # Drop all of the databases that were created in setup().
226 cfg = configuration()
227 connection = connect_superuser()
228
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{}
232
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)
238 end
239
240 connection.close()
241
242 # Get rid of the maildirs.
243 FileUtils.rm_rf(cfg.dovecot_mail_root())
244 end
245
246 end