Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrola committed Jun 27, 2024
1 parent 169c256 commit dbf9a3d
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ function Set-TargetResource
if ($Ensure -eq 'Present' -and $currentInstance.Ensure -eq 'Absent')
{
#following Microsoft recommendation, we will not create new EOPProtectionPolicyRule, instead we will enable the rule if not already done
Write-Verbose -Message "We not create new EOPProtectionPolicyRule, we try to enable the rule if not already done"
Enable-EOPProtectionPolicyRule
Write-Verbose -Message "We not create new EOPProtectionPolicyRule if it is not present"
}
elseif ($Ensure -eq 'Present' -and $currentInstance.Ensure -eq 'Present')
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function Get-TargetResource
[System.String]
$Identity,

[Parameter]
[Parameter()]
[System.Boolean]
$Enabled,

Expand Down Expand Up @@ -70,7 +70,7 @@ function Get-TargetResource
Write-Verbose -Message "Found the settings for ExternalInOutlook: $($instance.Identity)"
$results = @{
Identity = $instance.Identity
Enabled = [String]$instance.Enabled
Enabled = $instance.Enabled
AllowList = $instance.AllowList
Ensure = 'Present'
Credential = $Credential
Expand Down Expand Up @@ -102,7 +102,7 @@ function Set-TargetResource
[System.String]
$Identity,

[Parameter]
[Parameter()]
[System.Boolean]
$Enabled,

Expand Down Expand Up @@ -200,7 +200,7 @@ function Test-TargetResource
[System.String]
$Identity,

[Parameter]
[Parameter()]
[System.Boolean]
$Enabled,

Expand Down Expand Up @@ -238,30 +238,47 @@ function Test-TargetResource
Confirm-M365DSCDependencies

#region Telemetry
$ResourceName = $MyInvocation.MyCommand.ModuleName -replace 'MSFT_', ''
$ResourceName = $MyInvocation.MyCommand.ModuleName.Replace('MSFT_', '')
$CommandName = $MyInvocation.MyCommand
$data = Format-M365DSCTelemetryParameters -ResourceName $ResourceName `
-CommandName $CommandName `
-Parameters $PSBoundParameters
Add-M365DSCTelemetryEvent -Data $data
#endregion
Write-Verbose -Message "Testing ExternalInOutlook {$Identity}"

Write-Verbose -Message "Testing configuration of {$Identity}"

$CurrentValues = Get-TargetResource @PSBoundParameters
$ValuesToCheck = ([Hashtable]$PSBoundParameters).Clone()
$ValuesToCheck.Remove('Identity') | Out-Null

if ($CurrentValues.Ensure -eq 'Absent')
{
Write-Verbose -Message "Test-TargetResource returned $false"
return $false
}

Write-Verbose -Message "Current Values: $(Convert-M365DscHashtableToString -Hashtable $CurrentValues)"
Write-Verbose -Message "Target Values: $(Convert-M365DscHashtableToString -Hashtable $PSBoundParameters)"
Write-Verbose -Message "Target Values: $(Convert-M365DscHashtableToString -Hashtable $ValuesToCheck)"

$ValuesToCheck = $PSBoundParameters
#Convert any DateTime to String
foreach ($key in $ValuesToCheck.Keys)
{
if (($null -ne $CurrentValues[$key]) `
-and ($CurrentValues[$key].GetType().Name -eq 'DateTime'))
{
$CurrentValues[$key] = $CurrentValues[$key].toString()
}
}

$TestResult = Test-M365DSCParameterState -CurrentValues $CurrentValues `
$testResult = Test-M365DSCParameterState -CurrentValues $CurrentValues `
-Source $($MyInvocation.MyCommand.Source) `
-DesiredValues $PSBoundParameters `
-ValuesToCheck $ValuesToCheck.Keys

Write-Verbose -Message "Test-TargetResource returned $TestResult"
Write-Verbose -Message "Test-TargetResource returned $testResult"

return $TestResult
return $testResult
}

function Export-TargetResource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ Configuration Example
)
Import-DscResource -ModuleName Microsoft365DSC

$Domain = $Credscredential.Username.Split('@')[1]
node localhost
{
EXOExternalInOutlook "EXOExternalInOutlook"
{
Identity = "ExternalInOutlook";
AllowList = @("[email protected]","*contoso.onmicrosoft.com","contoso.com");
Enabled = $False;
Ensure = "Present";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,71 +33,42 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture {
Mock -CommandName Remove-PSSession -MockWith {
}

Mock -CommandName Set-EOPProtectionPolicyRule -MockWith {
}

Mock -CommandName New-EOPProtectionPolicyRule -MockWith {
}

Mock -CommandName Remove-EOPProtectionPolicyRule -MockWith {
}

Mock -CommandName New-M365DSCConnection -MockWith {
return "Credentials"
}

# Mock Write-Host to hide output during the tests
Mock -CommandName Write-Host -MockWith {
}
$Script:exportedInstances =$null
$Script:ExportMode = $false<AssignmentMock>
$Script:ExportMode = $false
}
# Test contexts
Context -Name "The EXOEOPProtectionPolicyRule should exist but it DOES NOT" -Fixture {
BeforeAll {
$testParams = @{
<TargetResourceFakeValues> Ensure = "Present"
Credential = $Credential;
}

Mock -CommandName Get-EOPProtectionPolicyRule -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-EOPProtectionPolicyRule -Exactly 1
}
}

Context -Name "The EXOEOPProtectionPolicyRule exists but it SHOULD NOT" -Fixture {
BeforeAll {
$testParams = @{
<TargetResourceFakeValues> Ensure = 'Absent'
Credential = $Credential;
Ensure = 'Absent'
Credential = $Credential;
ExceptIfRecipientDomainIs = @("contoso.onmicrosoft.com");
Identity = "Strict Preset Security Policy";
Name = "Strict Preset Security Policy";
Priority = 0;
State = "Disabled";
}

Mock -CommandName Get-EOPProtectionPolicyRule -MockWith {
return @{
Identity =
SentTo =
SentToMemberOf =
ExceptIfSentTo =
RecipientDomainIs =
Name = "FakeStringValue"
ExceptIfRecipientDomainIs =
Identity = "Strict Preset Security Policy"
Name = "Strict Preset Security Policy"
ExceptIfRecipientDomainIs = @("contoso.onmicrosoft.com")
Comments = "FakeStringValue"
ExceptIfSentToMemberOf =
Priority = 3

Priority = 0
}
}

Mock -CommandName Disable-EOPProtectionPolicyRule -MockWith {
return $null
}
}

It 'Should return Values from the Get method' {
Expand All @@ -110,29 +81,29 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture {

It 'Should Remove the group from the Set method' {
Set-TargetResource @testParams
Should -Invoke -CommandName Remove-EOPProtectionPolicyRule -Exactly 1
Should -Invoke -CommandName Disable-EOPProtectionPolicyRule -Exactly 1
}
}
Context -Name "The EXOEOPProtectionPolicyRule Exists and Values are already in the desired state" -Fixture {
BeforeAll {
$testParams = @{
<TargetResourceFakeValues> Ensure = 'Present'
Credential = $Credential;
Ensure = 'Present'
Credential = $Credential;
ExceptIfRecipientDomainIs = @("contoso.onmicrosoft.com");
Identity = "Strict Preset Security Policy";
Name = "Strict Preset Security Policy";
Priority = 0;
State = "Disabled";
}

Mock -CommandName Get-EOPProtectionPolicyRule -MockWith {
return @{
Identity =
SentTo =
SentToMemberOf =
ExceptIfSentTo =
RecipientDomainIs =
Name = "FakeStringValue"
ExceptIfRecipientDomainIs =
Identity = "Strict Preset Security Policy"
Name = "Strict Preset Security Policy"
ExceptIfRecipientDomainIs = @("contoso.onmicrosoft.com")
Comments = "FakeStringValue"
ExceptIfSentToMemberOf =
Priority = 3

Priority = 0
State = "Disabled";
}
}
}
Expand All @@ -146,24 +117,29 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture {
Context -Name "The EXOEOPProtectionPolicyRule exists and values are NOT in the desired state" -Fixture {
BeforeAll {
$testParams = @{
<TargetResourceFakeValues> Ensure = 'Present'
Credential = $Credential;
Ensure = 'Present'
Credential = $Credential;
ExceptIfRecipientDomainIs = @("contoso1.onmicrosoft.com");
Identity = "Strict Preset Security Policy 2";
Name = "Strict Preset Security Policy";
Priority = 0;
State = "Disabled";
}

Mock -CommandName Get-EOPProtectionPolicyRule -MockWith {
return @{
Identity =
SentTo =
SentToMemberOf =
ExceptIfSentTo =
RecipientDomainIs =
Name = "FakeStringValueDrift #Drift"
ExceptIfRecipientDomainIs =
Comments = "FakeStringValueDrift #Drift"
ExceptIfSentToMemberOf =
Priority = 2
Identity = "Strict Preset Security Policy"
Name = "Strict Preset Security Policy"
ExceptIfRecipientDomainIs = @("contoso.onmicrosoft.com")
Comments = "FakeStringValue"
Priority = 0
State = "Disabled"
}
}

Mock -CommandName Set-EOPProtectionPolicyRule -MockWith {
return $null
}
}

It 'Should return Values from the Get method' {
Expand All @@ -190,18 +166,12 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture {

Mock -CommandName Get-EOPProtectionPolicyRule -MockWith {
return @{
Identity =
SentTo =
SentToMemberOf =
ExceptIfSentTo =
RecipientDomainIs =
Name = "FakeStringValue"
ExceptIfRecipientDomainIs =
Comments = "FakeStringValue"
ExceptIfSentToMemberOf =
Priority = 3

}
Identity = "Strict Preset Security Policy"
Name = "Strict Preset Security Policy"
ExceptIfRecipientDomainIs = @("contoso.onmicrosoft.com")
Comments = "FakeStringValue"
Priority = 0
}
}
}
It 'Should Reverse Engineer resource from the Export method' {
Expand Down
48 changes: 48 additions & 0 deletions Tests/Unit/Stubs/Generic.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1226,3 +1226,51 @@ function Update-MgPlannerBucket
$BucketId
)
}

function Get-EOPProtectionPolicyRule
{
[CmdletBinding()]
Param(
[Parameter()]
[System.String]
$Identity,

[Parameter()]
[System.String]
$State
)
}

function Set-EOPProtectionPolicyRule
{
[CmdletBinding()]
Param(
[Parameter()]
[System.String]
$Identity,

[Parameter()]
[System.String]
$State
)
}

function Enable-EOPProtectionPolicyRule
{
[CmdletBinding()]
Param(
[Parameter()]
[System.String]
$Identity
)
}

function Disable-EOPProtectionPolicyRule
{
[CmdletBinding()]
Param(
[Parameter()]
[System.String]
$Identity
)
}

0 comments on commit dbf9a3d

Please sign in to comment.