Tests/Integration/MSFT_xGroupSet.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
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "")]
param ()

$TestEnvironment = Initialize-TestEnvironment `
    -DSCModuleName 'xPSDesiredStateConfiguration' `
    -DSCResourceName 'MSFT_xGroupSet' `
    -TestType Integration 

Describe "xGroupSet Integration Tests" {
    BeforeAll {
        Import-Module "$PSScriptRoot\..\Unit\MSFT_xGroupResource.TestHelper.psm1" -Force
        Import-Module "$PSScriptRoot\..\CommonTestHelper.psm1" -Force
        Import-Module "$PSScriptRoot\..\..\DSCResources\CommonResourceHelper.psm1" -Force
    }

    It "Create a xGroupSet" {
        $configurationName = "CreateTestGroup"
        $configurationPath = Join-Path -Path (Get-Location) -ChildPath $configurationName

        $testUserName1 = "LocalTestUser1"
        $testUserName2 = "LocalTestUser2"
        $testUserPassword = "StrongOne7."
        $testUserDescription = "Test users for creating an xGroupSet"

        $secureTestPassword = ConvertTo-SecureString $testUserPassword -AsPlainText -Force

        $testCredential1 = New-Object PSCredential ($testUserName1, $secureTestPassword)
        $testCredential2 = New-Object PSCredential ($testUserName2, $secureTestPassword)

        New-User -Credential $testCredential1 -Description $testUserDescription
        New-User -Credential $testCredential2 -Description $testUserDescription

        $membersToInclude = @($testUserName1, $testUserName2)
        $groupNames = @("TestGroupName123", "TestGroupName456")

        try
        {
            Configuration $configurationName
            {
                Import-DscResource -ModuleName xPSDesiredStateConfiguration

                xGroupSet GroupSet1
                {
                    GroupName = $groupNames
                    Ensure = "Present"
                    MembersToInclude = $membersToInclude
                }
            }

            & $configurationName -OutputPath $configurationPath

            Start-DscConfiguration -Path  $configurationPath -Wait -Force -Verbose

            if (Test-IsNanoServer)
            {
                foreach ($groupName in $groupNames)
                {
                    $localGroup = Get-LocalGroup -Name $groupName -ErrorAction SilentlyContinue
                    $localGroup | Should Not Be $null
                }
            }
            else
            {
                $groupEntries = [ADSI] "WinNT://$env:ComputerName"

                foreach ($groupName in $groupNames)
                {
                    $groupEntry = $groupEntries.Children | Where-Object Path -like "WinNT://*$env:ComputerName/$groupName"
                    $groupEntry | Should Not Be $null
                }
            }
        }
        finally
        {
            Remove-User $testUserName1

            Remove-User $testUserName2

            if (Test-Path $configurationPath)
            {
                Remove-Item -Path $configurationPath -Recurse -Force
            }
        }
    }

    It "Remove a xGroupSet" {
        $configurationName = "CreateTestGroup"
        $configurationPath = Join-Path -Path (Get-Location) -ChildPath $configurationName

        $testUserName1 = "LocalTestUser1"
        $testUserName2 = "LocalTestUser2"
        $testUserPassword = "StrongOne7."
        $testUserDescription = "Test users for removing an xGroupSet"

        $secureTestPassword = ConvertTo-SecureString $testUserPassword -AsPlainText -Force

        $testCredential1 = New-Object PSCredential ($testUserName1, $secureTestPassword)
        $testCredential2 = New-Object PSCredential ($testUserName2, $secureTestPassword)

        New-User -Credential $testCredential1 -Description $testUserDescription
        New-User -Credential $testCredential2 -Description $testUserDescription

        $membersToExclude = @($testUserName1, $testUserName2)
        $groupNames = @("TestGroupName123", "TestGroupName456")

        # Create test groups
        foreach($groupName in $groupNames)
        {
            New-Group -GroupName $groupName -Description $testUserDescription
        }

        try
        {
            Configuration $configurationName
            {
                Import-DscResource -ModuleName xPSDesiredStateConfiguration

                xGroupSet GroupSet1
                {
                    GroupName = $groupNames
                    Ensure = "Absent"
                    MembersToExclude = $membersToExclude
                }
            }

            & $configurationName -OutputPath $configurationPath
            Start-DscConfiguration -Path $configurationPath -Wait -Force -Verbose

            if (Test-IsNanoServer)
            {
                foreach ($groupName in $groupNames)
                {
                    $localGroup = Get-LocalGroup -Name $groupName -ErrorAction Ignore
                    $localGroup | Should Be $null
                }
            }
            else
            {
                $groupEntries = [ADSI] "WinNT://$env:ComputerName"
                foreach($groupName in $groupNames)
                {
                    $groupEntry = $groupEntries.Children | Where-Object Path -like "WinNT://*$env:ComputerName/$groupName"
                    $groupEntry | Should Be $null
                }
            }
        }
        finally
        {
            Remove-User $testUserName1

            Remove-User $testUserName2

            if (Test-Path $configurationPath)
            {
                Remove-Item -Path $configurationPath -Recurse -Force
            }
        }
    }
}