-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathConvertFrom-SqlDscDatabasePermission.Integration.Tests.ps1
More file actions
236 lines (197 loc) · 10.6 KB
/
ConvertFrom-SqlDscDatabasePermission.Integration.Tests.ps1
File metadata and controls
236 lines (197 loc) · 10.6 KB
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
227
228
229
230
231
232
233
234
235
236
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'Suppressing this rule because Script Analyzer does not understand Pester syntax.')]
param ()
BeforeDiscovery {
try
{
if (-not (Get-Module -Name 'DscResource.Test'))
{
# Assumes dependencies have been resolved, so if this module is not available, run 'noop' task.
if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable))
{
# Redirect all streams to $null, except the error stream (stream 2)
& "$PSScriptRoot/../../../build.ps1" -Tasks 'noop' 3>&1 4>&1 5>&1 6>&1 > $null
}
# If the dependencies have not been resolved, this will throw an error.
Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop'
}
}
catch [System.IO.FileNotFoundException]
{
throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks noop" first.'
}
}
BeforeAll {
$script:moduleName = 'SqlServerDsc'
Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop'
}
Describe 'ConvertFrom-SqlDscDatabasePermission' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
BeforeAll {
# Starting the named instance SQL Server service prior to running tests.
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
$script:mockInstanceName = 'DSCSQLTEST'
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
$script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword)
$script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential
}
AfterAll {
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject
# Stop the named instance SQL Server service to save memory on the build worker.
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
}
Context 'When converting DatabasePermission objects' {
Context 'When converting a single permission Grant state' {
It 'Should return a DatabasePermissionSet with correct permissions set' {
$databasePermission = & (Get-Module -Name $script:moduleName) {
[DatabasePermission] @{
State = 'Grant'
Permission = @('Connect', 'Select')
}
}
$result = ConvertFrom-SqlDscDatabasePermission -Permission $databasePermission -ErrorAction 'Stop'
$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.DatabasePermissionSet]
$result.Connect | Should -BeTrue
$result.Select | Should -BeTrue
$result.Update | Should -BeFalse
$result.Insert | Should -BeFalse
$result.Delete | Should -BeFalse
}
}
Context 'When converting multiple permissions' {
It 'Should return a DatabasePermissionSet with all specified permissions set' {
$databasePermission = & (Get-Module -Name $script:moduleName) {
[DatabasePermission] @{
State = 'Grant'
Permission = @('Connect', 'Select', 'Update', 'Insert')
}
}
$result = ConvertFrom-SqlDscDatabasePermission -Permission $databasePermission -ErrorAction 'Stop'
$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.DatabasePermissionSet]
$result.Connect | Should -BeTrue
$result.Select | Should -BeTrue
$result.Update | Should -BeTrue
$result.Insert | Should -BeTrue
$result.Delete | Should -BeFalse
$result.Alter | Should -BeFalse
}
}
Context 'When converting permission with GrantWithGrant state' {
It 'Should return a DatabasePermissionSet with correct permissions set regardless of state' {
$databasePermission = & (Get-Module -Name $script:moduleName) {
[DatabasePermission] @{
State = 'GrantWithGrant'
Permission = @('Alter', 'CreateTable')
}
}
$result = ConvertFrom-SqlDscDatabasePermission -Permission $databasePermission -ErrorAction 'Stop'
$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.DatabasePermissionSet]
$result.Alter | Should -BeTrue
$result.CreateTable | Should -BeTrue
$result.Connect | Should -BeFalse
$result.Select | Should -BeFalse
}
}
Context 'When converting permission with Deny state' {
It 'Should return a DatabasePermissionSet with correct permissions set regardless of state' {
$databasePermission = & (Get-Module -Name $script:moduleName) {
[DatabasePermission] @{
State = 'Deny'
Permission = @('Delete', 'Execute')
}
}
$result = ConvertFrom-SqlDscDatabasePermission -Permission $databasePermission -ErrorAction 'Stop'
$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.DatabasePermissionSet]
$result.Delete | Should -BeTrue
$result.Execute | Should -BeTrue
$result.Connect | Should -BeFalse
$result.Select | Should -BeFalse
}
}
Context 'When using pipeline input' {
It 'Should accept DatabasePermission objects from the pipeline' {
$databasePermission = & (Get-Module -Name $script:moduleName) {
[DatabasePermission] @{
State = 'Grant'
Permission = @('Connect', 'ViewDefinition')
}
}
$result = $databasePermission | ConvertFrom-SqlDscDatabasePermission -ErrorAction 'Stop'
$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.DatabasePermissionSet]
$result.Connect | Should -BeTrue
$result.ViewDefinition | Should -BeTrue
$result.Select | Should -BeFalse
}
}
Context 'When processing multiple DatabasePermission objects through pipeline' {
It 'Should process each permission object and return multiple DatabasePermissionSet objects' {
$databasePermissions = & (Get-Module -Name $script:moduleName) {
@(
[DatabasePermission] @{
State = 'Grant'
Permission = @('Connect')
},
[DatabasePermission] @{
State = 'Grant'
Permission = @('Select', 'Update')
}
)
}
$results = $databasePermissions | ConvertFrom-SqlDscDatabasePermission -ErrorAction 'Stop'
$results | Should -Not -BeNullOrEmpty
$results.Count | Should -Be 2
$results[0] | Should -BeOfType [Microsoft.SqlServer.Management.Smo.DatabasePermissionSet]
$results[1] | Should -BeOfType [Microsoft.SqlServer.Management.Smo.DatabasePermissionSet]
# First permission set should have Connect
$results[0].Connect | Should -BeTrue
$results[0].Select | Should -BeFalse
# Second permission set should have Select and Update
$results[1].Connect | Should -BeFalse
$results[1].Select | Should -BeTrue
$results[1].Update | Should -BeTrue
}
}
Context 'When converting with empty permission array' {
It 'Should return a DatabasePermissionSet with no permissions set' {
$databasePermission = & (Get-Module -Name $script:moduleName) {
[DatabasePermission] @{
State = 'Grant'
Permission = @()
}
}
$result = ConvertFrom-SqlDscDatabasePermission -Permission $databasePermission -ErrorAction 'Stop'
$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.DatabasePermissionSet]
$result.Connect | Should -BeFalse
$result.Select | Should -BeFalse
$result.Update | Should -BeFalse
$result.Insert | Should -BeFalse
$result.Delete | Should -BeFalse
}
}
Context 'When verifying SMO object compatibility' {
It 'Should return a DatabasePermissionSet that can be used with SMO database operations' {
$databasePermission = & (Get-Module -Name $script:moduleName) {
[DatabasePermission] @{
State = 'Grant'
Permission = @('Connect', 'Select')
}
}
$result = ConvertFrom-SqlDscDatabasePermission -Permission $databasePermission -ErrorAction 'Stop'
# Verify the result has the expected SMO properties
$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.DatabasePermissionSet]
# Verify it has the correct SMO properties and methods available
$result | Get-Member -Name 'Connect' -MemberType Property | Should -Not -BeNullOrEmpty
$result | Get-Member -Name 'Select' -MemberType Property | Should -Not -BeNullOrEmpty
$result | Get-Member -Name 'ToString' -MemberType Method | Should -Not -BeNullOrEmpty
# Verify ToString() method works
$result.ToString() | Should -Not -BeNullOrEmpty
}
}
}
}