Tests/Unit/MSFT_xPowerShellExecutionpolicy.test.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
<#
.Synopsis Unit test for PowerShellExecutionPolicy DSC Resource #> $Global:DSCModuleName = 'xPowerShellExecutionPolicy' $Global:DSCResourceName = 'MSFT_xPowerShellExecutionPolicy' #region HEADER if ( (-not (Test-Path -Path '.\DSCResource.Tests\')) -or ` (-not (Test-Path -Path '.\DSCResource.Tests\TestHelper.psm1')) ) { & git @('clone','https://github.com/PowerShell/DscResource.Tests.git') } else { & git @('-C',(Join-Path -Path (Get-Location) -ChildPath '\DSCResource.Tests\'),'pull') } Import-Module .\DSCResource.Tests\TestHelper.psm1 -Force $TestEnvironment = Initialize-TestEnvironment ` -DSCModuleName $Global:DSCModuleName ` -DSCResourceName $Global:DSCResourceName ` -TestType Unit #endregion $Global:invalidPolicyThrowMessage = "Cannot validate argument on parameter 'ExecutionPolicy'. The argument `"badParam`" does " $Global:invalidPolicyThrowMessage += "not belong to the set `"Bypass,Restricted,AllSigned,RemoteSigned,Unrestricted`" " $Global:invalidPolicyThrowMessage += "specified by the ValidateSet attribute. Supply an argument that is in the set and then " $Global:invalidPolicyThrowMessage += "try the command again." # Begin Testing try { #region Pester Tests # The InModuleScope command allows you to perform white-box unit testing on the internal # (non-exported) code of a Script Module. InModuleScope $Global:DSCResourceName { #region Pester Test Initialization #endregion #region Function Get-TargetResource Describe "$($Global:DSCResourceName)\Get-TargetResource" { It 'Throws when passed an invalid execution policy' { { Get-TargetResource -ExecutionPolicy 'badParam' } | should throw $invalidPolicyThrowMessage } It 'Returns correct execution policy' { $result = Get-TargetResource -ExecutionPolicy $(Get-ExecutionPolicy) $result.ExecutionPolicy | should be $(Get-ExecutionPolicy) } } #endregion #region Function Test-TargetResource Describe "$($Global:DSCResourceName)\Test-TargetResource" { It 'Throws when passed an invalid execution policy' { { Test-TargetResource -ExecutionPolicy 'badParam' } | should throw $invalidPolicyThrowMessage } It 'Returns true when current policy matches desired policy' { Test-TargetResource -ExecutionPolicy $(Get-ExecutionPolicy) | should be $True } It 'Returns false when current policy does not match desired policy' { Mock -CommandName Get-ExecutionPolicy -MockWith { "Restricted" } Test-TargetResource -ExecutionPolicy "Bypass" | should be $false } } #endregion #region Function Set-TargetResource Describe "$($Global:DSCResourceName)\Set-TargetResource" { It 'Throws when passed an invalid execution policy' { { Set-TargetResource -ExecutionPolicy 'badParam' } | should throw $invalidPolicyThrowMessage } It 'Set-ExecutionPolicy scope warning exception is caught' { Mock -CommandName Set-ExecutionPolicy -MockWith { Throw 'Windows PowerShell updated your execution policy successfully.' } $result = Set-TargetResource -ExecutionPolicy "Bypass" $result | should be $null } It 'Throws non-caught exceptions'{ Mock -CommandName Set-ExecutionPolicy -MockWith { Throw 'Throw me!' } { Set-TargetResource -ExecutionPolicy "Bypass" } | should throw 'Throw me!' } It 'Sets execution policy' { Mock -CommandName Set-ExecutionPolicy -MockWith { } Set-TargetResource -ExecutionPolicy "Bypass" Assert-MockCalled -CommandName Set-ExecutionPolicy -Exactly 1 -Scope It } } #endregion } #endregion } finally { #region FOOTER Restore-TestEnvironment -TestEnvironment $TestEnvironment #endregion } |