ps/Modules/Alkami.DevOps.Certificates/Public/Export-Certificates.tests.ps1
2023-05-30 22:51:22 -07:00

128 lines
4.7 KiB
PowerShell

. $PSScriptRoot\..\..\Load-PesterModules.ps1
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.tests\.', '.'
# Yes. I hate this at least as much as you do. If you can find a way around it, please show me.
$global:functionPath = Join-Path -Path $here -ChildPath $sut
Write-Host "Overriding SUT: $functionPath"
Import-Module $functionPath -Force
$moduleForMock = ""
$compiledModuleForMock = "Alkami.DevOps.Certificates"
$exportPassword = "Test"
$exportPath = "c:\temp\CertificateTest"
Remove-FileSystemItem -Path $exportPath -Force -Recurse -ErrorAction SilentlyContinue | Out-Null
New-Item -ItemType Directory $exportPath -Force | Out-Null
InModuleScope -ModuleName Alkami.DevOps.Certificates -ScriptBlock {
Write-Host "InModuleScope - Overriding SUT: $($global:functionPath)"
Import-Module $global:functionPath -Force
$inScopeModuleForAssert = "Alkami.DevOps.Certificates"
$exportPassword = "Test"
$exportPath = "c:\temp\CertificateTest"
$moduleForMock = ""
Mock -CommandName Export-Cert {
[Collections.Generic.List[Alkami.Ops.Common.Exceptions.CertificateExportException]] $emptyArr = @()
return ,$emptyArr
} -ModuleName $moduleForMock
#prevents the mehtod under test from clearing the screen during testing.
Mock -ModuleName $moduleForMock -CommandName Clear-Host {}
Mock -CommandName Write-Host -MockWith {} -ModuleName $moduleForMock
Describe "Export-Certificates" {
Context "When there are bad inputs when calling Export-Certificates" {
It "Throws Exception if all skip flags set" {
{ Export-Certificates $exportPassword -skipPersonalCert -skipRootCerts -skipTrustedCert -skipIACert } | Should Throw
}
}
Context "When the parameters are valid" {
It "Doesnt Require Password if not exporting Personal Certificates" {
{ Export-Certificates -skipPersonalCerts } | Should -Not -Throw
}
It "Creates Path if it doesn't exist" {
$path = 'c:\temp\badPath'
Export-Certificates $exportPassword -exportPath $path
$path | Should -Exist
}
It "Creates Personal Directory" {
Export-Certificates $exportPassword -exportPath $exportPath
Join-Path $exportPath "Personal" | Should -Exist
}
It "Creates IA Directory" {
Export-Certificates $exportPassword -exportPath $exportPath
Join-Path $exportPath "IA" | Should -Exist
}
It "Creates Root Directory" {
Export-Certificates $exportPassword -exportPath $exportPath
Join-Path $exportPath "Root" | Should -Exist
}
It "Creates TrustedPeople Directory" {
Export-Certificates $exportPassword -exportPath $exportPath
Join-Path $exportPath "TrustedPeople" | Should -Exist
}
It "Calls Export-Certs 4 times when no filters are used" {
Export-Certificates $exportPassword -exportPath $exportPath
#$result | Should be {}
Assert-MockCalled -ModuleName $inScopeModuleForAssert -CommandName Export-Cert -Times 4 -Exactly -Scope It
}
It "Calls Export-Certs 3 times when skipRootCerts filter used" {
Export-Certificates $exportPassword -exportPath $exportPath -skipRootCerts
#$result | Should be {}
Assert-MockCalled -ModuleName $inScopeModuleForAssert -CommandName Export-Cert -Times 3 -Exactly -Scope It
}
It "Calls Export-Certs 3 times when skipPersonalCerts filter used" {
Export-Certificates $exportPassword -exportPath $exportPath -skipPersonalCerts
#$result | Should be {}
Assert-MockCalled -ModuleName $inScopeModuleForAssert -CommandName Export-Cert -Times 3 -Exactly -Scope It
}
It "Calls Export-Certs 3 times when skipTrustedCerts filter used" {
Export-Certificates $exportPassword -exportPath $exportPath -skipTrustedCerts
#$result | Should be {}
Assert-MockCalled -ModuleName $inScopeModuleForAssert -CommandName Export-Cert -Times 3 -Exactly -Scope It
}
It "Calls Export-Certs 3 times when skipIACerts filter used" {
Export-Certificates $exportPassword -exportPath $exportPath -skipIACerts
#$result | Should be {}
Assert-MockCalled -ModuleName $inScopeModuleForAssert -CommandName Export-Cert -Times 3 -Exactly -Scope It
}
}
}
}