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