131 lines
4.9 KiB
PowerShell
131 lines
4.9 KiB
PowerShell
|
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
|
||
|
'PSAvoidUsingConvertToSecureStringWithPlainText',
|
||
|
'',
|
||
|
Justification = 'just used in pester tests.'
|
||
|
)]
|
||
|
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
|
||
|
'PSProvideCommentHelp',
|
||
|
'',
|
||
|
Justification = 'no need in internal pester helpers.'
|
||
|
)]
|
||
|
param ()
|
||
|
|
||
|
BeforeAll {
|
||
|
$ManifestFile = (Get-Item -Path './src/*.psd1').FullName
|
||
|
Import-Module $ManifestFile -Force
|
||
|
|
||
|
$PrivateFunctions = (Get-ChildItem -Path './src/Private/*.ps1' | Where-Object {
|
||
|
$_.BaseName -notmatch '.Tests'
|
||
|
}
|
||
|
).FullName
|
||
|
foreach ( $func in $PrivateFunctions) {
|
||
|
. $func
|
||
|
}
|
||
|
|
||
|
# Backup existing credential stores
|
||
|
$VerbosePreference = 'Continue'
|
||
|
Write-Verbose -Message 'Backup private Credential Store...'
|
||
|
$CSPath = Get-DefaultCredentialStorePath
|
||
|
$BackupFile = '{0}.back' -f $CSPath
|
||
|
if (Test-Path -Path $CSPath) {
|
||
|
Move-Item -Path $CSPath -Destination $BackupFile
|
||
|
}
|
||
|
Write-Verbose -Message 'Backup shared CredentialStore...'
|
||
|
$CSShared = Get-DefaultCredentialStorePath -Shared
|
||
|
$BackupSharedFile = '{0}.back' -f $CSShared
|
||
|
if (Test-Path -Path $CSShared) {
|
||
|
Move-Item -Path $CSShared -Destination $BackupSharedFile
|
||
|
}
|
||
|
Write-Verbose -Message 'Remove old CredentialStore in Temp dir'
|
||
|
$CSTemp = Join-Path -Path (Get-TempDir) -ChildPath '/CredentialStore.json'
|
||
|
if (Test-Path -Path $CSTemp) {
|
||
|
Remove-Item -Path $CSTemp
|
||
|
}
|
||
|
$VerbosePreference = 'SilentlyContinue'
|
||
|
}
|
||
|
|
||
|
Describe 'New-CredentialStoreItem' {
|
||
|
Context 'Private Credential Store tests' {
|
||
|
It 'Add entry to a private store.' {
|
||
|
# Create a fresh CredentialStore first
|
||
|
New-CredentialStore -Force
|
||
|
|
||
|
# Define the content of the CredentialStoreItem.
|
||
|
$RemoteHost = 'barfoo'
|
||
|
$UserName = 'MyUser'
|
||
|
$Password = 'fooobarysdfsfs' | ConvertTo-SecureString -AsPlainText -Force
|
||
|
|
||
|
# Form the CredentialObject.
|
||
|
$creds = [PSCredential]::new($UserName, $Password)
|
||
|
|
||
|
# Create the CredentialStoreItem.
|
||
|
New-CredentialStoreItem -RemoteHost $RemoteHost -Credential $creds
|
||
|
|
||
|
# Formulate an update to the CredentialStoreItem.
|
||
|
$ClearPassword = 'fooobaryadfafa'
|
||
|
$Password = $ClearPassword | ConvertTo-SecureString -AsPlainText -Force
|
||
|
$creds = [PSCredential]::new($UserName, $Password)
|
||
|
{
|
||
|
Set-CredentialStoreItem -RemoteHost $RemoteHost -Credential $creds
|
||
|
} | Should -Not -Throw
|
||
|
|
||
|
# Control the content of the CredentialStore.
|
||
|
$content = Get-CredentialStoreItem -RemoteHost $RemoteHost
|
||
|
$content.GetNetworkCredential().Password | Should -Be $ClearPassword
|
||
|
}
|
||
|
}
|
||
|
Context 'Shared Credential Store tests' {
|
||
|
It 'Add entry to a shared store.' {
|
||
|
# Create a fresh CredentialStore first
|
||
|
$tmpCS = Join-Path -Path (Get-TempDir) -ChildPath '/CredentialStore.json'
|
||
|
New-CredentialStore -Path $tmpCS -Force -Shared
|
||
|
|
||
|
# Define the content of the CredentialStoreItem.
|
||
|
$RemoteHost = 'barfoo'
|
||
|
$UserName = 'MyUser'
|
||
|
$Password = 'fooobarysdfsfs' | ConvertTo-SecureString -AsPlainText -Force
|
||
|
|
||
|
# Form the CredentialObject.
|
||
|
$creds = [PSCredential]::new($UserName, $Password)
|
||
|
|
||
|
# Create the CredentialStoreItem.
|
||
|
New-CredentialStoreItem -RemoteHost $RemoteHost -Credential $creds -Path $tmpCS -Shared
|
||
|
|
||
|
# Formulate an update to the CredentialStoreItem.
|
||
|
$ClearPassword = 'fooobaryadfafa'
|
||
|
$Password = $ClearPassword | ConvertTo-SecureString -AsPlainText -Force
|
||
|
$creds = [PSCredential]::new($UserName, $Password)
|
||
|
|
||
|
{
|
||
|
Set-CredentialStoreItem -RemoteHost $RemoteHost -Credential $creds -Path $tmpCS -Shared
|
||
|
} | Should -Not -Throw
|
||
|
|
||
|
# Control the content of the CredentialStore.
|
||
|
$content = Get-CredentialStoreItem -RemoteHost $RemoteHost -Path $tmpCS -Shared
|
||
|
$content.GetNetworkCredential().Password | Should -Be $ClearPassword
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
AfterAll {
|
||
|
# Cleanup test stores and restore existing ones.
|
||
|
$VerbosePreference = 'Continue'
|
||
|
Write-Verbose -Message 'Restoring private CredentialStore'
|
||
|
If (Test-Path -Path $BackupFile) {
|
||
|
If (Test-Path -Path $CSPath) {
|
||
|
Remove-Item -Path $CSPath
|
||
|
Move-Item -Path $BackupFile -Destination $CSPath
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Write-Verbose -Message 'Restoring shared CredentialStore'
|
||
|
If (Test-Path -Path $BackupSharedFile) {
|
||
|
If (Test-Path -Path $CSShared) {
|
||
|
Remove-Item -Path $CSShared
|
||
|
Move-Item -Path $BackupSharedFile -Destination $CSShared
|
||
|
}
|
||
|
}
|
||
|
$VerbosePreference = 'SilentlyContinue'
|
||
|
|
||
|
}
|