Tests/Public/New-PWSHYBKPIVCertificateRequest.Tests.ps1
|
BeforeAll { Import-Module (Resolve-Path "$PSScriptRoot\..\..\Posh-YBKPIV.psd1") -Force $script:RealConfigPath = (Resolve-Path "$PSScriptRoot\..\..\Config\Posh-YBKPIV.json").Path InModuleScope Posh-YBKPIV -Parameters @{ ConfigPath = $script:RealConfigPath } { param($ConfigPath) $script:ConfigPath = $ConfigPath } } AfterAll { Remove-Module Posh-YBKPIV -ErrorAction SilentlyContinue } Describe 'New-PWSHYBKPIVCertificateRequest' -Tag Unit { BeforeEach { $script:InstallExePath = 'C:\Program Files\Yubico\Yubico PIV Tool\bin\yubico-piv-tool.exe' Mock -ModuleName Posh-YBKPIV Test-Path { $true } -ParameterFilter { $Path -eq $script:InstallExePath } Mock -ModuleName Posh-YBKPIV Resolve-PWSHYBKPIVArchitecture { 'win64' } Mock -ModuleName Posh-YBKPIV Get-PWSHYBKPIVInstallPath { $script:InstallExePath } Mock -ModuleName Posh-YBKPIV Invoke-PWSHYBKPIVTool { [PSCustomObject]@{ ExitCode = 0; Output = @('-----BEGIN CERTIFICATE REQUEST-----') } } } It 'Requires the -Slot and -Subject dynamic parameters' { { New-PWSHYBKPIVCertificateRequest -Confirm:$false -ErrorAction Stop } | Should -Throw } It 'Returns the raw CSR output' { New-PWSHYBKPIVCertificateRequest -Slot '9a' -Subject '/CN=example.com/' -Confirm:$false | Should -Be @('-----BEGIN CERTIFICATE REQUEST-----') } It 'Calls Invoke-PWSHYBKPIVTool with the request-certificate action' { New-PWSHYBKPIVCertificateRequest -Slot '9a' -Subject '/CN=example.com/' -Confirm:$false | Out-Null Should -Invoke -ModuleName Posh-YBKPIV Invoke-PWSHYBKPIVTool -Times 1 -ParameterFilter { $Action -eq 'request-certificate' } } It 'Does not create the request when -WhatIf is used' { New-PWSHYBKPIVCertificateRequest -Slot '9a' -Subject '/CN=example.com/' -WhatIf Should -Invoke -ModuleName Posh-YBKPIV Invoke-PWSHYBKPIVTool -Times 0 } It 'Throws a clear error when yubico-piv-tool.exe is not installed' { Mock -ModuleName Posh-YBKPIV Test-Path { $false } -ParameterFilter { $Path -eq $script:InstallExePath } { New-PWSHYBKPIVCertificateRequest -Slot '9a' -Subject '/CN=example.com/' -Confirm:$false } | Should -Throw -ExpectedMessage '*Install-PWSHYBKPIVTool*' } It 'Logs and rethrows when Invoke-PWSHYBKPIVTool fails' { Mock -ModuleName Posh-YBKPIV Invoke-PWSHYBKPIVTool { throw 'boom' } { New-PWSHYBKPIVCertificateRequest -Slot '9a' -Subject '/CN=example.com/' -Confirm:$false } | Should -Throw -ExpectedMessage '*boom*' } } |