Skip to content

Commit

Permalink
ActiveDirectoryDsc: Fix more hashtables according to styleguideline (#…
Browse files Browse the repository at this point in the history
…523)

- Changes to ActiveDirectoryDsc
  - Fix more hashtables according to style guidelines (issue #516).
  • Loading branch information
johlju authored Oct 29, 2019
1 parent 9196253 commit bde63e2
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 40 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

- Changes to ActiveDirectoryDsc
- Resolved custom Script Analyzer rules that was added to the test framework.
- Resolve style guideline violations for hashtables
- Resolve style guideline violations for hashtables ([issue #516](https://github.com/PowerShell/ActiveDirectoryDsc/issues/516)).
- Changes to ADReplicationSite
- Added 'Description' attribute parameter ([issue #500](https://github.com/PowerShell/ActiveDirectoryDsc/issues/500)).
- Added Integration testing ([issue #355](https://github.com/PowerShell/ActiveDirectoryDsc/issues/355)).
Expand Down Expand Up @@ -40,7 +40,7 @@
in the `Tests/Unit/Stubs` folder ([issue #245](https://github.com/PowerShell/ActiveDirectoryDsc/issues/245)).
- Update all unit tests removing all local stub functions in favor of
the new stub modules.
- Enable PSSCriptAnalyzer default rules ([issue #491](https://github.com/PowerShell/ActiveDirectoryDsc/issues/491)).
- Enable PSScriptAnalyzer default rules ([issue #491](https://github.com/PowerShell/ActiveDirectoryDsc/issues/491)).
- Changes to ActiveDirectoryDsc.Common
- Updated common helper function `Find-DomainController` with the
optional parameter `WaitForValidCredentials` which will ignore
Expand Down
18 changes: 13 additions & 5 deletions Modules/ActiveDirectoryDsc.Common/ActiveDirectoryDsc.Common.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ function ConvertTo-TimeSpan
$TimeSpanType
)

$newTimeSpanParams = @{ }
$newTimeSpanParams = @{}

switch ($TimeSpanType)
{
Expand Down Expand Up @@ -1079,22 +1079,30 @@ function Get-ADCommonParameters
{
if ($PreferCommonName -and ($PSBoundParameters.ContainsKey('CommonName')))
{
$adConnectionParameters = @{ Name = $CommonName }
$adConnectionParameters = @{
Name = $CommonName
}
}
else
{
$adConnectionParameters = @{ Name = $Identity }
$adConnectionParameters = @{
Name = $Identity
}
}
}
else
{
if ($PreferCommonName -and ($PSBoundParameters.ContainsKey('CommonName')))
{
$adConnectionParameters = @{ Identity = $CommonName }
$adConnectionParameters = @{
Identity = $CommonName
}
}
else
{
$adConnectionParameters = @{ Identity = $Identity }
$adConnectionParameters = @{
Identity = $Identity
}
}
}

Expand Down
75 changes: 57 additions & 18 deletions Tests/Unit/ActiveDirectory.Common.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ InModuleScope 'ActiveDirectoryDsc.Common' {
Describe 'ActiveDirectoryDsc.Common\Test-DscParameterState' -Tag TestDscParameterState {
Context 'When passing values' {
It 'Should return true for two identical tables' {
$mockDesiredValues = @{ Example = 'test' }
$mockDesiredValues = @{
Example = 'test'
}

$testParameters = @{
CurrentValues = $mockDesiredValues
Expand All @@ -40,8 +42,13 @@ InModuleScope 'ActiveDirectoryDsc.Common' {
}

It 'Should return false when a value is different for [System.String]' {
$mockCurrentValues = @{ Example = [System.String] 'something' }
$mockDesiredValues = @{ Example = [System.String] 'test' }
$mockCurrentValues = @{
Example = [System.String] 'something'
}

$mockDesiredValues = @{
Example = [System.String] 'test'
}

$testParameters = @{
CurrentValues = $mockCurrentValues
Expand All @@ -52,8 +59,13 @@ InModuleScope 'ActiveDirectoryDsc.Common' {
}

It 'Should return false when a value is different for [System.Int32]' {
$mockCurrentValues = @{ Example = [System.Int32] 1 }
$mockDesiredValues = @{ Example = [System.Int32] 2 }
$mockCurrentValues = @{
Example = [System.Int32] 1
}

$mockDesiredValues = @{
Example = [System.Int32] 2
}

$testParameters = @{
CurrentValues = $mockCurrentValues
Expand All @@ -64,8 +76,13 @@ InModuleScope 'ActiveDirectoryDsc.Common' {
}

It 'Should return false when a value is different for [System.Int16]' {
$mockCurrentValues = @{ Example = [System.Int16] 1 }
$mockDesiredValues = @{ Example = [System.Int16] 2 }
$mockCurrentValues = @{
Example = [System.Int16] 1
}

$mockDesiredValues = @{
Example = [System.Int16] 2
}

$testParameters = @{
CurrentValues = $mockCurrentValues
Expand All @@ -76,8 +93,13 @@ InModuleScope 'ActiveDirectoryDsc.Common' {
}

It 'Should return false when a value is different for [System.UInt16]' {
$mockCurrentValues = @{ Example = [System.UInt16] 1 }
$mockDesiredValues = @{ Example = [System.UInt16] 2 }
$mockCurrentValues = @{
Example = [System.UInt16] 1
}

$mockDesiredValues = @{
Example = [System.UInt16] 2
}

$testParameters = @{
CurrentValues = $mockCurrentValues
Expand All @@ -88,8 +110,13 @@ InModuleScope 'ActiveDirectoryDsc.Common' {
}

It 'Should return false when a value is different for [System.Boolean]' {
$mockCurrentValues = @{ Example = [System.Boolean] $true }
$mockDesiredValues = @{ Example = [System.Boolean] $false }
$mockCurrentValues = @{
Example = [System.Boolean] $true
}

$mockDesiredValues = @{
Example = [System.Boolean] $false
}

$testParameters = @{
CurrentValues = $mockCurrentValues
Expand All @@ -100,8 +127,10 @@ InModuleScope 'ActiveDirectoryDsc.Common' {
}

It 'Should return false when a value is missing' {
$mockCurrentValues = @{ }
$mockDesiredValues = @{ Example = 'test' }
$mockCurrentValues = @{}
$mockDesiredValues = @{
Example = 'test'
}

$testParameters = @{
CurrentValues = $mockCurrentValues
Expand Down Expand Up @@ -152,7 +181,7 @@ InModuleScope 'ActiveDirectoryDsc.Common' {
}

It 'Should return false when an empty hash table is used in the current values' {
$mockCurrentValues = @{ }
$mockCurrentValues = @{}
$mockDesiredValues = @{
Example = 'test'
SecondExample = 'false'
Expand Down Expand Up @@ -302,7 +331,10 @@ InModuleScope 'ActiveDirectoryDsc.Common' {

Context 'When passing invalid types for DesiredValues' {
It 'Should throw the correct error when DesiredValues is of wrong type' {
$mockCurrentValues = @{ Example = 'something' }
$mockCurrentValues = @{
Example = 'something'
}

$mockDesiredValues = 'NotHashTable'

$testParameters = @{
Expand Down Expand Up @@ -333,8 +365,13 @@ InModuleScope 'ActiveDirectoryDsc.Common' {
}
}

$mockCurrentValues = @{ Example = New-Object -TypeName 'MockUnknownType' }
$mockDesiredValues = @{ Example = New-Object -TypeName 'MockUnknownType' }
$mockCurrentValues = @{
Example = New-Object -TypeName 'MockUnknownType'
}

$mockDesiredValues = @{
Example = New-Object -TypeName 'MockUnknownType'
}

$testParameters = @{
CurrentValues = $mockCurrentValues
Expand All @@ -349,7 +386,9 @@ InModuleScope 'ActiveDirectoryDsc.Common' {

Context 'When passing an CimInstance as DesiredValue and ValuesToCheck is $null' {
It 'Should throw the correct error' {
$mockCurrentValues = @{ Example = 'something' }
$mockCurrentValues = @{
Example = 'something'
}

$mockWin32ProcessProperties = @{
Handle = 0
Expand Down
5 changes: 4 additions & 1 deletion Tests/Unit/MSFT_ADKDSKey.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,10 @@ try
$objectParameter.Expected = $incorrectParameter.Value
$objectParameter.Pass = $false

$testCases += @{ Parameter = $incorrectParameter.Name; Value = $incorrectParameter.Value }
$testCases += @{
Parameter = $incorrectParameter.Name
Value = $incorrectParameter.Value
}
}

Mock -CommandName Compare-TargetResourceState -MockWith {
Expand Down
20 changes: 16 additions & 4 deletions Tests/Unit/MSFT_ADManagedServiceAccount.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,10 @@ try
$objectParameter.Expected = $incorrectParameter.Value
$objectParameter.Pass = $false

$testCases += @{ Parameter = $incorrectParameter.Name; Value = $incorrectParameter.Value }
$testCases += @{
Parameter = $incorrectParameter.Name
Value = $incorrectParameter.Value
}
}

Mock -CommandName Compare-TargetResourceState -ParameterFilter {
Expand Down Expand Up @@ -1000,7 +1003,10 @@ try
$objectParameter.Expected = $incorrectParameter.Value
$objectParameter.Pass = $false

$testCases += @{ Parameter = $incorrectParameter.Name; Value = $incorrectParameter.Value }
$testCases += @{
Parameter = $incorrectParameter.Name
Value = $incorrectParameter.Value
}
}

Mock -CommandName Compare-TargetResourceState -ParameterFilter {
Expand Down Expand Up @@ -1221,7 +1227,10 @@ try
$objectParameter.Expected = $incorrectParameter.Value
$objectParameter.Pass = $false

$testCases += @{ Parameter = $incorrectParameter.Name; Value = $incorrectParameter.Value }
$testCases += @{
Parameter = $incorrectParameter.Name
Value = $incorrectParameter.Value
}
}

Mock -CommandName Compare-TargetResourceState -ParameterFilter {
Expand Down Expand Up @@ -1384,7 +1393,10 @@ try
$objectParameter.Expected = $incorrectParameter.Value
$objectParameter.Pass = $false

$testCases += @{ Parameter = $incorrectParameter.Name; Value = $incorrectParameter.Value }
$testCases += @{
Parameter = $incorrectParameter.Name
Value = $incorrectParameter.Value
}
}

Mock -CommandName Compare-TargetResourceState -ParameterFilter {
Expand Down
4 changes: 3 additions & 1 deletion Tests/Unit/MSFT_ADObjectPermissionEntry.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ try
InheritedObjectType = [System.Guid] '00000000-0000-0000-0000-000000000000'
ObjectFlags = 'None'
AccessControlType = 'Allow'
IdentityReference = [PSCustomObject] @{ Value = 'CONTOSO\User' }
IdentityReference = [PSCustomObject] @{
Value = 'CONTOSO\User'
}
IsInherited = $false
InheritanceFlags = 'None'
PropagationFlags = 'None'
Expand Down
14 changes: 12 additions & 2 deletions Tests/Unit/MSFT_ADReplicationSiteLink.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,12 @@ try

Describe 'ADReplicationSiteLink\Set-TargetResource' {
Context 'Site Link is Absent but is desired Present' {
Mock -CommandName Get-TargetResource -MockWith { @{ Ensure = 'Absent' } }
Mock -CommandName Get-TargetResource -MockWith {
@{
Ensure = 'Absent'
}
}

Mock -CommandName New-ADReplicationSiteLink
Mock -CommandName Set-ADReplicationSiteLink
Mock -CommandName Remove-ADReplicationSiteLink
Expand All @@ -302,7 +307,12 @@ try
}

Context 'Site Link is Present but desired Absent' {
Mock -CommandName Get-TargetResource -MockWith { @{ Ensure = 'Present' } }
Mock -CommandName Get-TargetResource -MockWith {
@{
Ensure = 'Present'
}
}

Mock -CommandName New-ADReplicationSiteLink
Mock -CommandName Set-ADReplicationSiteLink
Mock -CommandName Remove-ADReplicationSiteLink
Expand Down
16 changes: 12 additions & 4 deletions Tests/Unit/MSFT_ADReplicationSubnet.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ try
}
}
Mock -CommandName Get-ADObject -MockWith {
[PSCustomObject] @{ Name = 'Default-First-Site-Name' }
[PSCustomObject] @{
Name = 'Default-First-Site-Name'
}
}

It 'Should return present with the correct subnet' {
Expand Down Expand Up @@ -159,7 +161,9 @@ try
}
}
Mock -CommandName Get-ADObject -MockWith {
[PSCustomObject] @{ Name = 'Default-First-Site-Name' }
[PSCustomObject] @{
Name = 'Default-First-Site-Name'
}
}

It 'Should return true for present' {
Expand Down Expand Up @@ -220,7 +224,9 @@ try

Mock -CommandName Get-ADReplicationSubnet
Mock -CommandName Get-ADObject -MockWith {
[PSCustomObject] @{ Name = 'Default-First-Site-Name' }
[PSCustomObject] @{
Name = 'Default-First-Site-Name'
}
}

Mock -CommandName New-ADReplicationSubnet -MockWith {
Expand Down Expand Up @@ -255,7 +261,9 @@ try
}
}
Mock -CommandName Get-ADObject -MockWith {
[PSCustomObject] @{ Name = 'Default-First-Site-Name' }
[PSCustomObject] @{
Name = 'Default-First-Site-Name'
}
}

Mock -CommandName Set-ADReplicationSubnet -ParameterFilter { $Site -ne $null } -MockWith {
Expand Down
11 changes: 8 additions & 3 deletions Tests/Unit/MSFT_ADServicePrincipalName.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,17 @@ try

Context 'Remove all SPNs' {
Mock -CommandName Set-ADObject
Mock -CommandName Get-ADObject -ParameterFilter { $Filter -eq ([ScriptBlock]::Create(' ServicePrincipalName -eq $ServicePrincipalName ')) } -MockWith {
[PSCustomObject] @{ SamAccountName = 'User'; DistinguishedName = 'CN=User,OU=Corp,DC=contoso,DC=com' }
Mock -CommandName Get-ADObject -ParameterFilter {
$Filter -eq ([ScriptBlock]::Create(' ServicePrincipalName -eq $ServicePrincipalName '))
} -MockWith {
[PSCustomObject] @{
SamAccountName = 'User'
DistinguishedName = 'CN=User,OU=Corp,DC=contoso,DC=com'
}
}

It 'Should call the Set-ADObject' {
$result = Set-TargetResource @testAbsentParams
{ Set-TargetResource @testAbsentParams } | Should -Not -Throw

Assert-MockCalled -CommandName Set-ADObject -Scope It -Times 1 -Exactly
}
Expand Down

0 comments on commit bde63e2

Please sign in to comment.