Fix variable names in the prune tests.
[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,
36 db_port,
37 db_opts,
38 db_tty,
39 db_name,
40 db_user,
41 db_pass)
42
43 return connection
44 end
45
46 def setup
47 # Connect to the database specified in the test configutation as
48 # the super user. Then, run all of the SQL scripts contained in
49 # test/sql. These scripts create the tables for the plugins listed
50 # in the test configuration, and then create some sample data.
51 #
52 # Here is the full list of what gets created. Every test case
53 # inheriting from this class can expect this schema and data to
54 # exist. The filesystem entries are located beneath mail_root from
55 # the configuration file.
56 #
57 # == Filesystem ==
58 #
59 # * example.com/alice
60 # * example.com/booger
61 # * example.com/jeremy
62 # * example.net/adam
63 #
64 # == Databases ==
65 #
66 # 1. agendav_test
67 #
68 # +------------------------------+
69 # | prefs |
70 # +--------------------+---------+
71 # | username | options |
72 # +--------------------+---------+
73 # | adam@example.net | herp |
74 # +--------------------+---------+
75 # | booger@example.com | herp |
76 # +------------------ +---------+
77 #
78 #
79 # +---------------------------------------------------------+
80 # | shared |
81 # +-----+--------------------+----------+-------------------+
82 # | sid | user_from | calendar | user_which |
83 # +-----+--------------------+----------+-------------------+
84 # | 1 | adam@example.net | derp | beth@example.net |
85 # +-----+--------------------+----------+-------------------+
86 # | 2 | booger@example.com | derp | carol@example.net |
87 # +-----+--------------------+----------+-------------------+
88 #
89 #
90 # 2. davical_test
91 #
92 # +--------------------------------------------------------+
93 # | usr |
94 # +---------+--------+----------------+--------------------+
95 # | user_no | active | joined | username |
96 # +---------+--------+----------------+--------------------+
97 # | 17 | t | 2014-01-04 ... | alice@example.com |
98 # +---------+--------+----------------+--------------------+
99 # | 18 | t | 2014-01-04 ... | booger@example.com |
100 # +---------+--------+----------------+--------------------+
101 #
102 #
103 # +-----------------------------------------+
104 # | usr_setting |
105 # +---------+--------------+----------------+
106 # | user_no | setting_name | setting_value |
107 # +---------+--------------+----------------+
108 # | 17 | dumb setting | its dumb value |
109 # +---------+--------------+----------------+
110 # | 18 | dumb setting | its dumb value |
111 # +---------+--------------+----------------+
112 #
113 #
114 # 3. postfixadmin_test
115 #
116 # +-------------+
117 # | domain |
118 # +-------------+
119 # | domain |
120 # +-------------+
121 # | ALL |
122 # +-------------+
123 # | example.com |
124 # +-------------+
125 # | example.net |
126 # +-------------+
127 #
128 #
129 # +----------------------------------------------+
130 # | mailbox |
131 # +-------------------+-------------+------------+
132 # | username | domain | local_part |
133 # +-------------------+-------------+------------+
134 # | alice@example.com | example.com | alice |
135 # +-------------------+-------------+------------+
136 # | bob@example.com | example.com | bob |
137 # +-------------------+-------------+------------+
138 # | adam@example.net | example.net | adam |
139 # +-------------------+-------------+------------+
140 # | beth@example.net | example.net | beth |
141 # +-------------------+-------------+------------+
142 # | carol@example.net | example.net | carol |
143 # +-------------------+-------------+------------+
144 #
145 #
146 # +------------------------------------------------------+
147 # | alias |
148 # +-------------------+-------------------+--------------+
149 # | address | goto | domain |
150 # +-------------------+-------------------+--------------+
151 # | alice@example.com | alice@example.com | example.com |
152 # +-------------------+-------------------+--------------+
153 # | bob@example.com | bob@example.com | example.com |
154 # +-------------------+-------------------+--------------+
155 # | adam@example.net | adam@example.net | example.net |
156 # +-------------------+-------------------+--------------+
157 # | beth@example.net | beth@example.net | example.net |
158 # +-------------------+-------------------+--------------+
159 # | carol@example.net | carol@example.net | example.net |
160 # +-------------------+-------------------+--------------+
161 #
162 #
163 # +---------------------------------+
164 # | domain_admins |
165 # +-------------------+-------------+
166 # | username | domain |
167 # +-------------------+-------------+
168 # | admin@example.com | example.com |
169 # +-------------------+-------------+
170 # | admin@example.com | example.net |
171 # +-------------------+-------------+
172 #
173 # 4. roundcube_test
174 #
175 #
176 # +---------+--------------------+
177 # | user_id | username |
178 # +---------+--------------------+
179 # | 1 | alice@example.com |
180 # +---------+--------------------+
181 # | 2 | booger@example.com |
182 # +---------+--------------------+
183 # | 3 | adam@example.net |
184 # +---------+--------------------+
185
186 cfg = configuration()
187
188 # First create the "mail directories".
189 FileUtils.mkdir_p("#{cfg.dovecot_mail_root()}/example.com/alice")
190 FileUtils.mkdir_p("#{cfg.dovecot_mail_root()}/example.com/booger")
191 FileUtils.mkdir_p("#{cfg.dovecot_mail_root()}/example.com/jeremy")
192 FileUtils.mkdir_p("#{cfg.dovecot_mail_root()}/example.net/adam")
193
194 # Now the databases and their content.
195 connection = connect_superuser()
196
197 cfg.plugins.each do |plugin|
198 plugin_dbname = cfg.send("#{plugin}_dbname")
199 next if plugin_dbname.nil? # Skip the dovecot plugin
200 query = "CREATE DATABASE #{plugin_dbname};"
201 connection.query(query)
202
203 plugin_dbhost = cfg.send("#{plugin}_dbhost")
204 plugin_dbport = cfg.send("#{plugin}_dbport")
205 plugin_dbopts = cfg.send("#{plugin}_dbopts")
206 plugin_dbtty = cfg.send("#{plugin}_dbtty")
207 plugin_dbuser = cfg.send("#{plugin}_dbuser")
208 plugin_dbpass = cfg.send("#{plugin}_dbpass")
209
210 plugin_conn = PGconn.connect(plugin_dbhost,
211 plugin_dbport,
212 plugin_dbopts,
213 plugin_dbtty,
214 plugin_dbname,
215 plugin_dbuser,
216 plugin_dbpass)
217
218 sql = File.open("test/sql/#{plugin}.sql").read()
219 plugin_conn.query(sql)
220 sql = File.open("test/sql/#{plugin}-fixtures.sql").read()
221 plugin_conn.query(sql)
222 plugin_conn.close()
223 end
224
225 connection.close()
226 end
227
228
229 def teardown
230 # Drop all of the databases that were created in setup().
231 cfg = configuration()
232 connection = connect_superuser()
233
234 cfg.plugins.each do |plugin|
235 plugin_dbname = cfg.send("#{plugin}_dbname")
236 next if plugin_dbname.nil? # Skip the dovecot plugin
237 query = "DROP DATABASE #{plugin_dbname};"
238 connection.query(query)
239 end
240
241 connection.close()
242
243 # Get rid of the maildirs.
244 FileUtils.rm_r(cfg.dovecot_mail_root())
245 end
246
247 end