File size: 5,388 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')]
param()
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()
$ps.Runspace = $rs
$ps.Commands.Clear()
}
AfterAll {
$rs.Close()
$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 | Should -BeOfType 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:[^:]+:Foo"
}
It "Get-Credential with title, produces a credential object" {
$cred = $ps.AddScript("Get-Credential -UserName Joe -Title CustomTitle").Invoke() | Select-Object -First 1
$cred | Should -BeOfType 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:CustomTitle:[^:]+"
}
It "Get-Credential with only username, produces a credential object" {
$cred = $ps.AddScript("Get-Credential -UserName Joe").Invoke() | Select-Object -First 1
$cred | Should -BeOfType 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 with title and message, produces a credential object" {
$cred = $ps.AddScript("Get-Credential -UserName Joe -Message Foo -Title CustomTitle").Invoke() | Select-Object -First 1
$cred | Should -BeOfType 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 -Be "Credential:CustomTitle:Foo"
}
It "Get-Credential without parameters" {
$cred = $ps.AddScript("Get-Credential").Invoke() | Select-Object -First 1
$cred | Should -BeOfType 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 | Should -BeOfType 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 | Should -BeOfType 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 | Should -BeOfType 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 | Should -BeOfType 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" {
#[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Demo/doc/test secret.")]
$password = ConvertTo-SecureString -String "CredTest" -AsPlainText -Force
$credential = [pscredential]::new("John", $password)
$cred = Get-Credential $credential
$cred | Should -BeOfType System.Management.Automation.PSCredential
$netcred = $cred.GetNetworkCredential()
$netcred.UserName | Should -Be "John"
$netcred.Password | Should -Be "CredTest"
}
}
|