Fix Get-Credential to not prompt twice when no parameter is specified

This commit is contained in:
Dongbo Wang
2016-09-27 09:49:28 -07:00
committed by Jason Shirk
parent f4b565161e
commit 1ea555810c
4 changed files with 84 additions and 25 deletions
+13 -6
View File
@@ -112,7 +112,8 @@ namespace TestHost
public string ReadLineData = "This is readline data";
public int PromptedChoice = 0;
public string StringForSecureString = "TEST";
public string promptResponse = "this is a prompt response";
public string UserNameForCredential = "Admin";
public object promptResponse = "this is a prompt response";
public Streams Streams = new Streams();
public override PSHostRawUserInterface RawUI
@@ -122,11 +123,15 @@ namespace TestHost
public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)
{
string s = String.Empty;
if ( descriptions[0] != null ) { s = descriptions[0].Name; }
if (descriptions == null || descriptions[0] == null)
{
throw new ArgumentException("descriptions");
}
string s = descriptions[0].Name;
Streams.Prompt.Add(caption + ":" + message + ":" + s);
Dictionary<string, PSObject> d = new Dictionary<string, PSObject>();
d.Add(descriptions[0].ToString(), new PSObject(promptResponse));
d.Add(s, new PSObject(promptResponse));
return d;
}
@@ -140,14 +145,16 @@ namespace TestHost
{
Streams.Prompt.Add("Credential:" + caption + ":" + message);
SecureString ss = ReadLineAsSecureString();
return new PSCredential(userName, ss);
string userNameToUse = string.IsNullOrEmpty(userName) ? UserNameForCredential : userName;
return new PSCredential(userNameToUse, ss);
}
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
{
Streams.Prompt.Add("Credential:" + caption + ":" + message);
SecureString ss = ReadLineAsSecureString();
return new PSCredential(userName, ss);
string userNameToUse = string.IsNullOrEmpty(userName) ? UserNameForCredential : userName;
return new PSCredential(userNameToUse, ss);
}
public override string ReadLine()
@@ -10,6 +10,7 @@ Describe "Get-Credential Test" -tag "CI" {
BeforeAll {
$th = New-TestHost
$th.UI.StringForSecureString = "This is a test"
$th.UI.UserNameForCredential = "John"
$rs = [runspacefactory]::Createrunspace($th)
$rs.open()
$ps = [powershell]::Create()
@@ -21,6 +22,9 @@ Describe "Get-Credential Test" -tag "CI" {
$rs.Dispose()
$ps.Dispose()
}
AfterEach {
$ps.Commands.Clear()
}
It "Get-Credential with message, produces a credential object" {
$cred = $ps.AddScript("Get-Credential -UserName Joe -Message Foo").Invoke() | Select-Object -First 1
$cred.gettype().FullName | Should Be "System.Management.Automation.PSCredential"
@@ -35,7 +39,7 @@ Describe "Get-Credential Test" -tag "CI" {
$netcred = $cred.GetNetworkCredential()
$netcred.UserName | Should be "Joe"
$netcred.Password | Should be "this is a test"
$th.ui.Streams.Prompt[-1] | should be "Credential:CustomTitle:"
$th.ui.Streams.Prompt[-1] | should Match "Credential:CustomTitle:[^:]+"
}
It "Get-Credential with only username, produces a credential object" {
$cred = $ps.AddScript("Get-Credential -UserName Joe").Invoke() | Select-Object -First 1
@@ -43,7 +47,7 @@ Describe "Get-Credential Test" -tag "CI" {
$netcred = $cred.GetNetworkCredential()
$netcred.UserName | Should be "Joe"
$netcred.Password | Should be "this is a test"
$th.ui.Streams.Prompt[-1] | Should Match "Credential:[^:]+:"
$th.ui.Streams.Prompt[-1] | Should Match "Credential:[^:]+:[^:]+"
}
It "Get-Credential with title and message, produces a credential object" {
$cred = $ps.AddScript("Get-Credential -UserName Joe -Message Foo -Title CustomTitle").Invoke() | Select-Object -First 1
@@ -53,4 +57,54 @@ Describe "Get-Credential Test" -tag "CI" {
$netcred.Password | Should be "this is a test"
$th.ui.Streams.Prompt[-1] | should be "Credential:CustomTitle:Foo"
}
It "Get-Credential without parameters" {
$cred = $ps.AddScript("Get-Credential").Invoke() | Select-Object -First 1
$cred.gettype().FullName | Should Be "System.Management.Automation.PSCredential"
$netcred = $cred.GetNetworkCredential()
$netcred.UserName | Should be "John"
$netcred.Password | Should be "This is a test"
$th.ui.Streams.Prompt[-1] | Should Match "Credential:[^:]+:[^:]+"
}
It "Get-Credential `$null" {
$cred = $ps.AddScript("Get-Credential `$null").Invoke() | Select-Object -First 1
$cred.gettype().FullName | Should Be "System.Management.Automation.PSCredential"
$netcred = $cred.GetNetworkCredential()
$netcred.UserName | Should be "John"
$netcred.Password | Should be "This is a test"
$th.ui.Streams.Prompt[-1] | Should Match "Credential:[^:]+:[^:]+"
}
It "Get-Credential -Credential `$null" {
$cred = $ps.AddScript("Get-Credential -Credential `$null").Invoke() | Select-Object -First 1
$cred.gettype().FullName | Should Be "System.Management.Automation.PSCredential"
$netcred = $cred.GetNetworkCredential()
$netcred.UserName | Should be "John"
$netcred.Password | Should be "This is a test"
$th.ui.Streams.Prompt[-1] | Should Match "Credential:[^:]+:[^:]+"
}
it "Get-Credential Joe" {
$cred = $ps.AddScript("Get-Credential Joe").Invoke() | Select-Object -First 1
$cred.gettype().FullName | Should Be "System.Management.Automation.PSCredential"
$netcred = $cred.GetNetworkCredential()
$netcred.UserName | Should be "Joe"
$netcred.Password | Should be "This is a test"
$th.ui.Streams.Prompt[-1] | Should Match "Credential:[^:]+:[^:]+"
}
it "Get-Credential -Credential Joe" {
$cred = $ps.AddScript("Get-Credential Joe").Invoke() | Select-Object -First 1
$cred.gettype().FullName | Should Be "System.Management.Automation.PSCredential"
$netcred = $cred.GetNetworkCredential()
$netcred.UserName | Should be "Joe"
$netcred.Password | Should be "This is a test"
$th.ui.Streams.Prompt[-1] | Should Match "Credential:[^:]+:[^:]+"
}
it "Get-Credential `$credential" {
$password = ConvertTo-SecureString -String "CredTest" -AsPlainText -Force
$credential = [pscredential]::new("John", $password)
$cred = Get-Credential $credential
$cred.gettype().FullName | Should Be "System.Management.Automation.PSCredential"
$netcred = $cred.GetNetworkCredential()
$netcred.UserName | Should be "John"
$netcred.Password | Should be "CredTest"
}
}