Public/Connect-Connector.tests.ps1

BeforeAll {
    $Script:Module = Import-Module "$($PSScriptRoot)/.." -Force -PassThru
    $Script:ATP = "connector-$(New-Guid)"
    Add-EntraIDExternalAccessTokenProfile -Name $Script:ATP -AccessToken (New-DummyJWT)
}

Describe "Connect-Connector" {
    It "Should throw when there is no access token profile" {
        {
            Connect-Connector -ConnectorId (New-Guid).ToString() -AccessTokenProfile (New-Guid).ToString()
        } | Should -Throw
    }

    It "Should call the get connector endpoint when an access token is available" {
        Mock -ModuleName $Script:Module.Name -CommandName Invoke-RestMethod -ParameterFilter { $Uri -like "http*/data/configuration" } -MockWith {
            @{
                IsSuccess = $true
                Data      = @{
                    Id = (New-Guid).ToString()
                }
            }
        }
        
        {
            Connect-Connector -ConnectorId (New-Guid).ToString() -AccessTokenProfile $Script:ATP
        } | Should -Not -Throw
    }
}
Describe "Connect-Connector -SyncJobId (<Name>)" -ForEach @(
    @{ Name = "Default"; Parameters = @{} }
    @{ Name = "Internal"; Parameters = @{ TenantId = "11111111-1111-1111-1111-111111111111" } }
    @{ Name = "Development"; Parameters = @{ ApiRoot = "https://localhost/iamcore/beta/sync/connectors/22222222-2222-2222-2222-222222222222/" } }
) {
    BeforeAll {
        $Script:ConnectorId = "22222222-2222-2222-2222-222222222222"
        $Script:FirstJob = "33333333-3333-3333-3333-333333333333"
        $Script:SecondJob = "44444444-4444-4444-4444-444444444444"

        Mock -ModuleName $Script:Module.Name -CommandName Get-ConnectorAuthSettings -MockWith { @{ fqdn = "example.invalid" } }
        Mock -ModuleName $Script:Module.Name -CommandName Invoke-RestMethod -ParameterFilter { $Uri -like "http*/data/configuration" } -MockWith {
            @{ IsSuccess = $true; Data = @{ Id = "22222222-2222-2222-2222-222222222222" } }
        }
        Mock -ModuleName $Script:Module.Name -CommandName Invoke-RestMethod -ParameterFilter { $Uri -like "http*/log" } -MockWith {}

        function Send-LogAfterConnecting([hashtable] $ConnectParameters) {
            Connect-Connector -ConnectorId $Script:ConnectorId -AccessTokenProfile $Script:ATP @Parameters @ConnectParameters
            Send-ConnectorLog -Text "entry"
            $Script:Posted = $null
            Should -Invoke -ModuleName $Script:Module.Name -CommandName Invoke-RestMethod -Scope It -ParameterFilter {
                if ($Uri -like "http*/log") { $Script:Posted = $Body | ConvertFrom-Json -AsHashtable; $true } else { $false }
            }
            $Script:Posted
        }
    }

    It "sends the job id it was connected with" {
        (Send-LogAfterConnecting @{ SyncJobId = $Script:FirstJob }).SyncJobId | Should -Be $Script:FirstJob
    }

    It "sends the new job id after reconnecting with another one" {
        $null = Send-LogAfterConnecting @{ SyncJobId = $Script:FirstJob }
        (Send-LogAfterConnecting @{ SyncJobId = $Script:SecondJob }).SyncJobId | Should -Be $Script:SecondJob
    }

    It "sends no job id after reconnecting without one" {
        $null = Send-LogAfterConnecting @{ SyncJobId = $Script:FirstJob }
        (Send-LogAfterConnecting @{}).ContainsKey("SyncJobId") | Should -BeFalse
    }
}