Skip to content

Commit

Permalink
wip: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Floca committed Apr 2, 2024
1 parent 87ff253 commit 2c85c5a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,34 +57,11 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture {
}

# Test contexts
Context -Name 'Role Group should exist. Role Group is missing. Test should fail.' -Fixture {
Context -Name 'Role Group member mismatch, missing member, need to add' -Fixture {
BeforeAll {
$testParams = @{
Name = 'Contoso Role Group'
Members = 'User1'
Description = 'This is the Contoso Role Group'
Ensure = 'Present'
Credential = $Credential
}

}

It 'Should return false from the Test method' {
Test-TargetResource @testParams | Should -Be $false
}

It 'Should call the Set method' {
Set-TargetResource @testParams
Assert-MockCalled -CommandName New-RoleGroup -Exactly 1
}
}

Context -Name 'Role Group should exist. Role Group exists. Test should pass.' -Fixture {
BeforeAll {
$testParams = @{
Name = 'Contoso Role Group'
Members = 'Exchange Administrator'
Roles = 'Address Lists'
Members = 'Group1', 'User1'
Description = 'This is the Contoso Role Group'
Ensure = 'Present'
Credential = $Credential
Expand All @@ -93,90 +70,83 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture {
Mock -CommandName Get-RoleGroup -MockWith {
return @{
Name = 'Contoso Role Group'
Members = 'Exchange Administrator'
Members = 'Group1'
Description = 'This is the Contoso Role Group'
}
}

Mock -Command Get-RoleGroupMember -parameterFilter { $name -eq 'Contoso Role Group'} -MockWith {
[PSCustomObject]@{Displayname = 'Exchange Administrator'}
[PSCustomObject]@{Name = 'Group1'}
}

}

It 'Should return true from the Test method' {
Test-TargetResource @testParams | Should -Be $true
It 'Should return false from the Test method' {
Test-TargetResource @testParams | Should -Be $false
}

It 'Should return Present from the Get Method' {
(Get-TargetResource @testParams).Ensure | Should -Be 'Present'
It 'Should call the Add-RoleGroupMember method' {
Set-TargetResource @testParams
Assert-MockCalled -CommandName Add-RoleGroupMember -Exactly 1
}
}

Context -Name 'Role Group exists, Member missing. Test should fail.' -Fixture {
Context -Name 'Role Group member mismatch, additional members, need to remove one' -Fixture {
BeforeAll {
$testParams = @{
Name = 'Contoso Role Group'
Members = 'User1'
Members = 'Group1', 'User1', 'User3'
Description = 'This is the Contoso Role Group'
Ensure = 'Present'
Credential = $Credential
}

Mock -CommandName Get-RoleGroup -MockWith {
return @{
Name = 'Contoso Role Group'
Members = 'User1'
Members = 'Group1', 'User1', 'User2', 'User3'
Description = 'This is the Contoso Role Group'
}
}

Mock -Command Get-RoleGroupMember -MockWith {
[PSCustomObject]@{Displayname = 'Drift Administrator'}
Mock -Command Get-RoleGroupMember -parameterFilter { $name -eq 'Contoso Role Group'} -MockWith {
[PSCustomObject]@{Name = 'Group1' }, [PSCustomObject]@{Name = 'User1' }, [PSCustomObject]@{Name = 'User2' }, [PSCustomObject]@{Name = 'User3' }
}
}

It 'Should return false from the Test method' {
Test-TargetResource @testParams | Should -Be $false
}

It 'Should return Present from the Get Method' {
(Get-TargetResource @testParams).Ensure | Should -Be 'Present'
}

It 'Should call the Add Members method' {
It 'Should call the Remove-RoleGroupMember method' {
Set-TargetResource @testParams
Assert-MockCalled -CommandName Add-RoleGroupMember -Exactly 1
Assert-MockCalled -CommandName Remove-RoleGroupMember -Exactly 1
}
}
Context -Name 'Role Group exists and it SHOULD NOT.' -Fixture {

Context -Name 'Configuration set as absent, need to remove all members declared in the configuration' -Fixture {
BeforeAll {
$testParams = @{
Name = 'Contoso Role Group'
Members = 'Exchange Administrator'
Members = 'Group1', 'User1', 'User2'
Description = 'This is the Contoso Role Group'
Ensure = 'Absent'
Credential = $Credential
}

Mock -CommandName Get-RoleGroup -MockWith {
return @{
Name = 'Contoso Role Group'
Members = 'Exchange Administrator'
Members = 'Group1', 'User1', 'User2'
Description = 'This is the Contoso Role Group'
}
}
}

It 'Should return true from the Test method' {
It 'Should return false from the Test method' {
Test-TargetResource @testParams | Should -Be $false
}

It 'Should return Present from the Get Method' {
(Get-TargetResource @testParams).Ensure | Should -Be 'Present'
}

It 'Should call the Set method' {
It 'Should call the Remove-RoleGroupMember method' {
Set-TargetResource @testParams
Assert-MockCalled -CommandName Remove-RoleGroup -Exactly 1
Assert-MockCalled -CommandName Remove-RoleGroupMember -Exactly 3
}
}

Expand All @@ -189,22 +159,28 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture {
}

$RoleGroup = @{
Name = 'Contoso Role Group'
Name = 'Contoso Role Group'
Members = 'Group1', 'User1', 'User2'
Description = 'This is the Contoso Role Group'
}

Mock -CommandName Get-RoleGroup -MockWith {
return @{
Name = 'Contoso Role Group'
Members = 'Exchange Administrator'
Description = 'This is the Contoso Role Group'
Name = 'Contoso Role Group'
Members = 'Group1', 'User1', 'User2'
Description = 'This is the Contoso Role Group'
}
}
Mock -Command Get-RoleGroupMember -parameterFilter { $name -eq 'Contoso Role Group'} -MockWith {
[PSCustomObject]@{Name = 'Group1' }, [PSCustomObject]@{Name = 'User1' }, [PSCustomObject]@{Name = 'User2' }
}
}

It 'Should Reverse Engineer resource from the Export method when single' {
$result = Export-TargetResource @testParams
$result | Should -Not -BeNullOrEmpty
}
# Remove the unnecessary closing brace
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions Tests/Unit/Stubs/Microsoft365.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -69236,6 +69236,41 @@ function Update-RoleGroupMember
$Members
)
}
function Add-RoleGroupMember
{
[CmdletBinding()]
param(
[Parameter()]
[System.Management.Automation.SwitchParameter]
$Confirm,

[Parameter()]
[System.Object]
$Identity,

[Parameter()]
[System.Object]
$Member
)
}

function Remove-RoleGroupMember
{
[CmdletBinding()]
param(
[Parameter()]
[System.Management.Automation.SwitchParameter]
$Confirm,

[Parameter()]
[System.Object]
$Identity,

[Parameter()]
[System.Object]
$Member
)
}
#endregion

#region PnP.PowerShell
Expand Down

0 comments on commit 2c85c5a

Please sign in to comment.