-
Notifications
You must be signed in to change notification settings - Fork 227
Integration test missing for Test-SqlDscIsDatabasePrincipal #2260
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
8 commits
Select commit
Hold shift + click to select a range
56f7015
Initial plan
Copilot 5ca6633
Merge branch 'main' into copilot/fix-2231
johlju 46ddef7
Add integration test for Test-SqlDscIsDatabasePrincipal command
Copilot 3e33bc4
Fix integration test file ending and update README with dependency info
Copilot 525bc6f
Merge branch 'main' into copilot/fix-2231
johlju 7340e60
Merge branch 'main' into copilot/fix-2231
johlju 4d58349
Refactor integration tests for Test-SqlDscIsDatabasePrincipal by remo…
johlju 4f79f46
Add DropConnections parameter to Remove-SqlDscDatabase function
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
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
226 changes: 226 additions & 0 deletions
226
tests/Integration/Commands/Test-SqlDscIsDatabasePrincipal.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,226 @@ | ||
| [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 'Test-SqlDscIsDatabasePrincipal' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') { | ||
| BeforeAll { | ||
| $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 | ||
|
|
||
| # Use a test database that should exist - we'll create it if it doesn't exist | ||
| $script:testDatabaseName = 'IntegrationTestDatabase' | ||
|
|
||
| # Create test database if it doesn't exist | ||
| if (-not $script:serverObject.Databases[$script:testDatabaseName]) | ||
| { | ||
| $null = New-SqlDscDatabase -ServerObject $script:serverObject -Name $script:testDatabaseName -Force -ErrorAction 'Stop' | ||
| } | ||
|
|
||
| # Test principals that should exist in the database | ||
| $script:testUserName = 'IntegrationTestUser' | ||
| $script:testRoleName = 'IntegrationTestRole' | ||
| $script:testAppRoleName = 'IntegrationTestAppRole' | ||
|
|
||
| # Create test database user if it doesn't exist | ||
| $testDatabase = $script:serverObject.Databases[$script:testDatabaseName] | ||
| if (-not $testDatabase.Users[$script:testUserName]) | ||
| { | ||
| $sqlCommand = "USE [$script:testDatabaseName]; CREATE USER [$script:testUserName] WITHOUT LOGIN;" | ||
| $null = $script:serverObject.ConnectionContext.ExecuteNonQuery($sqlCommand) | ||
| } | ||
|
|
||
| # Create test database role if it doesn't exist | ||
| if (-not $testDatabase.Roles[$script:testRoleName]) | ||
| { | ||
| $sqlCommand = "USE [$script:testDatabaseName]; CREATE ROLE [$script:testRoleName];" | ||
| $null = $script:serverObject.ConnectionContext.ExecuteNonQuery($sqlCommand) | ||
| } | ||
|
|
||
| # Create test application role if it doesn't exist | ||
| if (-not $testDatabase.ApplicationRoles[$script:testAppRoleName]) | ||
| { | ||
| $sqlCommand = "USE [$script:testDatabaseName]; CREATE APPLICATION ROLE [$script:testAppRoleName] WITH PASSWORD = 'TestPassword123';" | ||
| $null = $script:serverObject.ConnectionContext.ExecuteNonQuery($sqlCommand) | ||
| } | ||
|
|
||
| # Refresh database objects to ensure they are loaded | ||
| $null = $testDatabase.Users.Refresh() | ||
| $null = $testDatabase.Roles.Refresh() | ||
| $null = $testDatabase.ApplicationRoles.Refresh() | ||
| } | ||
|
|
||
| AfterAll { | ||
| Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject | ||
| } | ||
|
|
||
| Context 'When testing database user existence' { | ||
| It 'Should return True when database user exists' { | ||
| $result = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testUserName | ||
|
|
||
| $result | Should -BeOfType [System.Boolean] | ||
| $result | Should -BeTrue | ||
| } | ||
|
|
||
| It 'Should return False when database user does not exist' { | ||
| $result = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name 'NonExistentUser' | ||
|
|
||
| $result | Should -BeOfType [System.Boolean] | ||
| $result | Should -BeFalse | ||
| } | ||
|
|
||
| It 'Should return False when database user exists but ExcludeUsers is specified' { | ||
| $result = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testUserName -ExcludeUsers | ||
|
|
||
| $result | Should -BeOfType [System.Boolean] | ||
| $result | Should -BeFalse | ||
| } | ||
|
|
||
| It 'Should return True for dbo user' { | ||
| $result = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name 'dbo' | ||
|
|
||
| $result | Should -BeOfType [System.Boolean] | ||
| $result | Should -BeTrue | ||
| } | ||
| } | ||
|
|
||
| Context 'When testing database role existence' { | ||
| It 'Should return True when database role exists' { | ||
| $result = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testRoleName | ||
|
|
||
| $result | Should -BeOfType [System.Boolean] | ||
| $result | Should -BeTrue | ||
| } | ||
|
|
||
| It 'Should return True when fixed role exists' { | ||
| # Test with built-in db_datareader role | ||
| $result = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name 'db_datareader' | ||
|
|
||
| $result | Should -BeOfType [System.Boolean] | ||
| $result | Should -BeTrue | ||
| } | ||
|
|
||
| It 'Should return False when fixed role exists but ExcludeFixedRoles is specified' { | ||
| $result = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name 'db_datareader' -ExcludeFixedRoles | ||
|
|
||
| $result | Should -BeOfType [System.Boolean] | ||
| $result | Should -BeFalse | ||
| } | ||
|
|
||
| It 'Should return False when database role exists but ExcludeRoles is specified' { | ||
| $result = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testRoleName -ExcludeRoles | ||
|
|
||
| $result | Should -BeOfType [System.Boolean] | ||
| $result | Should -BeFalse | ||
| } | ||
|
|
||
| It 'Should return True for user-defined role when ExcludeFixedRoles is specified' { | ||
| $result = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testRoleName -ExcludeFixedRoles | ||
|
|
||
| $result | Should -BeOfType [System.Boolean] | ||
| $result | Should -BeTrue | ||
| } | ||
| } | ||
|
|
||
| Context 'When testing application role existence' { | ||
| It 'Should return True when application role exists' { | ||
| $result = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testAppRoleName | ||
|
|
||
| $result | Should -BeOfType [System.Boolean] | ||
| $result | Should -BeTrue | ||
| } | ||
|
|
||
| It 'Should return False when application role exists but ExcludeApplicationRoles is specified' { | ||
| $result = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testAppRoleName -ExcludeApplicationRoles | ||
|
|
||
| $result | Should -BeOfType [System.Boolean] | ||
| $result | Should -BeFalse | ||
| } | ||
| } | ||
|
|
||
| Context 'When testing multiple exclusion parameters' { | ||
| It 'Should return False when principal exists but all types are excluded' { | ||
| $result = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testUserName -ExcludeUsers -ExcludeRoles -ExcludeApplicationRoles | ||
|
|
||
| $result | Should -BeOfType [System.Boolean] | ||
| $result | Should -BeFalse | ||
| } | ||
|
|
||
| It 'Should work with combination of exclusion parameters' { | ||
| # Test role when users and app roles are excluded | ||
| $result = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testRoleName -ExcludeUsers -ExcludeApplicationRoles | ||
|
|
||
| $result | Should -BeOfType [System.Boolean] | ||
| $result | Should -BeTrue | ||
| } | ||
| } | ||
|
|
||
| Context 'When testing pipeline parameter support' { | ||
| It 'Should accept ServerObject from pipeline' { | ||
| $result = $script:serverObject | Test-SqlDscIsDatabasePrincipal -DatabaseName $script:testDatabaseName -Name $script:testUserName | ||
|
|
||
| $result | Should -BeOfType [System.Boolean] | ||
| $result | Should -BeTrue | ||
| } | ||
| } | ||
|
|
||
| Context 'When testing error conditions' { | ||
| It 'Should throw when database does not exist' { | ||
| { Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName 'NonExistentDatabase' -Name 'SomePrincipal' -ErrorAction 'Stop' } | Should -Throw | ||
| } | ||
| } | ||
|
|
||
| Context 'When testing case sensitivity' { | ||
| It 'Should handle case differences correctly for database names' { | ||
| # Database names are case-insensitive in SQL Server | ||
| $result1 = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName.ToUpper() -Name $script:testUserName | ||
| $result2 = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName.ToLower() -Name $script:testUserName | ||
|
|
||
| $result1 | Should -BeOfType [System.Boolean] | ||
| $result2 | Should -BeOfType [System.Boolean] | ||
| $result1 | Should -Be $result2 | ||
| } | ||
|
|
||
| It 'Should handle case differences correctly for principal names' { | ||
| # Principal names are case-insensitive in SQL Server | ||
| $result1 = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testUserName.ToUpper() | ||
| $result2 = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testUserName.ToLower() | ||
|
|
||
| $result1 | Should -BeOfType [System.Boolean] | ||
| $result2 | Should -BeOfType [System.Boolean] | ||
| $result1 | Should -Be $result2 | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.