Tests/Unit/Public/Targets.Tests.ps1

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

Describe 'Get-SIASSHCAPublicKey' {
    It 'calls the public-keys endpoint' {
        InModuleScope psSIA {
            Mock Invoke-SIARequest {
                param($Method, $Path)
                $Method | Should -Be 'GET'
                $Path | Should -Be '/api/public-keys'
                [pscustomobject]@{ key = 'ssh-rsa AAAA...' }
            }

            (Get-SIASSHCAPublicKey).key | Should -Match '^ssh-rsa'
        }
    }
}

Describe 'SSH CA key rotation cmdlets' {
    It 'Update-SIASSHCAPublicKey calls generate-new' {
        InModuleScope psSIA {
            Mock Invoke-SIARequest { param($Path) $Path | Should -Be '/api/public-keys/rotation/generate-new' }
            Update-SIASSHCAPublicKey -Confirm:$false
        }
    }

    It 'Disable-SIASSHCAPublicKey calls deactivate-previous' {
        InModuleScope psSIA {
            Mock Invoke-SIARequest { param($Path) $Path | Should -Be '/api/public-keys/rotation/deactivate-previous' }
            Disable-SIASSHCAPublicKey -Confirm:$false
        }
    }

    It 'Enable-SIASSHCAPublicKey calls reactivate-previous' {
        InModuleScope psSIA {
            Mock Invoke-SIARequest { param($Path) $Path | Should -Be '/api/public-keys/rotation/reactivate-previous' }
            Enable-SIASSHCAPublicKey -Confirm:$false
        }
    }
}

Describe 'Get-SIASSHHostKeyFingerprint' {
    It 'accepts TargetId from the pipeline by property name' {
        InModuleScope psSIA {
            Mock Invoke-SIARequest {
                param($Path)
                $Path | Should -Be '/api/ssh-fingerprints/target-9'
                [pscustomobject]@{ target_id = 'target-9' }
            }

            [pscustomobject]@{ target_id = 'target-9' } | Get-SIASSHHostKeyFingerprint | Out-Null
        }
    }
}

Describe 'Get-SIATargetSet' {
    It 'lists target sets' {
        InModuleScope psSIA {
            Mock Invoke-SIARequest {
                param($Path)
                $Path | Should -Be '/api/targetsets'
                @(@{ target_set = 'contoso.local' })
            }

            (Get-SIATargetSet).target_set | Should -Be 'contoso.local'
        }
    }
}

Describe 'New-SIASSHCAPublicKeyScript' {
    It 'calls the public-keys scripts endpoint' {
        InModuleScope psSIA {
            Mock Invoke-SIARequest {
                param($Method, $Path)
                $Method | Should -Be 'GET'
                $Path | Should -Be '/api/public-keys/scripts'
            }

            New-SIASSHCAPublicKeyScript | Out-Null
        }
    }
}

Describe 'Get-SIAMFAKey' {
    It 'calls the SSH SSO key endpoint' {
        InModuleScope psSIA {
            Mock Invoke-SIARequest {
                param($Method, $Path)
                $Method | Should -Be 'GET'
                $Path | Should -Be '/api/ssh/sso/key'
            }

            Get-SIAMFAKey | Out-Null
        }
    }
}

Describe 'SSH host key fingerprint write cmdlets' {
    It 'Add-SIASSHHostKeyFingerprint posts target_id and fingerprint to /api/ssh-fingerprints' {
        InModuleScope psSIA {
            Mock Invoke-SIARequest {
                param($Method, $Path, $Body)
                $Method | Should -Be 'POST'
                $Path | Should -Be '/api/ssh-fingerprints'
                $Body.target_id | Should -Be 'target-9'
                $Body.fingerprint | Should -Be 'SHA256:abc'
            }

            Add-SIASSHHostKeyFingerprint -TargetId 'target-9' -Fingerprint 'SHA256:abc' -Confirm:$false
        }
    }

    It 'Set-SIASSHHostKeyFingerprint puts target_id and fingerprint to /api/ssh-fingerprints' {
        InModuleScope psSIA {
            Mock Invoke-SIARequest {
                param($Method, $Path, $Body)
                $Method | Should -Be 'PUT'
                $Path | Should -Be '/api/ssh-fingerprints'
                $Body.target_id | Should -Be 'target-9'
                $Body.fingerprint | Should -Be 'SHA256:def'
            }

            Set-SIASSHHostKeyFingerprint -TargetId 'target-9' -Fingerprint 'SHA256:def' -Confirm:$false
        }
    }

    It 'Remove-SIASSHHostKeyFingerprint deletes /api/ssh-fingerprints/{target_id}' {
        InModuleScope psSIA {
            Mock Invoke-SIARequest {
                param($Method, $Path)
                $Method | Should -Be 'DELETE'
                $Path | Should -Be '/api/ssh-fingerprints/target-9'
            }

            Remove-SIASSHHostKeyFingerprint -TargetId 'target-9' -Confirm:$false
        }
    }

    It 'does not call the API under -WhatIf' {
        InModuleScope psSIA {
            Mock Invoke-SIARequest {}
            Remove-SIASSHHostKeyFingerprint -TargetId 'target-9' -WhatIf
            Should -Invoke Invoke-SIARequest -Times 0 -Exactly
        }
    }
}

Describe 'Target set write cmdlets' {
    It 'New-SIATargetSet posts to /api/targetsets/bulk' {
        InModuleScope psSIA {
            Mock Invoke-SIARequest {
                param($Method, $Path)
                $Method | Should -Be 'POST'
                $Path | Should -Be '/api/targetsets/bulk'
            }

            New-SIATargetSet -Body @(@{ target_set = 'contoso.local' }) -Confirm:$false
        }
    }

    It 'Remove-SIATargetSet deletes /api/targetsets/bulk' {
        InModuleScope psSIA {
            Mock Invoke-SIARequest {
                param($Method, $Path)
                $Method | Should -Be 'DELETE'
                $Path | Should -Be '/api/targetsets/bulk'
            }

            Remove-SIATargetSet -Body @(@{ target_set = 'contoso.local' }) -Confirm:$false
        }
    }
}