Remove pointless include.
[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 #
76 #
77 # +------------------------------------------------------+
78 # | shared |
79 # +-----+------------------+----------+------------------+
80 # | sid | user_from | calendar | user_which |
81 # +-----+------------------+----------+------------------+
82 # | 1 | adam@example.net | derp | beth@example.net |
83 # +-----+------------------+----------+------------------+
84 #
85 #
86 # 2. davical_test
87 #
88 # +-------------------------------------------------------+
89 # | usr |
90 # +---------+--------+----------------+-------------------+
91 # | user_no | active | joined | username |
92 # +---------+--------+----------------+-------------------+
93 # | 17 | t | 2014-01-04 ... | alice@example.com |
94 # +---------+--------+----------------+-------------------+
95 #
96 #
97 # +-----------------------------------------+
98 # | usr_setting |
99 # +---------+--------------+----------------+
100 # | user_no | setting_name | setting_value |
101 # +---------+--------------+----------------+
102 # | 17 | dumb setting | its dumb value |
103 # +---------+--------------+----------------+
104 #
105 #
106 # 3. postfixadmin_test
107 #
108 # +-------------+
109 # | domain |
110 # +-------------+
111 # | domain |
112 # +-------------+
113 # | ALL |
114 # +-------------+
115 # | example.com |
116 # +-------------+
117 # | example.net |
118 # +-------------+
119 #
120 #
121 # +----------------------------------------------+
122 # | mailbox |
123 # +-------------------+-------------+------------+
124 # | username | domain | local_part |
125 # +-------------------+-------------+------------+
126 # | alice@example.com | example.com | alice |
127 # +-------------------+-------------+------------+
128 # | bob@example.com | example.com | bob |
129 # +-------------------+-------------+------------+
130 # | adam@example.net | example.net | adam |
131 # +-------------------+-------------+------------+
132 # | beth@example.net | example.net | beth |
133 # +-------------------+-------------+------------+
134 # | carol@example.net | example.net | carol |
135 # +-------------------+-------------+------------+
136 #
137 #
138 # +------------------------------------------------------+
139 # | alias |
140 # +-------------------+-------------------+--------------+
141 # | address | goto | domain |
142 # +-------------------+-------------------+--------------+
143 # | alice@example.com | alice@example.com | example.com |
144 # +-------------------+-------------------+--------------+
145 # | bob@example.com | bob@example.com | example.com |
146 # +-------------------+-------------------+--------------+
147 # | adam@example.net | adam@example.net | example.net |
148 # +-------------------+-------------------+--------------+
149 # | beth@example.net | beth@example.net | example.net |
150 # +-------------------+-------------------+--------------+
151 # | carol@example.net | carol@example.net | example.net |
152 # +-------------------+-------------------+--------------+
153 #
154 #
155 # +---------------------------------+
156 # | domain_admins |
157 # +-------------------+-------------+
158 # | username | domain |
159 # +-------------------+-------------+
160 # | admin@example.com | example.com |
161 # +-------------------+-------------+
162 # | admin@example.com | example.net |
163 # +-------------------+-------------+
164 #
165 # 4. roundcube_test
166 #
167 #
168 # +---------+-------------------+
169 # | user_id | username |
170 # +---------+-------------------+
171 # | 1 | alice@example.com |
172 # +---------+-------------------+
173 # | 2 | adam@example.net |
174 # +---------+-------------------+
175
176 cfg = configuration()
177
178 # First create the "mail directories".
179 FileUtils.mkdir_p("#{cfg.dovecot_mail_root()}/example.com/alice")
180 FileUtils.mkdir_p("#{cfg.dovecot_mail_root()}/example.com/booger")
181 FileUtils.mkdir_p("#{cfg.dovecot_mail_root()}/example.com/jeremy")
182 FileUtils.mkdir_p("#{cfg.dovecot_mail_root()}/example.net/adam")
183
184 # Now the databases and their content.
185 connection = connect_superuser()
186
187 cfg.plugins.each do |plugin|
188 plugin_dbname = cfg.send("#{plugin}_dbname")
189 next if plugin_dbname.nil? # Skip the dovecot plugin
190 query = "CREATE DATABASE #{plugin_dbname};"
191 connection.query(query)
192
193 plugin_dbhost = cfg.send("#{plugin}_dbhost")
194 plugin_dbport = cfg.send("#{plugin}_dbport")
195 plugin_dbopts = cfg.send("#{plugin}_dbopts")
196 plugin_dbtty = cfg.send("#{plugin}_dbtty")
197 plugin_dbuser = cfg.send("#{plugin}_dbuser")
198 plugin_dbpass = cfg.send("#{plugin}_dbpass")
199
200 plugin_conn = PGconn.connect(plugin_dbhost,
201 plugin_dbport,
202 plugin_dbopts,
203 plugin_dbtty,
204 plugin_dbname,
205 plugin_dbuser,
206 plugin_dbpass)
207
208 sql = File.open("test/sql/#{plugin}.sql").read()
209 plugin_conn.query(sql)
210 sql = File.open("test/sql/#{plugin}-fixtures.sql").read()
211 plugin_conn.query(sql)
212 plugin_conn.close()
213 end
214
215 connection.close()
216 end
217
218
219 def teardown
220 # Drop all of the databases that were created in setup().
221 cfg = configuration()
222 connection = connect_superuser()
223
224 cfg.plugins.each do |plugin|
225 plugin_dbname = cfg.send("#{plugin}_dbname")
226 next if plugin_dbname.nil? # Skip the dovecot plugin
227 query = "DROP DATABASE #{plugin_dbname};"
228 connection.query(query)
229 end
230
231 connection.close()
232
233 # Get rid of the maildirs.
234 FileUtils.rm_r(cfg.dovecot_mail_root())
235 end
236
237 end