forked from OCram85/PSCredentialStore
151 lines
6.8 KiB
PowerShell
151 lines
6.8 KiB
PowerShell
|
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
|
||
|
'PSAvoidUsingConvertToSecureStringWithPlainText',
|
||
|
'',
|
||
|
Justification = 'just used in pester tests.'
|
||
|
)]
|
||
|
param ()
|
||
|
|
||
|
BeforeAll {
|
||
|
$Repo = Get-RepoPath
|
||
|
Import-Module $Repo.Src.Manifest.Item.FullName -Force
|
||
|
|
||
|
# Backup existing CredentialStores
|
||
|
$Paths = @(('{0}\AppData\Roaming' -f $env:USERPROFILE), ('{0}\ProgramData\PSCredentialStore\' -f $env:SystemDrive))
|
||
|
$Files = @('CredentialStore.json', 'PSCredentialStore.pfx')
|
||
|
|
||
|
foreach ($Filepath in $Paths) {
|
||
|
foreach ($File in $Files) {
|
||
|
$OrgPath = Join-Path -Path $FilePath -ChildPath $File
|
||
|
$NewPath = $OrgPath + '.orig'
|
||
|
if (Test-Path $OrgPath) {
|
||
|
try {
|
||
|
$null = Remove-Item -Path $NewPath -Force -Confirm:$false
|
||
|
$null = Rename-Item -Path $OrgPath -NewName $NewPath -Confirm:$false
|
||
|
}
|
||
|
catch {
|
||
|
$_.Exception.Message | Write-Warning
|
||
|
Write-Error -Message ('Unable to revert {0} to {1}' -f $OrgPath, $NewPath)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
# Construct the necessary CredentialStores for the Unit tests.
|
||
|
New-CredentialStore -Force
|
||
|
New-CredentialStore -Shared -Force
|
||
|
|
||
|
# Construct the necessary CredentialStoreItems for the Unit tests.
|
||
|
$CredentialUserName = 'MyUser'
|
||
|
$CredentialPassword = 'FooBar' | ConvertTo-SecureString -AsPlainText -Force
|
||
|
$Credential = [PSCredential]::new($CredentialUserName, $CredentialPassword)
|
||
|
|
||
|
# Create the CredentialStoreItems
|
||
|
New-CredentialStoreItem -RemoteHost 'test-case-a.domain.my' -Credential $Credential
|
||
|
New-CredentialStoreItem -Shared -RemoteHost 'test-case-a.domain.my' -Credential $Credential
|
||
|
|
||
|
New-CredentialStoreItem -Shared -RemoteHost 'test-case-b.domain.my' -Credential $Credential
|
||
|
New-CredentialStoreItem -RemoteHost 'test-case-c.domain.my' -Credential $Credential
|
||
|
|
||
|
New-CredentialStoreItem -RemoteHost 'test-case-a.domain.my' -Credential $Credential -Identifier 'Foo'
|
||
|
New-CredentialStoreItem -Shared -RemoteHost 'test-case-a.domain.my' -Credential $Credential -Identifier 'Foo'
|
||
|
|
||
|
New-CredentialStoreItem -Shared -RemoteHost 'test-case-b.domain.my' -Credential $Credential -Identifier 'Foo'
|
||
|
New-CredentialStoreItem -RemoteHost 'test-case-c.domain.my' -Credential $Credential -Identifier 'Foo'
|
||
|
}
|
||
|
|
||
|
AfterAll {
|
||
|
# Check if the private CredentialStore exists
|
||
|
$Paths = @(('{0}\AppData\Roaming' -f $env:USERPROFILE), ('{0}\ProgramData\PSCredentialStore\' -f $env:SystemDrive))
|
||
|
$Files = @('CredentialStore.json.orig', 'PSCredentialStore.pfx.orig')
|
||
|
|
||
|
foreach ($Filepath in $Paths) {
|
||
|
foreach ($File in $Files) {
|
||
|
$OrgPath = Join-Path -Path $FilePath -ChildPath $File
|
||
|
$NewPath = $OrgPath.Replace('.orig', '')
|
||
|
if (Test-Path $OrgPath) {
|
||
|
try {
|
||
|
$null = Remove-Item -Path $NewPath -Force -Confirm:$false -ErrorAction SilentlyContinue
|
||
|
$null = Rename-Item -Path $OrgPath -NewName $NewPath -Confirm:$false
|
||
|
}
|
||
|
catch {
|
||
|
$_.Exception.Message | Write-Warning
|
||
|
Write-Error -Message ('Unable to revert {0} to {1}' -f $OrgPath, $NewPath)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Describe 'Find-CredentialStoreItem' {
|
||
|
Context 'Default tests' -Tag 'Default' {
|
||
|
It 'Test Function' {
|
||
|
{ Get-Command -Name 'Find-CredentialStoreItem' -Module $Repo.Artifact } | Should -Not -Throw
|
||
|
}
|
||
|
|
||
|
It 'Test Help' {
|
||
|
{ Get-Help -Name 'Find-CredentialStoreItem' } | Should -Not -Throw
|
||
|
}
|
||
|
|
||
|
It 'Help Content' {
|
||
|
$foo = Get-Help -Name 'Find-CredentialStoreItem'
|
||
|
$foo.Synopsis.Length | Should -BeGreaterThan 5
|
||
|
$foo.Description.Count | Should -BeGreaterOrEqual 1
|
||
|
$foo.Description[0].Text.Length | Should -BeGreaterThan 5
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Context 'Coding tests' -Tag 'Coding' {
|
||
|
It 'Calling Find-CredentialStoreItem with wrong Type' {
|
||
|
{ Find-CredentialStoreItem -RemoteHost 'test-case-a.domain.my' -Type 'Foo' } | Should -Throw
|
||
|
}
|
||
|
|
||
|
It 'Calling Find-CredentialStoreItem present in both CredentialStores w/o Identifier' {
|
||
|
{ Find-CredentialStoreItem -RemoteHost 'test-case-a.domain.my' } | Should -Not -Throw
|
||
|
|
||
|
$foo = Find-CredentialStoreItem -RemoteHost 'test-case-a.domain.my'
|
||
|
$foo.UserName | Should -Be 'MyUser'
|
||
|
$foo.GetNetworkCredential().Password | Should -Be 'FooBar'
|
||
|
}
|
||
|
|
||
|
It 'Calling Find-CredentialStoreItem present only in shared CredentialStore w/o Identifier' {
|
||
|
{ Find-CredentialStoreItem -RemoteHost 'test-case-b.domain.my' } | Should -Not -Throw
|
||
|
|
||
|
$foo = Find-CredentialStoreItem -RemoteHost 'test-case-b.domain.my'
|
||
|
$foo.UserName | Should -Be 'MyUser'
|
||
|
$foo.GetNetworkCredential().Password | Should -Be 'FooBar'
|
||
|
}
|
||
|
|
||
|
It 'Calling Find-CredentialStoreItem present only in private CredentialStore w/o Identifier' {
|
||
|
{ Find-CredentialStoreItem -RemoteHost 'test-case-c.domain.my' } | Should -Not -Throw
|
||
|
|
||
|
$foo = Find-CredentialStoreItem -RemoteHost 'test-case-c.domain.my'
|
||
|
$foo.UserName | Should -Be 'MyUser'
|
||
|
$foo.GetNetworkCredential().Password | Should -Be 'FooBar'
|
||
|
}
|
||
|
|
||
|
It 'Calling Find-CredentialStoreItem present in both CredentialStores w Identifier' {
|
||
|
{ Find-CredentialStoreItem -RemoteHost 'test-case-a.domain.my' -Identifier 'Foo' } | Should -Not -Throw
|
||
|
|
||
|
$foo = Find-CredentialStoreItem -RemoteHost 'test-case-a.domain.my' -Identifier 'Foo'
|
||
|
$foo.UserName | Should -Be 'MyUser'
|
||
|
$foo.GetNetworkCredential().Password | Should -Be 'FooBar'
|
||
|
}
|
||
|
|
||
|
It 'Calling Find-CredentialStoreItem present only in shared CredentialStore w/o Identifier' {
|
||
|
{ Find-CredentialStoreItem -RemoteHost 'test-case-b.domain.my' -Identifier 'Foo' } | Should -Not -Throw
|
||
|
|
||
|
$foo = Find-CredentialStoreItem -RemoteHost 'test-case-b.domain.my' -Identifier 'Foo'
|
||
|
$foo.UserName | Should -Be 'MyUser'
|
||
|
$foo.GetNetworkCredential().Password | Should -Be 'FooBar'
|
||
|
}
|
||
|
|
||
|
It 'Calling Find-CredentialStoreItem present only in private CredentialStore w/o Identifier' {
|
||
|
{ Find-CredentialStoreItem -RemoteHost 'test-case-c.domain.my' -Identifier 'Foo' } | Should -Not -Throw
|
||
|
|
||
|
$foo = Find-CredentialStoreItem -RemoteHost 'test-case-c.domain.my' -Identifier 'Foo'
|
||
|
$foo.UserName | Should -Be 'MyUser'
|
||
|
$foo.GetNetworkCredential().Password | Should -Be 'FooBar'
|
||
|
}
|
||
|
}
|
||
|
}
|