Skip to content

Commit 7086cc0

Browse files
committed
Enhance integration tests for Restore-SqlDscDatabase by adding low-privilege user access verification and ensuring error handling for point-in-time capture
1 parent d2afa93 commit 7086cc0

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

tests/Integration/Commands/Restore-SqlDscDatabase.Integration.Tests.ps1

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ INSERT INTO dbo.TestData (Id, InsertTime, Value) VALUES (1, GETDATE(), 'Initial'
651651
Start-Sleep -Seconds 2
652652

653653
# Capture the point-in-time before adding more data
654-
$script:pointInTime = Get-SqlDscDateTime -ServerObject $script:serverObject
654+
$script:pointInTime = Get-SqlDscDateTime -ServerObject $script:serverObject -ErrorAction 'Stop'
655655

656656
# Wait another moment
657657
Start-Sleep -Seconds 2
@@ -972,16 +972,62 @@ WITH NOINIT, NOSKIP, REWIND, NOUNLOAD, STATS = 10;
972972
$restoredDb.UserAccess | Should -Be ([Microsoft.SqlServer.Management.Smo.DatabaseUserAccess]::Restricted) -Because 'Database should be in restricted user access mode'
973973
}
974974

975-
It 'Should verify restricted access by attempting connection with non-privileged user' {
975+
It 'Should verify sysadmin can access restricted database' {
976976
# Verify the database exists and is in restricted mode
977977
$restoredDb = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $script:restrictedUserDbName -Refresh -ErrorAction 'Stop'
978978
$restoredDb.UserAccess | Should -Be ([Microsoft.SqlServer.Management.Smo.DatabaseUserAccess]::Restricted)
979979

980-
# Verify that only members of db_owner, dbcreator, or sysadmin can access
981-
# Since we're using SqlAdmin credentials (which has sysadmin), we should be able to query
980+
# Verify that sysadmin can access the restricted database
982981
$query = "SELECT name FROM sys.databases WHERE name = N'$($script:restrictedUserDbName)';"
983982
$result = Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName 'master' -Query $query -PassThru -Force -ErrorAction 'Stop'
984983
$result.Tables[0].Rows.Count | Should -Be 1 -Because 'Sysadmin should be able to see the restricted database'
985984
}
985+
986+
Context 'When verifying restricted database access with low-privilege user' {
987+
BeforeAll {
988+
# Create a temporary low-privilege login
989+
$script:lowPrivLoginName = 'SqlDscLowPriv_' + (Get-Random)
990+
$script:lowPrivPassword = ConvertTo-SecureString -String 'TempP@ss' + (Get-Random) -AsPlainText -Force
991+
$script:lowPrivCredential = [System.Management.Automation.PSCredential]::new($script:lowPrivLoginName, $script:lowPrivPassword)
992+
993+
# Create the login
994+
$script:lowPrivLoginObject = New-SqlDscLogin -ServerObject $script:serverObject -Name $script:lowPrivLoginName -LoginType 'SqlLogin' -SecureString $script:lowPrivPassword -PassThru -Force -ErrorAction 'Stop'
995+
996+
# Grant VIEW ANY DATABASE permission
997+
$null = Grant-SqlDscServerPermission -Login $script:lowPrivLoginObject -Permission ViewAnyDatabase -Force -ErrorAction 'Stop'
998+
}
999+
1000+
BeforeEach {
1001+
# Connect with low-privilege credentials
1002+
$script:lowPrivServerObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:lowPrivCredential -ErrorAction 'Stop'
1003+
}
1004+
1005+
AfterEach {
1006+
# Disconnect the low-privilege connection
1007+
if ($script:lowPrivServerObject)
1008+
{
1009+
Disconnect-SqlDscDatabaseEngine -ServerObject $script:lowPrivServerObject
1010+
}
1011+
}
1012+
1013+
AfterAll {
1014+
# Clean up the temporary login
1015+
$loginObject = Get-SqlDscLogin -ServerObject $script:serverObject -Name $script:lowPrivLoginName -ErrorAction 'SilentlyContinue'
1016+
1017+
if ($loginObject)
1018+
{
1019+
$null = Remove-SqlDscLogin -LoginObject $loginObject -Force -ErrorAction 'SilentlyContinue'
1020+
}
1021+
}
1022+
1023+
It 'Should verify low-privilege user cannot access restricted database' {
1024+
# Attempt to access the restricted database
1025+
$query = "SELECT name FROM sys.databases WHERE name = N'$($script:restrictedUserDbName)';"
1026+
$result = Invoke-SqlDscQuery -ServerObject $script:lowPrivServerObject -DatabaseName 'master' -Query $query -PassThru -Force -ErrorAction 'Stop'
1027+
1028+
# The database should not be visible to low-privilege user
1029+
$result.Tables[0].Rows.Count | Should -Be 0 -Because 'Low-privilege user should not see the restricted database'
1030+
}
1031+
}
9861032
}
9871033
}

0 commit comments

Comments
 (0)