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,155 @@
1
+ require 'ghaki/account/password'
2
+ require File.join( File.dirname(__FILE__), 'syn_opts_helper' )
3
+
4
+ module Ghaki module Account module PasswordTesting
5
+ describe Password do
6
+
7
+ ########################################################################
8
+ extend SynOptsHelper
9
+ syn_opts_test_declare Password, :password, 'secret'
10
+
11
+ ########################################################################
12
+ # EIGEN TESTING
13
+ ########################################################################
14
+ context 'eigen class' do
15
+ subject { Password }
16
+ syn_opts_test_class :password
17
+ syn_opts_test_class_methods :password
18
+ end
19
+
20
+ ########################################################################
21
+ # INSTANCE TESTING
22
+ ########################################################################
23
+ context 'object instance' do
24
+ syn_opts_test_object :password
25
+ syn_opts_test_object_methods :password
26
+
27
+ describe '#passwords' do
28
+ it 'defaults empty when unset' do
29
+ subject.passwords.should be_empty
30
+ end
31
+ end
32
+
33
+ describe '#passwords=' do
34
+ it 'accepts single password' do
35
+ pass = 'secret'
36
+ subject.passwords = pass
37
+ subject.passwords.should == [pass]
38
+ end
39
+ it 'accepts several passwords' do
40
+ pass = ['a','b']
41
+ subject.passwords = pass
42
+ subject.passwords.should == pass
43
+ end
44
+ it 'clears when given nil' do
45
+ subject.passwords = nil
46
+ subject.passwords.should be_empty
47
+ end
48
+ end
49
+
50
+ describe '#password' do
51
+ it 'returns nil when passwords is empty' do
52
+ subject.password.should be_nil
53
+ end
54
+ it 'grabs first of passwords' do
55
+ subject.passwords = ['a','b']
56
+ subject.password.should == 'a'
57
+ end
58
+ end
59
+ describe '#password=' do
60
+ it 'aliases passwords=' do
61
+ subject.method(:password=).should == subject.method(:passwords=)
62
+ end
63
+ end
64
+
65
+ describe '#fail_password' do
66
+ it 'advances to next password' do
67
+ subject.passwords = ['a','b']
68
+ subject.fail_password
69
+ subject.password.should == 'b'
70
+ end
71
+ it 'runs out of passwords' do
72
+ subject.passwords = ['a','b']
73
+ subject.fail_password
74
+ subject.fail_password
75
+ subject.password.should be_nil
76
+ end
77
+ end
78
+
79
+ describe 'failed_passwords?' do
80
+ it 'handles no passwords' do
81
+ subject.failed_passwords?.should be_false
82
+ end
83
+ it 'detects lack of failures' do
84
+ subject.passwords = ['a','b']
85
+ subject.failed_passwords?.should be_false
86
+ end
87
+ it 'detects failures' do
88
+ subject.passwords = ['a','b']
89
+ subject.fail_password
90
+ subject.fail_password
91
+ subject.failed_passwords?.should be_true
92
+ end
93
+ end
94
+
95
+ describe '#retry_password?' do
96
+ it 'handles no passwords' do
97
+ subject.retry_password?.should be_false
98
+ end
99
+ it 'detects available password retries' do
100
+ subject.passwords = ['a','b']
101
+ subject.retry_password?.should be_true
102
+ end
103
+ it 'detects exhausted password retries' do
104
+ subject.passwords = ['a']
105
+ subject.fail_password
106
+ subject.retry_password?.should be_false
107
+ end
108
+ end
109
+
110
+ describe '#reset_passwords' do
111
+ it 'resets password attempts' do
112
+ subject.passwords = ['a','b']
113
+ subject.fail_password
114
+ subject.reset_passwords
115
+ subject.password.should == 'a'
116
+ end
117
+ end
118
+
119
+ describe '#ask_password' do
120
+ before(:each) do
121
+ @high_mock = stub_everything()
122
+ ::HighLine.stubs( :new => @high_mock )
123
+ end
124
+ it 'grabs password' do
125
+ @high_mock.expects(:ask).returns('secret').once
126
+ subject.ask_password.should == 'secret'
127
+ subject.password.should == 'secret'
128
+ end
129
+ it 'retries on failure' do
130
+ @high_mock.expects(:ask).returns(nil,'secret').twice
131
+ subject.ask_password.should == 'secret'
132
+ subject.password.should == 'secret'
133
+ end
134
+ it 'raises on failure' do
135
+ @high_mock.expects(:ask).returns(nil).times(3)
136
+ lambda do
137
+ subject.ask_password
138
+ end.should raise_error(InvalidPasswordError)
139
+ end
140
+ end
141
+
142
+ describe '#valid_password?' do
143
+ it 'denies missing' do
144
+ subject.valid_password?.should be_false
145
+ end
146
+ it 'accepts present' do
147
+ subject.password = 'secret'
148
+ subject.valid_password?.should be_true
149
+ end
150
+ end
151
+
152
+ end
153
+
154
+ end
155
+ end end end
@@ -0,0 +1,90 @@
1
+ ############################################################################
2
+ require 'singleton'
3
+
4
+ ############################################################################
5
+ module Ghaki module Account module SynOptsHelper
6
+
7
+ class SynOptsHolder < Hash
8
+ include Singleton
9
+ end
10
+
11
+ ##########################################################################
12
+ class SynOptsTester
13
+ attr_accessor :parent, :child,
14
+ :source, :target, :value, :names
15
+ end
16
+
17
+ ##########################################################################
18
+ def syn_opts_test_declare kparent, source, value, *names
19
+ test = SynOptsHolder.instance[source] = SynOptsTester.new
20
+ test.parent = kparent
21
+ test.child = Class.new
22
+ test.child.class_eval do
23
+ include kparent
24
+ end
25
+ test.source = source
26
+ test.value = value
27
+ test.names = names
28
+ test.names << source
29
+ test.target = ('opt_' + source.to_s).to_sym
30
+ end
31
+
32
+ ##########################################################################
33
+ def syn_opts_test_class source
34
+ test = SynOptsHolder.instance[source]
35
+ subject { test.parent }
36
+ it { should respond_to :parse_opts }
37
+ it { should respond_to :purge_opts }
38
+ end
39
+
40
+ ##########################################################################
41
+ def syn_opts_test_object source
42
+ test = SynOptsHolder.instance[source]
43
+ subject { test.child.new }
44
+ it { should respond_to test.target }
45
+ end
46
+
47
+ ##########################################################################
48
+ def syn_opts_test_class_methods source
49
+ test = SynOptsHolder.instance[source]
50
+ subject { test.parent }
51
+
52
+ describe '#parse_opts' do
53
+ test.names.each do |name|
54
+ it "accepts option <#{name}>" do
55
+ subject.parse_opts( { name => test.value } ).should == test.value
56
+ end
57
+ end
58
+ it 'accepts account value' do
59
+ account = mock()
60
+ account.expects(test.source).returns(test.value)
61
+ subject.parse_opts( { :account => account } ).should == test.value
62
+ end
63
+ end
64
+
65
+ describe '#purge_opts' do
66
+ test.names.each do |name|
67
+ it "removes option <#{name}>" do
68
+ subject.purge_opts( { name => test.value } ).should eql({})
69
+ end
70
+ end
71
+ end
72
+
73
+ end
74
+
75
+ ##########################################################################
76
+ def syn_opts_test_object_methods source
77
+ test = SynOptsHolder.instance[source]
78
+ subject { test.child.new }
79
+ describe "##{test.target}" do
80
+ test.names.each do |name|
81
+ it "accepts option <#{name}>" do
82
+ subject.send( test.target, { name => test.value } )
83
+ subject.send( test.source ).should == test.value
84
+ end
85
+ end
86
+ end
87
+ end
88
+
89
+ end end end
90
+ ############################################################################
@@ -0,0 +1,36 @@
1
+ require 'ghaki/account/syn_opts'
2
+ require File.join(File.dirname(__FILE__),'syn_opts_helper')
3
+
4
+ module Ghaki module Account
5
+ module UsingExtend
6
+ extend SynOpts
7
+ attr_syn_opts :foo, :bar
8
+ attr_accessor :foo
9
+ end
10
+ module SynOptsTesting
11
+ describe SynOpts do
12
+ extend SynOptsHelper
13
+
14
+ syn_opts_test_declare UsingExtend, :foo, 'fubar', :bar
15
+
16
+
17
+ ########################################################################
18
+ # EIGEN TESTING
19
+ ########################################################################
20
+ context 'eigen class' do
21
+ syn_opts_test_class :foo
22
+ syn_opts_test_class_methods :foo
23
+ end
24
+
25
+ ########################################################################
26
+ # INSTANCE TESTING
27
+ ########################################################################
28
+ context 'object instance' do
29
+ syn_opts_test_object :foo
30
+ syn_opts_test_object_methods :foo
31
+ it { should respond_to :foo }
32
+ it { should respond_to :foo= }
33
+ end
34
+
35
+ end
36
+ end end end
@@ -0,0 +1,69 @@
1
+ require 'ghaki/account/userdomain'
2
+ require File.join(File.dirname(__FILE__),'syn_opts_helper')
3
+
4
+ ############################################################################
5
+ module Ghaki module Account module UserDomainTesting
6
+ describe UserDomain do
7
+
8
+ ########################################################################
9
+ extend SynOptsHelper
10
+ syn_opts_test_declare UserDomain, :userdomain, 'domain', :user_domain
11
+
12
+ ########################################################################
13
+ # EIGEN TESTING
14
+ ########################################################################
15
+ context 'eigen class' do
16
+ syn_opts_test_class :userdomain
17
+ syn_opts_test_class_methods :userdomain
18
+
19
+ it { should respond_to :get_env }
20
+
21
+ describe '#get_env' do
22
+ it 'detects ENV value' do
23
+ ENV['USERDOMAIN'] = 'domain'
24
+ subject.get_env.should == 'domain'
25
+ end
26
+ end
27
+
28
+ end
29
+
30
+ ########################################################################
31
+ # INSTANCE TESTING
32
+ ########################################################################
33
+ context 'object instance' do
34
+
35
+ syn_opts_test_object :userdomain
36
+ syn_opts_test_object_methods :userdomain
37
+
38
+ it { should respond_to :userdomain }
39
+ it { should respond_to :userdomain= }
40
+ it { should respond_to :valid_userdomain? }
41
+
42
+ #---------------------------------------------------------------------
43
+ it { should respond_to :ask_userdomain }
44
+ describe '#ask_userdomain' do
45
+ before(:each) do
46
+ @high_mock = stub_everything()
47
+ ::HighLine.stubs( :new => @high_mock )
48
+ end
49
+ it 'grabs user domain' do
50
+ @high_mock.expects(:ask).returns('domain').once
51
+ subject.ask_userdomain.should == 'domain'
52
+ subject.userdomain.should == 'domain'
53
+ end
54
+ it 'retries on failure' do
55
+ @high_mock.expects(:ask).returns('domain bad','domain').twice
56
+ subject.ask_userdomain.should == 'domain'
57
+ subject.userdomain.should == 'domain'
58
+ end
59
+ it 'raises on failure' do
60
+ @high_mock.expects(:ask).returns('domain bad').times(3)
61
+ lambda do
62
+ subject.ask_userdomain
63
+ end.should raise_error(InvalidUserDomainError)
64
+ end
65
+ end
66
+ end
67
+
68
+ end
69
+ end end end
@@ -0,0 +1,67 @@
1
+ require 'ghaki/account/username'
2
+ require File.join(File.dirname(__FILE__),'syn_opts_helper')
3
+
4
+ module Ghaki module Account module UsernameTesting
5
+ describe Username do
6
+
7
+ extend SynOptsHelper
8
+ syn_opts_test_declare Username, :username, 'user',
9
+ :user, :user_name
10
+
11
+ ########################################################################
12
+ # EIGEN TESTING
13
+ ########################################################################
14
+ context 'eigen object' do
15
+ syn_opts_test_class :username
16
+ syn_opts_test_class_methods :username
17
+
18
+ it { should respond_to :get_env }
19
+ describe '#get_env' do
20
+ it 'detects ENV value' do
21
+ ::Etc.expects(:getlogin).returns('user')
22
+ subject.get_env.should == 'user'
23
+ end
24
+ end
25
+
26
+ end
27
+
28
+ ########################################################################
29
+ # INSTANCE TESTING
30
+ ########################################################################
31
+ context 'object instance' do
32
+ syn_opts_test_object :username
33
+ syn_opts_test_object_methods :username
34
+
35
+ it { should respond_to :username }
36
+ it { should respond_to :username= }
37
+ it { should respond_to :valid_username? }
38
+
39
+ it { should respond_to :ask_username }
40
+ describe '#ask_username' do
41
+ before(:each) do
42
+ @high_mock = stub_everything()
43
+ ::HighLine.stubs( :new => @high_mock )
44
+ end
45
+ it 'grabs username' do
46
+ @high_mock.expects(:ask).returns('user').once
47
+ subject.ask_username.should == 'user'
48
+ subject.username.should == 'user'
49
+ end
50
+ it 'retries on failure' do
51
+ @high_mock.expects(:ask).returns(nil,'user').twice
52
+ subject.ask_username.should == 'user'
53
+ subject.username.should == 'user'
54
+ end
55
+ it 'raises on failure' do
56
+ @high_mock.expects(:ask).returns(nil).times(3)
57
+ lambda do
58
+ subject.ask_username
59
+ end.should raise_error(InvalidUsernameError)
60
+ end
61
+ end
62
+
63
+
64
+ end
65
+
66
+ end
67
+ end end end
@@ -0,0 +1,107 @@
1
+ require 'ghaki/account/windos'
2
+
3
+ module Ghaki module Account module WinDosTesting
4
+ describe WinDos do
5
+
6
+ ########################################################################
7
+ # CONSTANTS
8
+ ########################################################################
9
+ OPTS_NAM = { :username => 'user' }
10
+ OPTS_DOM = { :userdomain => 'domain' }
11
+ OPTS_DAD = { :domain_address => "\\\\domain\\user" }
12
+ OPTS_ALL = OPTS_NAM.merge(OPTS_DOM).merge(OPTS_DAD )
13
+ EXTRA_OPTS = { :extra => true }
14
+
15
+ ########################################################################
16
+ # EIGEN TESTS
17
+ ########################################################################
18
+ context 'class' do
19
+ subject { WinDos }
20
+ specify { subject.ancestors.should include(Base) }
21
+ specify { subject.ancestors.should include(UserDomain) }
22
+ specify { subject.ancestors.should include(DomainAddress) }
23
+
24
+ #---------------------------------------------------------------------
25
+ describe '#from_opts' do
26
+ it 'accepts separate' do
27
+ acc = WinDos.from_opts(OPTS_ALL)
28
+ acc.username.should == 'user'
29
+ acc.userdomain.should == 'domain'
30
+ end
31
+ it 'passes thru whole' do
32
+ acc = WinDos.from_opts(OPTS_ALL)
33
+ opts = { :account => acc }
34
+ WinDos.from_opts(opts).should eql(acc)
35
+ end
36
+ end
37
+
38
+ #---------------------------------------------------------------------
39
+ describe '#purge_opts' do
40
+ it 'remove separate login parts' do
41
+ WinDos.purge_opts(OPTS_ALL.merge(EXTRA_OPTS)).should eql(EXTRA_OPTS)
42
+ end
43
+ end
44
+
45
+ end
46
+
47
+ ########################################################################
48
+ # INSTANCE TESTS
49
+ ########################################################################
50
+ context 'object instance' do
51
+
52
+ #---------------------------------------------------------------------
53
+ describe '#to_s' do
54
+ it 'defaults using user domain only' do
55
+ WinDos.new(OPTS_DOM).to_s.should == "\\\\domain\\*"
56
+ end
57
+ it 'defaults using username only' do
58
+ WinDos.new(OPTS_NAM).to_s.should == "\\\\*\\user"
59
+ end
60
+ it 'defaults using username and domain' do
61
+ WinDos.new(OPTS_ALL).to_s.should == "\\\\domain\\user"
62
+ end
63
+ end
64
+
65
+ #---------------------------------------------------------------------
66
+ describe '#domain_address' do
67
+ it 'accepts domain address directly' do
68
+ WinDos.new(OPTS_DAD).domain_address.should == "\\\\domain\\user"
69
+ end
70
+ it 'defaults from username only' do
71
+ WinDos.new(OPTS_NAM).domain_address.should == 'user'
72
+ end
73
+ it 'defaults from username and domain' do
74
+ WinDos.new(OPTS_ALL).domain_address.should == "\\\\domain\\user"
75
+ end
76
+ end
77
+
78
+ #---------------------------------------------------------------------
79
+ describe '#expand_opts' do
80
+ it 'creates from scratch' do
81
+ WinDos.new(OPTS_ALL).expand_opts.should eql(OPTS_ALL)
82
+ end
83
+ it 'creates using passed' do
84
+ WinDos.new(OPTS_ALL).expand_opts(EXTRA_OPTS.dup).should eql(OPTS_ALL.merge(EXTRA_OPTS))
85
+ end
86
+ end
87
+
88
+ #---------------------------------------------------------------------
89
+ describe '#collapse_opts' do
90
+ before(:each) do
91
+ @base = WinDos.new(OPTS_ALL)
92
+ end
93
+ it 'creates from scratch' do
94
+ @opts = { :account => @base }
95
+ @base.collapse_opts.should eql(@opts)
96
+ end
97
+ it 'creates using passed' do
98
+ @opts = { :account => @base }.merge(EXTRA_OPTS)
99
+ @base.collapse_opts(EXTRA_OPTS.dup).should eql(@opts)
100
+ end
101
+ end
102
+
103
+ end
104
+
105
+ end
106
+ end end end
107
+ ############################################################################