Tests/Unit/Private/Resolve-SCAUri.Tests.ps1

Import-Module (Join-Path $PSScriptRoot '../../../psSCA.psd1') -Force

Describe 'Resolve-SCAUri' {
    InModuleScope psSCA {
        BeforeAll {
            $script:testSession = [pscustomobject]@{
                PSTypeName      = 'psSCA.Session'
                TenantSubdomain = 'contoso'
            }
        }

        It 'builds the correct base URL per service' {
            (Resolve-SCAUri -Session $testSession -Service 'SCA' -Path '/access/sessions') |
                Should -Be 'https://contoso.sca.cyberark.cloud/api/access/sessions'

            (Resolve-SCAUri -Session $testSession -Service 'UAP' -Path '/policies') |
                Should -Be 'https://contoso.uap.cyberark.cloud/api/policies'

            (Resolve-SCAUri -Session $testSession -Service 'UAR' -Path '/workflows/requests') |
                Should -Be 'https://contoso.uar.cyberark.cloud/api/workflows/requests'

            (Resolve-SCAUri -Session $testSession -Service 'CDS' -Path '/detections/cloud-service-entitlements/list') |
                Should -Be 'https://contoso.cds.cyberark.cloud/detections/cloud-service-entitlements/list'

            (Resolve-SCAUri -Session $testSession -Service 'CEM' -Path '/delegations/workspace/search') |
                Should -Be 'https://contoso.cem.cyberark.cloud/delegations/workspace/search'

            (Resolve-SCAUri -Session $testSession -Service 'Compass' -Path '/recommendations') |
                Should -Be 'https://contoso.compass.cyberark.cloud/api/recommendations'
        }

        It 'expands path placeholders from -PathParameters' {
            $uri = Resolve-SCAUri -Session $testSession -Service 'SCA' -Path '/access/{csp}/eligibility' -PathParameters @{ csp = 'aws' }
            $uri | Should -Be 'https://contoso.sca.cyberark.cloud/api/access/aws/eligibility'
        }

        It 'URL-encodes expanded path parameter values' {
            $uri = Resolve-SCAUri -Session $testSession -Service 'UAR' -Path '/workflows/requests/{requestId}' -PathParameters @{ requestId = 'req 123' }
            $uri | Should -Be 'https://contoso.uar.cyberark.cloud/api/workflows/requests/req%20123'
        }

        It 'appends a query string when -QueryParameters is supplied' {
            $uri = Resolve-SCAUri -Session $testSession -Service 'UAR' -Path '/workflows/requests' -QueryParameters @{ status = 'Pending' }
            $uri | Should -Be 'https://contoso.uar.cyberark.cloud/api/workflows/requests?status=Pending'
        }

        It 'throws when a path placeholder is not resolved' {
            { Resolve-SCAUri -Session $testSession -Service 'SCA' -Path '/access/{csp}/eligibility' } |
                Should -Throw '*unresolved path placeholder*'
        }
    }
}