Add a real rm test.
[mailshears.git] / test / rm_account_test.rb
1 require 'pg'
2 require 'test/unit'
3
4 require 'common/configuration'
5
6 class RmAccountTest < Test::Unit::TestCase
7
8 TESTCONF_PATH = 'test/mailshears.test.conf.yml'
9
10 def connect_superuser()
11 db_host = 'localhost'
12 db_port = 5432
13 db_opts = nil
14 db_tty = nil
15 db_name = 'postgres'
16 db_user = 'postgres'
17 db_pass = nil
18
19 connection = PGconn.connect(db_host,
20 db_port,
21 db_opts,
22 db_tty,
23 db_name,
24 db_user,
25 db_pass)
26
27 return connection
28 end
29
30
31 def test_single_rm
32 cfg = Configuration.new(TESTCONF_PATH)
33 argv = ["adam@example.net"]
34
35 # Load each of the plugins that we'll need.
36 cfg.plugins.each do |plugin_file|
37 require "rm/plugins/#{plugin_file}"
38 end
39
40 # And the runners.
41 require "rm/rm_runner"
42 require "rm/rm_dummy_runner"
43
44 require 'stringio'
45 output_buffer = StringIO.new()
46
47 $stdout = output_buffer
48 plugin_class = RmPlugin.run(cfg, *argv)
49 $stdout = STDOUT
50
51 actual = output_buffer.string()
52
53 expected = "PostfixadminRm - Removed user: " +
54 "adam@example.net (adam@example.net)\n" +
55 "RoundcubeRm - Removed user: adam@example.net (User ID: 2)\n" +
56 "AgendavRm - Removed user: adam@example.net " +
57 "(Username: adam@example.net)\n" +
58 "DavicalRm - User not found: adam@example.net\n"
59 assert(actual == expected)
60 end
61
62 def setup
63 # Create databases using from the test configuration file.
64 cfg = Configuration.new(TESTCONF_PATH)
65 connection = connect_superuser()
66
67 cfg.plugins.each do |plugin|
68 plugin_dbname = cfg.send("#{plugin}_dbname")
69 query = "CREATE DATABASE #{plugin_dbname};"
70 connection.query(query)
71
72 plugin_dbhost = cfg.send("#{plugin}_dbhost")
73 plugin_dbport = cfg.send("#{plugin}_dbport")
74 plugin_dbopts = cfg.send("#{plugin}_dbopts")
75 plugin_dbtty = cfg.send("#{plugin}_dbtty")
76 plugin_dbuser = cfg.send("#{plugin}_dbuser")
77 plugin_dbpass = cfg.send("#{plugin}_dbpass")
78
79 plugin_conn = PGconn.connect(plugin_dbhost,
80 plugin_dbport,
81 plugin_dbopts,
82 plugin_dbtty,
83 plugin_dbname,
84 plugin_dbuser,
85 plugin_dbpass)
86
87 sql = File.open("test/sql/#{plugin}.sql").read()
88 plugin_conn.query(sql)
89 sql = File.open("test/sql/#{plugin}-fixtures.sql").read()
90 plugin_conn.query(sql)
91 plugin_conn.close()
92 end
93
94 connection.close()
95 end
96
97
98 def teardown
99 # Destroy databases using from the test configuration file.
100 cfg = Configuration.new(TESTCONF_PATH)
101 connection = connect_superuser()
102
103 cfg.plugins.each do |plugin|
104 plugin_dbname = cfg.send("#{plugin}_dbname")
105 query = "DROP DATABASE #{plugin_dbname};"
106 connection.query(query)
107 end
108
109 connection.close()
110 end
111
112 end