Tests/Unit/MSFT_xRemoteFile.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
$Global:DSCModuleName      = 'xPSDesiredStateConfiguration'
$Global:DSCResourceName    = 'MSFT_xRemoteFile'

#region HEADER
# Unit Test Template Version: 1.1.0
[String] $moduleRoot = Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $Script:MyInvocation.MyCommand.Path))
if ( (-not (Test-Path -Path (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
     (-not (Test-Path -Path (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
{
    & git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $moduleRoot -ChildPath '\DSCResource.Tests\'))
}

Import-Module (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force
$TestEnvironment = Initialize-TestEnvironment `
    -DSCModuleName $Global:DSCModuleName `
    -DSCResourceName $Global:DSCResourceName `
    -TestType Unit 
#endregion HEADER

# Create a working folder that all files will be created in
$Global:WorkingFolder = Join-Path -Path $ENV:Temp -ChildPath $Global:DSCResourceName
if (-not (Test-Path -Path $Global:WorkingFolder))
{
    $null = New-Item -Path $Global:WorkingFolder -ItemType Directory
}

# Begin Testing
try
{
    InModuleScope $Global:DSCResourceName {
        function Get-InvalidDataException
        {
            param(
                [parameter(Mandatory = $true)]
                [System.String]
                $errorId,

                [parameter(Mandatory = $true)]
                [System.String]
                $errorMessage
            )
            
            $errorCategory = [System.Management.Automation.ErrorCategory]::InvalidData
            $exception = New-Object `
                -TypeName System.InvalidOperationException `
                -ArgumentList $errorMessage 
            $errorRecord = New-Object `
                -TypeName System.Management.Automation.ErrorRecord `
                -ArgumentList $exception, $errorId, $errorCategory, $null
            return $errorRecord
        }

        #region Pester Tests
        $testURIFile = 'test.xml'
        $testURI = "http://contoso.com/$testURIFile"
        $testURIFileNotExist = 'testnotexist.xml'
        $testURINotExist = "http://contoso.com/$testURIFileNotExist"

        $testDestinationFolder = Join-Path `
            -Path $Global:WorkingFolder -ChildPath 'UnitTest_Folder'
        $testDestinationFolderFile = Join-Path `
            -Path $testDestinationFolder -ChildPath $testURIFile
        $testDestinationFolderFileNotExist = Join-Path `
            -Path $testDestinationFolder -ChildPath $testURIFileNotExist
        $testDestinationFile = Join-Path `
            -Path $Global:WorkingFolder -ChildPath 'UnitTest_File.xml'
        $testDestinationNotExist = Join-Path `
            -Path $Global:WorkingFolder -ChildPath 'UnitTest_NotExist'

        # Create the splats
        $testSplatFile = @{
            DestinationPath = $testDestinationFile;
            Uri = $testURI;
        }
        $testSplatFolderFileExists = @{
            DestinationPath = $testDestinationFolder;
            Uri = $testURI;
        }
        $testSplatFolderFileNotExist = @{
            DestinationPath = $testDestinationFolder;
            Uri = $testURINotExist;
        }

        # Create the test files/folders by clearing the working folder
        # if it exists and building a set of expected test files
        if (Test-Path -Path $Global:WorkingFolder)
        {
            $null = Remove-Item -Path $Global:WorkingFolder -Force -Recurse
        }
        $null = New-Item -Path $testDestinationFolder -ItemType Directory
        $null = Set-Content -Path $testDestinationFile -Value 'Dummy Content'
        $null = Set-Content -Path $testDestinationFolderFile -Value 'Dummy Content'

        Describe "$($Global:DSCResourceName)\Get-TargetResource" {
            $result = Get-TargetResource @testSplatFile
            It 'Returns "Present" when DestinationPath is a File and exists' {
                $Result.Ensure | Should Be 'Present'
            }

            $result = Get-TargetResource @testSplatFolderFileExists
            It 'Returns "Present" when DestinationPath is a Directory and exists and URI file exists' {
                $Result.Ensure | Should Be 'Present'
            }

            $result = Get-TargetResource @testSplatFolderFileNotExist
            It 'Returns "Absent" when DestinationPath is a Directory and exists but URI file does not' {
                $Result.Ensure | Should Be 'Absent'
            }

            Mock Get-PathItemType -MockWith { return 'Other' }
            $result = Get-TargetResource @testSplatFile
            It 'Returns "Absent" when DestinationPath is Other' {
                $Result.Ensure | Should Be 'Absent'
            }
        } #end Describe "$($Global:DSCResourceName)\Get-TargetResource"

        Describe "$($Global:DSCResourceName)\Set-TargetResource" {
            Context 'URI is "bad://.."' {
                It 'Throws a UriValidationFailure exeception' {
                    $splat = $testSplatFile.Clone()
                    $splat.Uri = 'bad://contoso.com/test.xml'
                    $errorMessage = $($LocalizedData.InvalidWebUriError) `
                                -f $splat.Uri
                    $errorRecord = Get-InvalidDataException `
                        -errorId "UriValidationFailure" `
                        -errorMessage $errorMessage
                    { Set-TargetResource @splat } | Should Throw $errorRecord
                }
            }
            Context 'DestinationPath is "bad://.."' {
                It 'Throws a DestinationPathSchemeValidationFailure exeception' {
                    $splat = $testSplatFile.Clone()
                    $splat.DestinationPath = 'bad://c:\test.xml'
                    $errorMessage = $($LocalizedData.InvalidDestinationPathSchemeError) `
                                -f $splat.DestinationPath
                    $errorRecord = Get-InvalidDataException `
                        -errorId "DestinationPathSchemeValidationFailure" `
                        -errorMessage $errorMessage
                    { Set-TargetResource @splat } | Should Throw $errorRecord
                }
            }
            Context 'DestinationPath starts with "\\"' {
                It 'Throws a DestinationPathIsUncFailure exeception' {
                    $splat = $testSplatFile.Clone()
                    $splat.DestinationPath = '\\myserver\share\test.xml'
                    $errorMessage = $($LocalizedData.DestinationPathIsUncError) `
                                -f $splat.DestinationPath
                    $errorRecord = Get-InvalidDataException `
                        -errorId "DestinationPathIsUncFailure" `
                        -errorMessage $errorMessage
                    { Set-TargetResource @splat } | Should Throw $errorRecord
                }
            }
            Context 'DestinationPath contains invalid characters "*"' {
                It 'Throws a DestinationPathHasInvalidCharactersError exeception' {
                    $splat = $testSplatFile.Clone()
                    $splat.DestinationPath = 'c:\*.xml'
                    $errorMessage = $($LocalizedData.DestinationPathHasInvalidCharactersError) `
                                -f $splat.DestinationPath
                    $errorRecord = Get-InvalidDataException `
                        -errorId "DestinationPathHasInvalidCharactersError" `
                        -errorMessage $errorMessage
                    { Set-TargetResource @splat } | Should Throw $errorRecord
                }
            }
            Mock Update-Cache
            Context 'URI is invalid, DestinationPath is a file' {
                It 'Throws a DownloadException exeception' {
                    $splat = $testSplatFile.Clone()
                    $splat.Uri = 'http://definitelydoesnotexist.com/reallydoesntexist.xml'
                    $errorMessage = $($LocalizedData.DownloadException) `
                                -f "The remote name could not be resolved: 'definitelydoesnotexist.com'"
                    $errorRecord = Get-InvalidDataException `
                        -errorId "DownloadException" `
                        -errorMessage $errorMessage
                    { Set-TargetResource @splat } | Should Throw $errorRecord
                }
                It 'Calls expected mocks' {
                    Assert-MockCalled Update-Cache -Exactly 0
                }
            }
            Mock Invoke-WebRequest
            Context 'URI is valid, DestinationPath is a file, download successful' {
                It 'Does not throw' {
                    { Set-TargetResource @testSplatFile } | Should Not Throw
                }
                It 'Calls expected mocks' {
                    Assert-MockCalled Invoke-WebRequest -Exactly 1
                    Assert-MockCalled Update-Cache -Exactly 1
                }
            }
        } #end Describe "$($Global:DSCResourceName)\Set-TargetResource"

        Describe "$($Global:DSCResourceName)\Test-TargetResource" {
            Mock Get-Cache
            Context 'URI is valid, DestinationPath is a File, file exists' {
                It 'Returns "False"' {
                    Test-TargetResource @testSplatFile | Should Be $False
                }
                It 'Calls expected mocks' {
                    Assert-MockCalled Get-Cache -Exactly 1
                }
            }
            Context 'URI is valid, DestinationPath is a File, file exists, matchsource is "False"' {
                It 'Returns "True"' {
                    $splat = $testSplatFile.Clone()
                    $splat.MatchSource = $False
                    Test-TargetResource @splat | Should Be $True
                }
                It 'Calls expected mocks' {
                    Assert-MockCalled Get-Cache -Exactly 0
                }
            }
            Context 'URI is valid, DestinationPath is a Folder, file exists' {
                It 'Returns "False"' {
                    Test-TargetResource @testSplatFolderFileExists | Should Be $False
                }
                It 'Calls expected mocks' {
                    Assert-MockCalled Get-Cache -Exactly 1
                }
            }
            Context 'URI is valid, DestinationPath is a Folder, file exists, matchsource is "False"' {
                It 'Returns "True"' {
                    $splat = $testSplatFolderFileExists.Clone()
                    $splat.MatchSource = $False
                    Test-TargetResource @splat | Should Be $True
                }
                It 'Calls expected mocks' {
                    Assert-MockCalled Get-Cache -Exactly 0
                }
            }
            Context 'URI is valid, DestinationPath is a Folder, file does not exist' {
                It 'Returns "False"' {
                    Test-TargetResource @testSplatFolderFileNotExist | Should Be $False
                }
                It 'Calls expected mocks' {
                    Assert-MockCalled Get-Cache -Exactly 0
                }
            }
            Context 'URI is valid, DestinationPath is a Folder, file exists, matchsource is "False"' {
                It 'Returns "False"' {
                    $splat = $testSplatFolderFileNotExist.Clone()
                    $splat.MatchSource = $False
                    Test-TargetResource @splat | Should Be $False
                }
                It 'Calls expected mocks' {
                    Assert-MockCalled Get-Cache -Exactly 0
                }
            }

        } #end Describe "$($Global:DSCResourceName)\Test-TargetResource"

        Describe "$($Global:DSCResourceName)\Test-UriScheme" {
            It 'Returns "True" when URI is "http://.." and scheme is "http|https|file"' {
                Test-UriScheme -Uri $testURI -Scheme 'http|https|file' | Should Be $true
            }
            It 'Returns "True" when URI is "http://.." and scheme is "http"' {
                Test-UriScheme -Uri $testURI -Scheme 'http' | Should Be $true
            }
            It 'Returns "False" when URI is "http://.." and scheme is "https"' {
                Test-UriScheme -Uri $testURI -Scheme 'https' | Should Be $false
            }
            It 'Returns "False" when URI is "bad://.." and scheme is "http|https|file"' {
                Test-UriScheme -Uri 'bad://contoso.com' -Scheme 'http|https|file' | Should Be $false
            }
        } #end Describe "$($Global:DSCResourceName)\Test-UriScheme"

        Describe "$($Global:DSCResourceName)\Get-PathItemType" {
            It 'Returns "Directory" when Path is a Directory' {
                Get-PathItemType -Path $testDestinationFolder | Should Be 'Directory'
            }
            It 'Returns "File" when Path is a File' {
                Get-PathItemType -Path $testDestinationFile | Should Be 'File'
            }
            It 'Returns "NotExists" when Path does not exist' {
                Get-PathItemType -Path $testDestinationNotExist | Should Be 'NotExists'
            }
            It 'Returns "Other" when Path is not in File System' {
                Get-PathItemType -Path HKLM:\Software | Should Be 'Other'
            }
        } #end Describe "$($Global:DSCResourceName)\Get-PathItemType"

        Describe "$($Global:DSCResourceName)\Get-Cache" {
            Mock Import-CliXml -MockWith { 'Expected Content' }
            Mock Test-Path -MockWith { $True }
            Context "DestinationPath 'c:\' and Uri $testURI and Cached Content exists" {
                $Result = Get-Cache -DestinationPath 'c:\' -Uri $testURI
                It "Returns Expected Content" {
                    $Result | Should Be 'Expected Content'
                }
                It "Calls expected mocks" {
                    Assert-MockCalled Import-CliXml -Exactly 1
                    Assert-MockCalled Test-Path -Exactly 1
                }
            }
            Mock Test-Path -MockWith { $False }
            Context "DestinationPath 'c:\' and Uri $testURI and Cached Content does not exist" {
                $Result = Get-Cache -DestinationPath 'c:\' -Uri $testURI
                It "Returns Null" {
                    $Result | Should BeNullOrEmpty
                }
                It "Calls expected mocks" {
                    Assert-MockCalled Import-CliXml -Exactly 0
                    Assert-MockCalled Test-Path -Exactly 1
                }
            }
        } #end Describe "$($Global:DSCResourceName)\Get-Cache"

        Describe "$($Global:DSCResourceName)\Update-Cache" {
            Mock Export-CliXml
            Mock Test-Path -MockWith { $True }
            Mock New-Item
            Context "DestinationPath 'c:\' and Uri $testURI and CacheLocation Exists" {
                It "Does Not Throw" {
                    { Update-Cache -DestinationPath 'c:\' -Uri $testURI -InputObject @{} } | Should Not Throw
                }
                It "Calls expected mocks" {
                    Assert-MockCalled Export-CliXml -Exactly 1
                    Assert-MockCalled Test-Path -Exactly 1
                    Assert-MockCalled New-Item -Exactly 0
                }
            }
            Mock Test-Path -MockWith { $False }
            Context "DestinationPath 'c:\' and Uri $testURI and CacheLocation does not exist" {
                It "Does Not Throw" {
                    { Update-Cache -DestinationPath 'c:\' -Uri $testURI -InputObject @{} } | Should Not Throw
                }
                It "Calls expected mocks" {
                    Assert-MockCalled Export-CliXml -Exactly 1
                    Assert-MockCalled Test-Path -Exactly 1
                    Assert-MockCalled New-Item -Exactly 1
                }
            }
        } #end Describe "$($Global:DSCResourceName)\Update-Cache"

        Describe "$($Global:DSCResourceName)\Get-CacheKey" {
            It "Returns -799765921 as Cache Key for DestinationPath 'c:\' and Uri $testURI" {
                Get-CacheKey -DestinationPath 'c:\' -Uri $testURI | Should Be -799765921
            }
            It "Returns 1266535016 as Cache Key for DestinationPath 'c:\Windows\System32' and Uri $testURINotExist" {
                Get-CacheKey -DestinationPath 'c:\Windows\System32' -Uri $testURINotExist | Should Be 1266535016
            }
        } #end Describe "$($Global:DSCResourceName)\Get-CacheKey"
    }
}
finally
{
    # Clean up the working folder
    $null = Remove-Item -Path $Global:WorkingFolder -Force -Recurse

    #region FOOTER
    Restore-TestEnvironment -TestEnvironment $TestEnvironment
    #endregion
}