|
| 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 'Get-SqlDscDatabasePermission' -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 | + # Create a test database for the integration tests |
| 44 | + $script:testDatabaseName = 'SqlDscDatabasePermissionTest_' + (Get-Random) |
| 45 | + $null = New-SqlDscDatabase -ServerObject $script:serverObject -Name $script:testDatabaseName -Force -ErrorAction 'Stop' |
| 46 | + |
| 47 | + # Create a test user in the database for permission testing |
| 48 | + $script:testUserName = 'SqlDscTestUser_' + (Get-Random) |
| 49 | + $testUserSql = "USE [$($script:testDatabaseName)]; CREATE USER [$($script:testUserName)] WITHOUT LOGIN;" |
| 50 | + Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query $testUserSql -Force -ErrorAction 'Stop' |
| 51 | + |
| 52 | + # Grant some permissions to the test user for testing |
| 53 | + $grantPermissionSql = "USE [$($script:testDatabaseName)]; GRANT CONNECT, SELECT TO [$($script:testUserName)];" |
| 54 | + Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query $grantPermissionSql -Force -ErrorAction 'Stop' |
| 55 | + } |
| 56 | + |
| 57 | + AfterAll { |
| 58 | + # Clean up test database (this will also remove the test user) |
| 59 | + $testDatabase = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $script:testDatabaseName -ErrorAction 'SilentlyContinue' |
| 60 | + if ($testDatabase) |
| 61 | + { |
| 62 | + $null = Remove-SqlDscDatabase -DatabaseObject $testDatabase -Force -ErrorAction 'Stop' |
| 63 | + } |
| 64 | + |
| 65 | + Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject |
| 66 | + } |
| 67 | + |
| 68 | + Context 'When connecting to SQL Server instance' { |
| 69 | + Context 'When getting permissions for valid database principals' { |
| 70 | + It 'Should return permissions for dbo user in master database' { |
| 71 | + $result = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName 'master' -Name 'dbo' |
| 72 | + |
| 73 | + $result | Should -Not -BeNullOrEmpty |
| 74 | + $result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.DatabasePermissionInfo] |
| 75 | + } |
| 76 | + |
| 77 | + It 'Should return permissions for dbo user in master database using pipeline' { |
| 78 | + $result = $script:serverObject | Get-SqlDscDatabasePermission -DatabaseName 'master' -Name 'dbo' |
| 79 | + |
| 80 | + $result | Should -Not -BeNullOrEmpty |
| 81 | + $result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.DatabasePermissionInfo] |
| 82 | + } |
| 83 | + |
| 84 | + It 'Should return permissions for public role in master database' { |
| 85 | + $result = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName 'master' -Name 'public' |
| 86 | + |
| 87 | + $result | Should -Not -BeNullOrEmpty |
| 88 | + $result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.DatabasePermissionInfo] |
| 89 | + } |
| 90 | + |
| 91 | + It 'Should return permissions for test user in test database' { |
| 92 | + $result = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testUserName |
| 93 | + |
| 94 | + $result | Should -Not -BeNullOrEmpty |
| 95 | + $result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.DatabasePermissionInfo] |
| 96 | + |
| 97 | + # Verify that the Connect and Select permissions we granted are present |
| 98 | + $connectPermission = $result | Where-Object { $_.PermissionType.Connect -eq $true } |
| 99 | + $selectPermission = $result | Where-Object { $_.PermissionType.Select -eq $true } |
| 100 | + |
| 101 | + $connectPermission | Should -Not -BeNullOrEmpty -Because 'Connect permission should have been granted to test user' |
| 102 | + $connectPermission.PermissionState | Should -Be 'Grant' |
| 103 | + |
| 104 | + $selectPermission | Should -Not -BeNullOrEmpty -Because 'Select permission should have been granted to test user' |
| 105 | + $selectPermission.PermissionState | Should -Be 'Grant' |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + Context 'When getting permissions for invalid principals' { |
| 110 | + It 'Should throw error for non-existent database with ErrorAction Stop' { |
| 111 | + { Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName 'NonExistentDatabase123' -Name 'dbo' -ErrorAction 'Stop' } | |
| 112 | + Should -Throw |
| 113 | + } |
| 114 | + |
| 115 | + It 'Should return null for non-existent database with ErrorAction SilentlyContinue' { |
| 116 | + $result = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName 'NonExistentDatabase123' -Name 'dbo' -ErrorAction 'SilentlyContinue' |
| 117 | + |
| 118 | + $result | Should -BeNullOrEmpty |
| 119 | + } |
| 120 | + |
| 121 | + It 'Should throw error for non-existent principal with ErrorAction Stop' { |
| 122 | + { Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName 'master' -Name 'NonExistentUser123' -ErrorAction 'Stop' } | |
| 123 | + Should -Throw |
| 124 | + } |
| 125 | + |
| 126 | + It 'Should return null for non-existent principal with ErrorAction SilentlyContinue' { |
| 127 | + $result = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName 'master' -Name 'NonExistentUser123' -ErrorAction 'SilentlyContinue' |
| 128 | + |
| 129 | + $result | Should -BeNullOrEmpty |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + Context 'When verifying permission properties' { |
| 134 | + BeforeAll { |
| 135 | + # Get permissions for a known principal that should have permissions |
| 136 | + $script:testPermissions = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:testUserName |
| 137 | + } |
| 138 | + |
| 139 | + It 'Should return DatabasePermissionInfo objects with PermissionState property' { |
| 140 | + $script:testPermissions | Should -Not -BeNullOrEmpty |
| 141 | + |
| 142 | + foreach ($permission in $script:testPermissions) { |
| 143 | + $permission.PermissionState | Should -BeIn @('Grant', 'Deny', 'GrantWithGrant') |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + It 'Should return DatabasePermissionInfo objects with PermissionType property' { |
| 148 | + $script:testPermissions | Should -Not -BeNullOrEmpty |
| 149 | + |
| 150 | + foreach ($permission in $script:testPermissions) { |
| 151 | + $permission.PermissionType | Should -Not -BeNullOrEmpty |
| 152 | + $permission.PermissionType | Should -BeOfType [Microsoft.SqlServer.Management.Smo.DatabasePermissionSet] |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + It 'Should return DatabasePermissionInfo objects with Grantee property' { |
| 157 | + $script:testPermissions | Should -Not -BeNullOrEmpty |
| 158 | + |
| 159 | + foreach ($permission in $script:testPermissions) { |
| 160 | + $permission.Grantee | Should -Be $script:testUserName |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + Context 'When working with built-in database roles' { |
| 166 | + It 'Should return permissions for db_datareader role' { |
| 167 | + # Note: The command excludes fixed roles by default, so this should return null or empty |
| 168 | + $result = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name 'db_datareader' -ErrorAction 'SilentlyContinue' |
| 169 | + |
| 170 | + # Fixed roles are excluded by default, so result should be null |
| 171 | + $result | Should -BeNullOrEmpty |
| 172 | + } |
| 173 | + |
| 174 | + It 'Should work with non-fixed database roles when they exist' { |
| 175 | + # Create a custom database role for testing |
| 176 | + $customRoleName = 'TestRole_' + (Get-Random) |
| 177 | + $createRoleSql = "USE [$($script:testDatabaseName)]; CREATE ROLE [$customRoleName];" |
| 178 | + Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query $createRoleSql -Force -ErrorAction 'Stop' |
| 179 | + |
| 180 | + try |
| 181 | + { |
| 182 | + # Grant a permission to the custom role |
| 183 | + $grantRolePermissionSql = "USE [$($script:testDatabaseName)]; GRANT CONNECT TO [$customRoleName];" |
| 184 | + Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query $grantRolePermissionSql -Force -ErrorAction 'Stop' |
| 185 | + |
| 186 | + # Test getting permissions for the custom role |
| 187 | + $result = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $customRoleName -Refresh |
| 188 | + |
| 189 | + $result | Should -Not -BeNullOrEmpty |
| 190 | + $result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.DatabasePermissionInfo] |
| 191 | + |
| 192 | + # Verify the Connect permission we granted is present |
| 193 | + $connectPermission = $result | Where-Object { $_.PermissionType.Connect -eq $true } |
| 194 | + $connectPermission | Should -Not -BeNullOrEmpty |
| 195 | + $connectPermission.PermissionState | Should -Be 'Grant' |
| 196 | + } |
| 197 | + finally |
| 198 | + { |
| 199 | + # Clean up the custom role |
| 200 | + $dropRoleSql = "USE [$($script:testDatabaseName)]; DROP ROLE [$customRoleName];" |
| 201 | + Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query $dropRoleSql -Force -ErrorAction 'SilentlyContinue' |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | +} |
0 commit comments