Skip to content

Commit

Permalink
Add pester tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrola committed Jun 28, 2024
1 parent dbf9a3d commit 4aaf921
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}
Expand Down Expand Up @@ -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' {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -50,69 +44,22 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture {
Mock -CommandName Write-Host -MockWith {
}
$Script:exportedInstances =$null
$Script:ExportMode = $false<AssignmentMock>
$Script:ExportMode = $false
}
# Test contexts
Context -Name "The EXOExternalInOutlook should exist but it DOES NOT" -Fixture {
BeforeAll {
$testParams = @{
<TargetResourceFakeValues> 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 = @{
<TargetResourceFakeValues> 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 = @{
<TargetResourceFakeValues> Ensure = 'Present'
identity = "ExternalInOutlook"
AllowList = @("[email protected]")
Ensure = 'Present'
Credential = $Credential;
}

Mock -CommandName Get-ExternalInOutlook -MockWith {
return @{

AllowList = @("[email protected]")
}
}
}
Expand All @@ -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 = @{
<TargetResourceFakeValues> Ensure = 'Present'
identity = "ExternalInOutlook"
AllowList = @("[email protected]","[email protected]")
Ensure = 'Present'
Credential = $Credential;
}

Mock -CommandName Get-ExternalInOutlook -MockWith {
return @{
identity = "ExternalInOutlook"
AllowList = @("[email protected]")
}
}
}
Expand All @@ -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 = @("[email protected]","[email protected]")
}
}
}
Expand Down
83 changes: 83 additions & 0 deletions Tests/Unit/Stubs/Microsoft365.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4aaf921

Please sign in to comment.