Tests/MSFT_xPfxImport.tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<#
.Summary
    Tests for xPfxImport
#>



$moduleName = ($PSCommandPath | Split-Path -Leaf) -replace '\..*$',''
Import-Module ($PSScriptRoot | Split-Path -Parent | Join-Path -ChildPath DSCResources | Join-Path -ChildPath $moduleName | Join-Path -ChildPath "$moduleName.psm1") -DisableNameChecking -Force

InModuleScope $moduleName {
    $invalidThumbprint = 'Zebra'
    $validThumbprint = (
        [System.AppDomain]::CurrentDomain.GetAssemblies().GetTypes() | Where-Object {
            $_.BaseType.BaseType -eq [System.Security.Cryptography.HashAlgorithm] -and
            ($_.Name -cmatch 'Managed$' -or $_.Name -cmatch 'Provider$')
        } | Select-Object -First 1 | ForEach-Object { 
            (New-Object $_).ComputeHash([String]::Empty) | ForEach-Object {
                '{0:x2}' -f $_
            }
        }
    ) -join ''

    $testFile = 'test.pfx'

    $invalidPath = 'TestDrive:'
    $validPath = "TestDrive:\$testFile"
    $validCertPath = "Cert:\LocalMachine\My"

    $PresentParams = @{
        Thumbprint = $validThumbprint
        Path = $validPath
        Ensure = 'Present'
        Location = 'LocalMachine'
        Store = 'My'
    }

    $AbsentParams = @{
        Thumbprint = $validThumbprint
        Path = $validPath
        Ensure = 'Absent'
        Location = 'LocalMachine'
        Store = 'My'
    }
    
    Describe 'Validate-PfxPath' {

        $null | Set-Content -Path $validPath

        Context 'a single existing file by parameter' {
            $result = Validate-PfxPath -Path $validPath
            It 'should return true' {
                ($result -is [bool]) | Should Be $true
                $result | Should Be $true
            }
        }
        Context 'a single missing file by parameter' {
            It 'should throw an exception' {
                # directories are not valid
                { Validate-PfxPath -Path $invalidPath } | Should Throw
            }
        }
        Context 'a single missing file by parameter with -Quiet' {
            $result = Validate-PfxPath -Path $invalidPath -Quiet
            It 'should return false' {
                ($result -is [bool]) | Should Be $true
                $result | Should Be $false
            }
        }
        Context 'a single existing file by pipeline' {
            $result = $validPath | Validate-PfxPath
            It 'should return true' {
                ($result -is [bool]) | Should Be $true
                $result | Should Be $true
            }
        }
        Context 'a single missing file by pipeline' {
            It 'should throw an exception' {
                # directories are not valid
                { $invalidPath | Validate-PfxPath } | Should Throw
            }
        }
        Context 'a single missing file by pipeline with -Quiet' {
            $result =  $invalidPath | Validate-PfxPath -Quiet
            It 'should return false' {
                ($result -is [bool]) | Should Be $true
                $result | Should Be $false
            }
        }
    }
    Describe 'Validate-Thumbprint' {

        Context 'a single valid thumbrpint by parameter' {
            $result = Validate-Thumbprint -Thumbprint $validThumbprint
            It 'should return true' {
                ($result -is [bool]) | Should Be $true
                $result | Should Be $true
            }
        }
        Context 'a single invalid thumbprint by parameter' {
            It 'should throw an exception' {
                # directories are not valid
                { Validate-Thumbprint -Thumbprint $invalidThumbprint } | Should Throw
            }
        }
        Context 'a single invalid thumbprint by parameter with -Quiet' {
            $result = Validate-Thumbprint $invalidThumbprint -Quiet
            It 'should return false' {
                ($result -is [bool]) | Should Be $true
                $result | Should Be $false
            }
        }
        Context 'a single valid thumbprint by pipeline' {
            $result = $validThumbprint | Validate-Thumbprint
            It 'should return true' {
                ($result -is [bool]) | Should Be $true
                $result | Should Be $true
            }
        }
        Context 'a single invalid thumborint by pipeline' {
            It 'should throw an exception' {
                # directories are not valid
                { $invalidThumbprint | Validate-Thumbprint } | Should Throw
            }
        }
        Context 'a single invalid thumbprint by pipeline with -Quiet' {
            $result =  $invalidThumbprint | Validate-Thumbprint -Quiet
            It 'should return false' {
                ($result -is [bool]) | Should Be $true
                $result | Should Be $false
            }
        }
    }
    Describe 'Get-TargetResource' {
        $null | Set-Content -Path $validPath

        $result = Get-TargetResource @PresentParams
        It 'should return a hashtable' {
            ($result -is [hashtable]) | Should Be $true
        }
        It 'should contain the input values' {
            $result.Thumbprint | Should BeExactly $validThumbprint
            $result.Path | Should BeExactly $validPath
        }
    }
    Describe 'Test-TargetResource' {
        $null | Set-Content -Path $validPath

        It 'should return a bool' {
            ((Test-TargetResource @PresentParams) -is [bool]) | Should Be $true
        }
        It 'Fails when valid path + thumbprint and Ensure is Absent' {
            Mock Get-TargetResource { 
                return @{
                    Thumbprint = $validThumbprint
                    Path = $validPath
                    Ensure = 'Absent'
                }
            }
        
        Test-TargetResource @PresentParams | Should Be $false
        }
        It 'Success when valid path + thumbprint and Ensure is Present' {
            Mock Get-TargetResource { 
                return @{
                    Thumbprint = $validThumbprint
                    Path = $validPath
                    Ensure = 'Present'
                }
            }
        
            Test-TargetResource @PresentParams | Should Be $true
        }
    }
    Describe 'Set-TargetResource' {
        $null | Set-Content -Path $validPath
        
        Mock Import-PfxCertificate {} -Verifiable

        Set-TargetResource @PresentParams

        It 'calls Import-PfxCertificate' {
            Assert-VerifiableMocks
        }
        It 'uses the parameters supplied' {
            Assert-MockCalled Import-PfxCertificate -Exactly -Times 1 -ParameterFilter {
                $CertStoreLocation -eq $validCertPath -and
                $FilePath -eq $validPath -and
                $Exportable -eq $false -and
                $Password -eq $null
            }
        }
        It 'calls Get-ChildItem' {
            Mock Get-ChildItem {} -Verifiable
            Set-TargetResource @AbsentParams
            Assert-MockCalled Get-ChildItem -Scope It
        }
    }
}