This commit is contained in:
OCram85 2022-06-28 15:18:22 +02:00
parent 8e73cf346c
commit 01de79fe11
3 changed files with 42 additions and 32 deletions

View File

@ -9,37 +9,39 @@ BeforeAll {
foreach ( $func in $PrivateFunctions) { foreach ( $func in $PrivateFunctions) {
. $func . $func
} }
$RepoRoot = (Get-Item -Path (Get-GitDirectory) -Force).Parent | Select-Object -ExpandProperty 'FullName'
} }
Describe "Get-CredentialStore" { Describe "Get-CredentialStore" {
Context "Basic logic tests" { Context "Basic logic tests" {
$TestCredentialStore = Join-Path -Path $RepoRoot -ChildPath 'resources/cs/CredentialStore.json'
$TestPfxCert = Join-Path -Path $RepoRoot -ChildPath 'resources/cs/PSCredentialStore.pfx'
'TestCredentialStore: {0}' -f $TestCredentialStore
It "Test1: Read CS without params" { It "Test1: Read CS without params" {
$TestCredentialStore = './resources/cs/CredentialStore.json'
$TestPfxCert = './resources/cs/PSCredentialStore.pfx'
if (! (Test-Path -Path (Get-DefaultCredentialStorePath)) ) { if (! (Test-Path -Path (Get-DefaultCredentialStorePath)) ) {
{ New-CredentialStore -Force } | Should -Not -Throw { New-CredentialStore -Force } | Should -Not -Throw
} }
{ Get-CredentialStore } | Should -Not -Throw { Get-CredentialStore } | Should -Not -Throw
} }
It "Test2: Read Credential Store with testing data" { It "Test2: Read Credential Store with testing data" {
{ Use-CSCertificate -Shared -CredentialStore $TestCredentialStore -Path $TestPfxCert } | Should -Not -Throw $TestCredentialStore = './resources/cs/CredentialStore.json'
$TestPfxCert = './resources/cs/PSCredentialStore.pfx'
{
Use-CSCertificate -Shared -CredentialStore $TestCredentialStore -Path $TestPfxCert
} | Should -Not -Throw
{ Get-CredentialStore -Shared -Path $TestCredentialStore } | Should -Not -Throw { Get-CredentialStore -Shared -Path $TestCredentialStore } | Should -Not -Throw
} }
It "Test3: Not existing path should return false" { It "Test3: Not existing path should return false" {
{ Get-CredentialStore -Shared -Path './CredentialStore.json' } | Should -Throw "Could not find the CredentialStore." {
Get-CredentialStore -Shared -Path './CredentialStore.json'
} | Should -Throw "Could not find the CredentialStore."
} }
} }
Context "Testing invalid json data" { Context "Testing invalid json data" {
#Mock Test-CredentialStore {return $true}
#Mock Get-Content {return '"foo":"bar",'}
$BrokenCS = Join-Path -Path $RepoRoot -ChildPath 'resources/cs/Broken_CS.json'
Write-Verbose -Message ('BrokenCS Path: {0}' -f $BrokenCS) -Verbose
It "Should throw with invalid CredentialStore" { It "Should throw with invalid CredentialStore" {
{ Get-CredentialStore -Path -Shared $BrokenCS } | Should -Throw $BrokenCS = './resources/cs/Broken_CS.json'
Write-Verbose -Message ('BrokenCS Path: {0}' -f $BrokenCS) -Verbose
{
Get-CredentialStore -Path -Shared $BrokenCS
} | Should -Throw
} }
} }
} }

View File

@ -33,8 +33,8 @@ BeforeAll {
Describe "New-CredentialStore" { Describe "New-CredentialStore" {
Context "Private CS tests" { Context "Private CS tests" {
$pCS = Get-DefaultCredentialStorePath
It "Test1: Create new private CredentialStore" { It "Test1: Create new private CredentialStore" {
$pCS = Get-DefaultCredentialStorePath
{ New-CredentialStore -Confirm:$false } | Should -Not -Throw { New-CredentialStore -Confirm:$false } | Should -Not -Throw
$result = Test-Path -Path $pCS $result = Test-Path -Path $pCS
$CS = Get-Content -Path $pCS -Raw | ConvertFrom-Json $CS = Get-Content -Path $pCS -Raw | ConvertFrom-Json
@ -44,6 +44,7 @@ Describe "New-CredentialStore" {
{ New-CredentialStore -Confirm:$false } | Should -Throw { New-CredentialStore -Confirm:$false } | Should -Throw
} }
It "Test3: Reset existing Credential Store" { It "Test3: Reset existing Credential Store" {
$pCS = Get-DefaultCredentialStorePath
$now = Get-Date $now = Get-Date
$CS = Get-Content -Path $pCS -Raw | ConvertFrom-Json $CS = Get-Content -Path $pCS -Raw | ConvertFrom-Json
$CSCreation = [DateTime]$CS.Created $CSCreation = [DateTime]$CS.Created
@ -52,8 +53,8 @@ Describe "New-CredentialStore" {
} }
} }
Context "Shared CS tests" { Context "Shared CS tests" {
$sCS = Get-DefaultCredentialStorePath -Shared
It "Test1: Create a new Shared Credential Store" { It "Test1: Create a new Shared Credential Store" {
$sCS = Get-DefaultCredentialStorePath -Shared
{ New-CredentialStore -Confirm:$false -Shared } | Should -Not -Throw { New-CredentialStore -Confirm:$false -Shared } | Should -Not -Throw
Test-Path -Path $sCS | Should -Be $true Test-Path -Path $sCS | Should -Be $true
} }
@ -61,6 +62,7 @@ Describe "New-CredentialStore" {
{ New-CredentialStore -Shared -Confirm:$false } | Should -Throw { New-CredentialStore -Shared -Confirm:$false } | Should -Throw
} }
It "Test3: Reset shared CredentialStore" { It "Test3: Reset shared CredentialStore" {
$sCS = Get-DefaultCredentialStorePath -Shared
$now = Get-Date $now = Get-Date
$CS = Get-Content -Path $sCS -Raw | ConvertFrom-Json $CS = Get-Content -Path $sCS -Raw | ConvertFrom-Json
$CSCreation = [DateTime]$CS.Created $CSCreation = [DateTime]$CS.Created
@ -69,14 +71,16 @@ Describe "New-CredentialStore" {
} }
} }
Context "Custom Shared CS tests" { Context "Custom Shared CS tests" {
$cCS = Join-Path -Path (Get-TempDir) -ChildPath "CredentialStore.json"
It "Test1: Create new custom shared" { It "Test1: Create new custom shared" {
$cCS = Join-Path -Path (Get-TempDir) -ChildPath "CredentialStore.json"
{ New-CredentialStore -Path $cCS -Shared -Confirm:$false } | Should -Not -Throw { New-CredentialStore -Path $cCS -Shared -Confirm:$false } | Should -Not -Throw
} }
It "Test2: Try to override exiting one" { It "Test2: Try to override exiting one" {
$cCS = Join-Path -Path (Get-TempDir) -ChildPath "CredentialStore.json"
{ New-CredentialStore -Path $cCS -Shared -Confirm:$false } | Should -Throw { New-CredentialStore -Path $cCS -Shared -Confirm:$false } | Should -Throw
} }
It "Test3: Reset existing custom CredentialStore" { It "Test3: Reset existing custom CredentialStore" {
$cCS = Join-Path -Path (Get-TempDir) -ChildPath "CredentialStore.json"
{ New-CredentialStore -Path $cCS -Shared -Force -Confirm:$false } | Should -Not -Throw { New-CredentialStore -Path $cCS -Shared -Force -Confirm:$false } | Should -Not -Throw
} }
} }
@ -109,21 +113,24 @@ Describe "New-CredentialStore" {
} }
} }
# Cleanup test stores and restore existing ones. AfterAll {
$VerbosePreference = "Continue" # Cleanup test stores and restore existing ones.
Write-Verbose "Restoring private CredentialStore" $VerbosePreference = "Continue"
If (Test-Path -Path $BackupFile) { Write-Verbose "Restoring private CredentialStore"
If (Test-Path -Path $CSPath) { If (Test-Path -Path $BackupFile) {
Remove-Item -Path $CSPath If (Test-Path -Path $CSPath) {
Move-Item -Path $BackupFile -Destination $CSPath Remove-Item -Path $CSPath
Move-Item -Path $BackupFile -Destination $CSPath
}
} }
}
Write-Verbose "Restoring shared CredentialStore" Write-Verbose "Restoring shared CredentialStore"
If (Test-Path -Path $BackupSharedFile) { If (Test-Path -Path $BackupSharedFile) {
If (Test-Path -Path $CSShared) { If (Test-Path -Path $CSShared) {
Remove-Item -Path $CSShared Remove-Item -Path $CSShared
Move-Item -Path $BackupSharedFile -Destination $CSShared Move-Item -Path $BackupSharedFile -Destination $CSShared
}
} }
$VerbosePreference = "SilentlyContinue"
} }
$VerbosePreference = "SilentlyContinue"

View File

@ -15,16 +15,17 @@ BeforeAll {
Describe "Test-CredentialStore" { Describe "Test-CredentialStore" {
Context "Basic logic tests" { Context "Basic logic tests" {
$TestCredentialStore = Join-Path -Path $RepoRoot -ChildPath '/resources/cs/CredentialStore.json'
It "Test1: Should Not Throw" { It "Test1: Should Not Throw" {
$TestCredentialStore = './resources/cs/CredentialStore.json'
{ Test-CredentialStore -Shared -Path $TestCredentialStore } | Should -Not -Throw { Test-CredentialStore -Shared -Path $TestCredentialStore } | Should -Not -Throw
} }
It "Test2: Read valid CredentialStore" { It "Test2: Read valid CredentialStore" {
$TestCredentialStore = './resources/cs/CredentialStore.json'
$res = Test-CredentialStore -Shared -Path $TestCredentialStore $res = Test-CredentialStore -Shared -Path $TestCredentialStore
$res | Should -Be $true $res | Should -Be $true
} }
It "Test3: Read a broken CredentialStore" { It "Test3: Read a broken CredentialStore" {
$BrokenCS = Join-Path -Path $RepoRoot -ChildPath '{0}/resources/cs/Broken_CS.json' $BrokenCS = './resources/cs/Broken_CS.json'
$oWarningPreference = $WarningPreference $oWarningPreference = $WarningPreference
$WarningPreference = 'SilentlyContinue' $WarningPreference = 'SilentlyContinue'
$res = Test-CredentialStore -Shared -Path $BrokenCS $res = Test-CredentialStore -Shared -Path $BrokenCS