From 01de79fe1196396268b26f13674801e2a4893e7a Mon Sep 17 00:00:00 2001 From: OCram85 Date: Tue, 28 Jun 2022 15:18:22 +0200 Subject: [PATCH] wip --- src/Store/Get-CredentialStore.Tests.ps1 | 28 ++++++++-------- src/Store/New-CredentialStore.Tests.ps1 | 41 ++++++++++++++---------- src/Store/Test-CredentialStore.Tests.ps1 | 5 +-- 3 files changed, 42 insertions(+), 32 deletions(-) diff --git a/src/Store/Get-CredentialStore.Tests.ps1 b/src/Store/Get-CredentialStore.Tests.ps1 index 4398a42..25a7fae 100644 --- a/src/Store/Get-CredentialStore.Tests.ps1 +++ b/src/Store/Get-CredentialStore.Tests.ps1 @@ -9,37 +9,39 @@ BeforeAll { foreach ( $func in $PrivateFunctions) { . $func } - - $RepoRoot = (Get-Item -Path (Get-GitDirectory) -Force).Parent | Select-Object -ExpandProperty 'FullName' } Describe "Get-CredentialStore" { 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" { + $TestCredentialStore = './resources/cs/CredentialStore.json' + $TestPfxCert = './resources/cs/PSCredentialStore.pfx' if (! (Test-Path -Path (Get-DefaultCredentialStorePath)) ) { { New-CredentialStore -Force } | Should -Not -Throw - } { Get-CredentialStore } | Should -Not -Throw } 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 } 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" { - #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" { - { 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 } } } diff --git a/src/Store/New-CredentialStore.Tests.ps1 b/src/Store/New-CredentialStore.Tests.ps1 index 4a32cf4..7180661 100644 --- a/src/Store/New-CredentialStore.Tests.ps1 +++ b/src/Store/New-CredentialStore.Tests.ps1 @@ -33,8 +33,8 @@ BeforeAll { Describe "New-CredentialStore" { Context "Private CS tests" { - $pCS = Get-DefaultCredentialStorePath It "Test1: Create new private CredentialStore" { + $pCS = Get-DefaultCredentialStorePath { New-CredentialStore -Confirm:$false } | Should -Not -Throw $result = Test-Path -Path $pCS $CS = Get-Content -Path $pCS -Raw | ConvertFrom-Json @@ -44,6 +44,7 @@ Describe "New-CredentialStore" { { New-CredentialStore -Confirm:$false } | Should -Throw } It "Test3: Reset existing Credential Store" { + $pCS = Get-DefaultCredentialStorePath $now = Get-Date $CS = Get-Content -Path $pCS -Raw | ConvertFrom-Json $CSCreation = [DateTime]$CS.Created @@ -52,8 +53,8 @@ Describe "New-CredentialStore" { } } Context "Shared CS tests" { - $sCS = Get-DefaultCredentialStorePath -Shared It "Test1: Create a new Shared Credential Store" { + $sCS = Get-DefaultCredentialStorePath -Shared { New-CredentialStore -Confirm:$false -Shared } | Should -Not -Throw Test-Path -Path $sCS | Should -Be $true } @@ -61,6 +62,7 @@ Describe "New-CredentialStore" { { New-CredentialStore -Shared -Confirm:$false } | Should -Throw } It "Test3: Reset shared CredentialStore" { + $sCS = Get-DefaultCredentialStorePath -Shared $now = Get-Date $CS = Get-Content -Path $sCS -Raw | ConvertFrom-Json $CSCreation = [DateTime]$CS.Created @@ -69,14 +71,16 @@ Describe "New-CredentialStore" { } } Context "Custom Shared CS tests" { - $cCS = Join-Path -Path (Get-TempDir) -ChildPath "CredentialStore.json" 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 } 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 } 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 } } @@ -109,21 +113,24 @@ Describe "New-CredentialStore" { } } -# 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 +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 + 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" + } -$VerbosePreference = "SilentlyContinue" diff --git a/src/Store/Test-CredentialStore.Tests.ps1 b/src/Store/Test-CredentialStore.Tests.ps1 index 33733e0..14eaeed 100644 --- a/src/Store/Test-CredentialStore.Tests.ps1 +++ b/src/Store/Test-CredentialStore.Tests.ps1 @@ -15,16 +15,17 @@ BeforeAll { Describe "Test-CredentialStore" { Context "Basic logic tests" { - $TestCredentialStore = Join-Path -Path $RepoRoot -ChildPath '/resources/cs/CredentialStore.json' It "Test1: Should Not Throw" { + $TestCredentialStore = './resources/cs/CredentialStore.json' { Test-CredentialStore -Shared -Path $TestCredentialStore } | Should -Not -Throw } It "Test2: Read valid CredentialStore" { + $TestCredentialStore = './resources/cs/CredentialStore.json' $res = Test-CredentialStore -Shared -Path $TestCredentialStore $res | Should -Be $true } 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 $WarningPreference = 'SilentlyContinue' $res = Test-CredentialStore -Shared -Path $BrokenCS