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