Tests/Private/Get-StorageAccountKey.Tests.ps1
|
BeforeAll { . "$PSScriptRoot/../../Private/Get-Timestamp.ps1" . "$PSScriptRoot/../../Private/Get-StorageAccountKey.ps1" } Describe 'Get-StorageAccountKey' { BeforeEach { Mock Write-Host {} } Context 'When key retrieval succeeds' { BeforeEach { Mock -CommandName 'az' -MockWith { $global:LASTEXITCODE = 0 'abc123secretkey==' } } It 'Returns the key string' { $result = Get-StorageAccountKey -AccountName 'teststorage' $result | Should -Be 'abc123secretkey==' } It 'Writes success message to host' { Get-StorageAccountKey -AccountName 'teststorage' | Out-Null Should -Invoke Write-Host -ParameterFilter { $Object -like "*Key retrieved*teststorage*" } } } Context 'When key retrieval fails' { BeforeEach { Mock -CommandName 'az' -MockWith { $global:LASTEXITCODE = 1 'AuthorizationFailed' } } It 'Throws with account name in message' { { Get-StorageAccountKey -AccountName 'teststorage' } | Should -Throw "*teststorage*" } } Context 'When stderr output is mixed with key output' { BeforeEach { Mock -CommandName 'az' -MockWith { $global:LASTEXITCODE = 0 # Simulate stderr warnings mixed with stdout Write-Error 'WARNING: Some deprecation notice' -ErrorAction SilentlyContinue 2>$null 'realkey123==' } } It 'Filters out error records and returns the clean key' { $result = Get-StorageAccountKey -AccountName 'teststorage' $result | Should -Be 'realkey123==' } } Context 'Parameter validation' { It 'AccountName parameter is mandatory' { $param = (Get-Command Get-StorageAccountKey).Parameters['AccountName'] $param.Attributes | Where-Object { $_ -is [System.Management.Automation.ParameterAttribute] } | ForEach-Object { $_.Mandatory | Should -BeTrue } } } } |