Skip to content

Commit

Permalink
Added UTs
Browse files Browse the repository at this point in the history
  • Loading branch information
kasaxena5 committed Nov 6, 2024
1 parent 767951d commit cff7025
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function Get-TargetResource
[System.Boolean]
$Enabled,

[Parameter(Mandatory = $true)]
[Parameter()]
[System.String]
$Name,

Expand Down Expand Up @@ -84,11 +84,10 @@ function Get-TargetResource
}

$results = @{
Identity = [System.String]$instance.Identity
Identity = $Identity
Description = [System.String]$instance.Description
Enabled = [System.Boolean]$instance.Enabled
Name = [System.String]$instance.Name
Refresh = [System.Boolean]$instance.Refresh
AzureKeyIDs = [System.String[]]$instance.AzureKeyIDs
Ensure = 'Present'
Credential = $Credential
Expand Down Expand Up @@ -129,7 +128,7 @@ function Set-TargetResource
[System.Boolean]
$Enabled,

[Parameter(Mandatory = $true)]
[Parameter()]
[System.String]
$Name,

Expand Down Expand Up @@ -187,19 +186,18 @@ function Set-TargetResource
if ($Ensure -eq 'Present' -and $currentInstance.Ensure -eq 'Absent')
{
$setParameters.Remove('Identity')
$setParameters.Remove('Refresh')
New-M365DataAtRestEncryptionPolicy @SetParameters
}
# UPDATE
elseif ($Ensure -eq 'Present' -and $currentInstance.Ensure -eq 'Present')
{
$setParameters.Remove('AzureKeyIDs')
$setParameters.Remove('Name')
Set-M365DataAtRestEncryptionPolicy @SetParameters
}
# REMOVE
elseif ($Ensure -eq 'Absent' -and $currentInstance.Ensure -eq 'Present')
{
Remove-M365DataAtRestEncryptionPolicy -Identity $Identity
Write-Warning "Removal of M365DataAtRestEncryptionPolicy is not supported."
}
}

Expand All @@ -221,7 +219,7 @@ function Test-TargetResource
[System.Boolean]
$Enabled,

[Parameter(Mandatory = $true)]
[Parameter()]
[System.String]
$Name,

Expand Down Expand Up @@ -357,7 +355,6 @@ function Export-TargetResource
Write-Host " |---[$i/$($Script:exportedInstances.Count)] $displayedKey" -NoNewline
$params = @{
Identity = $config.Identity
Name = $config.Name
Credential = $Credential
ApplicationId = $ApplicationId
TenantId = $TenantId
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,30 @@
{}
{
"resourceName": "M365DataAtRestEncryptionPolicy",
"description": "Microsoft 365 data-at-rest encryption policy for multi-workload usage.",
"roles": {
"read": [
"Global Reader"
],
"update": [
"Exchange Administrator"
]
},
"permissions": {
"graph": {
"delegated": {
"read": [],
"update": []
},
"application": {
"read": [],
"update": []
}
},
"exchange": {
"requiredroles": [
"Compliance Admin"
],
"requiredrolegroups": "Organization Management"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture {
return "Credentials"
}

##TODO - Mock any Remove/Set/New cmdlets
Mock -CommandName Set-M365DataAtRestEncryptionPolicy -MockWith {
return $null
}

Mock -CommandName New-M365DataAtRestEncryptionPolicy -MockWith {
return $null
}

# Mock Write-Host to hide output during the tests
Mock -CommandName Write-Host -MockWith {
Expand All @@ -47,13 +53,16 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture {
Context -Name "The instance should exist but it DOES NOT" -Fixture {
BeforeAll {
$testParams = @{
##TODO - Add Parameters
Identity = 'FakeStringValue'
Name = 'FakeStringValue'
Description = 'FakeStringValue'
Enabled = $true
AzureKeyIDs = @('FakeStringValue1', 'FakeStringValue2')
Ensure = 'Present'
Credential = $Credential;
}

##TODO - Mock the Get-Cmdlet to return $null
Mock -CommandName Get-Cmdlet -MockWith {
Mock -CommandName Get-M365DataAtRestEncryptionPolicy -MockWith {
return $null
}
}
Expand All @@ -65,24 +74,30 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture {
}

It 'Should create a new instance from the Set method' {
##TODO - Replace the New-Cmdlet by the appropriate one
Set-TargetResource @testParams
Should -Invoke -CommandName New-Cmdlet -Exactly 1
Should -Invoke -CommandName New-M365DataAtRestEncryptionPolicy -Exactly 1
}
}

Context -Name "The instance exists but it SHOULD NOT" -Fixture {
BeforeAll {
$testParams = @{
##TODO - Add Parameters
Identity = 'FakeStringValue'
Name = 'FakeStringValue'
Description = 'FakeStringValue'
Enabled = $true
AzureKeyIDs = @('FakeStringValue1', 'FakeStringValue2')
Ensure = 'Absent'
Credential = $Credential;
}

##TODO - Mock the Get-Cmdlet to return an instance
Mock -CommandName Get-Cmdlet -MockWith {
Mock -CommandName Get-M365DataAtRestEncryptionPolicy -MockWith {
return @{

Identity = 'FakeStringValue'
Name = 'FakeStringValue'
Description = 'FakeStringValue'
Enabled = $true
AzureKeyIDs = @('FakeStringValue1', 'FakeStringValue2')
}
}
}
Expand All @@ -95,23 +110,28 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture {

It 'Should remove the instance from the Set method' {
Set-TargetResource @testParams
##TODO - Replace the Remove-Cmdlet by the appropriate one
Should -Invoke -CommandName Remove-Cmdlet -Exactly 1
}
}

Context -Name "The instance exists and values are already in the desired state" -Fixture {
BeforeAll {
$testParams = @{
##TODO - Add Parameters
Identity = 'FakeStringValue'
Name = 'FakeStringValue'
Description = 'FakeStringValue'
Enabled = $true
AzureKeyIDs = @('FakeStringValue1', 'FakeStringValue2')
Ensure = 'Present'
Credential = $Credential;
}

##TODO - Mock the Get-Cmdlet to return the desired values
Mock -CommandName Get-Cmdlet -MockWith {
Mock -CommandName Get-M365DataAtRestEncryptionPolicy -MockWith {
return @{

Identity = 'FakeStringValue'
Name = 'FakeStringValue'
Description = 'FakeStringValue'
Enabled = $true
AzureKeyIDs = @('FakeStringValue1', 'FakeStringValue2')
}
}
}
Expand All @@ -124,15 +144,22 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture {
Context -Name "The instance exists and values are NOT in the desired state" -Fixture {
BeforeAll {
$testParams = @{
##TODO - Add Parameters
Identity = 'FakeStringValue'
Name = 'FakeStringValue'
Description = 'FakeStringValue'
Enabled = $true
AzureKeyIDs = @('FakeStringValue1', 'FakeStringValue2')
Ensure = 'Present'
Credential = $Credential;
}

##TODO - Mock the Get-Cmdlet to return a drift
Mock -CommandName Get-Cmdlet -MockWith {
Mock -CommandName Get-M365DataAtRestEncryptionPolicy -MockWith {
return @{

Identity = 'FakeStringValue'
Name = 'FakeStringValue'
Description = 'FakeStringValue2' #drift
Enabled = $true
AzureKeyIDs = @('FakeStringValue1', 'FakeStringValue2')
}
}
}
Expand All @@ -147,8 +174,7 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture {

It 'Should call the Set method' {
Set-TargetResource @testParams
##TODO - Replace the Update-Cmdlet by the appropriate one
Should -Invoke -CommandName Update-Cmdlet -Exactly 1
Should -Invoke -CommandName Set-M365DataAtRestEncryptionPolicy -Exactly 1
}
}

Expand All @@ -160,10 +186,13 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture {
Credential = $Credential;
}

##TODO - Mock the Get-Cmdlet to return an instance
Mock -CommandName Get-Cmdlet -MockWith {
Mock -CommandName Get-M365DataAtRestEncryptionPolicy -MockWith {
return @{

Identity = 'FakeStringValue'
Name = 'FakeStringValue'
Description = 'FakeStringValue2' #drift
Enabled = $true
AzureKeyIDs = @('FakeStringValue1', 'FakeStringValue2')
}
}
}
Expand Down
52 changes: 52 additions & 0 deletions Tests/Unit/Stubs/Microsoft365.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,58 @@ function Invoke-AzRest
}
#endregion

#region M365DataAtRestEncryptionPolicy
function Get-M365DataAtRestEncryptionPolicy
{
[CmdletBinding()]
param(
[Parameter()]
[System.String]
$Identity
)
}

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

[Parameter()]
[System.String]
$Description,

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

function New-M365DataAtRestEncryptionPolicy
{
[CmdletBinding()]
param(
[Parameter()]
[System.String]
$Description,

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

[Parameter()]
[System.String]
$Name,

[Parameter()]
[System.String[]]
$AzureKeyIDs
)
}
#endregion

function Get-MgBetaPolicyDeviceRegistrationPolicy
{
[CmdletBinding()]
Expand Down

0 comments on commit cff7025

Please sign in to comment.