-
Notifications
You must be signed in to change notification settings - Fork 227
Add integration test for Set-SqlDscDatabasePermission #2266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e5f81ef
Initial plan
Copilot 24542dd
Merge branch 'main' into copilot/fix-2235
johlju 12f7dda
Add integration test for Set-SqlDscDatabasePermission command
Copilot 19d01c0
Fix integration test issues: add DatabaseName parameter, update READM…
Copilot 183b15f
Merge branch 'main' into copilot/fix-2235
johlju 815c615
Merge branch 'main' into copilot/fix-2235
johlju adb2e94
Add -Force parameter to all Invoke-SqlDscQuery calls in integration test
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
274 changes: 274 additions & 0 deletions
274
tests/Integration/Commands/Set-SqlDscDatabasePermission.Integration.Tests.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,274 @@ | ||
| [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 'Set-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 -ErrorAction 'Stop' | ||
|
|
||
| # Use existing persistent test database from New-SqlDscDatabase integration tests | ||
| $script:testDatabaseName = 'SqlDscIntegrationTestDatabase' | ||
|
|
||
| # Use existing persistent principals created by earlier integration tests | ||
| $script:testLoginName = 'IntegrationTestSqlLogin' | ||
| $script:testRoleName = 'SqlDscIntegrationTestRole_Persistent' | ||
|
|
||
| # Ensure the test database exists | ||
| $existingDatabase = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $script:testDatabaseName -ErrorAction 'SilentlyContinue' | ||
|
|
||
| if (-not $existingDatabase) | ||
| { | ||
| $null = New-SqlDscDatabase -ServerObject $script:serverObject -Name $script:testDatabaseName -Force -ErrorAction 'Stop' | ||
| } | ||
|
|
||
| # Create a database user for the existing login for testing database permissions | ||
| $sqlQuery = @" | ||
| USE [$script:testDatabaseName]; | ||
| IF NOT EXISTS (SELECT * FROM sys.database_principals WHERE name = '$script:testLoginName') | ||
| BEGIN | ||
| CREATE USER [$script:testLoginName] FOR LOGIN [$script:testLoginName]; | ||
| END | ||
| "@ | ||
| $null = Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query $sqlQuery -Force -ErrorAction 'Stop' | ||
| } | ||
|
|
||
| AfterAll { | ||
| # Clean up test database user but leave database intact for other tests | ||
| $sqlQuery = @" | ||
| USE [$script:testDatabaseName]; | ||
| IF EXISTS (SELECT * FROM sys.database_principals WHERE name = '$script:testLoginName') | ||
| BEGIN | ||
| DROP USER [$script:testLoginName]; | ||
| END | ||
| "@ | ||
| $null = Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query $sqlQuery -Force -ErrorAction 'SilentlyContinue' | ||
|
|
||
| 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 setting database permissions with Grant state' { | ||
| BeforeEach { | ||
| # Revoke any existing permissions to start with clean state | ||
| $permissionSet = [Microsoft.SqlServer.Management.Smo.DatabasePermissionSet] @{ | ||
| Connect = $true | ||
| } | ||
| $null = Set-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testLoginName -State 'Revoke' -Permission $permissionSet -Force -ErrorAction 'SilentlyContinue' | ||
|
|
||
| $permissionSet = [Microsoft.SqlServer.Management.Smo.DatabasePermissionSet] @{ | ||
| Select = $true | ||
| } | ||
| $null = Set-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testLoginName -State 'Revoke' -Permission $permissionSet -Force -ErrorAction 'SilentlyContinue' | ||
| } | ||
|
|
||
| It 'Should grant Connect permission successfully' { | ||
| $permissionSet = [Microsoft.SqlServer.Management.Smo.DatabasePermissionSet] @{ | ||
| Connect = $true | ||
| } | ||
|
|
||
| $null = Set-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testLoginName -State 'Grant' -Permission $permissionSet -Force -ErrorAction 'Stop' | ||
|
|
||
| # Verify the permission was granted | ||
| $permissions = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testLoginName -ErrorAction 'Stop' | ||
|
|
||
| $permissions | Should -Not -BeNullOrEmpty | ||
| $grantedPermission = $permissions | Where-Object { $_.PermissionState -eq 'Grant' -and $_.PermissionType.Connect -eq $true } | ||
| $grantedPermission | Should -Not -BeNullOrEmpty | ||
| $grantedPermission.PermissionType.Connect | Should -BeTrue | ||
| } | ||
|
|
||
| It 'Should grant multiple permissions successfully' { | ||
| $permissionSet = [Microsoft.SqlServer.Management.Smo.DatabasePermissionSet] @{ | ||
| Connect = $true | ||
| Select = $true | ||
| } | ||
|
|
||
| $null = Set-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testLoginName -State 'Grant' -Permission $permissionSet -Force -ErrorAction 'Stop' | ||
|
|
||
| # Verify the permissions were granted | ||
| $permissions = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testLoginName -ErrorAction 'Stop' | ||
|
|
||
| $permissions | Should -Not -BeNullOrEmpty | ||
|
|
||
| $connectPermission = $permissions | Where-Object { $_.PermissionState -eq 'Grant' -and $_.PermissionType.Connect -eq $true } | ||
| $connectPermission | Should -Not -BeNullOrEmpty | ||
| $connectPermission.PermissionType.Connect | Should -BeTrue | ||
|
|
||
| $selectPermission = $permissions | Where-Object { $_.PermissionState -eq 'Grant' -and $_.PermissionType.Select -eq $true } | ||
| $selectPermission | Should -Not -BeNullOrEmpty | ||
| $selectPermission.PermissionType.Select | Should -BeTrue | ||
| } | ||
|
|
||
| It 'Should grant permissions with WithGrant option' { | ||
| $permissionSet = [Microsoft.SqlServer.Management.Smo.DatabasePermissionSet] @{ | ||
| Select = $true | ||
| } | ||
|
|
||
| $null = Set-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testLoginName -State 'Grant' -Permission $permissionSet -WithGrant -Force -ErrorAction 'Stop' | ||
|
|
||
| # Verify the permission was granted with grant option | ||
| $permissions = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testLoginName -ErrorAction 'Stop' | ||
|
|
||
| $permissions | Should -Not -BeNullOrEmpty | ||
| $grantedPermission = $permissions | Where-Object { $_.PermissionState -eq 'GrantWithGrant' -and $_.PermissionType.Select -eq $true } | ||
| $grantedPermission | Should -Not -BeNullOrEmpty | ||
| $grantedPermission.PermissionType.Select | Should -BeTrue | ||
| } | ||
| } | ||
|
|
||
| Context 'When setting database permissions with Deny state' { | ||
| BeforeEach { | ||
| # Start with clean state | ||
| $permissionSet = [Microsoft.SqlServer.Management.Smo.DatabasePermissionSet] @{ | ||
| Update = $true | ||
| } | ||
| $null = Set-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testLoginName -State 'Revoke' -Permission $permissionSet -Force -ErrorAction 'SilentlyContinue' | ||
| } | ||
|
|
||
| It 'Should deny Update permission successfully' { | ||
| $permissionSet = [Microsoft.SqlServer.Management.Smo.DatabasePermissionSet] @{ | ||
| Update = $true | ||
| } | ||
|
|
||
| $null = Set-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testLoginName -State 'Deny' -Permission $permissionSet -Force -ErrorAction 'Stop' | ||
|
|
||
| # Verify the permission was denied | ||
| $permissions = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testLoginName -ErrorAction 'Stop' | ||
|
|
||
| $permissions | Should -Not -BeNullOrEmpty | ||
| $deniedPermission = $permissions | Where-Object { $_.PermissionState -eq 'Deny' -and $_.PermissionType.Update -eq $true } | ||
| $deniedPermission | Should -Not -BeNullOrEmpty | ||
| $deniedPermission.PermissionType.Update | Should -BeTrue | ||
| } | ||
| } | ||
|
|
||
| Context 'When setting database permissions with Revoke state' { | ||
| BeforeEach { | ||
| # Grant a permission first to then revoke it | ||
| $permissionSet = [Microsoft.SqlServer.Management.Smo.DatabasePermissionSet] @{ | ||
| Insert = $true | ||
| } | ||
| $null = Set-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testLoginName -State 'Grant' -Permission $permissionSet -Force -ErrorAction 'Stop' | ||
| } | ||
|
|
||
| It 'Should revoke Insert permission successfully' { | ||
| $permissionSet = [Microsoft.SqlServer.Management.Smo.DatabasePermissionSet] @{ | ||
| Insert = $true | ||
| } | ||
|
|
||
| $null = Set-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testLoginName -State 'Revoke' -Permission $permissionSet -Force -ErrorAction 'Stop' | ||
|
|
||
| # Verify the permission was revoked (should not appear in granted permissions) | ||
| $permissions = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testLoginName -ErrorAction 'Stop' | ||
|
|
||
| if ($permissions) | ||
| { | ||
| $insertPermission = $permissions | Where-Object { $_.PermissionType.Insert -eq $true } | ||
| $insertPermission | Should -BeNullOrEmpty | ||
| } | ||
| } | ||
|
|
||
| It 'Should revoke permissions with WithGrant option to cascade revocation' { | ||
| # First grant with grant option | ||
| $permissionSet = [Microsoft.SqlServer.Management.Smo.DatabasePermissionSet] @{ | ||
| Delete = $true | ||
| } | ||
| $null = Set-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testLoginName -State 'Grant' -Permission $permissionSet -WithGrant -Force -ErrorAction 'Stop' | ||
|
|
||
| # Then revoke with WithGrant to cascade | ||
| $null = Set-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testLoginName -State 'Revoke' -Permission $permissionSet -WithGrant -Force -ErrorAction 'Stop' | ||
|
|
||
| # Verify the permission was revoked (should not appear in permissions) | ||
| $permissions = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testLoginName -ErrorAction 'Stop' | ||
|
|
||
| if ($permissions) | ||
| { | ||
| $deletePermission = $permissions | Where-Object { $_.PermissionType.Delete -eq $true } | ||
| $deletePermission | Should -BeNullOrEmpty | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Context 'When setting database permissions through pipeline' { | ||
| BeforeEach { | ||
| # Clean state | ||
| $permissionSet = [Microsoft.SqlServer.Management.Smo.DatabasePermissionSet] @{ | ||
| Execute = $true | ||
| } | ||
| $null = Set-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testLoginName -State 'Revoke' -Permission $permissionSet -Force -ErrorAction 'SilentlyContinue' | ||
| } | ||
|
|
||
| It 'Should accept ServerObject from pipeline' { | ||
| $permissionSet = [Microsoft.SqlServer.Management.Smo.DatabasePermissionSet] @{ | ||
| Execute = $true | ||
| } | ||
|
|
||
| $null = $script:serverObject | Set-SqlDscDatabasePermission -DatabaseName $script:testDatabaseName -Name $script:testLoginName -State 'Grant' -Permission $permissionSet -Force -ErrorAction 'Stop' | ||
|
|
||
| # Verify the permission was granted | ||
| $permissions = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testLoginName -ErrorAction 'Stop' | ||
|
|
||
| $permissions | Should -Not -BeNullOrEmpty | ||
| $grantedPermission = $permissions | Where-Object { $_.PermissionState -eq 'Grant' -and $_.PermissionType.Execute -eq $true } | ||
| $grantedPermission | Should -Not -BeNullOrEmpty | ||
| $grantedPermission.PermissionType.Execute | Should -BeTrue | ||
| } | ||
| } | ||
|
|
||
| Context 'When setting database permissions with error conditions' { | ||
| It 'Should throw an error when database does not exist' { | ||
| $permissionSet = [Microsoft.SqlServer.Management.Smo.DatabasePermissionSet] @{ | ||
| Connect = $true | ||
| } | ||
|
|
||
| { Set-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName 'NonExistentDatabase' -Name $script:testLoginName -State 'Grant' -Permission $permissionSet -Force -ErrorAction 'Stop' } | Should -Throw -ExpectedMessage '*database*' | ||
| } | ||
|
|
||
| It 'Should throw an error when database principal does not exist' { | ||
| $permissionSet = [Microsoft.SqlServer.Management.Smo.DatabasePermissionSet] @{ | ||
| Connect = $true | ||
| } | ||
|
|
||
| { Set-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name 'NonExistentPrincipal' -State 'Grant' -Permission $permissionSet -Force -ErrorAction 'Stop' } | Should -Throw -ExpectedMessage '*principal*' | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.