7ded82016072a526874f0c68bc172738c69d3b71
[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' => 'adam@example.net',
70 'goto' => 'adam@example.net'},
71 {'address' => 'alice@example.net',
72 'goto' => 'alice@example.net,' +
73 'adam@example.net,' +
74 'bob@example.com,' +
75 'carol@example.net'},
76 {'address' => 'beth@example.net',
77 'goto' => 'beth@example.net'},
78 {'address' => 'bob@example.com',
79 'goto' => 'bob@example.com'},
80 {'address' => 'carol@example.net',
81 'goto' => 'carol@example.net'}]
82 expected.each { |e| assert(actual.include?(e)) } # can't sort dicts
83 actual.each { |a| assert(expected.include?(a)) } # can't sort dicts
84
85
86 rmv = RoundcubeMv.new(cfg)
87 actual = rmv.list_users()
88 expected = [User.new('alice@example.net'),
89 User.new('booger@example.com'),
90 User.new('adam@example.net')]
91 assert_equal(expected.sort(), actual.sort())
92
93 # All the maildirs are still there.
94 assert(maildir_exists('example.net/alice'))
95 assert(maildir_exists('example.com/booger'))
96 assert(maildir_exists('example.com/jeremy'))
97 assert(maildir_exists('example.net/adam'))
98 end
99
100
101 def test_mv_twice
102 #
103 # If we move a user and then move it back, we should wind up back
104 # where we started.
105 #
106 cfg = configuration()
107 src = User.new("alice@example.com")
108 dst = User.new("alice@example.net")
109
110 output_buffer = StringIO.new()
111
112 $stdout = output_buffer
113 MvPlugin.run(cfg, src, dst)
114 MvPlugin.run(cfg, dst, src)
115 $stdout = STDOUT
116
117 # Skip output verification, it's ugly. But make sure the database
118 # has what we expect.
119
120 amv = AgendavMv.new(cfg)
121 actual = amv.list_users()
122 expected = [User.new('adam@example.net'),User.new('booger@example.com')]
123 assert_equal(expected.sort(), actual.sort())
124
125 dmv = DavicalMv.new(cfg)
126 actual = dmv.list_users()
127 expected = [User.new('alice@example.com'), User.new('booger@example.com')]
128 assert_equal(expected.sort(), actual.sort())
129
130 pfamv = PostfixadminMv.new(cfg)
131 actual = pfamv.list_users()
132 expected = [User.new('alice@example.com'),
133 User.new('bob@example.com'),
134 User.new('adam@example.net'),
135 User.new('beth@example.net'),
136 User.new('carol@example.net')]
137 assert_equal(expected.sort(), actual.sort())
138
139 actual = pfamv.list_domains()
140 expected = [Domain.new('example.com'), Domain.new('example.net')]
141 assert_equal(expected.sort(), actual.sort())
142
143 actual = pfamv.list_aliases()
144 expected = [{'address' => 'adam@example.net',
145 'goto' => 'adam@example.net'},
146 {'address' => 'alice@example.com',
147 'goto' => 'alice@example.com,' +
148 'adam@example.net,' +
149 'bob@example.com,' +
150 'carol@example.net'},
151 {'address' => 'beth@example.net',
152 'goto' => 'beth@example.net'},
153 {'address' => 'bob@example.com',
154 'goto' => 'bob@example.com'},
155 {'address' => 'carol@example.net',
156 'goto' => 'carol@example.net'}]
157 expected.each { |e| assert(actual.include?(e)) } # can't sort dicts
158 actual.each { |a| assert(expected.include?(a)) } # can't sort dicts
159
160
161 rmv = RoundcubeMv.new(cfg)
162 actual = rmv.list_users()
163 expected = [User.new('alice@example.com'),
164 User.new('booger@example.com'),
165 User.new('adam@example.net')]
166 assert_equal(expected.sort(), actual.sort())
167
168 # All the maildirs are still there.
169 assert(maildir_exists('example.com/alice'))
170 assert(maildir_exists('example.com/booger'))
171 assert(maildir_exists('example.com/jeremy'))
172 assert(maildir_exists('example.net/adam'))
173 end
174
175 end