Tests/Integration/Arcade.Tests.ps1


$modulePath = Resolve-Path -Path "$PSScriptRoot\..\..\.." | Select-Object -ExpandProperty Path
$moduleName = Resolve-Path -Path "$PSScriptRoot\..\.." | Get-Item | Select-Object -ExpandProperty BaseName

Remove-Module -Name $moduleName -Force -ErrorAction SilentlyContinue
Import-Module -Name "$modulePath\$moduleName" -Force

Describe 'Arcade' {

    Context 'Session' {

        It 'Should conncet to the adb server' {

            # Arrange
            $uri        = 'https://adb-test.arcade.ch'
            $credential = Use-VaultCredential -TargetName $uri

            # Act
            $actual = Adb\Connect-AdbServer -Uri $uri -Credential $credential -PassThru

            # Assert
            $actual.PSObject.TypeNames[0] | Should -Be 'Adb.Session'
            $actual.Type                  | Should -Be 'Credential'
            $actual.Uri                   | Should -Be "$uri/api/v1"
            $actual.Token                 | Should -Not -BeNullOrEmpty
        }

        It 'Should disconnect from the adb server' {

            # Act & Assert
            { Adb\Disconnect-AdbServer } | Should -Not -Throw
        }
    }

    Context 'User' {

        BeforeAll {

            $uri        = 'https://adb-test.arcade.ch'
            $credential = Use-VaultCredential -TargetName $uri

            Adb\Connect-AdbServer -Uri $uri -Credential $credential
        }

        AfterAll {

            Adb\Disconnect-AdbServer
        }

        It 'Should return the correct user' {

            # Arrange
            $expectedUser = $credential.UserName

            # Act
            $actual = Adb\Get-AdbUser

            # Assert
            $actual.User    | Should -Be $expectedUser
            $actual.Role[0] | Should -Be 'admin'
        }
    }

    Context 'Item' {

        BeforeAll {

            $uri        = 'https://adb-test.arcade.ch'
            $credential = Use-VaultCredential -TargetName $uri

            Adb\Connect-AdbServer -Uri $uri -Credential $credential
        }

        AfterAll {

            Adb\Disconnect-AdbServer
        }

        It 'Should return an item' {

            # Arrange
            $expectedName = 'h_vslabdc01.lab.local'

            # Act
            $actual = Adb\Get-AdbItem -Name $expectedName

            # Assert
            $actual.Name | Should -Be $expectedName
        }

        It 'Should create and delete an item piped via pipeline' {

            # Arrange
            $name = 'z_test_{0}' -f [System.Guid]::NewGuid()

            # Act
            New-AdbResource -Type 'Item' -Resource @{ name = $name }
            $actual = Adb\Get-AdbItem -Name $name
            Adb\Get-AdbItem -Name $name | Adb\Remove-AdbResource

            # Assert
            $actual.name                    | Should -Be $name
            { Adb\Get-AdbItem -Name $name } | Should -Throw
        }

        It 'Should create and delete an item specified via paramter' {

            # Arrange
            $name = 'z_test_{0}' -f [System.Guid]::NewGuid()

            # Act
            Adb\New-AdbResource -Type 'Item' -Resource @{ name = $name }
            $actual = Adb\Get-AdbItem -Name $name
            Adb\Remove-AdbResource -Name $name -Type 'Item'

            # Assert
            $actual.name                    | Should -Be $name
            { Adb\Get-AdbItem -Name $name } | Should -Throw
        }

        It 'Should update an item' {

            # Arrange
            $name = 'z_test_{0}' -f [System.Guid]::NewGuid()

            # Act
            Adb\New-AdbResource -Type 'Item' -Resource @{ name = $name }
            $item = Adb\Get-AdbItem -Name $name
            $item.properties | Add-Member 'net_ipaddress' '1.2.3.4'
            $item | Adb\Set-AdbResource
            $actual = Adb\Get-AdbItem -Name $name
            Adb\Remove-AdbResource -Name $name -Type 'Item'

            # Assert
            $actual.name                     | Should -Be $name
            $actual.properties.net_ipaddress | Should -Be '1.2.3.4'
            { Adb\Get-AdbItem -Name $name }  | Should -Throw
        }
    }

    Context 'Validation' {

        BeforeAll {

            $uri        = 'https://adb-test.arcade.ch'
            $credential = Use-VaultCredential -TargetName $uri

            Adb\Connect-AdbServer -Uri $uri -Credential $credential
        }

        AfterAll {

            Adb\Disconnect-AdbServer
        }

        It 'Should validate a valid object and return a result object' {

            # Act
            $actual = Adb\Test-AdbItemValidation -Template 'g_customer' -Name 'g_customer_lab'

            # Assert
            $actual.Result  | Should -BeTrue
            $actual.Message | Should -BeNullOrEmpty
        }

        It 'Should validate a valid object and return nothing' {

            # Act & Assert
            { Adb\Test-AdbItemValidation -Template 'g_customer' -Name 'g_customer_lab' -Quiet } | Should -Not -Throw
        }

        It 'Should return an error for a invalid objectand return a result object' {

            # Act
            $actual = Adb\Test-AdbItemValidation -Template 'g_customer' -Name 'c_automation'

            # Assert
            $actual.Result  | Should -BeFalse
            $actual.Message | Should -Be 'path ".name": should match pattern "^g_customer_.*$", path ".properties": should have required property ''customer'', path ".properties": should have required property ''customer_short'''
        }

        It 'Should return an error for a invalid object' {

            # Act & Assert
            { Adb\Test-AdbItemValidation -Template 'g_customer' -Name 'c_automation' -Quiet } | Should -Throw
        }
    }
}