ghaki-account 2011.11.29.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. data/LICENSE +19 -0
  2. data/README +23 -0
  3. data/VERSION +1 -0
  4. data/lib/ghaki/account/base.rb +78 -0
  5. data/lib/ghaki/account/database.rb +66 -0
  6. data/lib/ghaki/account/db_connect.rb +45 -0
  7. data/lib/ghaki/account/db_driver.rb +22 -0
  8. data/lib/ghaki/account/db_name.rb +22 -0
  9. data/lib/ghaki/account/db_port.rb +22 -0
  10. data/lib/ghaki/account/domain_address.rb +39 -0
  11. data/lib/ghaki/account/email_address.rb +39 -0
  12. data/lib/ghaki/account/errors.rb +10 -0
  13. data/lib/ghaki/account/hostname.rb +95 -0
  14. data/lib/ghaki/account/password.rb +101 -0
  15. data/lib/ghaki/account/syn_opts.rb +42 -0
  16. data/lib/ghaki/account/userdomain.rb +88 -0
  17. data/lib/ghaki/account/username.rb +93 -0
  18. data/lib/ghaki/account/windos.rb +58 -0
  19. data/spec/ghaki/account/base_spec.rb +121 -0
  20. data/spec/ghaki/account/database_spec.rb +96 -0
  21. data/spec/ghaki/account/db_connect_spec.rb +79 -0
  22. data/spec/ghaki/account/db_driver_spec.rb +44 -0
  23. data/spec/ghaki/account/db_name_spec.rb +43 -0
  24. data/spec/ghaki/account/db_port_spec.rb +49 -0
  25. data/spec/ghaki/account/domain_address_spec.rb +30 -0
  26. data/spec/ghaki/account/email_address_spec.rb +30 -0
  27. data/spec/ghaki/account/hostname_spec.rb +110 -0
  28. data/spec/ghaki/account/password_spec.rb +155 -0
  29. data/spec/ghaki/account/syn_opts_helper.rb +90 -0
  30. data/spec/ghaki/account/syn_opts_spec.rb +36 -0
  31. data/spec/ghaki/account/userdomain_spec.rb +69 -0
  32. data/spec/ghaki/account/username_spec.rb +67 -0
  33. data/spec/ghaki/account/windos_spec.rb +107 -0
  34. data/spec/spec_helper.rb +6 -0
  35. metadata +139 -0
@@ -0,0 +1,96 @@
1
+ require 'ghaki/account/database'
2
+
3
+ module Ghaki module Account module DatabaseTesting
4
+ describe Database do
5
+
6
+ ########################################################################
7
+ OPTS_NAME = { :db_name => 'MYDB' }
8
+ OPTS_DRIV = { :db_driver => 'Mysql' }
9
+ OPTS_PORT = { :db_port => 1521 }
10
+ OPTS_HOST = { :hostname => 'db.host.com' }
11
+ OPTS_ALL = OPTS_NAME.merge(OPTS_DRIV).merge(OPTS_PORT).merge(OPTS_HOST)
12
+ EXTRA_OPTS = { :extra => true }
13
+
14
+ ########################################################################
15
+ # EIGEN TESTING
16
+ ########################################################################
17
+ context 'class' do
18
+ subject { Database }
19
+ specify { subject.ancestors.should include(Base) }
20
+ specify { subject.ancestors.should include(DB_Name) }
21
+ specify { subject.ancestors.should include(DB_Port) }
22
+ specify { subject.ancestors.should include(DB_Driver) }
23
+ specify { subject.ancestors.should include(DB_Connect) }
24
+
25
+ #---------------------------------------------------------------------
26
+ describe '#from_opts' do
27
+ it 'accepts separate' do
28
+ acc = Database.from_opts(OPTS_ALL)
29
+ acc.db_driver.should == 'Mysql'
30
+ acc.db_name.should == 'MYDB'
31
+ acc.db_port.should == 1521
32
+ acc.db_connect.should == 'dbi:Mysql:MYDB:db.host.com:1521'
33
+ end
34
+ it 'passes thru whole' do
35
+ acc = Database.from_opts(OPTS_ALL)
36
+ Database.from_opts( :account => acc ).should eql(acc)
37
+ end
38
+ end
39
+
40
+ #---------------------------------------------------------------------
41
+ describe '#purge_opts' do
42
+ it 'removes separate login parts' do
43
+ Database.purge_opts(OPTS_ALL.merge(EXTRA_OPTS)).should eql(EXTRA_OPTS)
44
+ end
45
+ end
46
+
47
+ end
48
+
49
+ ########################################################################
50
+ # INSTANCE TESTING
51
+ ########################################################################
52
+ context 'object instance' do
53
+
54
+ #---------------------------------------------------------------------
55
+ describe '#to_s' do
56
+ it 'creates from just db name' do
57
+ Database.new(OPTS_NAME).to_s.should == 'MYDB:*@localhost'
58
+ end
59
+ it 'creates from db and host' do
60
+ Database.new(OPTS_ALL).to_s.should == 'MYDB:*@db.host.com'
61
+ end
62
+ it 'creates from with db, host, and user' do
63
+ Database.new(OPTS_ALL.merge(:username => 'user')).to_s.should == 'MYDB:user@db.host.com'
64
+ end
65
+ end
66
+
67
+ #---------------------------------------------------------------------
68
+ describe '#expand_opts' do
69
+ it 'creates from scratch' do
70
+ Database.new(OPTS_ALL).expand_opts.should eql(OPTS_ALL)
71
+ end
72
+ it 'creates using passed' do
73
+ Database.new(OPTS_ALL).expand_opts(EXTRA_OPTS.dup).should eql(OPTS_ALL.merge(EXTRA_OPTS))
74
+ end
75
+ end
76
+
77
+ #---------------------------------------------------------------------
78
+ describe '#collapse_opts' do
79
+ before(:each) do
80
+ @base = Database.new(OPTS_ALL)
81
+ end
82
+ it 'creates from scratch' do
83
+ @opts = { :account => @base }
84
+ @base.collapse_opts.should eql(@opts)
85
+ end
86
+ it 'creates passed' do
87
+ @opts = EXTRA_OPTS.merge( :account => @base )
88
+ @base.collapse_opts(EXTRA_OPTS.dup).should eql(@opts)
89
+ end
90
+ end
91
+
92
+ end
93
+
94
+ end
95
+ end end end
96
+ ############################################################################
@@ -0,0 +1,79 @@
1
+ require 'ghaki/account/db_connect'
2
+ require File.join(File.dirname(__FILE__),'syn_opts_helper')
3
+
4
+ module Ghaki module Account module DB_ConnectTesting
5
+ describe Ghaki::Account::DB_Connect do
6
+
7
+ ########################################################################
8
+ GOOD_EXAMPLE = 'dbi:Mysql:MYDB'
9
+ extend SynOptsHelper
10
+ syn_opts_test_declare DB_Connect, :db_connect, GOOD_EXAMPLE,
11
+ :database_connect, :db_connector, :database_connector
12
+
13
+ ########################################################################
14
+ # EIGEN CLASS
15
+ ########################################################################
16
+ context 'eigen class' do
17
+ syn_opts_test_class :db_connect
18
+ syn_opts_test_class_methods :db_connect
19
+ end
20
+
21
+ ########################################################################
22
+ # INSTANCE TESTING
23
+ ########################################################################
24
+ context 'object instance' do
25
+ syn_opts_test_object :db_connect
26
+ syn_opts_test_object_methods :db_connect
27
+
28
+
29
+ it { should respond_to :valid_db_connect? }
30
+ describe '#valid_db_connect?' do
31
+ it "accepts <#{GOOD_EXAMPLE}>" do
32
+ subject.db_connect = GOOD_EXAMPLE
33
+ subject.valid_db_connect?.should be_true
34
+ end
35
+ it 'rejects when missing' do
36
+ subject.db_connect = nil
37
+ subject.valid_db_connect?.should be_false
38
+ end
39
+ end
40
+
41
+ it { should respond_to :db_connect }
42
+ it { should respond_to :db_connect= }
43
+ describe '#db_connect' do
44
+ require 'ghaki/account/hostname'
45
+ require 'ghaki/account/db_driver'
46
+ require 'ghaki/account/db_name'
47
+ require 'ghaki/account/db_port'
48
+ class UsingAll
49
+ include DB_Connect
50
+ include Hostname
51
+ include DB_Driver
52
+ include DB_Name
53
+ include DB_Port
54
+ end
55
+ subject { UsingAll.new }
56
+ it 'creates when all parts are present' do
57
+ subject.db_driver = 'Mysql'
58
+ subject.hostname = 'db.host.com'
59
+ subject.db_name = 'MYDB'
60
+ subject.db_port = 1521
61
+ subject.db_connect.should == 'dbi:Mysql:MYDB:db.host.com:1521'
62
+ end
63
+ it 'uses default localhost when port is present' do
64
+ subject.db_driver = 'Mysql'
65
+ subject.db_name = 'MYDB'
66
+ subject.db_port = 1521
67
+ subject.db_connect.should == 'dbi:Mysql:MYDB:localhost:1521'
68
+ end
69
+ it 'skips default localhost when port is not present' do
70
+ subject.db_driver = 'Mysql'
71
+ subject.db_name = 'MYDB'
72
+ subject.db_connect.should == 'dbi:Mysql:MYDB'
73
+ end
74
+ end
75
+
76
+ end
77
+
78
+ end
79
+ end end end
@@ -0,0 +1,44 @@
1
+ require 'ghaki/account/db_driver'
2
+ require File.join(File.dirname(__FILE__),'syn_opts_helper')
3
+
4
+ module Ghaki module Account module DB_DriverTesting
5
+ describe Ghaki::Account::DB_Driver do
6
+
7
+ ########################################################################
8
+ extend SynOptsHelper
9
+ syn_opts_test_declare DB_Driver, :db_driver, 'dbi:Mysql', :database_driver
10
+
11
+ ########################################################################
12
+ # EIGEN CLASS
13
+ ########################################################################
14
+ context 'eigen class' do
15
+ syn_opts_test_class :db_driver
16
+ syn_opts_test_class_methods :db_driver
17
+ end
18
+
19
+ ########################################################################
20
+ # INSTANCE TESTING
21
+ ########################################################################
22
+ context 'object instance' do
23
+ syn_opts_test_object :db_driver
24
+ syn_opts_test_object_methods :db_driver
25
+
26
+ it { should respond_to :db_driver }
27
+ it { should respond_to :db_driver= }
28
+
29
+ it { should respond_to :valid_db_driver? }
30
+ describe '#valid_db_driver?' do
31
+ it 'accepts <dbi:Mysql>' do
32
+ subject.db_driver = 'dbi:Mysql'
33
+ subject.valid_db_driver?.should be_true
34
+ end
35
+ it 'rejects when missing' do
36
+ subject.db_driver = nil
37
+ subject.valid_db_driver?.should be_false
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+ end end end
@@ -0,0 +1,43 @@
1
+ require 'ghaki/account/db_name'
2
+ require File.join( File.dirname(__FILE__),'syn_opts_helper' )
3
+
4
+ module Ghaki module Account module DB_NameTesting
5
+ describe Ghaki::Account::DB_Name do
6
+
7
+ ########################################################################
8
+ extend SynOptsHelper
9
+ syn_opts_test_declare DB_Name, :db_name, 'MYDB', :database_name
10
+
11
+ ########################################################################
12
+ # EIGEN CLASS
13
+ ########################################################################
14
+ context 'eigen class' do
15
+ syn_opts_test_class :db_name
16
+ syn_opts_test_class_methods :db_name
17
+ end
18
+
19
+ ########################################################################
20
+ # INSTANCE TESTING
21
+ ########################################################################
22
+ context 'objects' do
23
+ syn_opts_test_object :db_name
24
+ syn_opts_test_object_methods :db_name
25
+
26
+ it { should respond_to :db_name }
27
+ it { should respond_to :db_name= }
28
+
29
+ it { should respond_to :valid_db_name? }
30
+ describe '#valid_db_name?' do
31
+ it 'accepts <MYDB>' do
32
+ subject.db_name = 'MYDB'
33
+ subject.valid_db_name?.should be_true
34
+ end
35
+ it 'rejects when missing' do
36
+ subject.db_name = nil
37
+ subject.valid_db_name?.should be_false
38
+ end
39
+ end
40
+ end
41
+
42
+ end
43
+ end end end
@@ -0,0 +1,49 @@
1
+ require 'ghaki/account/db_port'
2
+ require File.join(File.dirname(__FILE__),'syn_opts_helper')
3
+
4
+ module Ghaki module Account module DB_PortTesting
5
+ describe Ghaki::Account::DB_Port do
6
+
7
+ ########################################################################
8
+ extend SynOptsHelper
9
+ syn_opts_test_declare DB_Port, :db_port, 1521, :database_port
10
+
11
+ ########################################################################
12
+ # EIGEN CLASS
13
+ ########################################################################
14
+ context 'class' do
15
+ syn_opts_test_class :db_port
16
+ syn_opts_test_class_methods :db_port
17
+ end
18
+
19
+ ########################################################################
20
+ # INSTANCE TESTING
21
+ ########################################################################
22
+ context 'objects' do
23
+
24
+ syn_opts_test_object :db_port
25
+ syn_opts_test_object_methods :db_port
26
+
27
+ it { should respond_to :db_port }
28
+ it { should respond_to :db_port= }
29
+
30
+ it { should respond_to :valid_db_port? }
31
+ describe '#valid_db_port?' do
32
+ it 'accepts <1521>' do
33
+ subject.db_port = 1521
34
+ subject.valid_db_port?.should be_true
35
+ end
36
+ it 'accepts when missing' do
37
+ subject.db_port = nil
38
+ subject.valid_db_port?.should be_true
39
+ end
40
+ it 'rejects when NAN' do
41
+ subject.db_port = 'moo'
42
+ subject.valid_db_port?.should be_false
43
+ end
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+ end end end
@@ -0,0 +1,30 @@
1
+ require 'ghaki/account/domain_address'
2
+ require File.join( File.dirname(__FILE__), 'syn_opts_helper' )
3
+
4
+ module Ghaki module Account module DomainAddressTesting
5
+ describe DomainAddress do
6
+
7
+ ########################################################################
8
+ extend SynOptsHelper
9
+ syn_opts_test_declare DomainAddress, :domain_address, "\\\\domain\\user"
10
+
11
+ ########################################################################
12
+ # EIGEN TESTING
13
+ ########################################################################
14
+ context 'eigen class' do
15
+ syn_opts_test_class :domain_address
16
+ syn_opts_test_class_methods :domain_address
17
+ end
18
+
19
+ ########################################################################
20
+ # INSTANCE TESTING
21
+ ########################################################################
22
+ context 'object instance' do
23
+ syn_opts_test_object :domain_address
24
+ syn_opts_test_object_methods :domain_address
25
+ it { should respond_to :domain_address }
26
+ it { should respond_to :domain_address= }
27
+ end
28
+
29
+ end
30
+ end end end
@@ -0,0 +1,30 @@
1
+ require 'ghaki/account/email_address'
2
+ require File.join(File.dirname(__FILE__),'syn_opts_helper')
3
+
4
+ module Ghaki module Account module EMailAddressTesting
5
+ describe EMailAddress do
6
+
7
+ ########################################################################
8
+ extend SynOptsHelper
9
+ syn_opts_test_declare EMailAddress, :email_address, 'user@host', :email
10
+
11
+ ########################################################################
12
+ # EIGEN TESTING
13
+ ########################################################################
14
+ context 'eigen class' do
15
+ syn_opts_test_class :email_address
16
+ syn_opts_test_class_methods :email_address
17
+ end
18
+
19
+ ########################################################################
20
+ # INSTANCE TESTING
21
+ ########################################################################
22
+ context 'object instance' do
23
+ syn_opts_test_object :email_address
24
+ syn_opts_test_object_methods :email_address
25
+ it { should respond_to :email_address }
26
+ it { should respond_to :email_address= }
27
+ end
28
+
29
+ end
30
+ end end end
@@ -0,0 +1,110 @@
1
+ require 'ghaki/account/hostname'
2
+ require File.join( File.dirname(__FILE__), 'syn_opts_helper' )
3
+
4
+ ############################################################################
5
+ module Ghaki module Account module HostnameTesting
6
+ describe Hostname do
7
+
8
+ ########################################################################
9
+ extend SynOptsHelper
10
+ syn_opts_test_declare Hostname, :hostname, 'host', :host, :host_name
11
+
12
+ ########################################################################
13
+ # EIGEN TESTING
14
+ ########################################################################
15
+ context 'eigen class' do
16
+ syn_opts_test_class :hostname
17
+ syn_opts_test_class_methods :hostname
18
+
19
+ #---------------------------------------------------------------------
20
+ it { should respond_to :get_env }
21
+ describe '#get_env' do
22
+ it 'detects ENV value' do
23
+ ENV['HOSTNAME'] = 'host'
24
+ subject.get_env.should == 'host'
25
+ end
26
+ it 'gets socket name' do
27
+ ENV.delete('HOSTNAME')
28
+ ::Socket.expects(:gethostname).returns('host')
29
+ subject.get_env.should == 'host'
30
+ end
31
+ end
32
+
33
+ end
34
+
35
+ ########################################################################
36
+ # INSTANCE TESTING
37
+ ########################################################################
38
+ context 'object' do
39
+ syn_opts_test_object :hostname
40
+ syn_opts_test_object_methods :hostname
41
+
42
+ it { should respond_to :hostname }
43
+ it { should respond_to :hostname= }
44
+ it { should respond_to :ask_hostname }
45
+ it { should respond_to :valid_hostname? }
46
+
47
+ #---------------------------------------------------------------------
48
+ describe '#hostname=' do
49
+ it 'should accept ENV default' do
50
+ ENV['HOSTNAME'] = 'host'
51
+ subject.hostname = :env
52
+ subject.hostname.should == 'host'
53
+ end
54
+ it 'should trim whitespace' do
55
+ subject.hostname = ' host '
56
+ subject.hostname.should == 'host'
57
+ end
58
+ end
59
+
60
+ #---------------------------------------------------------------------
61
+ describe '#valid_hostname?' do
62
+ it 'should deny missing' do
63
+ subject.valid_hostname?.should be_false
64
+ end
65
+ it 'should deny empty' do
66
+ subject.hostname = ''
67
+ subject.valid_hostname?.should be_false
68
+ end
69
+ it 'should deny inner whitespace' do
70
+ subject.hostname = 'quack moo'
71
+ subject.valid_hostname?.should be_false
72
+ end
73
+ it 'should accept single word' do
74
+ subject.hostname = 'host'
75
+ subject.valid_hostname?.should be_true
76
+ end
77
+ it 'should accept fully qualified' do
78
+ subject.hostname = 'host.domain'
79
+ subject.valid_hostname?.should be_true
80
+ end
81
+ end
82
+
83
+ #---------------------------------------------------------------------
84
+ describe '#ask_hostname' do
85
+ before(:each) do
86
+ @high_mock = stub_everything()
87
+ ::HighLine.stubs( :new => @high_mock )
88
+ end
89
+ it 'should grab hostname' do
90
+ @high_mock.expects(:ask).returns('host')
91
+ subject.ask_hostname.should == 'host'
92
+ subject.hostname.should == 'host'
93
+ end
94
+ it 'should retry on failure' do
95
+ @high_mock.expects(:ask).returns('host bad','host').twice
96
+ subject.ask_hostname.should == 'host'
97
+ subject.hostname.should == 'host'
98
+ end
99
+ it 'should raise on failure' do
100
+ @high_mock.expects(:ask).returns('host bad').times(3)
101
+ lambda do
102
+ subject.ask_hostname
103
+ end.should raise_error(InvalidHostnameError)
104
+ end
105
+ end
106
+
107
+ end
108
+
109
+ end
110
+ end end end