]> gitweb.michael.orlitzky.com - mailshears.git/blob - test/mailshears_test.rb
mailshears.gemspec: bump version to 0.1.0
[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. davical_test
69 #
70 # +--------------------------------------------------------+
71 # | usr |
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 # +---------+--------+----------------+--------------------+
79 #
80 #
81 # +-----------------------------------------+
82 # | usr_setting |
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 # +---------+--------------+----------------+
90 #
91 #
92 # 2. postfixadmin_test
93 #
94 # +-------------+
95 # | domain |
96 # +-------------+
97 # | domain |
98 # +-------------+
99 # | ALL |
100 # +-------------+
101 # | example.com |
102 # +-------------+
103 # | example.net |
104 # +-------------+
105 #
106 #
107 # +----------------------------------------------+
108 # | mailbox |
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 # +-------------------+-------------+------------+
122 #
123 #
124 # +-------------------------------------------------------+
125 # | alias |
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 # +-------------------+--------------------+--------------+
142 #
143 #
144 # +---------------------------------+
145 # | domain_admins |
146 # +-------------------+-------------+
147 # | username | domain |
148 # +-------------------+-------------+
149 # | admin@example.com | example.com |
150 # +-------------------+-------------+
151 # | admin@example.com | example.net |
152 # +-------------------+-------------+
153 #
154 # 3. roundcube_test
155 #
156 #
157 # +---------+--------------------+
158 # | user_id | username |
159 # +---------+--------------------+
160 # | 1 | alice@example.com |
161 # +---------+--------------------+
162 # | 2 | booger@example.com |
163 # +---------+--------------------+
164 # | 3 | adam@example.net |
165 # +---------+--------------------+
166
167 # First make sure we get rid of everything so we don't get random
168 # failures from databases and directories that already exist.
169 teardown()
170
171 cfg = configuration()
172
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")
178
179 # Now the databases and their content.
180 connection = connect_superuser()
181
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)
187
188 plugin_dbhash = {
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")
195 }
196
197 plugin_conn = PG::Connection.new(plugin_dbhash)
198
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)
203 plugin_conn.close()
204 end
205
206 connection.close()
207 end
208
209
210 def teardown
211 # Drop all of the databases that were created in setup().
212 cfg = configuration()
213 connection = connect_superuser()
214
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{}
218
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)
224 end
225
226 connection.close()
227
228 # Get rid of the maildirs.
229 FileUtils.rm_rf(cfg.dovecot_mail_root())
230 end
231
232 end