summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJay Mundrawala <jdmundrawala@gmail.com>2015-03-11 20:05:08 -0700
committerJay Mundrawala <jdmundrawala@gmail.com>2015-03-20 14:38:05 -0700
commit9a65eb8552db53fdd0bd93c945e001905ad793e3 (patch)
tree5765c2c51cac1f804de07fee70c71e3edf5c1093
parenta6902ea47197a9f2600795a7d1d48713ea05dbd1 (diff)
downloadchef-9a65eb8552db53fdd0bd93c945e001905ad793e3.tar.gz
Refactor ps_credential for easier mocking
-rw-r--r--lib/chef/util/powershell/ps_credential.rb10
-rw-r--r--spec/unit/util/powershell/ps_credential_spec.rb7
2 files changed, 10 insertions, 7 deletions
diff --git a/lib/chef/util/powershell/ps_credential.rb b/lib/chef/util/powershell/ps_credential.rb
index 20c2055247..701b3ce685 100644
--- a/lib/chef/util/powershell/ps_credential.rb
+++ b/lib/chef/util/powershell/ps_credential.rb
@@ -22,11 +22,17 @@ class Chef::Util::Powershell
class PSCredential
def initialize(username, password)
@username = username
- @encrypted_password = Chef::ReservedNames::Win32::Crypto.encrypt(password)
+ @password = password
end
def to_psobject
- "New-Object System.Management.Automation.PSCredential('#{@username}',\('#{@encrypted_password}' | ConvertTo-SecureString))"
+ "New-Object System.Management.Automation.PSCredential('#{@username}',('#{encrypt(@password)}' | ConvertTo-SecureString))"
+ end
+
+ private
+
+ def encrypt(str)
+ Chef::ReservedNames::Win32::Crypto.encrypt(str)
end
end
end
diff --git a/spec/unit/util/powershell/ps_credential_spec.rb b/spec/unit/util/powershell/ps_credential_spec.rb
index a2d51246df..e043fd92ec 100644
--- a/spec/unit/util/powershell/ps_credential_spec.rb
+++ b/spec/unit/util/powershell/ps_credential_spec.rb
@@ -24,17 +24,14 @@ describe Chef::Util::Powershell::PSCredential do
@node = Chef::Node.new
end
- before (:example) do
- allow(Chef::ReservedNames::Win32::Crypto).to receive(:encrypt).and_return("encrypted")
- end
-
let (:username) { 'foo' }
let (:password) { 'password' }
-
+
context 'when username and password are provided' do
let(:ps_credential) { Chef::Util::Powershell::PSCredential.new(username, password)}
context 'when calling to_psobject' do
it 'should create the script to create a PSCredential when calling' do
+ allow(ps_credential).to receive(:encrypt).with(password).and_return('encrypted')
expect(ps_credential.to_psobject).to eq(
"New-Object System.Management.Automation.PSCredential('#{username}',('encrypted' | ConvertTo-SecureString))")
end