]> gitweb.michael.orlitzky.com - mailshears.git/blob - test/mailshears_test.rb
22c634f43b643b41c3764f21ec79fbaf5bc1c186
[mailshears.git] / test / mailshears_test.rb
1 # Without this, I get...
2 #
3 # Warning: you should require 'minitest/autorun' instead.
4 # Warning: or add 'gem "minitest"' before 'require "minitest/autorun"'
5 #
6 # Whatever.
7 gem 'minitest'
8 require 'minitest/autorun'
9
10 require 'common/configuration'
11 require 'fileutils'
12 require 'pg'
13
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.
18
19 def configuration()
20 # Return the test config object.
21 return Configuration.new('test/mailshears.test.conf.yml')
22 end
23
24 def maildir_exists(dir)
25 # Check if the given mail directory of the form "example.com/user"
26 # exists.
27 cfg = configuration()
28 return File.directory?("#{cfg.dovecot_mail_root()}/#{dir}")
29 end
30
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."
35 db_hash = {
36 :host => 'localhost',
37 :port => 5432,
38 :options => nil,
39 :dbname => 'postgres',
40 :user => 'postgres',
41 :password => nil
42 }
43 connection = PG::Connection.new(db_hash)
44
45 return connection
46 end
47
48 def setup
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.
53 #
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.
58 #
59 # == Filesystem ==
60 #
61 # * example.com/alice
62 # * example.com/booger
63 # * example.com/jeremy
64 # * example.net/adam
65 #
66 # == Databases ==
67 #
68 # 1. agendav_test
69 #
70 # +------------------------------+
71 # | prefs |
72 # +--------------------+---------+
73 # | username | options |
74 # +--------------------+---------+
75 # | adam@example.net | herp |
76 # +--------------------+---------+
77 # | booger@example.com | herp |
78 # +------------------ +---------+
79 #
80 #
81 # +---------------------------------------------------------------------------------------------------------------------+
82 # | shares |
83 # +-----+-----------------------------------+---------------------------------------------------+-----------------------+
84 # | sid | owner | calendar | with |
85 # +-----+-----------------------------------+---------------------------------------------------+-----------------------+
86 # | 1 | /caldav.php/adam%40example.net/ | /caldav.php/adam%40example.net/calendar-default | /beth%40example.net/ |
87 # +-----+-----------------------------------+---------------------------------------------------+-----------------------+
88 # | 2 | /caldav.php/booger%40example.com/ | /caldav.php/booger%40example.com/calendar-default | /carol%40example.net/ |
89 # +-----+-----------------------------------+---------------------------------------------------+-----------------------+
90 #
91 #
92 # 2. davical_test
93 #
94 # +--------------------------------------------------------+
95 # | usr |
96 # +---------+--------+----------------+--------------------+
97 # | user_no | active | joined | username |
98 # +---------+--------+----------------+--------------------+
99 # | 17 | t | 2014-01-04 ... | alice@example.com |
100 # +---------+--------+----------------+--------------------+
101 # | 18 | t | 2014-01-04 ... | booger@example.com |
102 # +---------+--------+----------------+--------------------+
103 #
104 #
105 # +-----------------------------------------+
106 # | usr_setting |
107 # +---------+--------------+----------------+
108 # | user_no | setting_name | setting_value |
109 # +---------+--------------+----------------+
110 # | 17 | dumb setting | its dumb value |
111 # +---------+--------------+----------------+
112 # | 18 | dumb setting | its dumb value |
113 # +---------+--------------+----------------+
114 #
115 #
116 # 3. postfixadmin_test
117 #
118 # +-------------+
119 # | domain |
120 # +-------------+
121 # | domain |
122 # +-------------+
123 # | ALL |
124 # +-------------+
125 # | example.com |
126 # +-------------+
127 # | example.net |
128 # +-------------+
129 #
130 #
131 # +----------------------------------------------+
132 # | mailbox |
133 # +-------------------+-------------+------------+
134 # | username | domain | local_part |
135 # +-------------------+-------------+------------+
136 # | alice@example.com | example.com | alice |
137 # +-------------------+-------------+------------+
138 # | bob@example.com | example.com | bob |
139 # +-------------------+-------------+------------+
140 # | adam@example.net | example.net | adam |
141 # +-------------------+-------------+------------+
142 # | beth@example.net | example.net | beth |
143 # +-------------------+-------------+------------+
144 # | carol@example.net | example.net | carol |
145 # +-------------------+-------------+------------+
146 #
147 #
148 # +-------------------------------------------------------+
149 # | alias |
150 # +-------------------+--------------------+--------------+
151 # | address | goto | domain |
152 # +-------------------+--------------------+--------------+
153 # | alice@example.com | alice@example.com, | example.com |
154 # | | adam@example.net, | |
155 # | | bob@example.com, | |
156 # | | carol@example.net | |
157 # +-------------------+--------------------+--------------+
158 # | bob@example.com | bob@example.com | example.com |
159 # +-------------------+--------------------+--------------+
160 # | adam@example.net | adam@example.net | example.net |
161 # +-------------------+--------------------+--------------+
162 # | beth@example.net | beth@example.net | example.net |
163 # +-------------------+--------------------+--------------+
164 # | carol@example.net | carol@example.net | example.net |
165 # +-------------------+--------------------+--------------+
166 #
167 #
168 # +---------------------------------+
169 # | domain_admins |
170 # +-------------------+-------------+
171 # | username | domain |
172 # +-------------------+-------------+
173 # | admin@example.com | example.com |
174 # +-------------------+-------------+
175 # | admin@example.com | example.net |
176 # +-------------------+-------------+
177 #
178 # 4. roundcube_test
179 #
180 #
181 # +---------+--------------------+
182 # | user_id | username |
183 # +---------+--------------------+
184 # | 1 | alice@example.com |
185 # +---------+--------------------+
186 # | 2 | booger@example.com |
187 # +---------+--------------------+
188 # | 3 | adam@example.net |
189 # +---------+--------------------+
190
191 # First make sure we get rid of everything so we don't get random
192 # failures from databases and directories that already exist.
193 teardown()
194
195 cfg = configuration()
196
197 # First create the "mail directories".
198 FileUtils.mkdir_p("#{cfg.dovecot_mail_root()}/example.com/alice")
199 FileUtils.mkdir_p("#{cfg.dovecot_mail_root()}/example.com/booger")
200 FileUtils.mkdir_p("#{cfg.dovecot_mail_root()}/example.com/jeremy")
201 FileUtils.mkdir_p("#{cfg.dovecot_mail_root()}/example.net/adam")
202
203 # Now the databases and their content.
204 connection = connect_superuser()
205
206 cfg.plugins.each do |plugin|
207 plugin_dbname = cfg.send("#{plugin}_dbname")
208 next if plugin_dbname.nil? # Skip the dovecot plugin
209 query = "CREATE DATABASE #{plugin_dbname};"
210 connection.sync_exec(query)
211
212 plugin_dbhash = {
213 :host => cfg.send("#{plugin}_dbhost"),
214 :port => cfg.send("#{plugin}_dbport"),
215 :options => cfg.send("#{plugin}_dbopts"),
216 :dbname => plugin_dbname,
217 :user => cfg.send("#{plugin}_dbuser"),
218 :password => cfg.send("#{plugin}_dbpass")
219 }
220
221 plugin_conn = PG::Connection.new(plugin_dbhash)
222
223 sql = File.open("test/sql/#{plugin}.sql").read()
224 plugin_conn.sync_exec(sql)
225 sql = File.open("test/sql/#{plugin}-fixtures.sql").read()
226 plugin_conn.sync_exec(sql)
227 plugin_conn.close()
228 end
229
230 connection.close()
231 end
232
233
234 def teardown
235 # Drop all of the databases that were created in setup().
236 cfg = configuration()
237 connection = connect_superuser()
238
239 # Don't emit notices about missing tables. Why this happens when I
240 # explicitly say IF EXISTS is beyond me.
241 connection.set_notice_processor{}
242
243 cfg.plugins.each do |plugin|
244 plugin_dbname = cfg.send("#{plugin}_dbname")
245 next if plugin_dbname.nil? # Skip the dovecot plugin
246 query = "DROP DATABASE IF EXISTS #{plugin_dbname};"
247 connection.sync_exec(query)
248 end
249
250 connection.close()
251
252 # Get rid of the maildirs.
253 FileUtils.rm_rf(cfg.dovecot_mail_root())
254 end
255
256 end