tests/Unit/MSFT_xFirefoxPreference.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
$Script:DSCModuleName = 'xFirefox'
$Script:DSCResourceName = 'MSFT_xFirefoxPreference'
#region Header
#Unit Test Template Version: 1.0.0

$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 -Name (Join-Path -Path $script:moduleRoot -ChildPath 'DscResource.Tests\TestHelper.psm1') -Force
Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath 'DscResources\FirefoxPreferenceHelper.psm1')

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

# Begin Tests
try
{
    InModuleScope $DscResourceName {
        Describe 'MSFT_xFirefoxPreference\Get-TargetResource' {
            $firefoxPreference = @{
                PreferenceType   = 'lockPref'
                PreferenceName   = 'security.default_personal_cert'
                PreferenceValue  = 'Ask Every Time'
                InstallDirectory = "$TestDrive\Mozilla Firefox"
            }

            [System.Object[]] $firefoxcfg = @(
                '// FireFox preference file'
                'lockPref("security.default_personal_cert", "Ask Every Time");'
            )

            Mock -CommandName Get-Content -MockWith {$firefoxcfg}
            Mock -CommandName Get-FirefoxPreference -MockWith {$firefoxPreference}

            Context 'When Firefox InstallDirectory is missing or incorrect' {
                Mock -CommandName Test-Path -MockWith {$false} -ParameterFilter {$Path -eq $firefoxPreference.InstallDirectory}

                It 'Should return a null "CurrentConfiguration" and the desired "InstallDirectory"'{
                    $result = Get-TargetResource -InstallDirectory $firefoxPreference.InstallDirectory -PreferenceName $firefoxPreference.PreferenceName
                    $result.InstallDirectory | Should -Be $firefoxPreference.InstallDirectory
                    $result.PreferenceType | Should -Be $null
                    $result.PreferenceName | Should -Be $null
                    $result.PreferenceValue | Should -Be $null

                    Assert-MockCalled -CommandName Test-Path -Exactly -Times 1 -Scope It
                }
            }
            Context 'When Mozilla.cfg path is missing or incorrect' {
                Mock -CommandName Test-Path -MockWith {$true} -ParameterFilter {$Path -eq $firefoxPreference.InstallDirectory}
                Mock -CommandName Test-Path -MockWith {$false} -ParameterFilter {$Path -eq "$($firefoxPreference.InstallDirectory)\Mozilla.cfg"}
                Mock -CommandName Write-Verbose

                It 'Should return a null "CurrentConfiguration" and the desired "InstallDirectory"' {
                    $result = Get-TargetResource -InstallDirectory $firefoxPreference.InstallDirectory -PreferenceName $firefoxPreference.PreferenceName
                    $result.InstallDirectory | Should -Be $firefoxPreference.InstallDirectory
                    $result.PreferenceType | Should -Be $null
                    $result.PreferenceName | Should -Be $null
                    $result.PreferenceValue | Should -Be $null

                    Assert-MockCalled -CommandName Test-Path -Exactly -Times 2 -Scope It
                    Assert-MockCalled -CommandName Write-Verbose -Exactly -Times 1 -Scope It
                }
            }
            Context 'When Mozilla.cfg does exist' {
                Mock -CommandName Test-Path -MockWith {$true}

                It 'Should return the correct preferences in the correct format' {
                    $result = Get-TargetResource -InstallDirectory $firefoxPreference.InstallDirectory -PreferenceName $firefoxPreference.PreferenceName
                    $result.InstallDirectory | Should -Be $firefoxPreference.InstallDirectory
                    $result.PreferenceType | Should -Be $firefoxPreference.PreferenceType
                    $result.PreferenceName | Should -Be $firefoxPreference.PreferenceName
                    $result.PreferenceValue | Should -Be $firefoxPreference.PreferenceValue

                    Assert-MockCalled -CommandName Test-Path -Exactly -Times 2 -Scope It
                    Assert-MockCalled -CommandName Get-FirefoxPreference -Exactly -Times 1 -Scope It
                }
            }
        }

        Describe 'MSFT_xFirefoxPreference\Set-TargetResource'{
            $firefoxPreference = @{
                PreferenceType   = 'lockPref'
                PreferenceName   = 'security.default_personal_cert'
                PreferenceValue  = 'Ask Every Time'
                InstallDirectory = "$TestDrive\Mozilla Firefox"
            }

            Mock -CommandName Set-FirefoxPreconfiguration
            Mock -CommandName Set-FirefoxPreference

            Context 'When The Install Directory Path is incorrect'{
                Mock -CommandName Test-Path -MockWith {$false}

                It 'Should throw the correct exception' {
                    {Set-TargetResource @firefoxPreference} | Should -Throw "$($firefoxPreference.InstallDirectory) not found. Verify Firefox is installed and the correct Install Directory is defined."
                }
            }
            Context 'When Firefox preconfigurations are not complete'{
                Mock -CommandName Test-Path -MockWith {$true}
                Mock -CommandName Test-FirefoxPreconfiguration -MockWith {'autoconfigfile'}

                It 'Should call Set-FirefoxPreconfiguration' {
                    Set-TargetResource @firefoxPreference
                    Assert-MockCalled -CommandName 'Set-FirefoxPreconfiguration' -Times 1 -Exactly -Scope It
                }
                It 'Should call Set-FirefoxConfiguration' {
                    Set-TargetResource @firefoxPreference
                    Assert-MockCalled -CommandName 'Set-FirefoxPreference' -Times 1 -Exactly -Scope It
                }
            }
            Context 'When Firefox preconfigurations are complete'{
                Mock -CommandName Test-Path -MockWith {$true}
                Mock -CommandName Test-FirefoxPreconfiguration

                It 'Should not call Set-FirefoxPreconfiguration' {
                    Set-TargetResource @firefoxPreference
                    Assert-MockCalled -CommandName 'Set-FirefoxPreconfiguration' -Times 0 -Exactly -Scope It
                }
                It 'Should call Set-FirefoxPreference' {
                    Set-TargetResource @firefoxPreference
                    Assert-MockCalled -CommandName 'Set-FirefoxPreference' -Times 1 -Exactly -Scope It
                }
            }
        }

        Describe 'MSFT_xFirefoxPreference\Test-TargetResource'{
            $firefoxPreference = @{
                PreferenceType   = 'lockPref'
                PreferenceName   = 'security.default_personal_cert'
                PreferenceValue  = 'Ask Every Time'
                InstallDirectory = "$TestDrive\Mozilla Firefox"
            }

            Mock -CommandName Get-TargetResource -MockWith {$firefoxPreference}

            Context 'When Firefox Preconfigurations are not complete'{
                Mock -CommandName Test-FirefoxPreconfiguration -MockWith {'AutoConfigFile'}
                Mock -CommandName Test-FirefoxPreference -MockWith {$true}

                It 'Should return False'{
                    $result = Test-TargetResource @firefoxPreference
                    $result | Should -Be $false
                }
                It 'Should not call "Test-FirefoxPreference"' {
                    Test-TargetResource @firefoxPreference
                    Assert-MockCalled -CommandName Test-FirefoxPreconfiguration -Times 1 -Exactly -Scope It
                    Assert-MockCalled -CommandName Test-FirefoxPreference -Times 0 -Exactly -Scope It
                }
            }
            Context 'When Firefox preferences are incorrect' {
                Mock -CommandName Test-FirefoxPreconfiguration
                Mock -CommandName Test-FirefoxPreference -MockWith {$false}

                It 'Should return False' {
                    $result = Test-TargetResource @firefoxPreference
                    $result | Should -Be $false
                }
            }
            Context 'When Firefox preferences are correct' {
                Mock -CommandName Test-FirefoxPreconfiguration
                Mock -CommandName Test-FirefoxPreference -MockWith {$true}

                It 'Should return True' {
                    $result = Test-TargetResource @firefoxPreference
                    $result | Should -Be $true
                }
            }
        }
    }
}
finally
{
    Restore-TestEnvironment -TestEnvironment $TestEnvironment
}