Tests/Unit/MSFT_xADServicePrincipalName.Tests.ps1

$script:dscModuleName = 'xActiveDirectory'
$script:dscResourceName = 'MSFT_xADServicePrincipalName'

#region HEADER

# Unit Test Template Version: 1.2.4
$script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
    (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
{
    & git @('clone', 'https://github.com/PowerShell/DscResource.Tests.git', (Join-Path -Path $script:moduleRoot -ChildPath 'DscResource.Tests'))
}

Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'DSCResource.Tests' -ChildPath 'TestHelper.psm1')) -Force

$TestEnvironment = Initialize-TestEnvironment `
    -DSCModuleName $script:dscModuleName `
    -DSCResourceName $script:dscResourceName `
    -ResourceType 'Mof' `
    -TestType Unit

#endregion HEADER

function Invoke-TestSetup
{
}

function Invoke-TestCleanup
{
    Restore-TestEnvironment -TestEnvironment $TestEnvironment
}

# Begin Testing
try
{
    Invoke-TestSetup

    InModuleScope $script:dscResourceName {
        #region Function Get-TargetResource
        Describe 'xADServicePrincipalName\Get-TargetResource' {
            $testDefaultParameters = @{
                ServicePrincipalName = 'HOST/demo'
            }

            Context 'No SPN set' {

                Mock -CommandName Get-ADObject

                It 'Should return absent' {

                    $result = Get-TargetResource @testDefaultParameters

                    $result.Ensure               | Should -Be 'Absent'
                    $result.ServicePrincipalName | Should -Be 'HOST/demo'
                    $result.Account              | Should -Be ''
                }
            }

            Context 'One SPN set' {

                Mock -CommandName Get-ADObject -MockWith {
                    [PSCustomObject] @{ SamAccountName = 'User' }
                }

                It 'Should return present with the correct account' {

                    $result = Get-TargetResource @testDefaultParameters

                    $result.Ensure               | Should -Be 'Present'
                    $result.ServicePrincipalName | Should -Be 'HOST/demo'
                    $result.Account              | Should -Be 'User'
                }
            }

            Context 'Multiple SPN set' {

                Mock -CommandName Get-ADObject -MockWith {
                    [PSCustomObject] @{ SamAccountName = 'User' },
                    [PSCustomObject] @{ SamAccountName = 'Computer' }
                }

                It 'Should return present with the multiple accounts' {

                    $result = Get-TargetResource @testDefaultParameters

                    $result.Ensure               | Should -Be 'Present'
                    $result.ServicePrincipalName | Should -Be 'HOST/demo'
                    $result.Account              | Should -Be 'User;Computer'
                }
            }
        }
        #endregion

        #region Function Test-TargetResource
        Describe 'xADServicePrincipalName\Test-TargetResource' {
            $testDefaultParameters = @{
                ServicePrincipalName = 'HOST/demo'
                Account              = 'User'
            }

            Context 'No SPN set' {

                Mock -CommandName Get-ADObject

                It 'Should return false for present' {

                    $result = Test-TargetResource -Ensure 'Present' @testDefaultParameters
                    $result | Should -Be $false
                }

                It 'Should return true for absent' {

                    $result = Test-TargetResource -Ensure 'Absent' @testDefaultParameters
                    $result | Should -Be $true
                }
            }

            Context 'Correct SPN set' {

                Mock -CommandName Get-ADObject -MockWith {
                    [PSCustomObject] @{ SamAccountName = 'User' }
                }

                It 'Should return true for present' {

                    $result = Test-TargetResource -Ensure 'Present' @testDefaultParameters
                    $result | Should -Be $true
                }

                It 'Should return false for absent' {

                    $result = Test-TargetResource -Ensure 'Absent' @testDefaultParameters
                    $result | Should -Be $false
                }
            }

            Context 'Wrong SPN set' {

                Mock -CommandName Get-ADObject -MockWith {
                    [PSCustomObject] @{ SamAccountName = 'Computer' }
                }

                It 'Should return false for present' {

                    $result = Test-TargetResource -Ensure 'Present' @testDefaultParameters
                    $result | Should -Be $false
                }

                It 'Should return false for absent' {

                    $result = Test-TargetResource -Ensure 'Absent' @testDefaultParameters
                    $result | Should -Be $false
                }
            }

            Context 'Multiple SPN set' {

                Mock -CommandName Get-ADObject -MockWith {
                    [PSCustomObject] @{ SamAccountName = 'User' },
                    [PSCustomObject] @{ SamAccountName = 'Computer' }
                }

                It 'Should return false for present' {

                    $result = Test-TargetResource -Ensure 'Present' @testDefaultParameters
                    $result | Should -Be $false
                }

                It 'Should return false for absent' {

                    $result = Test-TargetResource -Ensure 'Absent' @testDefaultParameters
                    $result | Should -Be $false
                }
            }

        }
        #endregion

        #region Function Set-TargetResource
        Describe 'xADServicePrincipalName\Set-TargetResource' {
            $testPresentParams = @{
                Ensure               = 'Present'
                ServicePrincipalName = 'HOST/demo'
                Account              = 'User'
            }

            $testAbsentParams = @{
                Ensure               = 'Absent'
                ServicePrincipalName = 'HOST/demo'
            }

            Context 'AD Object not existing' {

                Mock -CommandName Get-ADObject

                It 'Should throw the correct exception' {

                    { Set-TargetResource @testPresentParams } | Should -Throw "AD object with SamAccountName 'User' not found!"
                }
            }

            Context 'No SPN set' {

                Mock -CommandName Get-ADObject -ParameterFilter { $Filter -eq ([ScriptBlock]::Create(' ServicePrincipalName -eq $ServicePrincipalName ')) }
                Mock -CommandName Get-ADObject -MockWith {
                    [PSCustomObject] @{ SamAccountName = 'User' }
                }
                Mock -CommandName Set-ADObject

                It 'Should call the Set-ADObject' {

                    $result = Set-TargetResource @testPresentParams

                    Assert-MockCalled -CommandName Set-ADObject -Scope It -Times 1 -Exactly
                }
            }

            Context 'Wrong SPN set' {

                Mock -CommandName Get-ADObject -ParameterFilter { $Filter -eq ([ScriptBlock]::Create(' ServicePrincipalName -eq $ServicePrincipalName ')) } -MockWith {
                    [PSCustomObject] @{ SamAccountName = 'Computer'; DistinguishedName = 'CN=Computer,OU=Corp,DC=contoso,DC=com' }
                }
                Mock -CommandName Get-ADObject -MockWith {
                    [PSCustomObject] @{ SamAccountName = 'User' }
                }
                Mock -CommandName Set-ADObject -ParameterFilter { $null -ne $Add }
                Mock -CommandName Set-ADObject -ParameterFilter { $null -ne $Remove }

                It 'Should call the Set-ADObject twice' {

                    $result = Set-TargetResource @testPresentParams

                    Assert-MockCalled -CommandName Set-ADObject -Scope It -Times 1 -Exactly -ParameterFilter { $null -ne $Add }
                    Assert-MockCalled -CommandName Set-ADObject -Scope It -Times 1 -Exactly -ParameterFilter { $null -ne $Remove }
                }
            }

            Context 'Remove all SPNs' {

                Mock -CommandName Get-ADObject -ParameterFilter { $Filter -eq ([ScriptBlock]::Create(' ServicePrincipalName -eq $ServicePrincipalName ')) } -MockWith {
                    [PSCustomObject] @{ SamAccountName = 'User'; DistinguishedName = 'CN=User,OU=Corp,DC=contoso,DC=com' }
                }
                Mock -CommandName Set-ADObject

                It 'Should call the Set-ADObject' {

                    $result = Set-TargetResource @testAbsentParams

                    Assert-MockCalled -CommandName Set-ADObject -Scope It -Times 1 -Exactly
                }
            }
        }
        #endregion
    }
}
finally
{
    Invoke-TestCleanup
}