From b6036b19df52323210d10ef21796f870b9e1bf2b Mon Sep 17 00:00:00 2001 From: Patrick Heijmann Date: Tue, 3 Dec 2019 14:57:11 +0100 Subject: [PATCH 1/4] Updated syntax --- Tests/TestHelpers/CommonTestHelper.psm1 | 4 +- Tests/Unit/CertificateDsc.Common.Tests.ps1 | 153 +++++++++++++++----- Tests/Unit/MSFT_CertReq.Tests.ps1 | 21 ++- Tests/Unit/MSFT_CertificateExport.Tests.ps1 | 42 ++++-- 4 files changed, 162 insertions(+), 58 deletions(-) diff --git a/Tests/TestHelpers/CommonTestHelper.psm1 b/Tests/TestHelpers/CommonTestHelper.psm1 index 8dd376e4..6bb92007 100644 --- a/Tests/TestHelpers/CommonTestHelper.psm1 +++ b/Tests/TestHelpers/CommonTestHelper.psm1 @@ -48,11 +48,11 @@ function Get-InvalidOperationRecord [CmdletBinding()] param ( - [ValidateNotNullOrEmpty()] + [Parameter][ValidateNotNullOrEmpty()] [String] $Message, - [ValidateNotNull()] + [Parameter][ValidateNotNull()] [System.Management.Automation.ErrorRecord] $ErrorRecord ) diff --git a/Tests/Unit/CertificateDsc.Common.Tests.ps1 b/Tests/Unit/CertificateDsc.Common.Tests.ps1 index 30867d54..e5609ec8 100644 --- a/Tests/Unit/CertificateDsc.Common.Tests.ps1 +++ b/Tests/Unit/CertificateDsc.Common.Tests.ps1 @@ -19,7 +19,9 @@ InModuleScope 'CertificateDsc.Common' { Describe 'CertificateDsc.Common\Test-DscParameterState' -Tag TestDscParameterState { Context -Name 'When passing values' -Fixture { It 'Should return true for two identical tables' { - $mockDesiredValues = @{ Example = 'test' } + $mockDesiredValues = @{ + Example = 'test' + } $testParameters = @{ CurrentValues = $mockDesiredValues @@ -30,8 +32,12 @@ InModuleScope 'CertificateDsc.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 @@ -42,8 +48,12 @@ InModuleScope 'CertificateDsc.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 @@ -54,8 +64,12 @@ InModuleScope 'CertificateDsc.Common' { } It 'Should return false when a value is different for [Int16]' { - $mockCurrentValues = @{ Example = [System.Int16] 1 } - $mockDesiredValues = @{ Example = [System.Int16] 2 } + $mockCurrentValues = @{ + Example = [System.Int16] 1 + } + $mockDesiredValues = @{ + Example = [System.Int16] 2 + } $testParameters = @{ CurrentValues = $mockCurrentValues @@ -66,8 +80,12 @@ InModuleScope 'CertificateDsc.Common' { } It 'Should return false when a value is different for [UInt16]' { - $mockCurrentValues = @{ Example = [System.UInt16] 1 } - $mockDesiredValues = @{ Example = [System.UInt16] 2 } + $mockCurrentValues = @{ + Example = [System.UInt16] 1 + } + $mockDesiredValues = @{ + Example = [System.UInt16] 2 + } $testParameters = @{ CurrentValues = $mockCurrentValues @@ -78,8 +96,12 @@ InModuleScope 'CertificateDsc.Common' { } It 'Should return false when a value is different for [Boolean]' { - $mockCurrentValues = @{ Example = [System.Boolean] $true } - $mockDesiredValues = @{ Example = [System.Boolean] $false } + $mockCurrentValues = @{ + Example = [System.Boolean] $true + } + $mockDesiredValues = @{ + Example = [System.Boolean] $false + } $testParameters = @{ CurrentValues = $mockCurrentValues @@ -91,7 +113,9 @@ InModuleScope 'CertificateDsc.Common' { It 'Should return false when a value is missing' { $mockCurrentValues = @{ } - $mockDesiredValues = @{ Example = 'test' } + $mockDesiredValues = @{ + Example = 'test' + } $testParameters = @{ CurrentValues = $mockCurrentValues @@ -102,8 +126,15 @@ InModuleScope 'CertificateDsc.Common' { } It 'Should return true when only a specified value matches, but other non-listed values do not' { - $mockCurrentValues = @{ Example = 'test'; SecondExample = 'true' } - $mockDesiredValues = @{ Example = 'test'; SecondExample = 'false' } + $mockCurrentValues = @{ + Example = 'test' + SecondExample = 'true' + } + + $mockDesiredValues = @{ + Example = 'test' + SecondExample = 'false' + } $testParameters = @{ CurrentValues = $mockCurrentValues @@ -115,8 +146,14 @@ InModuleScope 'CertificateDsc.Common' { } It 'Should return false when only specified values do not match, but other non-listed values do ' { - $mockCurrentValues = @{ Example = 'test'; SecondExample = 'true' } - $mockDesiredValues = @{ Example = 'test'; SecondExample = 'false' } + $mockCurrentValues = @{ + Example = 'test' + SecondExample = 'true' + } + $mockDesiredValues = @{ + Example = 'test' + SecondExample = 'false' + } $testParameters = @{ CurrentValues = $mockCurrentValues @@ -129,7 +166,10 @@ InModuleScope 'CertificateDsc.Common' { It 'Should return false when an empty hash table is used in the current values' { $mockCurrentValues = @{ } - $mockDesiredValues = @{ Example = 'test'; SecondExample = 'false' } + $mockDesiredValues = @{ + Example = 'test' + SecondExample = 'false' + } $testParameters = @{ CurrentValues = $mockCurrentValues @@ -140,7 +180,10 @@ InModuleScope 'CertificateDsc.Common' { } It 'Should return true when evaluating a table against a CimInstance' { - $mockCurrentValues = @{ Handle = '0'; ProcessId = '1000' } + $mockCurrentValues = @{ + Handle = '0' + ProcessId = '1000' + } $mockWin32ProcessProperties = @{ Handle = 0 @@ -166,7 +209,10 @@ InModuleScope 'CertificateDsc.Common' { } It 'Should return false when evaluating a table against a CimInstance and a value is wrong' { - $mockCurrentValues = @{ Handle = '1'; ProcessId = '1000' } + $mockCurrentValues = @{ + Handle = '1' + ProcessId = '1000' + } $mockWin32ProcessProperties = @{ Handle = 0 @@ -192,8 +238,14 @@ InModuleScope 'CertificateDsc.Common' { } It 'Should return true when evaluating a hash table containing an array' { - $mockCurrentValues = @{ Example = 'test'; SecondExample = @('1', '2') } - $mockDesiredValues = @{ Example = 'test'; SecondExample = @('1', '2') } + $mockCurrentValues = @{ + Example = 'test' + SecondExample = @('1', '2') + } + $mockDesiredValues = @{ + Example = 'test' + SecondExample = @('1', '2') + } $testParameters = @{ CurrentValues = $mockCurrentValues @@ -204,8 +256,15 @@ InModuleScope 'CertificateDsc.Common' { } It 'Should return false when evaluating a hash table containing an array with wrong values' { - $mockCurrentValues = @{ Example = 'test'; SecondExample = @('A', 'B') } - $mockDesiredValues = @{ Example = 'test'; SecondExample = @('1', '2') } + $mockCurrentValues = @{ + Example = 'test' + SecondExample = @('A', 'B') + } + + $mockDesiredValues = @{ + Example = 'test' + SecondExample = @('1', '2') + } $testParameters = @{ CurrentValues = $mockCurrentValues @@ -216,8 +275,14 @@ InModuleScope 'CertificateDsc.Common' { } It 'Should return false when evaluating a hash table containing an array, but the CurrentValues are missing an array' { - $mockCurrentValues = @{ Example = 'test' } - $mockDesiredValues = @{ Example = 'test'; SecondExample = @('1', '2') } + $mockCurrentValues = @{ + Example = 'test' + } + + $mockDesiredValues = @{ + Example = 'test' + SecondExample = @('1', '2') + } $testParameters = @{ CurrentValues = $mockCurrentValues @@ -228,8 +293,15 @@ InModuleScope 'CertificateDsc.Common' { } It 'Should return false when evaluating a hash table containing an array, but the property i CurrentValues is $null' { - $mockCurrentValues = @{ Example = 'test'; SecondExample = $null } - $mockDesiredValues = @{ Example = 'test'; SecondExample = @('1', '2') } + $mockCurrentValues = @{ + Example = 'test' + SecondExample = $null + } + + $mockDesiredValues = @{ + Example = 'test' + SecondExample = @('1', '2') + } $testParameters = @{ CurrentValues = $mockCurrentValues @@ -242,7 +314,9 @@ InModuleScope 'CertificateDsc.Common' { Context -Name 'When passing invalid types for DesiredValues' -Fixture { It 'Should throw the correct error when DesiredValues is of wrong type' { - $mockCurrentValues = @{ Example = 'something' } + $mockCurrentValues = @{ + Example = 'something' + } $mockDesiredValues = 'NotHashTable' $testParameters = @{ @@ -273,8 +347,13 @@ InModuleScope 'CertificateDsc.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 @@ -289,7 +368,9 @@ InModuleScope 'CertificateDsc.Common' { Context -Name 'When passing an CimInstance as DesiredValue and ValuesToCheck is $null' -Fixture { It 'Should throw the correct error' { - $mockCurrentValues = @{ Example = 'something' } + $mockCurrentValues = @{ + Example = 'something' + } $mockWin32ProcessProperties = @{ Handle = 0 @@ -769,7 +850,9 @@ InModuleScope 'CertificateDsc.Common' { } Context 'When FIPS is enabled' { - Mock -CommandName Get-ItemProperty -MockWith { @{ Enabled = 1 } } + Mock -CommandName Get-ItemProperty -MockWith { @{ + Enabled = 1 + } } Context 'When a single valid FIPS thumbrpint by parameter is passed' { $result = Test-Thumbprint -Thumbprint $validFipsThumbprint @@ -1276,7 +1359,7 @@ InModuleScope 'CertificateDsc.Common' { [CmdletBinding()] param ( - $DomainName + [Parameter]$DomainName ) return New-Object -TypeName psobject -Property @{ Children = @( @@ -1317,7 +1400,7 @@ InModuleScope 'CertificateDsc.Common' { [CmdletBinding()] param ( - $DomainName + [Parameter]$DomainName ) return New-Object -TypeName psobject -Property @{ Children = @( @@ -1356,7 +1439,7 @@ InModuleScope 'CertificateDsc.Common' { [CmdletBinding()] param ( - $DomainName + [Parameter]$DomainName ) New-InvalidOperationException ` -Message ($LocalizedData.DomainNotJoinedError) diff --git a/Tests/Unit/MSFT_CertReq.Tests.ps1 b/Tests/Unit/MSFT_CertReq.Tests.ps1 index 344b9f0f..849f4ea5 100644 --- a/Tests/Unit/MSFT_CertReq.Tests.ps1 +++ b/Tests/Unit/MSFT_CertReq.Tests.ps1 @@ -115,7 +115,10 @@ try return $true } - $sanOid = New-Object -TypeName System.Security.Cryptography.Oid -Property @{FriendlyName = 'Subject Alternative Name' } + $sanOid = New-Object -TypeName System.Security.Cryptography.Oid -Property @{ + FriendlyName = 'Subject Alternative Name' + } + $sanExt = [PSCustomObject] @{ Oid = $sanOid Critical = $false @@ -1831,10 +1834,18 @@ OID = $oid Describe 'MSFT_CertReq\ConvertTo-StringEnclosedInDoubleQuotes' { Context 'When called with test values' { $testValues = @( - @{ Value = 'test' }, - @{ Value = '"test' }, - @{ Value = 'test"' }, - @{ Value = '"test"' } + @{ + Value = 'test' + }, + @{ + Value = '"test' + }, + @{ + Value = 'test"' + }, + @{ + Value = '"test"' + } ) It 'Should return ''"test"'' when called with ''''' -TestCases $testValues { diff --git a/Tests/Unit/MSFT_CertificateExport.Tests.ps1 b/Tests/Unit/MSFT_CertificateExport.Tests.ps1 index 70fcf67c..888e5434 100644 --- a/Tests/Unit/MSFT_CertificateExport.Tests.ps1 +++ b/Tests/Unit/MSFT_CertificateExport.Tests.ps1 @@ -40,15 +40,25 @@ try Issuer = "CN=$certificateSubject" FriendlyName = $certificateFriendlyName DnsNameList = @( - @{ Unicode = $certificateDNSNames[0] } - @{ Unicode = $certificateDNSNames[1] } + @{ + Unicode = $certificateDNSNames[0] + } + @{ + Unicode = $certificateDNSNames[1] + } ) Extensions = @( - @{ EnhancedKeyUsages = ($certificateKeyUsage -join ', ') } + @{ + EnhancedKeyUsages = ($certificateKeyUsage -join ', ') + } ) EnhancedKeyUsages = @( - @{ FriendlyName = $certificateEKU[0] } - @{ FriendlyName = $certificateEKU[1] } + @{ + FriendlyName = $certificateEKU[0] + } + @{ + FriendlyName = $certificateEKU[1] + } ) NotBefore = (Get-Date).AddDays(-30) # Issued on NotAfter = (Get-Date).AddDays(31) # Expires after @@ -222,10 +232,10 @@ try [CmdletBinding()] param ( - $FilePath, - $Cert, - $Force, - $Type + [Parameter]$FilePath, + [Parameter]$Cert, + [Parameter]$Force, + [Parameter]$Type ) } @@ -254,13 +264,13 @@ try [CmdletBinding()] param ( - $FilePath, - $Cert, - $Force, - $Type, - $Password, - $ChainOption, - $ProtectTo + [Parameter]$FilePath, + [Parameter]$Cert, + [Parameter]$Force, + [Parameter]$Type, + [Parameter]$Password, + [Parameter]$ChainOption, + [Parameter]$ProtectTo ) } From 41ad9a3fbccb04a8031bf30819bbd7128682c885 Mon Sep 17 00:00:00 2001 From: Patrick Heijmann Date: Fri, 6 Dec 2019 14:13:54 +0100 Subject: [PATCH 2/4] Updating tests --- Tests/Unit/CertificateDsc.Common.Tests.ps1 | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/Tests/Unit/CertificateDsc.Common.Tests.ps1 b/Tests/Unit/CertificateDsc.Common.Tests.ps1 index e5609ec8..b00b4f73 100644 --- a/Tests/Unit/CertificateDsc.Common.Tests.ps1 +++ b/Tests/Unit/CertificateDsc.Common.Tests.ps1 @@ -83,6 +83,7 @@ InModuleScope 'CertificateDsc.Common' { $mockCurrentValues = @{ Example = [System.UInt16] 1 } + $mockDesiredValues = @{ Example = [System.UInt16] 2 } @@ -99,6 +100,7 @@ InModuleScope 'CertificateDsc.Common' { $mockCurrentValues = @{ Example = [System.Boolean] $true } + $mockDesiredValues = @{ Example = [System.Boolean] $false } @@ -112,7 +114,8 @@ InModuleScope 'CertificateDsc.Common' { } It 'Should return false when a value is missing' { - $mockCurrentValues = @{ } + $mockCurrentValues = @{} + $mockDesiredValues = @{ Example = 'test' } @@ -150,6 +153,7 @@ InModuleScope 'CertificateDsc.Common' { Example = 'test' SecondExample = 'true' } + $mockDesiredValues = @{ Example = 'test' SecondExample = 'false' @@ -165,7 +169,8 @@ InModuleScope 'CertificateDsc.Common' { } It 'Should return false when an empty hash table is used in the current values' { - $mockCurrentValues = @{ } + $mockCurrentValues = @{} + $mockDesiredValues = @{ Example = 'test' SecondExample = 'false' @@ -242,6 +247,7 @@ InModuleScope 'CertificateDsc.Common' { Example = 'test' SecondExample = @('1', '2') } + $mockDesiredValues = @{ Example = 'test' SecondExample = @('1', '2') @@ -850,9 +856,11 @@ InModuleScope 'CertificateDsc.Common' { } Context 'When FIPS is enabled' { - Mock -CommandName Get-ItemProperty -MockWith { @{ - Enabled = 1 - } } + Mock -CommandName Get-ItemProperty -MockWith { + @{ + Enabled = 1 + } + } Context 'When a single valid FIPS thumbrpint by parameter is passed' { $result = Test-Thumbprint -Thumbprint $validFipsThumbprint From c4e416c0f170782108f54ad1243a2fa7350595b8 Mon Sep 17 00:00:00 2001 From: Patrick Heijmann Date: Fri, 6 Dec 2019 15:16:33 +0100 Subject: [PATCH 3/4] Updated parameters and change log --- CHANGELOG.md | 1 + Tests/Unit/CertificateDsc.Common.Tests.ps1 | 9 +++-- Tests/Unit/MSFT_CertificateExport.Tests.ps1 | 42 +++++++++++++++------ 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35ab0f78..1b34b7e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- Fixed unit test according to styling guide. - Corrected incorrectly located entries in `CHANGELOG.MD`. - Fix bug `Find-Certificate` when invalid certificate path is passed - fixes [Issue #208](https://github.com/PowerShell/CertificateDsc/issues/208). diff --git a/Tests/Unit/CertificateDsc.Common.Tests.ps1 b/Tests/Unit/CertificateDsc.Common.Tests.ps1 index b00b4f73..aafa4911 100644 --- a/Tests/Unit/CertificateDsc.Common.Tests.ps1 +++ b/Tests/Unit/CertificateDsc.Common.Tests.ps1 @@ -1367,7 +1367,8 @@ InModuleScope 'CertificateDsc.Common' { [CmdletBinding()] param ( - [Parameter]$DomainName + [Parameter()] + $DomainName ) return New-Object -TypeName psobject -Property @{ Children = @( @@ -1408,7 +1409,8 @@ InModuleScope 'CertificateDsc.Common' { [CmdletBinding()] param ( - [Parameter]$DomainName + [Parameter()] + $DomainName ) return New-Object -TypeName psobject -Property @{ Children = @( @@ -1447,7 +1449,8 @@ InModuleScope 'CertificateDsc.Common' { [CmdletBinding()] param ( - [Parameter]$DomainName + [Parameter()] + $DomainName ) New-InvalidOperationException ` -Message ($LocalizedData.DomainNotJoinedError) diff --git a/Tests/Unit/MSFT_CertificateExport.Tests.ps1 b/Tests/Unit/MSFT_CertificateExport.Tests.ps1 index 888e5434..051904b7 100644 --- a/Tests/Unit/MSFT_CertificateExport.Tests.ps1 +++ b/Tests/Unit/MSFT_CertificateExport.Tests.ps1 @@ -232,10 +232,17 @@ try [CmdletBinding()] param ( - [Parameter]$FilePath, - [Parameter]$Cert, - [Parameter]$Force, - [Parameter]$Type + [Parameter()] + $FilePath, + + [Parameter()] + $Cert, + + [Parameter()] + $Force, + + [Parameter()] + $Type ) } @@ -264,13 +271,26 @@ try [CmdletBinding()] param ( - [Parameter]$FilePath, - [Parameter]$Cert, - [Parameter]$Force, - [Parameter]$Type, - [Parameter]$Password, - [Parameter]$ChainOption, - [Parameter]$ProtectTo + [Parameter()] + $FilePath, + + [Parameter()] + $Cert, + + [Parameter()] + $Force, + + [Parameter()] + $Type, + + [Parameter()] + $Password, + + [Parameter()] + $ChainOption, + + [Parameter()] + $ProtectTo ) } From c4c1ac5ae03aeb4c5ad890d3f361ee8f734c64bb Mon Sep 17 00:00:00 2001 From: Patrick Heijmann Date: Fri, 6 Dec 2019 15:23:04 +0100 Subject: [PATCH 4/4] Changed parameter --- Tests/TestHelpers/CommonTestHelper.psm1 | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Tests/TestHelpers/CommonTestHelper.psm1 b/Tests/TestHelpers/CommonTestHelper.psm1 index 6bb92007..f3cdc78d 100644 --- a/Tests/TestHelpers/CommonTestHelper.psm1 +++ b/Tests/TestHelpers/CommonTestHelper.psm1 @@ -48,11 +48,13 @@ function Get-InvalidOperationRecord [CmdletBinding()] param ( - [Parameter][ValidateNotNullOrEmpty()] + [Parameter] + [ValidateNotNullOrEmpty()] [String] $Message, - [Parameter][ValidateNotNull()] + [Parameter] + [ValidateNotNull()] [System.Management.Automation.ErrorRecord] $ErrorRecord )