|
| 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-SqlDscServerPermission' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') { |
| 33 | + BeforeAll { |
| 34 | + $script:mockInstanceName = 'DSCSQLTEST' |
| 35 | + |
| 36 | + $mockSqlAdministratorUserName = 'SqlAdmin' |
| 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 -ErrorAction 'Stop' |
| 42 | + } |
| 43 | + |
| 44 | + AfterAll { |
| 45 | + Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject -ErrorAction 'Stop' |
| 46 | + } |
| 47 | + |
| 48 | + Context 'When converting empty collection of ServerPermissionInfo' { |
| 49 | + It 'Should return empty array for empty ServerPermissionInfo collection' { |
| 50 | + $emptyCollection = [Microsoft.SqlServer.Management.Smo.ServerPermissionInfo[]] @() |
| 51 | + |
| 52 | + $result = ConvertTo-SqlDscServerPermission -ServerPermissionInfo $emptyCollection |
| 53 | + |
| 54 | + $result | Should -BeNullOrEmpty |
| 55 | + } |
| 56 | + |
| 57 | + It 'Should accept empty collection through pipeline' { |
| 58 | + $emptyCollection = [Microsoft.SqlServer.Management.Smo.ServerPermissionInfo[]] @() |
| 59 | + |
| 60 | + $result = $emptyCollection | ConvertTo-SqlDscServerPermission |
| 61 | + |
| 62 | + $result | Should -BeNullOrEmpty |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + Context 'When converting ServerPermissionInfo from SQL Server' { |
| 67 | + It 'Should convert ServerPermissionInfo to ServerPermission objects for sa principal' { |
| 68 | + # Get permissions for the sa login |
| 69 | + $serverPermissionInfo = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'sa' -ErrorAction 'Stop' |
| 70 | + |
| 71 | + $result = ConvertTo-SqlDscServerPermission -ServerPermissionInfo $serverPermissionInfo |
| 72 | + |
| 73 | + # Validate the result structure |
| 74 | + $result | Should -Not -BeNullOrEmpty |
| 75 | + |
| 76 | + # Each result should have State and Permission properties |
| 77 | + foreach ($permission in $result) |
| 78 | + { |
| 79 | + $permission.State | Should -Not -BeNullOrEmpty |
| 80 | + $permission.Permission | Should -Not -BeNullOrEmpty |
| 81 | + |
| 82 | + # Validate that permission state is one of the expected values |
| 83 | + $permission.State | Should -BeIn @('Grant', 'Deny', 'GrantWithGrant') |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + It 'Should accept ServerPermissionInfo through pipeline' { |
| 88 | + # Get permissions for the sa login |
| 89 | + $serverPermissionInfo = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'sa' -ErrorAction 'Stop' |
| 90 | + |
| 91 | + $result = $serverPermissionInfo | ConvertTo-SqlDscServerPermission |
| 92 | + |
| 93 | + # Validate the result structure |
| 94 | + $result | Should -Not -BeNullOrEmpty |
| 95 | + |
| 96 | + # Each result should have State and Permission properties |
| 97 | + foreach ($permission in $result) |
| 98 | + { |
| 99 | + $permission.State | Should -Not -BeNullOrEmpty |
| 100 | + $permission.Permission | Should -Not -BeNullOrEmpty |
| 101 | + |
| 102 | + # Validate that permission state is one of the expected values |
| 103 | + $permission.State | Should -BeIn @('Grant', 'Deny', 'GrantWithGrant') |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + Context 'When converting ServerPermissionInfo for system logins' { |
| 109 | + It 'Should handle NT AUTHORITY\SYSTEM permissions correctly' { |
| 110 | + # Get permissions for NT AUTHORITY\SYSTEM |
| 111 | + $serverPermissionInfo = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'NT AUTHORITY\SYSTEM' -ErrorAction 'SilentlyContinue' |
| 112 | + |
| 113 | + $result = ConvertTo-SqlDscServerPermission -ServerPermissionInfo $serverPermissionInfo |
| 114 | + |
| 115 | + # Validate the result structure |
| 116 | + $result | Should -Not -BeNullOrEmpty |
| 117 | + |
| 118 | + # Each result should have State and Permission properties |
| 119 | + foreach ($permission in $result) |
| 120 | + { |
| 121 | + $permission.State | Should -Not -BeNullOrEmpty |
| 122 | + $permission.Permission | Should -Not -BeNullOrEmpty |
| 123 | + |
| 124 | + # Validate that permission state is one of the expected values |
| 125 | + $permission.State | Should -BeIn @('Grant', 'Deny', 'GrantWithGrant') |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + Context 'When converting ServerPermissionInfo for server roles' { |
| 131 | + It 'Should handle public server role permissions correctly' { |
| 132 | + # Get permissions for the public server role |
| 133 | + $serverPermissionInfo = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'public' -ErrorAction 'Stop' |
| 134 | + |
| 135 | + $result = ConvertTo-SqlDscServerPermission -ServerPermissionInfo $serverPermissionInfo |
| 136 | + |
| 137 | + # Validate the result structure |
| 138 | + $result | Should -Not -BeNullOrEmpty |
| 139 | + |
| 140 | + # Each result should have State and Permission properties |
| 141 | + foreach ($permission in $result) |
| 142 | + { |
| 143 | + $permission.State | Should -Not -BeNullOrEmpty |
| 144 | + $permission.Permission | Should -Not -BeNullOrEmpty |
| 145 | + |
| 146 | + # Validate that permission state is one of the expected values |
| 147 | + $permission.State | Should -BeIn @('Grant', 'Deny', 'GrantWithGrant') |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + It 'Should handle SqlDscIntegrationTestRole_Persistent server role permissions correctly' { |
| 152 | + # Get permissions for the SqlDscIntegrationTestRole_Persistent server role |
| 153 | + $serverPermissionInfo = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'SqlDscIntegrationTestRole_Persistent' -ErrorAction 'Stop' |
| 154 | + |
| 155 | + # Only proceed if we have permission data to work with |
| 156 | + $result = ConvertTo-SqlDscServerPermission -ServerPermissionInfo $serverPermissionInfo |
| 157 | + |
| 158 | + # Validate the result structure |
| 159 | + $result | Should -Not -BeNullOrEmpty |
| 160 | + |
| 161 | + # Each result should have State and Permission properties |
| 162 | + foreach ($permission in $result) |
| 163 | + { |
| 164 | + $permission.State | Should -Not -BeNullOrEmpty |
| 165 | + $permission.Permission | Should -Not -BeNullOrEmpty |
| 166 | + |
| 167 | + # Validate that permission state is one of the expected values |
| 168 | + $permission.State | Should -BeIn @('Grant', 'Deny', 'GrantWithGrant') |
| 169 | + } |
| 170 | + |
| 171 | + # Verify that the CreateEndpoint permission granted by Grant-SqlDscServerPermission test is present |
| 172 | + $grantPermission = $result | Where-Object { $_.State -eq 'Grant' } |
| 173 | + if ($grantPermission) |
| 174 | + { |
| 175 | + $grantPermission.Permission | Should -Contain 'CreateEndpoint' -Because 'CreateEndpoint permission should have been granted by Grant-SqlDscServerPermission integration test' |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + Context 'When testing conversion functionality with multiple permission states' { |
| 181 | + It 'Should group permissions by state correctly' { |
| 182 | + # Get permissions from a principal that might have various permission states |
| 183 | + $serverPermissionInfo = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'SqlDscIntegrationTestRole_Persistent' -ErrorAction 'SilentlyContinue' |
| 184 | + |
| 185 | + $result = ConvertTo-SqlDscServerPermission -ServerPermissionInfo $serverPermissionInfo |
| 186 | + |
| 187 | + # Validate that permissions are properly grouped by state |
| 188 | + $uniqueStates = $result.State | Sort-Object -Unique |
| 189 | + |
| 190 | + foreach ($state in $uniqueStates) |
| 191 | + { |
| 192 | + $permissionsForState = $result | Where-Object { $_.State -eq $state } |
| 193 | + $permissionsForState | Should -HaveCount 1 |
| 194 | + $permissionsForState[0].Permission | Should -Not -BeNullOrEmpty |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments