Skip to content

Commit af4a70a

Browse files
authored
Add integration test for ConvertTo-SqlDscDatabasePermission command (#2271)
1 parent c25588e commit af4a70a

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Added integration tests for `Remove-SqlDscAudit` command to ensure it functions
1414
correctly in real environments
1515
[issue #2241](https://github.com/dsccommunity/SqlServerDsc/issues/2241).
16+
- Added integration tests for `ConvertTo-SqlDscDatabasePermission` command to
17+
ensure command reliability
18+
[issue #2209](https://github.com/dsccommunity/SqlServerDsc/issues/2209).
1619
- Added integration test for `ConvertTo-SqlDscEditionName` command to ensure
1720
command reliability in real environments
1821
[issue #2208](https://github.com/dsccommunity/SqlServerDsc/issues/2208).

azure-pipelines.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ stages:
321321
'tests/Integration/Commands/New-SqlDscDatabase.Integration.Tests.ps1'
322322
'tests/Integration/Commands/Set-SqlDscDatabase.Integration.Tests.ps1'
323323
'tests/Integration/Commands/Test-SqlDscDatabase.Integration.Tests.ps1'
324+
'tests/Integration/Commands/ConvertTo-SqlDscDatabasePermission.Integration.Tests.ps1'
324325
'tests/Integration/Commands/Get-SqlDscAgentAlert.Integration.Tests.ps1'
325326
'tests/Integration/Commands/New-SqlDscAgentAlert.Integration.Tests.ps1'
326327
'tests/Integration/Commands/Set-SqlDscAgentAlert.Integration.Tests.ps1'
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'Suppressing this rule because Script Analyzer does not understand Pester syntax.')]
2+
param ()
3+
4+
BeforeDiscovery {
5+
try
6+
{
7+
if (-not (Get-Module -Name 'DscResource.Test'))
8+
{
9+
# Assumes dependencies have been resolved, so if this module is not available, run 'noop' task.
10+
if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable))
11+
{
12+
# Redirect all streams to $null, except the error stream (stream 2)
13+
& "$PSScriptRoot/../../../build.ps1" -Tasks 'noop' 3>&1 4>&1 5>&1 6>&1 > $null
14+
}
15+
16+
# If the dependencies have not been resolved, this will throw an error.
17+
Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop'
18+
}
19+
}
20+
catch [System.IO.FileNotFoundException]
21+
{
22+
throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks noop" first.'
23+
}
24+
}
25+
26+
BeforeAll {
27+
$script:moduleName = 'SqlServerDsc'
28+
29+
Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop'
30+
}
31+
32+
Describe 'ConvertTo-SqlDscDatabasePermission' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
33+
BeforeAll {
34+
# Starting the named instance SQL Server service prior to running tests.
35+
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
36+
37+
$script:mockInstanceName = 'DSCSQLTEST'
38+
39+
$mockSqlAdministratorUserName = 'SqlAdmin'
40+
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
41+
42+
$script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword)
43+
44+
$script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential
45+
}
46+
47+
AfterAll {
48+
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject
49+
50+
# Stop the named instance SQL Server service to save memory on the build worker.
51+
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
52+
}
53+
54+
Context 'When converting empty collection of DatabasePermissionInfo' {
55+
It 'Should return empty array for empty DatabasePermissionInfo collection' {
56+
$emptyCollection = [Microsoft.SqlServer.Management.Smo.DatabasePermissionInfo[]] @()
57+
58+
$result = ConvertTo-SqlDscDatabasePermission -DatabasePermissionInfo $emptyCollection
59+
60+
$result | Should -BeNullOrEmpty
61+
}
62+
63+
It 'Should accept empty collection through pipeline' {
64+
$emptyCollection = [Microsoft.SqlServer.Management.Smo.DatabasePermissionInfo[]] @()
65+
66+
$result = $emptyCollection | ConvertTo-SqlDscDatabasePermission
67+
68+
$result | Should -BeNullOrEmpty
69+
}
70+
}
71+
72+
Context 'When converting DatabasePermissionInfo from system database' {
73+
It 'Should convert DatabasePermissionInfo to DatabasePermission objects for dbo principal' {
74+
# Get permissions for the dbo user from master database
75+
$databasePermissionInfo = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName 'master' -Name 'dbo' -ErrorAction 'Stop'
76+
77+
# Only proceed if we have permission data to work with
78+
if ($databasePermissionInfo) {
79+
$result = ConvertTo-SqlDscDatabasePermission -DatabasePermissionInfo $databasePermissionInfo
80+
81+
# Validate the result structure
82+
$result | Should -Not -BeNullOrEmpty
83+
84+
# Each result should have State and Permission properties
85+
foreach ($permission in $result) {
86+
$permission.State | Should -Not -BeNullOrEmpty
87+
$permission.Permission | Should -Not -BeNullOrEmpty
88+
89+
# Validate that permission state is one of the expected values
90+
$permission.State | Should -BeIn @('Grant', 'Deny', 'GrantWithGrant')
91+
}
92+
}
93+
}
94+
95+
It 'Should accept DatabasePermissionInfo through pipeline' {
96+
# Get permissions for the dbo user from master database
97+
$databasePermissionInfo = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName 'master' -Name 'dbo' -ErrorAction 'Stop'
98+
99+
# Only proceed if we have permission data to work with
100+
if ($databasePermissionInfo) {
101+
$result = $databasePermissionInfo | ConvertTo-SqlDscDatabasePermission
102+
103+
# Validate the result structure
104+
$result | Should -Not -BeNullOrEmpty
105+
106+
# Each result should have State and Permission properties
107+
foreach ($permission in $result) {
108+
$permission.State | Should -Not -BeNullOrEmpty
109+
$permission.Permission | Should -Not -BeNullOrEmpty
110+
111+
# Validate that permission state is one of the expected values
112+
$permission.State | Should -BeIn @('Grant', 'Deny', 'GrantWithGrant')
113+
}
114+
}
115+
}
116+
}
117+
118+
Context 'When converting DatabasePermissionInfo for guest user' {
119+
It 'Should handle guest user permissions correctly' {
120+
# Get permissions for the guest user from master database (guest should have Connect permission)
121+
$databasePermissionInfo = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName 'master' -Name 'guest' -ErrorAction 'SilentlyContinue'
122+
123+
# Only proceed if we have permission data to work with
124+
if ($databasePermissionInfo) {
125+
$result = ConvertTo-SqlDscDatabasePermission -DatabasePermissionInfo $databasePermissionInfo
126+
127+
# Validate the result structure
128+
$result | Should -Not -BeNullOrEmpty
129+
130+
# Each result should have State and Permission properties
131+
foreach ($permission in $result) {
132+
$permission.State | Should -Not -BeNullOrEmpty
133+
$permission.Permission | Should -Not -BeNullOrEmpty
134+
135+
# Validate that permission state is one of the expected values
136+
$permission.State | Should -BeIn @('Grant', 'Deny', 'GrantWithGrant')
137+
}
138+
}
139+
}
140+
}
141+
142+
Context 'When testing conversion functionality with multiple permission states' {
143+
It 'Should group permissions by state correctly' {
144+
# Try to get permissions from system databases where we might have various permission states
145+
$databasePermissionInfo = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName 'msdb' -Name 'dbo' -ErrorAction 'SilentlyContinue'
146+
147+
# Only proceed if we have permission data to work with
148+
if ($databasePermissionInfo) {
149+
$result = ConvertTo-SqlDscDatabasePermission -DatabasePermissionInfo $databasePermissionInfo
150+
151+
if ($result) {
152+
# Validate that permissions are properly grouped by state
153+
$uniqueStates = $result.State | Sort-Object -Unique
154+
155+
foreach ($state in $uniqueStates) {
156+
$permissionsForState = $result | Where-Object { $_.State -eq $state }
157+
$permissionsForState | Should -HaveCount 1
158+
$permissionsForState[0].Permission | Should -Not -BeNullOrEmpty
159+
}
160+
}
161+
}
162+
}
163+
}
164+
}

tests/Integration/Commands/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Get-SqlDscDatabase | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTES
7373
New-SqlDscDatabase | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | Test databases
7474
Set-SqlDscDatabase | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
7575
Test-SqlDscDatabase | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
76+
ConvertTo-SqlDscDatabasePermission | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
7677
Get-SqlDscAgentAlert | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
7778
New-SqlDscAgentAlert | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | Test alerts
7879
Set-SqlDscAgentAlert | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -

0 commit comments

Comments
 (0)