Tests/Integration/SIA.Integration.Tests.ps1

# Integration tests exercise a real SIA tenant. They are skipped unless every
# required environment variable is present, so they never run as part of the
# default unit test suite or in CI without explicit configuration:
#
# SIA_SUBDOMAIN SIA tenant subdomain
# SIA_IDENTITY_TENANT_ID Identity tenant ID used for authentication
# SIA_CLIENT_ID Service account client ID
# SIA_CLIENT_SECRET Service account client secret
#
# Run explicitly with:
# Invoke-Pester -Path ./Tests/Integration

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

    $script:HasCredentials = @(
        $env:SIA_SUBDOMAIN, $env:SIA_IDENTITY_TENANT_ID, $env:SIA_CLIENT_ID, $env:SIA_CLIENT_SECRET
    ) -notcontains $null -and @(
        $env:SIA_SUBDOMAIN, $env:SIA_IDENTITY_TENANT_ID, $env:SIA_CLIENT_ID, $env:SIA_CLIENT_SECRET
    ) -notcontains ''
}

Describe 'SIA integration' -Skip:(-not $script:HasCredentials) {
    BeforeAll {
        $credential = [PSCredential]::new($env:SIA_CLIENT_ID, (ConvertTo-SecureString $env:SIA_CLIENT_SECRET -AsPlainText -Force))
        New-SIASession -Subdomain $env:SIA_SUBDOMAIN -IdentityTenantId $env:SIA_IDENTITY_TENANT_ID -Credential $credential | Out-Null
    }

    AfterAll {
        Close-SIASession -Confirm:$false
    }

    It 'authenticates and starts a session' {
        (Get-SIASession).IsExpired | Should -Be $false
    }

    It 'lists connectors without error' {
        { Get-SIAConnector } | Should -Not -Throw
    }

    It 'lists SIA settings without error' {
        { Get-SIASetting } | Should -Not -Throw
    }
}