From 025f18725bb0ac2f92ea049e2408697d4c3a896d Mon Sep 17 00:00:00 2001 From: pinguinfuss Date: Thu, 6 Oct 2022 21:43:14 +0200 Subject: [PATCH] Add unit tests for Set-CredentialStoreItem --- src/Item/Set-CredentialStoreItem.Tests.ps1 | 130 +++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 src/Item/Set-CredentialStoreItem.Tests.ps1 diff --git a/src/Item/Set-CredentialStoreItem.Tests.ps1 b/src/Item/Set-CredentialStoreItem.Tests.ps1 new file mode 100644 index 0000000..218b812 --- /dev/null +++ b/src/Item/Set-CredentialStoreItem.Tests.ps1 @@ -0,0 +1,130 @@ +[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 "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 "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 "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 "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 "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" + +}