Tests/Private/New-SasToken.Tests.ps1

BeforeAll {
    . "$PSScriptRoot/../../Private/Get-Timestamp.ps1"
    . "$PSScriptRoot/../../Private/New-SasToken.ps1"
}

Describe 'New-SasToken' {
    BeforeEach {
        Mock Write-Host {}
    }

    Context 'When SAS generation succeeds' {
        BeforeEach {
            Mock -CommandName 'az' -MockWith {
                $global:LASTEXITCODE = 0
                'se=2026-04-10&sp=rl&sig=abc123'
            }
        }

        It 'Returns the SAS token string' {
            $result = New-SasToken -AccountName 'teststorage' -Permissions 'rl'
            $result | Should -Be 'se=2026-04-10&sp=rl&sig=abc123'
        }

        It 'Uses default ExpiryHours of 24' {
            New-SasToken -AccountName 'teststorage' -Permissions 'rl' | Out-Null
            Should -Invoke Write-Host -ParameterFilter { $Object -like '*expires in 24h*' }
        }

        It 'Respects custom ExpiryHours' {
            New-SasToken -AccountName 'teststorage' -Permissions 'rl' -ExpiryHours 48 | Out-Null
            Should -Invoke Write-Host -ParameterFilter { $Object -like '*expires in 48h*' }
        }
    }

    Context 'When SAS generation fails' {
        BeforeEach {
            Mock -CommandName 'az' -MockWith {
                $global:LASTEXITCODE = 1
                'AuthorizationFailure'
            }
        }

        It 'Throws with account name' {
            { New-SasToken -AccountName 'teststorage' -Permissions 'rl' } | Should -Throw "*teststorage*"
        }
    }

    Context 'Parameter validation' {
        It 'AccountName parameter is mandatory' {
            $param = (Get-Command New-SasToken).Parameters['AccountName']
            $param.Attributes | Where-Object { $_ -is [System.Management.Automation.ParameterAttribute] } | ForEach-Object {
                $_.Mandatory | Should -BeTrue
            }
        }

        It 'Permissions parameter is mandatory' {
            $param = (Get-Command New-SasToken).Parameters['Permissions']
            $param.Attributes | Where-Object { $_ -is [System.Management.Automation.ParameterAttribute] } | ForEach-Object {
                $_.Mandatory | Should -BeTrue
            }
        }
    }
}