PowerShell/test/powershell/Modules/Microsoft.PowerShell.Security/GetCredential.Tests.ps1
Ilya fd70adbc90 Improve console cmdlets tests (#3101)
* Improve console cmdlets tests
Main improvements refer to tests of the Write-Host cmdlet.
Original tests:
1. Slow because run external processes
2. Don't test colors and -NoNewLine in fact.

1. The original tests is preserved (deleted one as redundant) but marked
by 'Slow' tag. They is preserved because they actually check the output
on the work, not a test console.
2. Add negative color tests. (Code cover grow!)
3. Add tests based on TestHostCS. This test host has been refined so we
can see colors and a new line in output.
4. Add minor fixes for test modules loads.

Also I add support for Information stream. I originally planned to use
it but not actually used. However, I have left this as a useful addition
for future tests.
I wonder that a Write-Host console output is duplicated in Information
Stream - Is it by design? I left a debug print on this matter in the
test code.

* Fix after code review
* Corrections after code review
Suppress import-module warnings
Rename Describes
Add "-Object" test
Add Stream.Information tests with TestHostCS
* Add checks for Streams.Information and add comments
2017-02-14 11:00:03 -08:00

106 lines
5.3 KiB
PowerShell
Executable file

$hostmodule = Join-Path $PSScriptRoot "../../Common/TestHostCS.psm1"
import-module $hostmodule -ErrorAction SilentlyContinue
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.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:[^:]+: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.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:CustomTitle:[^:]+"
}
It "Get-Credential with only username, produces a credential object" {
$cred = $ps.AddScript("Get-Credential -UserName 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 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.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 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"
}
}