Tests/Unit/cUserRight.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
#requires -Version 4.0 -Modules Pester

$Global:DSCModuleName = 'cUserRightsAssignment'
$Global:DSCResourceName = 'cUserRight'

#region Header

$ModuleRoot = Split-Path -Path $Script:MyInvocation.MyCommand.Path -Parent | Split-Path -Parent | Split-Path -Parent

if (
    (-not (Test-Path -Path (Join-Path -Path $ModuleRoot -ChildPath 'DSCResource.Tests') -PathType Container)) -or
    (-not (Test-Path -Path (Join-Path -Path $ModuleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -PathType Leaf))
)
{
    & git @('clone', 'https://github.com/PowerShell/DscResource.Tests.git', (Join-Path -Path $ModuleRoot -ChildPath 'DSCResource.Tests'))
}
else
{
    & git @('-C', (Join-Path -Path $ModuleRoot -ChildPath 'DSCResource.Tests'), 'pull')
}

Import-Module -Name (Join-Path -Path $ModuleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force

$TestEnvironment = Initialize-TestEnvironment -DSCModuleName $Global:DSCModuleName -DSCResourceName $Global:DSCResourceName -TestType Unit

#endregion

# Begin Testing
try
{
    #region Unit Tests

    InModuleScope $Global:DSCResourceName {

        $TestParameters = [PSCustomObject]@{
            Constant = 'SeBatchLogonRight'
            Principal = 'CONTOSO\John_Doe'
        }

        Describe "$Global:DSCResourceName\Get-TargetResource" {

            Context 'User right is granted' {

                Mock -CommandName Get-UserRight -MockWith {
                    [PSCustomObject]@{
                        Principal = $TestParameters.Principal
                        UserRights = [String[]]@($TestParameters.Constant)
                    }
                }

                $Result = Get-TargetResource -Constant $TestParameters.Constant -Principal $TestParameters.Principal

                It 'Should call the Get-UserRight helper function once' {
                    Assert-MockCalled -CommandName Get-UserRight -Exactly 1
                }

                It 'Should return Ensure set to Present' {
                    $Result.Ensure | Should Be 'Present'
                }

                It 'Should return Constant' {
                    $Result.Constant | Should Be $TestParameters.Constant
                }

                It 'Should return Principal' {
                    $Result.Principal | Should Be $TestParameters.Principal
                }

            }

            Context 'User right is not granted' {

                Mock -CommandName Get-UserRight -MockWith {
                    [PSCustomObject]@{
                        Principal = $TestParameters.Principal
                        UserRights = [String[]]@()
                    }
                }

                $Result = Get-TargetResource -Constant $TestParameters.Constant -Principal $TestParameters.Principal

                It 'Should call the Get-UserRight helper function once' {
                    Assert-MockCalled -CommandName Get-UserRight -Exactly 1
                }

                It 'Should return Ensure set to Absent' {
                    $Result.Ensure | Should Be 'Absent'
                }

                It 'Should return Constant' {
                    $Result.Constant | Should Be $TestParameters.Constant
                }

                It 'Should return Principal' {
                    $Result.Principal | Should Be $TestParameters.Principal
                }

            }

        }

        Describe "$Global:DSCResourceName\Test-TargetResource" {

            Context 'Ensure is set to Absent and user right is granted' {

                Mock -CommandName Get-UserRight -MockWith {
                    [PSCustomObject]@{
                        Principal = $TestParameters.Principal
                        UserRights = [String[]]@($TestParameters.Constant)
                    }
                }

                $Result = Test-TargetResource -Ensure 'Absent' -Constant $TestParameters.Constant -Principal $TestParameters.Principal

                It 'Should return False' {
                    $Result | Should Be $false
                }

            }

            Context 'Ensure is set to Absent and user right is not granted' {

                Mock -CommandName Get-UserRight -MockWith {
                    [PSCustomObject]@{
                        Principal = $TestParameters.Principal
                        UserRights = [String[]]@()
                    }
                }

                $Result = Test-TargetResource -Ensure 'Absent' -Constant $TestParameters.Constant -Principal $TestParameters.Principal

                It 'Should return True' {
                    $Result | Should Be $true
                }

            }

            Context 'Ensure is set to Present and user right is granted' {

                Mock -CommandName Get-UserRight -MockWith {
                    [PSCustomObject]@{
                        Principal = $TestParameters.Principal
                        UserRights = [String[]]@($TestParameters.Constant)
                    }
                }

                $Result = Test-TargetResource -Ensure 'Present' -Constant $TestParameters.Constant -Principal $TestParameters.Principal

                It 'Should return True' {
                    $Result | Should Be $true
                }

            }

            Context 'Ensure is set to Present and user right is not granted' {

                Mock -CommandName Get-UserRight -MockWith {
                    [PSCustomObject]@{
                        Principal = $TestParameters.Principal
                        UserRights = [String[]]@()
                    }
                }

                $Result = Test-TargetResource -Ensure 'Present' -Constant $TestParameters.Constant -Principal $TestParameters.Principal

                It 'Should return False' {
                    $Result | Should Be $false
                }

            }

        }

        Describe "$Global:DSCResourceName\Set-TargetResource" {

            Context 'Ensure is set to Absent' {

                Mock -CommandName Revoke-UserRight -ParameterFilter {
                    $Principal -eq $TestParameters.Principal -and $UserRights -eq $TestParameters.Constant
                }

                Set-TargetResource -Ensure 'Absent' -Constant $TestParameters.Constant -Principal $TestParameters.Principal

                It 'Should call the Revoke-UserRight helper function once' {
                    Assert-MockCalled -CommandName Revoke-UserRight -Exactly 1
                }

            }

            Context 'Ensure is set to Present' {

                Mock -CommandName Grant-UserRight -ParameterFilter {
                    $Principal -eq $TestParameters.Principal -and $UserRights -eq $TestParameters.Constant
                }

                Set-TargetResource -Ensure 'Present' -Constant $TestParameters.Constant -Principal $TestParameters.Principal

                It 'Should call the Grant-UserRight helper function once' {
                    Assert-MockCalled -CommandName Grant-UserRight -Exactly 1
                }

            }

        }

        Describe "$Global:DSCResourceName\Initialize-CustomType" {

            It 'Should not throw' {
                {Initialize-CustomType} | Should Not Throw
            }

        }

    }

    #endregion
}
finally
{
    #region Footer

    Restore-TestEnvironment -TestEnvironment $TestEnvironment

    #endregion
}