Tests/Unit/TrustedHost/Remove-TrustedHosts.Tests.ps1


BeforeAll {

    $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 'Remove-TrustedHost' -Skip:($PSVersionTable.PSVersion.Major -gt 5 -and -not $IsWindows) {

    Context 'Not Administrator' {

        BeforeAll {

            Mock 'Test-AdministratorRole' -ModuleName $moduleName {
                throw 'Access denied. Please start this functions as an administrator.'
            }
        }

        It 'should throw an exception' {

            # Arrange, Act, Assert
            { Remove-TrustedHost -ComputerName $Env:COMPUTERNAME } | Should -Throw
        }
    }

    Context 'Is Administrator' {

        BeforeAll {

            Mock 'Test-AdministratorRole' -ModuleName $moduleName {
                # Return nothing, so the function will continue
            }

            Mock 'Get-Item' -ModuleName $ModuleName {
                [PSCustomObject] @{ Value = '10.0.0.1,SERVER,10.0.0.2,10.0.0.3,*.contoso.com' }
            }

            Mock 'Set-Item' -ModuleName $ModuleName -Verifiable -ParameterFilter { $Path -eq 'WSMan:\localhost\Client\TrustedHosts' -and $Value -eq '10.0.0.1,10.0.0.2,*.contoso.com' } {
                # Return nothing
            }
        }

        It 'should add two entries via parameter' {

            # Arrange
            $list = 'SERVER', '10.0.0.3'

            # Act
            Remove-TrustedHost -ComputerName $list

            # Assert
            Assert-MockCalled 'Set-Item' -ModuleName $moduleName -Times 1 -Exactly
        }
    }
}