ps/Modules/Alkami.DevOps.SystemEngineering/Public/Get-ACMCertificateDetailsListByName.tests.ps1

88 lines
5.4 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
. $PSScriptRoot\..\..\Load-PesterModules.ps1
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.tests\.', '.'
$functionPath = Join-Path -Path $here -ChildPath $sut
Write-Host "Overriding SUT: $functionPath"
Import-Module $functionPath -Force
$moduleForMock = ""
Describe "Get-ACMCertificateDetailsListByName" {
Mock -CommandName Get-AWSRegion -ModuleName $moduleForMock -MockWith { return @( @{ 'Region' = 'us-east-1' } ) }
Mock -CommandName Get-LogLeadName -ModuleName $moduleForMock -MockWith { return 'Get-ACMCertificateDetailsListByName.tests' }
Mock -CommandName Import-AWSModule -ModuleName $moduleForMock -MockWith {}
Mock -CommandName Write-Error -ModuleName $moduleForMock -MockWith {}
Mock -CommandName Write-Warning -ModuleName $moduleForMock -MockWith {}
Context "Error Handling" {
It "Outputs an object of type PSObject[]" {
(Get-Command Get-ACMCertificateDetailsListByName).OutputType.Type.ToString() | Should -BeExactly "System.Management.Automation.PSObject[]"
}
It "Writes Error and Returns Null If ACM Certificate List Throws" {
Mock -CommandName Get-ACMCertificateList -ModuleName $moduleForMock -MockWith { throw "Test1" }
Mock -CommandName Test-IsCollectionNullOrEmpty -ModuleName $moduleForMock -MockWith { return $true }
Mock -CommandName Get-ACMCertificateDetail -ModuleName $moduleForMock -MockWith { throw "Test2" }
Get-ACMCertificateDetailsListByName -DomainName "Test" -ProfileName 'test' -Region 'us-east-1' | Should -BeNull
Assert-MockCalled -CommandName Write-Error `
-ParameterFilter { $Message -match "Unable to retrieve ACM certificate list from AWS .* Test1" } -Times 1 -Exactly -Scope It
Assert-MockCalled -CommandName Get-ACMCertificateList -Times 1 -Exactly -Scope It
Assert-MockCalled -CommandName Test-IsCollectionNullOrEmpty -Times 0 -Exactly -Scope It
}
It "Writes Warning and Returns Empty Array If ACM Certificate List Returns Null" {
Mock -CommandName Get-ACMCertificateList -ModuleName $moduleForMock -MockWith { return $null }
Mock -CommandName Test-IsCollectionNullOrEmpty -ModuleName $moduleForMock -MockWith { return $true }
Mock -CommandName Get-ACMCertificateDetail -ModuleName $moduleForMock -MockWith { throw "Test2" }
Get-ACMCertificateDetailsListByName -DomainName "Test" -ProfileName 'test' -Region 'us-east-1' | Should -BeExactly @()
Assert-MockCalled -CommandName Write-Warning `
-ParameterFilter { $Message -match "No certificates found" } -Times 1 -Exactly -Scope It
Assert-MockCalled -CommandName Get-ACMCertificateList -Times 1 -Exactly -Scope It
Assert-MockCalled -CommandName Get-ACMCertificateDetail -Times 0 -Exactly -Scope It
Assert-MockCalled -CommandName Test-IsCollectionNullOrEmpty -Times 1 -Exactly -Scope It
Assert-MockCalled -CommandName Write-Error -Times 0 -Exactly -Scope It
}
It "Writes Warning and Returns Empty Array If ACM Certificate List Returns Empty Array" {
Mock -CommandName Get-ACMCertificateList -ModuleName $moduleForMock -MockWith { return @() }
Mock -CommandName Test-IsCollectionNullOrEmpty -ModuleName $moduleForMock -MockWith { return $true }
Mock -CommandName Get-ACMCertificateDetail -ModuleName $moduleForMock -MockWith { throw "Test2" }
Get-ACMCertificateDetailsListByName -DomainName "Test" -ProfileName 'test' -Region 'us-east-1' | Should -BeExactly @()
Assert-MockCalled -CommandName Write-Warning `
-ParameterFilter { $Message -match "No certificates found" } -Times 1 -Exactly -Scope It
Assert-MockCalled -CommandName Get-ACMCertificateList -Times 1 -Exactly -Scope It
Assert-MockCalled -CommandName Get-ACMCertificateDetail -Times 0 -Exactly -Scope It
Assert-MockCalled -CommandName Test-IsCollectionNullOrEmpty -Times 1 -Exactly -Scope It
Assert-MockCalled -CommandName Write-Error -Times 0 -Exactly -Scope It
}
It "Writes Warning and Returns Empty Array If ACM Certificate Detail Throws" {
Mock -CommandName Get-ACMCertificateList -ModuleName $moduleForMock -MockWith { return @(@{ DomainName = 'Test'; CertificateArn = 'TestArn'}) }
Mock -CommandName Test-IsCollectionNullOrEmpty -ModuleName $moduleForMock -MockWith { return $false }
Mock -CommandName Get-ACMCertificateDetail -ModuleName $moduleForMock -MockWith { throw "Test2" }
Get-ACMCertificateDetailsListByName -DomainName "Test" -ProfileName 'test' -Region 'us-east-1' | Should -BeExactly @()
Assert-MockCalled -CommandName Write-Warning `
-ParameterFilter { $Message -match "Unable to retrieve ACM certificate details for ARN \[TestArn\] : Test2" } -Times 1 -Exactly -Scope It
Assert-MockCalled -CommandName Get-ACMCertificateList -Times 1 -Exactly -Scope It
Assert-MockCalled -CommandName Get-ACMCertificateDetail -Times 1 -Exactly -Scope It
Assert-MockCalled -CommandName Test-IsCollectionNullOrEmpty -Times 1 -Exactly -Scope It
Assert-MockCalled -CommandName Write-Error -Times 0 -Exactly -Scope It
}
}
}