-
Notifications
You must be signed in to change notification settings - Fork 227
Add integration test for ConvertFrom-SqlDscDatabasePermission command #2274
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
17 commits
Select commit
Hold shift + click to select a range
bd5dfa1
Initial plan
Copilot 4d6eaae
Add integration test for ConvertFrom-SqlDscDatabasePermission command
Copilot 2f7fef9
Update CHANGELOG.md for ConvertFrom-SqlDscDatabasePermission integrat…
Copilot d45b0b3
Merge branch 'main' into copilot/fix-2211
johlju 89d9a60
Fix DatabasePermission class access in integration test using module …
Copilot 9064e8c
Merge branch 'main' into copilot/fix-2211
johlju 34fac99
Fix integration test: change build to noop, add README entry, ensure …
Copilot 51e1811
Fix integration test expectation for multiple pipeline objects - comm…
Copilot fffcdde
Fix duplicate closing brace syntax error in integration test
Copilot 16c09d0
Merge branch 'main' into copilot/fix-2211
johlju 5c61594
Add validation steps for empty permission array test to debug object …
Copilot eb3ba87
Remove unnecessary service start/stop logic from ConvertFrom-SqlDscDa…
johlju 654bf91
Merge branch 'main' into copilot/fix-2211
johlju 096e6f4
Merge branch 'main' into copilot/fix-2211
johlju 356a0fa
Merge branch 'main' into copilot/fix-2211
johlju 488680d
Change empty permission array validation to use Should -BeNullOrEmpty…
Copilot 1a89b9a
Enhance documentation for ConvertFrom-SqlDscDatabasePermission and ad…
johlju 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
218 changes: 218 additions & 0 deletions
218
tests/Integration/Commands/ConvertFrom-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,218 @@ | ||
| [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') { | ||
| 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 combine them into single DatabasePermissionSet object' { | ||
| $databasePermissions = & (Get-Module -Name $script:moduleName) { | ||
| @( | ||
| [DatabasePermission] @{ | ||
| State = 'Grant' | ||
| Permission = @('Connect') | ||
| }, | ||
| [DatabasePermission] @{ | ||
| State = 'Grant' | ||
| Permission = @('Select', 'Update') | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| $result = $databasePermissions | ConvertFrom-SqlDscDatabasePermission -ErrorAction 'Stop' | ||
|
|
||
| # The command combines all permissions into a single DatabasePermissionSet | ||
| $result | Should -Not -BeNullOrEmpty | ||
| $result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.DatabasePermissionSet] | ||
|
|
||
| # All permissions from both objects should be set to true | ||
| $result.Connect | Should -BeTrue | ||
| $result.Select | Should -BeTrue | ||
| $result.Update | Should -BeTrue | ||
| $result.Insert | Should -BeFalse | ||
| $result.Delete | Should -BeFalse | ||
| } | ||
| } | ||
|
|
||
| # 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 = @() | ||
| # } | ||
| # } | ||
|
|
||
| # # Verify the DatabasePermission object was created successfully | ||
| # $databasePermission | Should -Not -BeNullOrEmpty | ||
| # $databasePermission.State | Should -Be 'Grant' | ||
| # $databasePermission.Permission | Should -HaveCount 0 | ||
|
|
||
| # $result = ConvertFrom-SqlDscDatabasePermission -Permission $databasePermission -ErrorAction 'Stop' | ||
|
|
||
| # # TODO: This fails with: "Expected a value, but got $null or empty", but the unit tests pass. | ||
| # $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 | ||
| } | ||
| } | ||
| } | ||
| } |
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
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.