Tests/Unit/MSFT_SslSettings.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
$script:DSCModuleName = 'WebAdministrationDsc'
$script:DSCResourceName = 'MSFT_SslSettings'

#region HEADER
$script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
 if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
      (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
{
    & git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $script:moduleRoot -ChildPath '\DSCResource.Tests\'))
}

Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force

Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'Tests\MockWebAdministrationWindowsFeature.psm1')

$TestEnvironment = Initialize-TestEnvironment `
    -DSCModuleName $script:DSCModuleName `
    -DSCResourceName $script:DSCResourceName `
    -TestType Unit
#endregion

# Begin Testing

try
{
    #region Pester Tests

    InModuleScope $DSCResourceName {

        Describe "$script:DSCResourceName\Test-TargetResource" {
            Context 'Ensure is Present and SSLSettings is Present' {
                Mock Get-TargetResource -Verifiable {return @{
                    Name = 'Test'
                    Bindings = @('Ssl')
                    Ensure = 'Present'
                }}

                $result = Test-TargetResource -Name 'Test' -Ensure 'Present' -Bindings 'Ssl'

                Assert-VerifiableMocks

                It 'should return true' {
                    $result | should be $true
                }
            }

            Context 'Ensure is Absent and SslSettings is Absent' {
                Mock Get-TargetResource {return @{
                    Name = 'Test'
                    Bindings = @('Ssl')
                    Ensure = 'Absent'
                }}

                $result = Test-TargetResource -Name 'Test' -Ensure 'Absent' -Bindings 'Ssl'

                Assert-VerifiableMocks

                It 'should return true' {
                    $result | should be $true
                }
            }

            Context 'Ensure is Present and SslSettings is Absent' {
                Mock Get-TargetResource {return @{
                    Name = 'Test'
                    Bindings = @('Ssl')
                    Ensure = 'Absent'
                }}

                $result = Test-TargetResource -Name 'Test' -Ensure 'Present' -Bindings 'Ssl'

                Assert-VerifiableMocks

                It 'should return true' {
                    $result | should be $false
                }
            }
        }

        Describe "$script:DSCResourceName\Get-TargetResource" {
            Context 'Command finds SSL Settings' {
                Mock Assert-Module -Verifiable {}
                Mock Get-WebConfigurationProperty -Verifiable { return 'Ssl' }

                $result = Get-TargetResource -Name 'Name' -Bindings 'Ssl'
                $expected = @{
                    Name = 'Name'
                    Bindings = 'Ssl'
                    Ensure = 'Present'
                }

                Assert-VerifiableMocks

                It 'should return the correct bindings' {
                    $result.Bindings | should be $expected.Bindings
                }

                It 'should return the correct ensure' {
                    $result.Ensure | Should Be $expected.Ensure
                }
            }

            Context 'Command does not find Ssl Settings' {
                Mock Assert-Module -Verifiable {}
                Mock Get-WebConfigurationProperty -Verifiable { return $false }

                $result = Get-TargetResource -Name 'Name' -Bindings 'Ssl'
                $expected = @{
                    Name = 'Name'
                    Bindings = 'Ssl'
                    Ensure = 'Absent'
                }

                Assert-VerifiableMocks

                It 'should return the correct bindings' {
                    $result.Bindings | should be $expected.Bindings
                }

                It 'should return the correct ensure' {
                    $result.Ensure | Should Be $expected.Ensure
                }
            }
        }

        Describe "$script:DSCResourceName\Set-TargetResource" {
            Context 'SSL Bindings set to none' {
                Mock Assert-Module -Verifiable { }
                Mock Set-WebConfigurationProperty -Verifiable {}

                $result = (Set-TargetResource -Name 'Name' -Bindings '' -Ensure 'Present' -Verbose) 4>&1
                
                # Check that the LocalizedData message from the Set-TargetResource is correct
                $resultMessage = $LocalizedData.SettingSSLConfig -f 'Name', ''

                Assert-VerifiableMocks

                It 'should return the correct string' {
                    $result | Should Be $resultMessage
                }
            }

            Context 'Ssl Bindings set to Ssl' {
                Mock Assert-Module -Verifiable { }
                Mock Set-WebConfigurationProperty -Verifiable {}

                $result = (Set-TargetResource -Name 'Name' -Bindings 'Ssl' -Ensure 'Present' -Verbose) 4>&1
                
                # Check that the LocalizedData message from the Set-TargetResource is correct
                $resultMessage = $LocalizedData.SettingSSLConfig -f 'Name', 'Ssl'

                Assert-VerifiableMocks

                It 'should return the correct string' {
                    $result | Should Be $resultMessage
                }
            }

            Context 'Ssl Bindings set to Ssl,SslNegotiateCert,SslRequireCert' {
                Mock Assert-Module -Verifiable {}
                Mock Set-WebConfigurationProperty -Verifiable {}

                $result = (Set-TargetResource -Name 'Name' -Bindings @('Ssl','SslNegotiateCert','SslRequireCert') -Ensure 'Present' -Verbose) 4>&1
                
                # Check that the LocalizedData message from the Set-TargetResource is correct
                $resultMessage = $LocalizedData.SettingSSLConfig -f 'Name', 'Ssl,SslNegotiateCert,SslRequireCert'

                Assert-VerifiableMocks

                It 'should return the correct string' {
                    $result | Should Be $resultMessage
                }
            }
        }
    }
}
finally
{
    Restore-TestEnvironment -TestEnvironment $TestEnvironment
}