Tests/Unit/Private/Test-SIASession.Tests.ps1

BeforeAll {
    $moduleRoot = (Resolve-Path (Join-Path $PSScriptRoot '../../..')).Path
    Import-Module (Join-Path $moduleRoot 'psSIA.psd1') -Force
}

Describe 'Test-SIASession' {
    AfterEach {
        InModuleScope psSIA { $script:SIASession = $null }
    }

    It 'throws when there is no active session' {
        InModuleScope psSIA {
            { Test-SIASession } | Should -Throw '*No active SIA session*'
        }
    }

    It 'throws when the session has expired' {
        InModuleScope psSIA {
            $script:SIASession = [pscustomobject]@{
                Subdomain = 'contoso'
                ExpiresAt = (Get-Date).ToUniversalTime().AddMinutes(-1)
            }
            { Test-SIASession } | Should -Throw '*expired*'
        }
    }

    It 'returns nothing by default when the session is valid' {
        InModuleScope psSIA {
            $script:SIASession = [pscustomobject]@{
                Subdomain = 'contoso'
                ExpiresAt = (Get-Date).ToUniversalTime().AddMinutes(10)
            }
            Test-SIASession | Should -BeNullOrEmpty
        }
    }

    It 'returns the session object with -PassThru' {
        InModuleScope psSIA {
            $script:SIASession = [pscustomobject]@{
                Subdomain = 'contoso'
                ExpiresAt = (Get-Date).ToUniversalTime().AddMinutes(10)
            }
            (Test-SIASession -PassThru).Subdomain | Should -Be 'contoso'
        }
    }
}