Skip to content

Commit

Permalink
Migrate tests to Pester syntax v4.x (issue dsccommunity#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
johlju committed Jun 15, 2019
1 parent bbf0579 commit b44554b
Show file tree
Hide file tree
Showing 14 changed files with 242 additions and 241 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- Deleted the obsolete xActiveDirectory_TechNetDocumentation.html file
- Cleanup of code
- Removed semicolon throughout where it is not needed.
- Migrate tests to Pester syntax v4.x ([issue #322](https://github.com/PowerShell/xActiveDirectory/issues/322)).
- Changes to xADComputer
- Refactored the resource and the unit tests.
- BREAKING CHANGE: The `Enabled` property is **DEPRECATED** and is no
Expand Down
24 changes: 12 additions & 12 deletions Tests/Unit/MSFT_xADDomain.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ try

$result = Get-TargetResource @testDefaultParams -DomainName $correctDomainName

$result -is [System.Collections.Hashtable] | Should Be $true
$result -is [System.Collections.Hashtable] | Should -Be $true
}

It 'Calls "Get-ADDomain" without credentials if domain member' {
Expand Down Expand Up @@ -133,23 +133,23 @@ try
}

## Match operator is case-sensitive!
{ Get-TargetResource @testDefaultParams -DomainName $incorrectDomainName } | Should Throw 'invalid credentials'
{ Get-TargetResource @testDefaultParams -DomainName $incorrectDomainName } | Should -Throw 'invalid credentials'
}

It 'Throws "Computer is already a domain member" when is already a domain member' {
Mock -CommandName Get-ADDomain -ParameterFilter { $Identity.ToString() -eq $incorrectDomainName } -MockWith {
Write-Error -Exception (New-Object Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException)
}

{ Get-TargetResource @testDefaultParams -DomainName $incorrectDomainName } | Should Throw 'Computer is already a domain member'
{ Get-TargetResource @testDefaultParams -DomainName $incorrectDomainName } | Should -Throw 'Computer is already a domain member'
}

It 'Does not throw when domain cannot be located' {
Mock -CommandName Get-ADDomain -ParameterFilter { $Identity.ToString() -eq $missingDomainName } -MockWith {
Write-Error -Exception (New-Object Microsoft.ActiveDirectory.Management.ADServerDownException)
}

{ Get-TargetResource @testDefaultParams -DomainName $missingDomainName } | Should Not Throw
{ Get-TargetResource @testDefaultParams -DomainName $missingDomainName } | Should -Not -Throw
}

It 'Returns the correct domain mode' {
Expand All @@ -161,7 +161,7 @@ try
}
Mock -CommandName Get-ADForest -MockWith { [psobject]@{ForestMode = $mgmtForestMode} }

(Get-TargetResource @testDefaultParams -DomainName $correctDomainName).DomainMode | Should Be $domainMode
(Get-TargetResource @testDefaultParams -DomainName $correctDomainName).DomainMode | Should -Be $domainMode
}

It 'Returns the correct forest mode' {
Expand All @@ -173,7 +173,7 @@ try
}
Mock -CommandName Get-ADForest -MockWith { [psobject]@{ForestMode = $mgmtForestMode} }

(Get-TargetResource @testDefaultParams -DomainName $correctDomainName).ForestMode | Should Be $forestMode
(Get-TargetResource @testDefaultParams -DomainName $correctDomainName).ForestMode | Should -Be $forestMode
}
}
#endregion
Expand Down Expand Up @@ -209,47 +209,47 @@ try

$result = Test-TargetResource @testDefaultParams -DomainName $correctDomainName

$result | Should Be $true
$result | Should -Be $true
}

It 'Returns "False" when "DomainName" does not match' {
Mock -CommandName Get-TargetResource -MockWith { return $stubDomain; }

$result = Test-TargetResource @testDefaultParams -DomainName $incorrectDomainName

$result | Should Be $false
$result | Should -Be $false
}

It 'Returns "True" when "DomainNetBIOSName" matches' {
Mock -CommandName Get-TargetResource -MockWith { return $stubDomain; }

$result = Test-TargetResource @testDefaultParams -DomainName $correctDomainName -DomainNetBIOSName $correctDomainNetBIOSName

$result | Should Be $true
$result | Should -Be $true
}

It 'Returns "False" when "DomainNetBIOSName" does not match' {
Mock -CommandName Get-TargetResource -MockWith { return $stubDomain; }

$result = Test-TargetResource @testDefaultParams -DomainName $correctDomainName -DomainNetBIOSName 'INCORRECT'

$result | Should Be $false
$result | Should -Be $false
}

It 'Returns "True" when "ParentDomainName" matches' {
Mock -CommandName Get-TargetResource -MockWith { return $stubChildDomain; }

$result = Test-TargetResource @testDefaultParams -DomainName $correctChildDomainName -ParentDomainName $parentDomainName

$result | Should Be $true
$result | Should -Be $true
}

It 'Returns "False" when "ParentDomainName" does not match' {
Mock -CommandName Get-TargetResource -MockWith { return $stubChildDomain; }

$result = Test-TargetResource @testDefaultParams -DomainName $correctChildDomainName -ParentDomainName 'incorrect.com'

$result | Should Be $false
$result | Should -Be $false
}

}
Expand Down
8 changes: 4 additions & 4 deletions Tests/Unit/MSFT_xADDomainDefaultPasswordPolicy.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ try

$result = Get-TargetResource @testDefaultParams

$result -is [System.Collections.Hashtable] | Should Be $true
$result -is [System.Collections.Hashtable] | Should -Be $true
}

It 'Calls "Get-ADDefaultDomainPasswordPolicy" without credentials by default' {
Expand Down Expand Up @@ -141,7 +141,7 @@ try

$result = Test-TargetResource @testDefaultParams

$result -is [System.Boolean] | Should Be $true
$result -is [System.Boolean] | Should -Be $true
}

It 'Calls "Get-TargetResource" with "Credential" parameter when specified' {
Expand Down Expand Up @@ -169,7 +169,7 @@ try

$result = Test-TargetResource @propertyDefaultParams

$result | Should Be $true
$result | Should -Be $true
}

It "Fails when '$propertyName' parameter does not match resource property value" {
Expand All @@ -191,7 +191,7 @@ try

$result = Test-TargetResource @propertyDefaultParams

$result | Should Be $false
$result | Should -Be $false
}
} #end foreach property

Expand Down
44 changes: 22 additions & 22 deletions Tests/Unit/MSFT_xADGroup.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ try
Mock -CommandName Get-ADGroup { return $fakeADGroup; }
Mock -CommandName Get-ADGroupMember { return @($fakeADUser1, $fakeADUser2); }

(Get-TargetResource @testPresentParams).Ensure | Should Be 'Present'
(Get-TargetResource @testPresentParams).Ensure | Should -Be 'Present'
}

It "Returns 'Ensure' is 'Absent' when group does not exist" {
Mock -CommandName Get-ADGroup { throw New-Object Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException }

(Get-TargetResource @testPresentParams).Ensure | Should Be 'Absent'
(Get-TargetResource @testPresentParams).Ensure | Should -Be 'Absent'
}


Expand Down Expand Up @@ -174,7 +174,7 @@ try

$targetResource = Test-TargetResource @testPresentParams -Members $fakeADUser1.$attribute, $fakeADUser2.$attribute -MembershipAttribute $attribute

$targetResource | Should Be $true
$targetResource | Should -Be $true
}

It "Fails when group membership counts do not match using '$attribute'" {
Expand All @@ -183,7 +183,7 @@ try

$targetResource = Test-TargetResource @testPresentParams -Members $fakeADUser2.$attribute, $fakeADUser3.$attribute -MembershipAttribute $attribute

$targetResource | Should Be $false
$targetResource | Should -Be $false
}

It "Fails when group 'Members' do not match using '$attribute'" {
Expand All @@ -192,7 +192,7 @@ try

$targetResource = Test-TargetResource @testPresentParams -Members $fakeADUser2.$attribute, $fakeADUser3.$attribute -MembershipAttribute $attribute

$targetResource | Should Be $false
$targetResource | Should -Be $false
}

It "Passes when specified 'MembersToInclude' match using '$attribute'" {
Expand All @@ -201,7 +201,7 @@ try

$targetResource = Test-TargetResource @testPresentParams -MembersToInclude $fakeADUser2.$attribute -MembershipAttribute $attribute

$targetResource | Should Be $true
$targetResource | Should -Be $true
}

It "Fails when specified 'MembersToInclude' are missing using '$attribute'" {
Expand All @@ -210,7 +210,7 @@ try

$targetResource = Test-TargetResource @testPresentParams -MembersToInclude $fakeADUser3.$attribute -MembershipAttribute $attribute

$targetResource | Should Be $false
$targetResource | Should -Be $false
}

It "Passes when specified 'MembersToExclude' are missing using '$attribute'" {
Expand All @@ -219,7 +219,7 @@ try

$targetResource = Test-TargetResource @testPresentParams -MembersToExclude $fakeADUser3.$attribute -MembershipAttribute $attribute

$targetResource | Should Be $true
$targetResource | Should -Be $true
}

It "Fails when when specified 'MembersToExclude' match using '$attribute'" {
Expand All @@ -228,15 +228,15 @@ try

$targetResource = Test-TargetResource @testPresentParams -MembersToExclude $fakeADUser2.$attribute -MembershipAttribute $attribute

$targetResource | Should Be $false
$targetResource | Should -Be $false
}

} #end foreach attribute

It "Fails when group does not exist and 'Ensure' is 'Present'" {
Mock -CommandName Get-TargetResource -MockWith { return $testAbsentParams }

Test-TargetResource @testPresentParams | Should Be $false
Test-TargetResource @testPresentParams | Should -Be $false
}

It "Fails when group exists, 'Ensure' is 'Present' but 'Scope' is wrong" {
Expand All @@ -246,7 +246,7 @@ try
return $duffADGroup
}

Test-TargetResource @testPresentParams | Should Be $false
Test-TargetResource @testPresentParams | Should -Be $false
}

It "Fails when group exists, 'Ensure' is 'Present' but 'Category' is wrong" {
Expand All @@ -256,7 +256,7 @@ try
return $duffADGroup
}

Test-TargetResource @testPresentParams | Should Be $false
Test-TargetResource @testPresentParams | Should -Be $false
}

It "Fails when group exists, 'Ensure' is 'Present' but 'Path' is wrong" {
Expand All @@ -266,7 +266,7 @@ try
return $duffADGroup
}

Test-TargetResource @testPresentParams | Should Be $false
Test-TargetResource @testPresentParams | Should -Be $false
}

It "Fails when group exists, 'Ensure' is 'Present' but 'Description' is wrong" {
Expand All @@ -276,7 +276,7 @@ try
return $duffADGroup
}

Test-TargetResource @testPresentParams | Should Be $false
Test-TargetResource @testPresentParams | Should -Be $false
}

It "Fails when group exists, 'Ensure' is 'Present' but 'DisplayName' is wrong" {
Expand All @@ -286,7 +286,7 @@ try
return $duffADGroup
}

Test-TargetResource @testPresentParams | Should Be $false
Test-TargetResource @testPresentParams | Should -Be $false
}

It "Fails when group exists, 'Ensure' is 'Present' but 'ManagedBy' is wrong" {
Expand All @@ -296,7 +296,7 @@ try
return $duffADGroup
}

Test-TargetResource @testPresentParams | Should Be $false
Test-TargetResource @testPresentParams | Should -Be $false
}

It "Fails when group exists, 'Ensure' is 'Present' but 'Notes' is wrong" {
Expand All @@ -306,25 +306,25 @@ try
return $duffADGroup
}

Test-TargetResource @testPresentParams | Should Be $false
Test-TargetResource @testPresentParams | Should -Be $false
}

It "Fails when group exists and 'Ensure' is 'Absent'" {
Mock -CommandName Get-TargetResource -MockWith { return $testPresentParams }

Test-TargetResource @testAbsentParams | Should Be $false
Test-TargetResource @testAbsentParams | Should -Be $false
}

It "Passes when group exists, target matches and 'Ensure' is 'Present'" {
Mock -CommandName Get-TargetResource -MockWith { return $testPresentParams }

Test-TargetResource @testPresentParams | Should Be $true
Test-TargetResource @testPresentParams | Should -Be $true
}

It "Passes when group does not exist and 'Ensure' is 'Absent'" {
Mock -CommandName Get-TargetResource -MockWith { return $testAbsentParams }

Test-TargetResource @testAbsentParams | Should Be $true
Test-TargetResource @testAbsentParams | Should -Be $true
}

}
Expand Down Expand Up @@ -629,7 +629,7 @@ try
Mock -CommandName Add-ADCommonGroupMember

$universalGroupInCompliance = Test-TargetResource -GroupName $testUniversalPresentParams.GroupName -DisplayName $testUniversalPresentParams.DisplayName
$universalGroupInCompliance | Should Be $true
$universalGroupInCompliance | Should -Be $true
}

# tests for issue 183
Expand All @@ -644,7 +644,7 @@ try
Mock -CommandName Add-ADCommonGroupMember

$universalGroupInCompliance = Test-TargetResource -GroupName $testUniversalPresentParams.GroupName -DisplayName $testUniversalPresentParams.DisplayName
$universalGroupInCompliance | Should Be $true
$universalGroupInCompliance | Should -Be $true
}

It "Calls Restore-AdCommonObject when RestoreFromRecycleBin is used" {
Expand Down
6 changes: 3 additions & 3 deletions Tests/Unit/MSFT_xADKDSKey.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ try

$getTargetResourceResult = Get-TargetResource @getTargetResourceParametersFuture

$getTargetResourceResult.Ensure | Should Be 'Absent'
$getTargetResourceResult.Ensure | Should -Be 'Absent'

Assert-MockCalled -CommandName Assert-HasDomainAdminRights -Scope It -Exactly -Times 1
Assert-MockCalled -CommandName Get-KdsRootKey -Scope It -Exactly -Times 1
Expand All @@ -422,7 +422,7 @@ try
}

$getTargetResourceResult = Get-TargetResource @getTargetResourceParametersFuture
$getTargetResourceResult.Ensure | Should Be 'Absent'
$getTargetResourceResult.Ensure | Should -Be 'Absent'

Assert-MockCalled -CommandName Assert-HasDomainAdminRights -Scope It -Exactly -Times 1
Assert-MockCalled -CommandName Get-KdsRootKey -Scope It -Exactly -Times 1
Expand Down Expand Up @@ -606,7 +606,7 @@ try
}

$testTargetResourceParametersFuture[$Parameter] = $value
Test-TargetResource @testTargetResourceParametersFuture | Should Be $false
Test-TargetResource @testTargetResourceParametersFuture | Should -Be $false

Assert-MockCalled -CommandName Compare-TargetResourceState -ParameterFilter {
$mockKDSRootKeyFuture.EffectiveTime -eq $EffectiveTime
Expand Down
Loading

0 comments on commit b44554b

Please sign in to comment.