ivanoats-whm_xml 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/whm.rb CHANGED
@@ -7,10 +7,12 @@ require 'active_support' # For stringifying keys, etc.
7
7
  require 'parameters' # For parameter requirements in methods
8
8
  require 'validatable' # For object validation
9
9
 
10
- require 'whm/exceptions'
11
- require 'whm/server'
12
- require 'whm/account'
10
+ WHM_DIRECTORY = File.join(File.dirname(__FILE__),'whm')
11
+
12
+ require File.join(WHM_DIRECTORY,'exceptions')
13
+ require File.join(WHM_DIRECTORY,'server')
14
+ require File.join(WHM_DIRECTORY,'account')
13
15
 
14
16
  module Whm
15
- VERSION = '0.3.0'
17
+ VERSION = '0.3.1'
16
18
  end
@@ -0,0 +1,64 @@
1
+ module Whm
2
+ class Account < Server #:nodoc:
3
+ include Parameters, Validatable
4
+
5
+ validates_presence_of :username, :domain, :groups => :creation
6
+ validates_format_of :savepkg, :with => /(1|0)/, :message => "must be 1 or 0", :groups => :creation
7
+
8
+ attr_accessor :server
9
+ attr_accessor :attributes
10
+ attr_accessor :writable_attributes
11
+
12
+
13
+ @@default_attributes = {}
14
+ @@writable_attributes = %w(username domain plan pkgname savepkg featurelist quota password ip cgi frontpage hasshell contactemail cpmod maxftp maxsql maxpop maxlst maxsub maxpark maxaddon bwlimit customip language useregns hasuseregns reseller)
15
+ @@readonly_attributes = %w(disklimit diskused email ip owner partition plan startdate suspended suspendreason theme unix_startdate user)
16
+
17
+ def initialize(attributes = {})
18
+ self.attributes = attributes
19
+ self.writable_attributes = {}
20
+ end
21
+
22
+ def user
23
+ self.attributes['user']
24
+ end
25
+ alias :name :user
26
+
27
+ (@@readonly_attributes).each do |attribute|
28
+ define_method attribute do
29
+ self.attributes[attribute.to_s]
30
+ end
31
+ end
32
+
33
+ (@@writable_attributes).each do |attribute|
34
+ define_method attribute do
35
+ self.writable_attributes[attribute.to_s] || self.attributes[attribute.to_s]
36
+ end
37
+ define_method "#{attribute}=" do |*parameters|
38
+ raise ArgumentError, "expected 1 parameter" unless parameters.length == 1
39
+ self.writable_attributes[attribute.to_s] = parameters.first
40
+ end
41
+ end
42
+
43
+ def password=(password)
44
+ server.change_account_password(:user => user, :pass => password)
45
+ end
46
+
47
+ def suspend!( reason = '')
48
+ server.suspend_account(:user => user, :reason => reason)
49
+ end
50
+
51
+ def unsuspend!
52
+ server.unsuspend_account(:user => user)
53
+ end
54
+
55
+ def terminate!( keepdns = "n")
56
+ server.terminate_account(:user => user,:keepdns => keepdns)
57
+ end
58
+
59
+ def package=( new_package)
60
+ server.change_package(:user => user, :pkg => new_package)
61
+ end
62
+
63
+ end
64
+ end
@@ -0,0 +1,111 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+ require 'lib/whm'
3
+ require 'pp'
4
+
5
+ describe "A Whm Account that doesn't exist" do
6
+
7
+ before do
8
+ @server = Whm::Server.new(
9
+ :username => "username",
10
+ :password => "password",
11
+ :host => "dedicated.server.com"
12
+ )
13
+ end
14
+
15
+ it "should throw an exception when an account doesn't exist" do
16
+ data = open(File.dirname(__FILE__) + '/fixtures/error2.xml').read
17
+
18
+ @server.expects(:get_xml).with(:url => "accountsummary", :params => {:user=>"doesntexist"}).returns(XmlSimple.xml_in(data, { 'ForceArray' => false }))
19
+ lambda { @server.account("doesntexist")}.should raise_error( Whm::CommandFailedError)
20
+ end
21
+ end
22
+
23
+ describe "Whm Accounts" do
24
+ before do
25
+ @server = Whm::Server.new(
26
+ :username => "username",
27
+ :password => "password",
28
+ :host => "dedicated.server.com"
29
+ )
30
+ end
31
+
32
+ it "should find all accounts" do
33
+ data = open(File.dirname(__FILE__) + '/fixtures/listaccts.xml').read
34
+ @server.expects(:get_xml).returns(XmlSimple.xml_in(data, { 'ForceArray' => false }))
35
+
36
+ @server.accounts.length.should == 3
37
+ end
38
+
39
+ it "should find all accounts when there is only one account" do
40
+ data = open(File.dirname(__FILE__) + '/fixtures/listaccts_single.xml').read
41
+ @server.expects(:get_xml).returns(XmlSimple.xml_in(data, { 'ForceArray' => false }))
42
+ @server.accounts.length.should == 1
43
+ end
44
+
45
+ it "should find an account by name" do
46
+ data = open(File.dirname(__FILE__) + '/fixtures/accountsummary.xml').read
47
+ @server.expects(:get_xml).with(:url => "accountsummary",:params => {:user=>"magic"}).returns(XmlSimple.xml_in(data, { 'ForceArray' => false }))
48
+ @server.account("magic").should_not be_nil
49
+ end
50
+ end
51
+
52
+ describe "A Whm Account" do
53
+
54
+ before do
55
+ @server = Whm::Server.new(
56
+ :username => "username",
57
+ :password => "password",
58
+ :host => "dedicated.server.com"
59
+ )
60
+ data = open(File.dirname(__FILE__) + '/fixtures/accountsummary.xml').read
61
+ @server.expects(:get_xml).with(:url => "accountsummary", :params => {:user=>"magic"}).returns(XmlSimple.xml_in(data,{ 'ForceArray' => false }))
62
+ @account = @server.account("magic")
63
+ end
64
+
65
+
66
+ it "should load account attributes" do
67
+ @account.attributes['user'].should == "magic"
68
+ @account.attributes['domain'].should == "magic.example.com"
69
+ end
70
+
71
+ it "should have a name" do
72
+ @account.name.should == "magic"
73
+ @account.user.should == "magic"
74
+ end
75
+
76
+ it "should change the password" do
77
+ @server.expects(:change_account_password).returns("new_password")
78
+ @account.password=("new_password").should == "new_password"
79
+ end
80
+
81
+ it "should suspend an account" do
82
+ @server.expects(:suspend_account).returns("ok")
83
+ @account.suspend!.should == "ok"
84
+ end
85
+
86
+ it "should unsuspend an account" do
87
+ @server.expects(:unsuspend_account).returns("ok")
88
+ @account.unsuspend!.should == "ok"
89
+ end
90
+
91
+ it "should terminate an account" do
92
+ @server.expects(:terminate_account).returns("ok")
93
+ @account.terminate!.should == "ok"
94
+ end
95
+
96
+ it "should change the package on an account" do
97
+ @server.expects(:change_package).returns("ok")
98
+
99
+ @account.package=("new_package").should == "new_package"
100
+ end
101
+
102
+ it "should read an attribute" do
103
+ @account.domain.should == 'magic.example.com'
104
+ end
105
+
106
+ it "should write an attribute" do
107
+ @account.username = 'newname'
108
+ @account.username.should == 'newname'
109
+ end
110
+
111
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ivanoats-whm_xml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Storck
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2009-04-17 00:00:00 -07:00
14
+ date: 2009-04-30 00:00:00 -07:00
15
15
  default_executable:
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
@@ -66,9 +66,11 @@ files:
66
66
  - Rakefile
67
67
  - README.rdoc
68
68
  - lib/parameters.rb
69
+ - lib/whm/account.rb
69
70
  - lib/whm/exceptions.rb
70
71
  - lib/whm/server.rb
71
72
  - lib/whm.rb
73
+ - spec/account_spec.rb
72
74
  - spec/server_spec.rb
73
75
  - spec/spec_helper.rb
74
76
  has_rdoc: true