Add some tests for moving users.
[mailshears.git] / test / test_mv.rb
1 # WARNING: Test output is dependent on the order these classes include
2 # certain modules; i.e. on the 'require' order.
3 require 'common/domain'
4 require 'common/user'
5 require 'mailshears_test'
6 require 'minitest/autorun'
7 require 'mv/plugins/agendav'
8 require 'mv/plugins/davical'
9 require 'mv/plugins/dovecot'
10 require 'mv/plugins/postfixadmin'
11 require 'mv/plugins/roundcube'
12 require 'mv/mv_runner'
13
14
15 class TestMv < MailshearsTest
16
17 def test_mv_once
18 cfg = configuration()
19 src = User.new("alice@example.com")
20 dst = User.new("alice@example.net")
21
22 output_buffer = StringIO.new()
23
24 $stdout = output_buffer
25 MvPlugin.run(cfg, src, dst)
26 $stdout = STDOUT
27
28 actual = output_buffer.string()
29
30 expected =
31 "AgendavMv - Source user alice@example.com not found.\n" +
32 "DavicalMv - Moved user alice@example.com (Principal ID: 1) " +
33 "to alice@example.net (Principal ID: 1).\n" +
34 "DovecotMv - Moved user alice@example.com " +
35 "(#{cfg.dovecot_mail_root()}/example.com/alice) to " +
36 "alice@example.net (#{cfg.dovecot_mail_root()}/example.net/alice).\n" +
37 "PostfixadminMv - Moved user alice@example.com to alice@example.net.\n" +
38 "RoundcubeMv - Moved user alice@example.com (User ID: 1) to " +
39 "alice@example.net (User ID: 1).\n"
40
41 assert_equal(expected, actual)
42
43 # Now check the database.
44
45 amv = AgendavMv.new(cfg)
46 actual = amv.list_users()
47 expected = [User.new('adam@example.net'),User.new('booger@example.com')]
48 assert_equal(expected.sort(), actual.sort())
49
50 dmv = DavicalMv.new(cfg)
51 actual = dmv.list_users()
52 expected = [User.new('alice@example.net'), User.new('booger@example.com')]
53 assert_equal(expected.sort(), actual.sort())
54
55 pfamv = PostfixadminMv.new(cfg)
56 actual = pfamv.list_users()
57 expected = [User.new('alice@example.net'),
58 User.new('bob@example.com'),
59 User.new('adam@example.net'),
60 User.new('beth@example.net'),
61 User.new('carol@example.net')]
62 assert_equal(expected.sort(), actual.sort())
63
64 actual = pfamv.list_domains()
65 expected = [Domain.new('example.com'), Domain.new('example.net')]
66 assert_equal(expected.sort(), actual.sort())
67
68 actual = pfamv.list_aliases()
69 expected = [{'address' => 'alice@example.net',
70 'goto' => 'alice@example.net'},
71 {'address' => 'bob@example.com',
72 'goto' => 'bob@example.com'},
73 {'address' => 'adam@example.net',
74 'goto' => 'adam@example.net'},
75 {'address' => 'beth@example.net',
76 'goto' => 'beth@example.net'},
77 {'address' => 'carol@example.net',
78 'goto' => 'carol@example.net'}]
79 expected.each { |e| assert(actual.include?(e)) } # can't sort dicts
80 actual.each { |a| assert(expected.include?(a)) } # can't sort dicts
81
82
83 rmv = RoundcubeMv.new(cfg)
84 actual = rmv.list_users()
85 expected = [User.new('alice@example.net'),
86 User.new('booger@example.com'),
87 User.new('adam@example.net')]
88 assert_equal(expected.sort(), actual.sort())
89
90 # All the maildirs are still there.
91 assert(maildir_exists('example.net/alice'))
92 assert(maildir_exists('example.com/booger'))
93 assert(maildir_exists('example.com/jeremy'))
94 assert(maildir_exists('example.net/adam'))
95 end
96
97
98 def test_mv_twice
99 #
100 # If we move a user and then move it back, we should wind up back
101 # where we started.
102 #
103 cfg = configuration()
104 src = User.new("alice@example.com")
105 dst = User.new("alice@example.net")
106
107 output_buffer = StringIO.new()
108
109 $stdout = output_buffer
110 MvPlugin.run(cfg, src, dst)
111 MvPlugin.run(cfg, dst, src)
112 $stdout = STDOUT
113
114 # Skip output verification, it's ugly. But make sure the database
115 # has what we expect.
116
117 amv = AgendavMv.new(cfg)
118 actual = amv.list_users()
119 expected = [User.new('adam@example.net'),User.new('booger@example.com')]
120 assert_equal(expected.sort(), actual.sort())
121
122 dmv = DavicalMv.new(cfg)
123 actual = dmv.list_users()
124 expected = [User.new('alice@example.com'), User.new('booger@example.com')]
125 assert_equal(expected.sort(), actual.sort())
126
127 pfamv = PostfixadminMv.new(cfg)
128 actual = pfamv.list_users()
129 expected = [User.new('alice@example.com'),
130 User.new('bob@example.com'),
131 User.new('adam@example.net'),
132 User.new('beth@example.net'),
133 User.new('carol@example.net')]
134 assert_equal(expected.sort(), actual.sort())
135
136 actual = pfamv.list_domains()
137 expected = [Domain.new('example.com'), Domain.new('example.net')]
138 assert_equal(expected.sort(), actual.sort())
139
140 actual = pfamv.list_aliases()
141 expected = [{'address' => 'alice@example.com',
142 'goto' => 'alice@example.com'},
143 {'address' => 'bob@example.com',
144 'goto' => 'bob@example.com'},
145 {'address' => 'adam@example.net',
146 'goto' => 'adam@example.net'},
147 {'address' => 'beth@example.net',
148 'goto' => 'beth@example.net'},
149 {'address' => 'carol@example.net',
150 'goto' => 'carol@example.net'}]
151 expected.each { |e| assert(actual.include?(e)) } # can't sort dicts
152 actual.each { |a| assert(expected.include?(a)) } # can't sort dicts
153
154
155 rmv = RoundcubeMv.new(cfg)
156 actual = rmv.list_users()
157 expected = [User.new('alice@example.com'),
158 User.new('booger@example.com'),
159 User.new('adam@example.net')]
160 assert_equal(expected.sort(), actual.sort())
161
162 # All the maildirs are still there.
163 assert(maildir_exists('example.com/alice'))
164 assert(maildir_exists('example.com/booger'))
165 assert(maildir_exists('example.com/jeremy'))
166 assert(maildir_exists('example.net/adam'))
167 end
168
169 end