postfix_admin 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,28 +1,10 @@
1
1
  require 'data_mapper'
2
2
 
3
- #
4
- # This extension is to avoid 'ArgumentError: invalid date' when datetime value of
5
- # MySQL is '0000-00-00 00:00:00'.
6
- #
7
- class DateTime
8
- class << self
9
-
10
- alias org_new new
11
- def new(year = -4712, mon = 1, mday = 1, hour = 0, min = 0, sec = 0, offset = 0, start = Date::ITALY)
12
- if year == 0
13
- nil
14
- else
15
- org_new(year, mon, mday, hour, min, sec, offset, start)
16
- end
17
- end
18
- end
19
- end
20
-
21
3
  module PostfixAdmin
22
4
  class Admin
23
5
  include ::DataMapper::Resource
24
6
  property :username, String, :key => true
25
- property :password, String
7
+ property :password, String, :length => 0..255
26
8
  property :created, DateTime, :default => DateTime.now
27
9
  property :modified, DateTime, :default => DateTime.now
28
10
 
@@ -51,6 +33,11 @@ module PostfixAdmin
51
33
  exist_domain?('ALL')
52
34
  end
53
35
 
36
+ def clear_domains
37
+ domains.clear
38
+ save or raise "Could save Admin"
39
+ end
40
+
54
41
  def self.find(username)
55
42
  Admin.first(:username => username)
56
43
  end
@@ -59,12 +46,6 @@ module PostfixAdmin
59
46
  !!Admin.find(username)
60
47
  end
61
48
 
62
- def self.unnecessary
63
- all.delete_if do |admin|
64
- admin.domains.size > 0
65
- end
66
- end
67
-
68
49
  private
69
50
 
70
51
  def exist_domain?(domain_name)
@@ -110,6 +91,11 @@ module PostfixAdmin
110
91
  def num_total_aliases
111
92
  aliases.count - mailboxes.count
112
93
  end
94
+
95
+ def clear_admins
96
+ admins.clear
97
+ save or raise "Could not save Domain"
98
+ end
113
99
  end
114
100
 
115
101
  class DomainAdmin
@@ -128,7 +114,7 @@ module PostfixAdmin
128
114
  property :username, String, :key => true
129
115
  property :name, String
130
116
  property :domain_name, String, :field => 'domain'
131
- property :password, String
117
+ property :password, String, :length => 0..255
132
118
  property :maildir, String
133
119
  property :quota, Integer
134
120
  # property :local_part, String
@@ -160,6 +146,15 @@ module PostfixAdmin
160
146
 
161
147
  storage_names[:default] = 'alias'
162
148
 
149
+ def self.mailbox(address)
150
+ mail_alias = Alias.new
151
+ mail_alias.attributes = {
152
+ :address => address,
153
+ :goto => address,
154
+ }
155
+ mail_alias
156
+ end
157
+
163
158
  def self.find(address)
164
159
  Alias.first(:address => address)
165
160
  end
@@ -167,5 +162,9 @@ module PostfixAdmin
167
162
  def self.exist?(address)
168
163
  !!Alias.find(address)
169
164
  end
165
+
166
+ def mailbox?
167
+ Mailbox.exist?(address)
168
+ end
170
169
  end
171
170
  end
@@ -14,7 +14,7 @@ module PostfixAdmin
14
14
  runner{ @cli.show_summary(domain_name) }
15
15
  end
16
16
 
17
- desc "show [example.com]", "List of domains"
17
+ desc "show [example.com | user@example.com]", "Show domains or mailboxes"
18
18
  def show(domain_name=nil)
19
19
  runner{ @cli.show(domain_name) }
20
20
  end
@@ -45,6 +45,21 @@ module PostfixAdmin
45
45
  runner{ @cli.add_domain(domain_name) }
46
46
  end
47
47
 
48
+ desc "edit_domain example.com", "Edit a domain limitation"
49
+ method_option :aliases, :type => :numeric, :aliases => "-a", :desc => "Edit aliases limitation"
50
+ method_option :mailboxes, :type => :numeric, :aliases => "-m", :desc => "Edit mailboxes limitation"
51
+ method_option :maxquota, :type => :numeric, :aliases => "-q", :desc => "Edit max quota limitation"
52
+ def edit_domain(domain_name)
53
+ runner do
54
+ if options.size == 0
55
+ warn "Use one or more options."
56
+ help('edit_domain')
57
+ else
58
+ @cli.edit_domain(domain_name, options)
59
+ end
60
+ end
61
+ end
62
+
48
63
  desc "delete_domain example.com", "Delete a domain"
49
64
  def delete_domain(domain_name)
50
65
  runner{ @cli.delete_domain(domain_name) }
@@ -65,6 +80,19 @@ module PostfixAdmin
65
80
  runner{ @cli.add_account(address, password) }
66
81
  end
67
82
 
83
+ desc "edit_account user@example.com", "Edit an account"
84
+ method_option :quota, :type => :numeric, :aliases => "-q", :desc => "Edit quota limitation"
85
+ def edit_account(address)
86
+ runner do
87
+ if options.size == 0
88
+ warn "Use one or more options."
89
+ help('edit_account')
90
+ else
91
+ @cli.edit_account(address, options)
92
+ end
93
+ end
94
+ end
95
+
68
96
  desc "add_admin admin@example.com password", "Add an admin user"
69
97
  method_option :super, :type => :boolean, :aliases => "-s", :desc => "register as a super admin"
70
98
  def add_admin(user_name, password)
@@ -76,6 +104,12 @@ module PostfixAdmin
76
104
  runner{ @cli.add_admin_domain(user_name, domain_name) }
77
105
  end
78
106
 
107
+ desc "delete_admin_domain admin@example.com example.com", "Delete admin_domain"
108
+ def delete_admin_domain(user_name, domain_name)
109
+ runner{ @cli.delete_admin_domain(user_name, domain_name) }
110
+ end
111
+
112
+
79
113
  desc "add_alias alias@example.com goto@example.net", "Add an alias"
80
114
  def add_alias(address, goto)
81
115
  runner{ @cli.add_alias(address, goto) }
@@ -89,7 +123,7 @@ module PostfixAdmin
89
123
  desc "version", "Show postfix_admin version"
90
124
  def version
91
125
  require 'postfix_admin/version'
92
- say "postfix_admin #{VERSION}"
126
+ runner{ say "postfix_admin #{VERSION}" }
93
127
  end
94
128
 
95
129
  private
@@ -97,7 +131,7 @@ module PostfixAdmin
97
131
  def runner
98
132
  begin
99
133
  yield
100
- rescue => e
134
+ rescue Error, ArgumentError => e
101
135
  warn e.message
102
136
  end
103
137
  end
@@ -1,3 +1,3 @@
1
1
  module PostfixAdmin
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -1,10 +1,11 @@
1
- # -*- encoding: utf-8 -*-
2
1
  require File.expand_path('../lib/postfix_admin/version', __FILE__)
3
2
 
4
3
  Gem::Specification.new do |gem|
5
4
  gem.add_dependency 'thor'
6
5
  gem.add_dependency 'data_mapper'
6
+ gem.add_dependency 'do_mysql', '>= 0.10.9'
7
7
  gem.add_dependency 'dm-mysql-adapter'
8
+ gem.add_development_dependency 'rspec', '~> 2.11.0'
8
9
  gem.add_development_dependency 'dm-sqlite-adapter'
9
10
 
10
11
  gem.authors = ["Hitoshi Kurokawa"]
data/spec/base_spec.rb CHANGED
@@ -32,7 +32,7 @@ describe PostfixAdmin::Base do
32
32
  @base.config[:aliases].should == 30
33
33
  @base.config[:mailboxes].should == 30
34
34
  @base.config[:maxquota].should == 100
35
- @base.config[:mailbox_quota].should == 100 * 1024 * 1000
35
+ @base.config[:mailbox_quota].should == 100 * KB_TO_MB
36
36
  end
37
37
 
38
38
  it "#domain_exist?" do
@@ -112,9 +112,7 @@ describe PostfixAdmin::Base do
112
112
  describe "#add_admin_domain" do
113
113
  it "#add_admin_domain" do
114
114
  @base.add_admin_domain('admin@example.com', 'example.org')
115
- Admin.find('admin@example.com').domains.find do |domain|
116
- domain.domain_name == 'example.org'
117
- end.should be_true
115
+ Admin.find('admin@example.com').has_domain?('example.org').should be_true
118
116
  end
119
117
 
120
118
  it "can not add unknown domain for an admin" do
@@ -130,6 +128,25 @@ describe PostfixAdmin::Base do
130
128
  end
131
129
  end
132
130
 
131
+ describe "#delete_admin_domain" do
132
+ it "#delete_admin_domain" do
133
+ lambda{ @base.delete_admin_domain('admin@example.com', 'example.com') }.should_not raise_error
134
+ Admin.find('admin@example.com').has_domain?('example.com').should be_false
135
+ end
136
+
137
+ it "can not delete not administrated domain" do
138
+ lambda{ @base.delete_admin_domain('admin@example.com', 'example.org') }.should raise_error Error
139
+ end
140
+
141
+ it "raise error when unknown admin" do
142
+ lambda{ @base.delete_admin_domain('unknown_admin@example.com', 'example.com') }.should raise_error Error
143
+ end
144
+
145
+ it "raise error when unknown domain" do
146
+ lambda{ @base.delete_admin_domain('admin@example.com', 'unknown.example.com') }.should raise_error Error
147
+ end
148
+ end
149
+
133
150
  describe "#add_alias" do
134
151
  it "can add a new alias" do
135
152
  num_aliases = Alias.count
data/spec/cli_spec.rb CHANGED
@@ -1,10 +1,52 @@
1
1
 
2
2
  require 'postfix_admin/cli'
3
3
 
4
+ describe PostfixAdmin::CLI, "when config file does not exist" do
5
+ before do
6
+ config_initialize
7
+ @file = File.join(File.dirname(__FILE__) , 'tmp/postfix_admin.conf')
8
+ CLI.config_file = @file
9
+ FileUtils.rm(@file) if File.exist?(@file)
10
+ end
11
+
12
+ it "::config_file#=" do
13
+ CLI.config_file.should == @file
14
+ end
15
+
16
+ it "#new should raise SystemExit and create config_file, permission should be 600" do
17
+ lambda { CLI.new }.should raise_error SystemExit
18
+ File.exist?(@file).should === true
19
+ ("%o" % File.stat(@file).mode).should == "100600"
20
+ end
21
+ end
22
+
4
23
  describe PostfixAdmin::CLI do
5
24
  before do
6
25
  db_initialize
7
- @cli = PostfixAdmin::CLI.new
26
+ @cli = CLI.new
27
+ end
28
+
29
+ describe "#show" do
30
+ it "show all domains information by nil domain name" do
31
+ lambda { @cli.show(nil) }.should_not raise_error
32
+ end
33
+
34
+ it "show domain information" do
35
+ lambda { @cli.show('example.com') }.should_not raise_error
36
+ end
37
+
38
+ it "can use frozen domain name" do
39
+ domain = 'example.com'
40
+ lambda { @cli.show(domain.freeze) }.should_not raise_error
41
+ end
42
+
43
+ it "upcase will convert to downcase" do
44
+ lambda { @cli.show('ExAmpLe.CoM') }.should_not raise_error
45
+ end
46
+
47
+ it "when unknown domain, raises Error" do
48
+ lambda { @cli.show('unknown.example.com') }.should raise_error Error
49
+ end
8
50
  end
9
51
 
10
52
  it "#show_domain" do
@@ -12,9 +54,33 @@ describe PostfixAdmin::CLI do
12
54
  capture(:stdout){ @cli.show_domain }.should_not =~ /ALL/
13
55
  end
14
56
 
15
- it "#show_summary" do
16
- lambda { @cli.show_summary }.should_not raise_error
17
- lambda { @cli.show_summary('unknown.example.com') }.should raise_error Error
57
+ describe "#show_account" do
58
+ it "shows information of an account" do
59
+ lambda { @cli.show_account('user@example.com') }.should_not raise_error
60
+ capture(:stdout){ @cli.show_account('user@example.com') }.should =~ /Quota/
61
+ end
62
+
63
+ it "raises error when unknown account" do
64
+ lambda { @cli.show_account('unknown@example.com') }.should raise_error Error
65
+ end
66
+ end
67
+
68
+ describe "#show_summary" do
69
+ it "show summary of all domain" do
70
+ lambda { @cli.show_summary }.should_not raise_error
71
+ end
72
+
73
+ it "show summary of domain" do
74
+ lambda { @cli.show_summary('example.com') }.should_not raise_error
75
+ end
76
+
77
+ it "upcase will convert to downcase" do
78
+ lambda { @cli.show_summary('example.COM') }.should_not raise_error
79
+ end
80
+
81
+ it "when unknown domain, raises Error" do
82
+ lambda { @cli.show_summary('unknown.example.com') }.should raise_error Error
83
+ end
18
84
  end
19
85
 
20
86
  it "#show_admin" do
@@ -87,6 +153,17 @@ describe PostfixAdmin::CLI do
87
153
  Admin.exist?('admin@example.com').should be_false
88
154
  end
89
155
 
156
+ it "can delete a super admin" do
157
+ lambda { @cli.delete_admin('all@example.com') }.should_not raise_error
158
+ Admin.exist?('all@example.com').should be_false
159
+ end
160
+
161
+ it "can delete an admin whish has multiple domains" do
162
+ @cli.add_admin_domain('admin@example.com', 'example.org')
163
+ lambda { @cli.delete_admin('admin@example.com') }.should_not raise_error
164
+ Admin.exist?('admin@example.com').should be_false
165
+ end
166
+
90
167
  it "can not delete unknown admin" do
91
168
  lambda { @cli.delete_admin('unknown_admin@example.com') }.should raise_error Error
92
169
  end
@@ -132,34 +209,78 @@ describe PostfixAdmin::CLI do
132
209
  end
133
210
  end
134
211
 
135
- it "add and delete methods" do
136
- lambda { @cli.add_domain('example.net') }.should_not raise_error
212
+ describe "#add_domain" do
213
+ it "can add a new domain" do
214
+ lambda { @cli.add_domain('example.net') }.should_not raise_error
215
+ end
137
216
 
138
- lambda { @cli.add_admin('admin@example.net', 'password') }.should_not raise_error
139
- lambda { @cli.add_admin_domain('admin@example.net', 'example.net') }.should_not raise_error
217
+ it "upcase will convert to downcase" do
218
+ lambda{ @cli.add_domain('ExAmPle.NeT') }.should_not raise_error
219
+ Domain.exist?('example.net').should be_true
220
+ end
140
221
 
141
- @cli.add_admin('admin2@example.net', 'password')
142
- @cli.add_admin_domain('admin2@example.net', 'example.net')
222
+ it "can not add exist domain" do
223
+ lambda{ @cli.add_domain('example.com') }.should raise_error Error
224
+ lambda{ @cli.add_domain('ExAmPle.Com') }.should raise_error Error
225
+ end
226
+ end
143
227
 
144
- @cli.add_admin('common@example.net', 'password')
145
- @cli.add_admin_domain('common@example.net', 'example.com')
146
- @cli.add_admin_domain('common@example.net', 'example.net')
147
- lambda { @cli.delete_admin('common@example.net') }.should_not raise_error
148
- Admin.exist?('common@example.net').should be_false
228
+ describe "#edit_domain" do
229
+ it "can update domain limitations" do
230
+ lambda{ @cli.edit_domain('example.com', {:aliases => 40, :mailboxes => 40, :maxquota => 400}) }.should_not raise_error
231
+ domain = Domain.find('example.com')
232
+ domain.maxaliases.should == 40
233
+ domain.maxmailboxes.should == 40
234
+ domain.maxquota.should == 400
235
+ end
236
+ end
149
237
 
150
- @cli.add_admin('common@example.net', 'password')
151
- @cli.add_admin_domain('common@example.net', 'example.com')
152
- @cli.add_admin_domain('common@example.net', 'example.net')
238
+ describe "#edit_account" do
239
+ it "can update account" do
240
+ lambda { @cli.edit_account('user@example.com', {:quota => 50}) }.should_not raise_error
241
+ Mailbox.find('user@example.com').quota.should == 50 * KB_TO_MB
242
+ end
153
243
 
154
- lambda { @cli.add_account('user1@example.net', 'password') }.should_not raise_error
155
- lambda { @cli.add_account('user2@example.net', 'password') }.should_not raise_error
244
+ it "raise error when unknown account" do
245
+ lambda { @cli.edit_account('unknown@example.com', {:quota => 50}) }.should raise_error Error
246
+ end
247
+ end
248
+
249
+ describe "#delete_domain" do
250
+ it "can delete exist domain" do
251
+ lambda { @cli.delete_domain('example.com') }.should_not raise_error
252
+ Domain.exist?('example.net').should be_false
253
+ end
254
+
255
+ it "upcase will convert to downcase" do
256
+ lambda { @cli.delete_domain('eXaMplE.cOm') }.should_not raise_error
257
+ Domain.exist?('example.com').should be_false
258
+ end
259
+
260
+ it "can delete related admins, addresses and aliases" do
261
+ @cli.add_admin('admin@example.org', 'password')
262
+ @cli.add_admin_domain('admin@example.org', 'example.org')
263
+ @cli.add_account('user2@example.com', 'password')
156
264
 
157
- lambda { @cli.delete_domain('example.net') }.should_not raise_error
158
- Admin.exist?('admin@example.net').should be_false
159
- Admin.exist?('admin2@example.net').should be_false
265
+ @cli.add_admin('other_admin@example.com', 'password')
266
+ @cli.add_admin_domain('other_admin@example.com', 'example.com')
160
267
 
161
- Admin.exist?('common@example.net').should be_true
162
- lambda { @cli.delete_admin('common@example.net') }.should_not raise_error
163
- Admin.exist?('common@example.net').should be_false
268
+ @cli.add_admin('no_related@example.com', 'password')
269
+
270
+ lambda { @cli.delete_domain('example.com') }.should_not raise_error
271
+ Admin.exist?('admin@example.com').should be_false
272
+ Admin.exist?('admin@example.org').should be_true
273
+ Admin.exist?('other_admin@example.com').should be_false
274
+ Admin.exist?('no_related@example.com').should be_true
275
+
276
+ # aliases should be removed
277
+ Alias.exist?('alias@example.com').should be_false
278
+ Alias.exist?('user@example.com').should be_false
279
+ Alias.exist?('user2@example.com').should be_false
280
+
281
+ # mailboxes should be removed
282
+ Mailbox.exist?('user@example.com').should be_false
283
+ Mailbox.exist?('user2@example.com').should be_false
284
+ end
164
285
  end
165
286
  end