|
| 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 'Test-SqlDscIsDatabasePrincipal' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') { |
| 33 | + BeforeAll { |
| 34 | + $script:mockInstanceName = 'DSCSQLTEST' |
| 35 | + |
| 36 | + $mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception. |
| 37 | + $mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force |
| 38 | + |
| 39 | + $script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword) |
| 40 | + |
| 41 | + $script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential |
| 42 | + |
| 43 | + # Use a test database that should exist - we'll create it if it doesn't exist |
| 44 | + $script:testDatabaseName = 'IntegrationTestDatabase' |
| 45 | + |
| 46 | + # Create test database if it doesn't exist |
| 47 | + if (-not $script:serverObject.Databases[$script:testDatabaseName]) |
| 48 | + { |
| 49 | + $null = New-SqlDscDatabase -ServerObject $script:serverObject -Name $script:testDatabaseName -Force -ErrorAction 'Stop' |
| 50 | + } |
| 51 | + |
| 52 | + # Test principals that should exist in the database |
| 53 | + $script:testUserName = 'IntegrationTestUser' |
| 54 | + $script:testRoleName = 'IntegrationTestRole' |
| 55 | + $script:testAppRoleName = 'IntegrationTestAppRole' |
| 56 | + |
| 57 | + # Create test database user if it doesn't exist |
| 58 | + $testDatabase = $script:serverObject.Databases[$script:testDatabaseName] |
| 59 | + if (-not $testDatabase.Users[$script:testUserName]) |
| 60 | + { |
| 61 | + $sqlCommand = "USE [$script:testDatabaseName]; CREATE USER [$script:testUserName] WITHOUT LOGIN;" |
| 62 | + $null = $script:serverObject.ConnectionContext.ExecuteNonQuery($sqlCommand) |
| 63 | + } |
| 64 | + |
| 65 | + # Create test database role if it doesn't exist |
| 66 | + if (-not $testDatabase.Roles[$script:testRoleName]) |
| 67 | + { |
| 68 | + $sqlCommand = "USE [$script:testDatabaseName]; CREATE ROLE [$script:testRoleName];" |
| 69 | + $null = $script:serverObject.ConnectionContext.ExecuteNonQuery($sqlCommand) |
| 70 | + } |
| 71 | + |
| 72 | + # Create test application role if it doesn't exist |
| 73 | + if (-not $testDatabase.ApplicationRoles[$script:testAppRoleName]) |
| 74 | + { |
| 75 | + $sqlCommand = "USE [$script:testDatabaseName]; CREATE APPLICATION ROLE [$script:testAppRoleName] WITH PASSWORD = 'TestPassword123';" |
| 76 | + $null = $script:serverObject.ConnectionContext.ExecuteNonQuery($sqlCommand) |
| 77 | + } |
| 78 | + |
| 79 | + # Refresh database objects to ensure they are loaded |
| 80 | + $null = $testDatabase.Users.Refresh() |
| 81 | + $null = $testDatabase.Roles.Refresh() |
| 82 | + $null = $testDatabase.ApplicationRoles.Refresh() |
| 83 | + } |
| 84 | + |
| 85 | + AfterAll { |
| 86 | + Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject |
| 87 | + } |
| 88 | + |
| 89 | + Context 'When testing database user existence' { |
| 90 | + It 'Should return True when database user exists' { |
| 91 | + $result = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testUserName |
| 92 | + |
| 93 | + $result | Should -BeOfType [System.Boolean] |
| 94 | + $result | Should -BeTrue |
| 95 | + } |
| 96 | + |
| 97 | + It 'Should return False when database user does not exist' { |
| 98 | + $result = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name 'NonExistentUser' |
| 99 | + |
| 100 | + $result | Should -BeOfType [System.Boolean] |
| 101 | + $result | Should -BeFalse |
| 102 | + } |
| 103 | + |
| 104 | + It 'Should return False when database user exists but ExcludeUsers is specified' { |
| 105 | + $result = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testUserName -ExcludeUsers |
| 106 | + |
| 107 | + $result | Should -BeOfType [System.Boolean] |
| 108 | + $result | Should -BeFalse |
| 109 | + } |
| 110 | + |
| 111 | + It 'Should return True for dbo user' { |
| 112 | + $result = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name 'dbo' |
| 113 | + |
| 114 | + $result | Should -BeOfType [System.Boolean] |
| 115 | + $result | Should -BeTrue |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + Context 'When testing database role existence' { |
| 120 | + It 'Should return True when database role exists' { |
| 121 | + $result = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testRoleName |
| 122 | + |
| 123 | + $result | Should -BeOfType [System.Boolean] |
| 124 | + $result | Should -BeTrue |
| 125 | + } |
| 126 | + |
| 127 | + It 'Should return True when fixed role exists' { |
| 128 | + # Test with built-in db_datareader role |
| 129 | + $result = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name 'db_datareader' |
| 130 | + |
| 131 | + $result | Should -BeOfType [System.Boolean] |
| 132 | + $result | Should -BeTrue |
| 133 | + } |
| 134 | + |
| 135 | + It 'Should return False when fixed role exists but ExcludeFixedRoles is specified' { |
| 136 | + $result = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name 'db_datareader' -ExcludeFixedRoles |
| 137 | + |
| 138 | + $result | Should -BeOfType [System.Boolean] |
| 139 | + $result | Should -BeFalse |
| 140 | + } |
| 141 | + |
| 142 | + It 'Should return False when database role exists but ExcludeRoles is specified' { |
| 143 | + $result = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testRoleName -ExcludeRoles |
| 144 | + |
| 145 | + $result | Should -BeOfType [System.Boolean] |
| 146 | + $result | Should -BeFalse |
| 147 | + } |
| 148 | + |
| 149 | + It 'Should return True for user-defined role when ExcludeFixedRoles is specified' { |
| 150 | + $result = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testRoleName -ExcludeFixedRoles |
| 151 | + |
| 152 | + $result | Should -BeOfType [System.Boolean] |
| 153 | + $result | Should -BeTrue |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + Context 'When testing application role existence' { |
| 158 | + It 'Should return True when application role exists' { |
| 159 | + $result = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testAppRoleName |
| 160 | + |
| 161 | + $result | Should -BeOfType [System.Boolean] |
| 162 | + $result | Should -BeTrue |
| 163 | + } |
| 164 | + |
| 165 | + It 'Should return False when application role exists but ExcludeApplicationRoles is specified' { |
| 166 | + $result = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testAppRoleName -ExcludeApplicationRoles |
| 167 | + |
| 168 | + $result | Should -BeOfType [System.Boolean] |
| 169 | + $result | Should -BeFalse |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + Context 'When testing multiple exclusion parameters' { |
| 174 | + It 'Should return False when principal exists but all types are excluded' { |
| 175 | + $result = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testUserName -ExcludeUsers -ExcludeRoles -ExcludeApplicationRoles |
| 176 | + |
| 177 | + $result | Should -BeOfType [System.Boolean] |
| 178 | + $result | Should -BeFalse |
| 179 | + } |
| 180 | + |
| 181 | + It 'Should work with combination of exclusion parameters' { |
| 182 | + # Test role when users and app roles are excluded |
| 183 | + $result = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testRoleName -ExcludeUsers -ExcludeApplicationRoles |
| 184 | + |
| 185 | + $result | Should -BeOfType [System.Boolean] |
| 186 | + $result | Should -BeTrue |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + Context 'When testing pipeline parameter support' { |
| 191 | + It 'Should accept ServerObject from pipeline' { |
| 192 | + $result = $script:serverObject | Test-SqlDscIsDatabasePrincipal -DatabaseName $script:testDatabaseName -Name $script:testUserName |
| 193 | + |
| 194 | + $result | Should -BeOfType [System.Boolean] |
| 195 | + $result | Should -BeTrue |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + Context 'When testing error conditions' { |
| 200 | + It 'Should throw when database does not exist' { |
| 201 | + { Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName 'NonExistentDatabase' -Name 'SomePrincipal' -ErrorAction 'Stop' } | Should -Throw |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + Context 'When testing case sensitivity' { |
| 206 | + It 'Should handle case differences correctly for database names' { |
| 207 | + # Database names are case-insensitive in SQL Server |
| 208 | + $result1 = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName.ToUpper() -Name $script:testUserName |
| 209 | + $result2 = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName.ToLower() -Name $script:testUserName |
| 210 | + |
| 211 | + $result1 | Should -BeOfType [System.Boolean] |
| 212 | + $result2 | Should -BeOfType [System.Boolean] |
| 213 | + $result1 | Should -Be $result2 |
| 214 | + } |
| 215 | + |
| 216 | + It 'Should handle case differences correctly for principal names' { |
| 217 | + # Principal names are case-insensitive in SQL Server |
| 218 | + $result1 = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testUserName.ToUpper() |
| 219 | + $result2 = Test-SqlDscIsDatabasePrincipal -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testUserName.ToLower() |
| 220 | + |
| 221 | + $result1 | Should -BeOfType [System.Boolean] |
| 222 | + $result2 | Should -BeOfType [System.Boolean] |
| 223 | + $result1 | Should -Be $result2 |
| 224 | + } |
| 225 | + } |
| 226 | +} |
0 commit comments