Happypath1.tests.ps1

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

    # Mock /configuration api call
    Mock -ModuleName $Script:Module.Name -CommandName Invoke-RestMethod -ParameterFilter { $Uri -like "http*://*.*fortytwo.*/connectors/$($Script:ConnectorId)/" } -MockWith {
        @{
            IsSuccess = $true
            Data      = @{
                id            = $Script:ConnectorId
                configuration = @{}
                secrets       = @{}
            }
        }
    }

    # Mock /data api call
    $Script:Person98765Id = [Guid]::NewGuid().ToString()
    $Script:Person66666Id = [Guid]::NewGuid().ToString()
    Mock -ModuleName $Script:Module.Name -CommandName Invoke-RestMethod -ParameterFilter { $Uri -like "http*://*.*fortytwo.*/connectors/$($Script:ConnectorId)/data" -and $Method -eq "GET" } -MockWith {
        @{
            IsSuccess = $true
            Data      = @(
                @{
                    id         = $Script:Person98765Id
                    objectType = "person"
                    externalId = "98765"
                    data       = @{
                        firstname = "test"
                    }
                }

                @{
                    id         = $Script:Person66666Id
                    objectType = "person"
                    externalId = "66666"
                    data       = @{
                        firstname = "test"
                    }
                }
            )
        }
    }

    Mock -ModuleName $Script:Module.Name -CommandName Invoke-RestMethod -ParameterFilter { $Uri -like "http*://*.*fortytwo.*/connectors/$($Script:ConnectorId)/data" -and $Method -eq "POST" } -MockWith { @{
            IsSuccess = $true
            Data      = @{Id = [Guid]::NewGuid() }
        } }

    Mock -ModuleName $Script:Module.Name -CommandName Invoke-RestMethod -ParameterFilter { $Uri -like "http*://*.*fortytwo.*/connectors/$($Script:ConnectorId)/data/$($Script:Person98765Id)" -and $Method -eq "DELETE" } -MockWith {}
    Mock -ModuleName $Script:Module.Name -CommandName Invoke-RestMethod -ParameterFilter { $Uri -like "http*://*.*fortytwo.*/connectors/$($Script:ConnectorId)/data/$($Script:Person66666Id)" -and $Method -eq "PUT" } -MockWith {}
}

Describe "Happypath1" -Tag "happypath" {
    It "Should succesfully connect" {
        {
            Connect-Connector -ConnectorId $Script:ConnectorId -AccessTokenProfile $Script:ATP
        } | Should -Not -Throw
    }

    It "Is possible to start sync session" {
        {
            Start-ConnectorSyncSession
        } | Should -Not -Throw
    }

    It "Is possible to build and add connector object to session" {
        Build-ConnectorObject -ExternalId "12345" -ObjectType "person" -Data @{
            firstname = "Test"
            lastname  = "Test"
        } | Add-ConnectorSyncSessionObject
    }

    It "Is possible to build and add connector object to session (2)" {
        Build-ConnectorObject -ExternalId "66666" -ObjectType "person" -Data @{
            firstname = "Test"
            lastname  = "Test"
        } | Add-ConnectorSyncSessionObject
    }

    It "Returns the expected create operation, and can complete it" {
        $Operations = Get-ConnectorSyncSessionOperation

        $Create = $Operations | ? OperationType -eq "Create"
        $Create.Id | Should -BeExactly $null
        $Create.ConnectorObject.externalId | Should -BeExactly "12345"
        $Create.ConnectorObject.data.firstname | Should -BeExactly "Test"
        $Create.ConnectorObject.objectType | Should -BeExactly "person"

        $Create | Complete-ConnectorSyncSessionOperation
        Assert-MockCalled -ModuleName $Script:Module.Name -CommandName Invoke-RestMethod -Times 1 -ParameterFilter { $Method -eq "Post" -and $Uri -like "*/data" }
    }

    It "Returns the expected update operation, and can complete it" {
        $Operations = Get-ConnectorSyncSessionOperation

        $Update = $Operations | ? OperationType -eq "Update"
        $Update.Id | Should -BeExactly $Script:Person66666Id
        $Update.ConnectorObject.externalId | Should -BeExactly "66666"
        $Update.ConnectorObject.data.firstname | Should -BeExactly "Test"
        $Update.ConnectorObject.data.lastname | Should -BeExactly "Test"
        $Update.ConnectorObject.objectType | Should -BeExactly "person"

        $Update | Complete-ConnectorSyncSessionOperation
        Assert-MockCalled -ModuleName $Script:Module.Name -CommandName Invoke-RestMethod -Times 1 -ParameterFilter { $Method -eq "Put" -and $Uri -like "*/data/*-*-*-*-*" }
    }

    It "Returns the expected delete operation, and can complete it" {
        $Operations = Get-ConnectorSyncSessionOperation

        $Delete = $Operations | ? OperationType -eq "Delete"
        $Delete.Id | Should -BeExactly $Script:Person98765Id

        $Delete | Complete-ConnectorSyncSessionOperation
        Assert-MockCalled -ModuleName $Script:Module.Name -CommandName Invoke-RestMethod -Times 1 -ParameterFilter { $Method -eq "Delete" -and $Uri -like "*/data/*-*-*-*-*" }
    }
}