From 4aaf9216718db59ba05b9c01e26292daf80a593e Mon Sep 17 00:00:00 2001 From: Sandro Lanfranchi Date: Fri, 28 Jun 2024 11:36:27 +0200 Subject: [PATCH] Add pester tests --- .../MSFT_EXOEOPProtectionPolicyRule.psm1 | 23 +++-- ...65DSC.EXOEOPProtectionPolicyRule.Tests.ps1 | 9 +- ...osoft365DSC.EXOExternalInOutlook.Tests.ps1 | 73 +++------------- Tests/Unit/Stubs/Microsoft365.psm1 | 83 +++++++++++++++++++ 4 files changed, 112 insertions(+), 76 deletions(-) diff --git a/Modules/Microsoft365DSC/DSCResources/MSFT_EXOEOPProtectionPolicyRule/MSFT_EXOEOPProtectionPolicyRule.psm1 b/Modules/Microsoft365DSC/DSCResources/MSFT_EXOEOPProtectionPolicyRule/MSFT_EXOEOPProtectionPolicyRule.psm1 index a020b850a7..6681eb455d 100644 --- a/Modules/Microsoft365DSC/DSCResources/MSFT_EXOEOPProtectionPolicyRule/MSFT_EXOEOPProtectionPolicyRule.psm1 +++ b/Modules/Microsoft365DSC/DSCResources/MSFT_EXOEOPProtectionPolicyRule/MSFT_EXOEOPProtectionPolicyRule.psm1 @@ -237,9 +237,19 @@ function Set-TargetResource Write-Verbose -Message "Updating {$Identity}" $UpdateParameters = ([Hashtable]$BoundParameters).Clone() - $UpdateParameters.Remove('Verbose') | Out-Null $UpdateParameters.Remove('State') | Out-Null + $keys = $UpdateParameters.Keys + foreach ($key in $keys) + { + if ($null -ne $UpdateParameters.$key -and $UpdateParameters.$key.GetType().Name -like '*cimInstance*') + { + $keyValue = Convert-M365DSCDRGComplexTypeToHashtable -ComplexObject $UpdateParameters.$key + $UpdateParameters.Remove($key) | Out-Null + $UpdateParameters.Add($keyName, $keyValue) + } + } + if($currentInstance.State -ne $State) { if($State -eq 'Enabled') @@ -252,17 +262,6 @@ function Set-TargetResource } } - $keys = $UpdateParameters.Keys - foreach ($key in $keys) - { - if ($null -ne $UpdateParameters.$key -and $UpdateParameters.$key.GetType().Name -like '*cimInstance*') - { - $keyValue = Convert-M365DSCDRGComplexTypeToHashtable -ComplexObject $UpdateParameters.$key - $UpdateParameters.Remove($key) | Out-Null - $UpdateParameters.Add($keyName, $keyValue) - } - } - Set-EOPProtectionPolicyRule @UpdateParameters | Out-Null } elseif ($Ensure -eq 'Absent' -and $currentInstance.Ensure -eq 'Present') diff --git a/Tests/Unit/Microsoft365DSC/Microsoft365DSC.EXOEOPProtectionPolicyRule.Tests.ps1 b/Tests/Unit/Microsoft365DSC/Microsoft365DSC.EXOEOPProtectionPolicyRule.Tests.ps1 index 371a50866e..0c2b3e53a5 100644 --- a/Tests/Unit/Microsoft365DSC/Microsoft365DSC.EXOEOPProtectionPolicyRule.Tests.ps1 +++ b/Tests/Unit/Microsoft365DSC/Microsoft365DSC.EXOEOPProtectionPolicyRule.Tests.ps1 @@ -36,6 +36,11 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture { Mock -CommandName New-M365DSCConnection -MockWith { return "Credentials" } + + Mock -CommandName Set-EOPProtectionPolicyRule -MockWith { + return $null + } + # Mock Write-Host to hide output during the tests Mock -CommandName Write-Host -MockWith { } @@ -136,10 +141,6 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture { State = "Disabled" } } - - Mock -CommandName Set-EOPProtectionPolicyRule -MockWith { - return $null - } } It 'Should return Values from the Get method' { diff --git a/Tests/Unit/Microsoft365DSC/Microsoft365DSC.EXOExternalInOutlook.Tests.ps1 b/Tests/Unit/Microsoft365DSC/Microsoft365DSC.EXOExternalInOutlook.Tests.ps1 index 891cb8e0ae..1904003ab7 100644 --- a/Tests/Unit/Microsoft365DSC/Microsoft365DSC.EXOExternalInOutlook.Tests.ps1 +++ b/Tests/Unit/Microsoft365DSC/Microsoft365DSC.EXOExternalInOutlook.Tests.ps1 @@ -36,12 +36,6 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture { Mock -CommandName Set-ExternalInOutlook -MockWith { } - Mock -CommandName New-ExternalInOutlook -MockWith { - } - - Mock -CommandName Remove-ExternalInOutlook -MockWith { - } - Mock -CommandName New-M365DSCConnection -MockWith { return "Credentials" } @@ -50,69 +44,22 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture { Mock -CommandName Write-Host -MockWith { } $Script:exportedInstances =$null - $Script:ExportMode = $false + $Script:ExportMode = $false } # Test contexts - Context -Name "The EXOExternalInOutlook should exist but it DOES NOT" -Fixture { - BeforeAll { - $testParams = @{ - Ensure = "Present" - Credential = $Credential; - } - - Mock -CommandName Get-ExternalInOutlook -MockWith { - return $null - } - } - It 'Should return Values from the Get method' { - (Get-TargetResource @testParams).Ensure | Should -Be 'Absent' - } - It 'Should return false from the Test method' { - Test-TargetResource @testParams | Should -Be $false - } - It 'Should Create the group from the Set method' { - Set-TargetResource @testParams - Should -Invoke -CommandName New-ExternalInOutlook -Exactly 1 - } - } - - Context -Name "The EXOExternalInOutlook exists but it SHOULD NOT" -Fixture { - BeforeAll { - $testParams = @{ - Ensure = 'Absent' - Credential = $Credential; - } - - Mock -CommandName Get-ExternalInOutlook -MockWith { - return @{ - - } - } - } - It 'Should return Values from the Get method' { - (Get-TargetResource @testParams).Ensure | Should -Be 'Present' - } - - It 'Should return true from the Test method' { - Test-TargetResource @testParams | Should -Be $false - } - - It 'Should Remove the group from the Set method' { - Set-TargetResource @testParams - Should -Invoke -CommandName Remove-ExternalInOutlook -Exactly 1 - } - } Context -Name "The EXOExternalInOutlook Exists and Values are already in the desired state" -Fixture { BeforeAll { $testParams = @{ - Ensure = 'Present' + identity = "ExternalInOutlook" + AllowList = @("test@contoso.com") + Ensure = 'Present' Credential = $Credential; } Mock -CommandName Get-ExternalInOutlook -MockWith { return @{ - + AllowList = @("test@contoso.com") } } } @@ -126,12 +73,16 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture { Context -Name "The EXOExternalInOutlook exists and values are NOT in the desired state" -Fixture { BeforeAll { $testParams = @{ - Ensure = 'Present' + identity = "ExternalInOutlook" + AllowList = @("test@contoso.com","test2@contoso.com") + Ensure = 'Present' Credential = $Credential; } Mock -CommandName Get-ExternalInOutlook -MockWith { return @{ + identity = "ExternalInOutlook" + AllowList = @("test@contoso.com") } } } @@ -154,13 +105,15 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture { BeforeAll { $Global:CurrentModeIsExport = $true $Global:PartialExportFileName = "$(New-Guid).partial.ps1" + $testParams = @{ Credential = $Credential } Mock -CommandName Get-ExternalInOutlook -MockWith { return @{ - + identity = "ExternalInOutlook" + AllowList = @("test@contoso.com","test2@contoso.com") } } } diff --git a/Tests/Unit/Stubs/Microsoft365.psm1 b/Tests/Unit/Stubs/Microsoft365.psm1 index e915d786e8..272d7224d3 100644 --- a/Tests/Unit/Stubs/Microsoft365.psm1 +++ b/Tests/Unit/Stubs/Microsoft365.psm1 @@ -84608,6 +84608,89 @@ function Get-MgBetaDeviceAppManagementMobileAppConfigurationAssignment $CountVariable ) } +function Set-EOPProtectionPolicyRule +{ + [CmdletBinding()] + param + ( + [Parameter()] + [System.String] + $Identity, + + [Parameter()] + [System.String] + $Comments, + + [Parameter()] + [System.String[]] + $ExceptIfRecipientDomainIs, + + [Parameter()] + [System.String[]] + $ExceptIfSentTo, + + [Parameter()] + [System.String[]] + $ExceptIfSentToMemberOf, + + [Parameter()] + [System.String] + $Name, + + [Parameter()] + [System.Int32] + $Priority, + + [Parameter()] + [System.String[]] + $RecipientDomainIs, + + [Parameter()] + [system.String[]] + $SentTo, + + [Parameter()] + [System.String[]] + $SentToMemberOf + ) +} + +function Get-ExternalInOutlook +{ + [CmdletBinding()] + param + ( + [Parameter()] + [System.String] + $Identity, + + [Parameter()] + [System.String[]] + $AllowList, + + [Parameter()] + [System.Boolean] + $Enabled + ) +} +function Set-ExternalInOutlook +{ + [CmdletBinding()] + param + ( + [Parameter()] + [System.String] + $Identity, + + [Parameter()] + [System.String[]] + $AllowList, + + [Parameter()] + [System.Boolean] + $Enabled + ) +} #endregion