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/TestHelpers/CommonTestHelper.psm1 b/Tests/TestHelpers/CommonTestHelper.psm1 index 8dd376e4..f3cdc78d 100644 --- a/Tests/TestHelpers/CommonTestHelper.psm1 +++ b/Tests/TestHelpers/CommonTestHelper.psm1 @@ -48,10 +48,12 @@ function Get-InvalidOperationRecord [CmdletBinding()] param ( + [Parameter] [ValidateNotNullOrEmpty()] [String] $Message, + [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..aafa4911 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,13 @@ 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 +97,13 @@ 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 @@ -90,8 +114,11 @@ InModuleScope 'CertificateDsc.Common' { } It 'Should return false when a value is missing' { - $mockCurrentValues = @{ } - $mockDesiredValues = @{ Example = 'test' } + $mockCurrentValues = @{} + + $mockDesiredValues = @{ + Example = 'test' + } $testParameters = @{ CurrentValues = $mockCurrentValues @@ -102,8 +129,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 +149,15 @@ 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 @@ -128,8 +169,12 @@ InModuleScope 'CertificateDsc.Common' { } It 'Should return false when an empty hash table is used in the current values' { - $mockCurrentValues = @{ } - $mockDesiredValues = @{ Example = 'test'; SecondExample = 'false' } + $mockCurrentValues = @{} + + $mockDesiredValues = @{ + Example = 'test' + SecondExample = 'false' + } $testParameters = @{ CurrentValues = $mockCurrentValues @@ -140,7 +185,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 +214,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 +243,15 @@ 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 +262,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 +281,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 +299,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 +320,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 +353,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 +374,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 +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 @@ -1276,6 +1367,7 @@ InModuleScope 'CertificateDsc.Common' { [CmdletBinding()] param ( + [Parameter()] $DomainName ) return New-Object -TypeName psobject -Property @{ @@ -1317,6 +1409,7 @@ InModuleScope 'CertificateDsc.Common' { [CmdletBinding()] param ( + [Parameter()] $DomainName ) return New-Object -TypeName psobject -Property @{ @@ -1356,6 +1449,7 @@ InModuleScope 'CertificateDsc.Common' { [CmdletBinding()] param ( + [Parameter()] $DomainName ) New-InvalidOperationException ` 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..051904b7 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,9 +232,16 @@ try [CmdletBinding()] param ( + [Parameter()] $FilePath, + + [Parameter()] $Cert, + + [Parameter()] $Force, + + [Parameter()] $Type ) } @@ -254,12 +271,25 @@ try [CmdletBinding()] param ( + [Parameter()] $FilePath, + + [Parameter()] $Cert, + + [Parameter()] $Force, + + [Parameter()] $Type, + + [Parameter()] $Password, + + [Parameter()] $ChainOption, + + [Parameter()] $ProtectTo ) }