Tests/Unit/MSFT_xUserResource.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
#To run these tests, the currently logged on user must have rights to create a user
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "")]
param ()

$TestEnvironment = Initialize-TestEnvironment `
    -DSCModuleName 'xPSDesiredStateConfiguration' `
    -DSCResourceName 'MSFT_xUserResource' `
    -TestType Unit

InModuleScope 'MSFT_xUserResource' {
    Describe 'xUser Unit Tests' {
        BeforeAll {
            Import-Module "$PSScriptRoot\..\CommonTestHelper.psm1" -Force
            Import-Module "$PSScriptRoot\MSFT_xUserResource.TestHelper.psm1" -Force
        }

        It 'Get-TargetResource user present' {
            $testUserName = "TestUserName12345"
            $testUserPassword = "StrongOne7."
            $testUserDescription = "Some Description"

            $secureTestPassword = ConvertTo-SecureString $testUserPassword -AsPlainText -Force
            $testCredential = New-Object PSCredential ($testUserName, $secureTestPassword)

            New-User -Credential $testCredential -Description $testUserDescription

            try
            {
                $getTargetResourceResult = Get-TargetResource $testUserName

                $getTargerResourceResultProperties = @('UserName', 'Ensure', 'Description', 'Disabled', 'PasswordNeverExpires', 'PasswordChangeNotAllowed')

                Test-GetTargetResourceResult -GetTargetResourceResult $getTargetResourceResult -GetTargetResourceResultProperties $getTargerResourceResultProperties

                $getTargetResourceResult["UserName"]                | Should Be $testUserName
                $getTargetResourceResult["Ensure"]                  | Should Be "Present"
                $getTargetResourceResult["Description"]             | Should Be $testUserDescription
                $getTargetResourceResult["PasswordChangeRequired"]  | Should Be $null
            }
            finally
            {
                Remove-User -UserName $testUserName
            }
        }

        It 'Get-TargetResource user absent' {
            $testUserName = "AbsentUserUserName123456789"

            $getTargetResourceResult = Get-TargetResource $testUserName

            $getTargerResourceResultProperties = @('UserName', 'Ensure')

            Test-GetTargetResourceResult -GetTargetResourceResult $getTargetResourceResult -GetTargetResourceResultProperties $getTargerResourceResultProperties

            $getTargetResourceResult["UserName"]   | Should Be $TestUserName
            $getTargetResourceResult["Ensure"]     | Should Be "Absent"
        }

        It 'Test-TargetResource user present and correct description' {
            $testUserName = "TestUserName12345"
            $testUserPassword = "StrongOne7."
            $testUserDescription = "Some Description"

            $secureTestPassword = ConvertTo-SecureString $testUserPassword -AsPlainText -Force
            $testCredential = New-Object PSCredential ($testUserName, $secureTestPassword)

            New-User -Credential $testCredential -Description $testUserDescription

            try
            {
                $testTargetResourceResult = Test-TargetResource $testUserName -Description $testUserDescription
                $testTargetResourceResult | Should Be $true
            }
            finally
            {
                Remove-User -UserName $testUserName
            }
        }

        It 'Test-TargetResource user present and wrong description' {
            $testUserName = "TestUserName12345"
            $testUserPassword = "StrongOne7."
            $testUserDescription = "Some Description"

            $secureTestPassword = ConvertTo-SecureString $testUserPassword -AsPlainText -Force
            $testCredential = New-Object PSCredential ($testUserName, $secureTestPassword)

            New-User -Credential $testCredential -Description $testUserDescription

            try
            {
                $testTargetResourceResult = Test-TargetResource $testUserName -Description "Wrong description"
                $testTargetResourceResult | Should Be $false
            }
            finally
            {
                Remove-User -UserName $testUserName
            }
        }

        It 'Test-TargetResource user absent' {
            $absentUserName = "AbsentUserUserName123456789"
            $testTargetResourceResult = Test-TargetResource $absentUserName
            $testTargetResourceResult | Should Be $false
        }

        It 'Set-TargetResource user present and ensure present' {
            $testUserName = "TestUserName12345"
            $testUserPassword = "StrongOne7."
            $testUserDescription = "Some Description"

            $secureTestPassword = ConvertTo-SecureString $testUserPassword -AsPlainText -Force
            $testCredential = New-Object PSCredential ($testUserName, $secureTestPassword)

            New-User -Credential $testCredential -Description $testUserDescription

            try
            {
                $setTargetResourceResult = Set-TargetResource $testUserName -Ensure Present
                Test-User -UserName $testUserName | Should Be $true
            }
            finally
            {
                Remove-User -UserName $testUserName
            }
        }

        It 'Set-TargetResource user present and ensure absent' {
            $testUserName = "TestUserName12345"
            $testUserPassword = "StrongOne7."
            $testUserDescription = "Some Description"

            $secureTestPassword = ConvertTo-SecureString $testUserPassword -AsPlainText -Force
            $testCredential = New-Object PSCredential ($testUserName, $secureTestPassword)

            New-User -Credential $testCredential -Description $testUserDescription

            try
            {
                $setTargetResourceResult = Set-TargetResource $testUserName -Ensure Absent
                Test-User -UserName $testUserName | Should Be $false
            }
            finally
            {
                Remove-User -UserName $testUserName
            }
        }
    }
}