|
| 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 'ConvertTo-SqlDscDatabasePermission' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') { |
| 33 | + BeforeAll { |
| 34 | + # Starting the named instance SQL Server service prior to running tests. |
| 35 | + Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop' |
| 36 | + |
| 37 | + $script:mockInstanceName = 'DSCSQLTEST' |
| 38 | + |
| 39 | + $mockSqlAdministratorUserName = 'SqlAdmin' |
| 40 | + $mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force |
| 41 | + |
| 42 | + $script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword) |
| 43 | + |
| 44 | + $script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential |
| 45 | + } |
| 46 | + |
| 47 | + AfterAll { |
| 48 | + Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject |
| 49 | + |
| 50 | + # Stop the named instance SQL Server service to save memory on the build worker. |
| 51 | + Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop' |
| 52 | + } |
| 53 | + |
| 54 | + Context 'When converting empty collection of DatabasePermissionInfo' { |
| 55 | + It 'Should return empty array for empty DatabasePermissionInfo collection' { |
| 56 | + $emptyCollection = [Microsoft.SqlServer.Management.Smo.DatabasePermissionInfo[]] @() |
| 57 | + |
| 58 | + $result = ConvertTo-SqlDscDatabasePermission -DatabasePermissionInfo $emptyCollection |
| 59 | + |
| 60 | + $result | Should -BeNullOrEmpty |
| 61 | + } |
| 62 | + |
| 63 | + It 'Should accept empty collection through pipeline' { |
| 64 | + $emptyCollection = [Microsoft.SqlServer.Management.Smo.DatabasePermissionInfo[]] @() |
| 65 | + |
| 66 | + $result = $emptyCollection | ConvertTo-SqlDscDatabasePermission |
| 67 | + |
| 68 | + $result | Should -BeNullOrEmpty |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + Context 'When converting DatabasePermissionInfo from system database' { |
| 73 | + It 'Should convert DatabasePermissionInfo to DatabasePermission objects for dbo principal' { |
| 74 | + # Get permissions for the dbo user from master database |
| 75 | + $databasePermissionInfo = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName 'master' -Name 'dbo' -ErrorAction 'Stop' |
| 76 | + |
| 77 | + # Only proceed if we have permission data to work with |
| 78 | + if ($databasePermissionInfo) { |
| 79 | + $result = ConvertTo-SqlDscDatabasePermission -DatabasePermissionInfo $databasePermissionInfo |
| 80 | + |
| 81 | + # Validate the result structure |
| 82 | + $result | Should -Not -BeNullOrEmpty |
| 83 | + |
| 84 | + # Each result should have State and Permission properties |
| 85 | + foreach ($permission in $result) { |
| 86 | + $permission.State | Should -Not -BeNullOrEmpty |
| 87 | + $permission.Permission | Should -Not -BeNullOrEmpty |
| 88 | + |
| 89 | + # Validate that permission state is one of the expected values |
| 90 | + $permission.State | Should -BeIn @('Grant', 'Deny', 'GrantWithGrant') |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + It 'Should accept DatabasePermissionInfo through pipeline' { |
| 96 | + # Get permissions for the dbo user from master database |
| 97 | + $databasePermissionInfo = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName 'master' -Name 'dbo' -ErrorAction 'Stop' |
| 98 | + |
| 99 | + # Only proceed if we have permission data to work with |
| 100 | + if ($databasePermissionInfo) { |
| 101 | + $result = $databasePermissionInfo | ConvertTo-SqlDscDatabasePermission |
| 102 | + |
| 103 | + # Validate the result structure |
| 104 | + $result | Should -Not -BeNullOrEmpty |
| 105 | + |
| 106 | + # Each result should have State and Permission properties |
| 107 | + foreach ($permission in $result) { |
| 108 | + $permission.State | Should -Not -BeNullOrEmpty |
| 109 | + $permission.Permission | Should -Not -BeNullOrEmpty |
| 110 | + |
| 111 | + # Validate that permission state is one of the expected values |
| 112 | + $permission.State | Should -BeIn @('Grant', 'Deny', 'GrantWithGrant') |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + Context 'When converting DatabasePermissionInfo for guest user' { |
| 119 | + It 'Should handle guest user permissions correctly' { |
| 120 | + # Get permissions for the guest user from master database (guest should have Connect permission) |
| 121 | + $databasePermissionInfo = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName 'master' -Name 'guest' -ErrorAction 'SilentlyContinue' |
| 122 | + |
| 123 | + # Only proceed if we have permission data to work with |
| 124 | + if ($databasePermissionInfo) { |
| 125 | + $result = ConvertTo-SqlDscDatabasePermission -DatabasePermissionInfo $databasePermissionInfo |
| 126 | + |
| 127 | + # Validate the result structure |
| 128 | + $result | Should -Not -BeNullOrEmpty |
| 129 | + |
| 130 | + # Each result should have State and Permission properties |
| 131 | + foreach ($permission in $result) { |
| 132 | + $permission.State | Should -Not -BeNullOrEmpty |
| 133 | + $permission.Permission | Should -Not -BeNullOrEmpty |
| 134 | + |
| 135 | + # Validate that permission state is one of the expected values |
| 136 | + $permission.State | Should -BeIn @('Grant', 'Deny', 'GrantWithGrant') |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + Context 'When testing conversion functionality with multiple permission states' { |
| 143 | + It 'Should group permissions by state correctly' { |
| 144 | + # Try to get permissions from system databases where we might have various permission states |
| 145 | + $databasePermissionInfo = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName 'msdb' -Name 'dbo' -ErrorAction 'SilentlyContinue' |
| 146 | + |
| 147 | + # Only proceed if we have permission data to work with |
| 148 | + if ($databasePermissionInfo) { |
| 149 | + $result = ConvertTo-SqlDscDatabasePermission -DatabasePermissionInfo $databasePermissionInfo |
| 150 | + |
| 151 | + if ($result) { |
| 152 | + # Validate that permissions are properly grouped by state |
| 153 | + $uniqueStates = $result.State | Sort-Object -Unique |
| 154 | + |
| 155 | + foreach ($state in $uniqueStates) { |
| 156 | + $permissionsForState = $result | Where-Object { $_.State -eq $state } |
| 157 | + $permissionsForState | Should -HaveCount 1 |
| 158 | + $permissionsForState[0].Permission | Should -Not -BeNullOrEmpty |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments