Skip to content

Commit 125203e

Browse files
committed
Refactor Get-SqlDscServerPermission to simplify role check logic
1 parent 139f03f commit 125203e

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

source/Public/Get-SqlDscServerPermission.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ function Get-SqlDscServerPermission
105105
$false
106106
}
107107

108-
$isRole = if ($checkRole -and -not $isLogin)
108+
$isRole = if ($checkRole)
109109
{
110110
Test-SqlDscIsRole @testSqlDscIsPrincipalParameters
111111
}

tests/Integration/Commands/Get-SqlDscServerPermission.Integration.Tests.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ Describe 'Get-SqlDscServerPermission' -Tag @('Integration_SQL2016', 'Integration
123123
Context 'When getting permissions for invalid principals' {
124124
It 'Should throw error for non-existent login with ErrorAction Stop' {
125125
{ Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'NonExistentLogin123' -ErrorAction 'Stop' } |
126-
Should -Throw -ExpectedMessage "*does not exist*"
126+
Should -Throw -ExpectedMessage "*is not a login nor role*"
127127
}
128128

129129
It 'Should return null for non-existent login with ErrorAction SilentlyContinue' {
@@ -134,7 +134,7 @@ Describe 'Get-SqlDscServerPermission' -Tag @('Integration_SQL2016', 'Integration
134134

135135
It 'Should throw error for non-existent server role with ErrorAction Stop' {
136136
{ Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'NonExistentRole123' -ErrorAction 'Stop' } |
137-
Should -Throw -ExpectedMessage "*does not exist*"
137+
Should -Throw -ExpectedMessage "*is not a login nor role*"
138138
}
139139

140140
It 'Should return null for non-existent server role with ErrorAction SilentlyContinue' {
@@ -192,12 +192,12 @@ Describe 'Get-SqlDscServerPermission' -Tag @('Integration_SQL2016', 'Integration
192192

193193
It 'Should throw error when looking for login as role' {
194194
{ Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'sa' -PrincipalType 'Role' -ErrorAction 'Stop' } |
195-
Should -Throw -ExpectedMessage "*does not exist*"
195+
Should -Throw -ExpectedMessage "*is not a login nor role*"
196196
}
197197

198198
It 'Should throw error when looking for role as login' {
199199
{ Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'sysadmin' -PrincipalType 'Login' -ErrorAction 'Stop' } |
200-
Should -Throw -ExpectedMessage "*does not exist*"
200+
Should -Throw -ExpectedMessage "*is not a login nor role*"
201201
}
202202

203203
It 'Should return null when looking for login as role with SilentlyContinue' {

tests/Unit/Public/Get-SqlDscServerPermission.Tests.ps1

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,16 @@ Describe 'Get-SqlDscServerPermission' -Tag 'Public' {
244244
}
245245
}
246246

247-
It 'Should call Test-SqlDscIsLogin but not Test-SqlDscIsRole when login is found' {
247+
It 'Should call both Test-SqlDscIsLogin and Test-SqlDscIsRole' {
248248
$null = Get-SqlDscServerPermission -ServerObject $mockServerObject -Name 'TestPrincipal' -ErrorAction 'SilentlyContinue'
249249

250250
Should -Invoke -CommandName Test-SqlDscIsLogin -ParameterFilter {
251251
$ServerObject.Equals($mockServerObject) -and $Name -eq 'TestPrincipal'
252252
} -Exactly -Times 1
253253

254-
Should -Invoke -CommandName Test-SqlDscIsRole -Exactly -Times 0
254+
Should -Invoke -CommandName Test-SqlDscIsRole -ParameterFilter {
255+
$ServerObject.Equals($mockServerObject) -and $Name -eq 'TestPrincipal'
256+
} -Exactly -Times 1
255257
}
256258
}
257259

@@ -266,7 +268,7 @@ Describe 'Get-SqlDscServerPermission' -Tag 'Public' {
266268
}
267269
}
268270

269-
It 'Should call both Test-SqlDscIsLogin and Test-SqlDscIsRole when login is not found' {
271+
It 'Should call both Test-SqlDscIsLogin and Test-SqlDscIsRole' {
270272
$null = Get-SqlDscServerPermission -ServerObject $mockServerObject -Name 'TestPrincipal' -ErrorAction 'SilentlyContinue'
271273

272274
Should -Invoke -CommandName Test-SqlDscIsLogin -ParameterFilter {
@@ -334,14 +336,16 @@ Describe 'Get-SqlDscServerPermission' -Tag 'Public' {
334336
}
335337
}
336338

337-
It 'Should call Test-SqlDscIsLogin but not Test-SqlDscIsRole when login is found' {
339+
It 'Should call both Test-SqlDscIsLogin and Test-SqlDscIsRole when both types are specified' {
338340
$null = Get-SqlDscServerPermission -ServerObject $mockServerObject -Name 'TestPrincipal' -PrincipalType 'Login', 'Role' -ErrorAction 'SilentlyContinue'
339341

340342
Should -Invoke -CommandName Test-SqlDscIsLogin -ParameterFilter {
341343
$ServerObject.Equals($mockServerObject) -and $Name -eq 'TestPrincipal'
342344
} -Exactly -Times 1
343345

344-
Should -Invoke -CommandName Test-SqlDscIsRole -Exactly -Times 0
346+
Should -Invoke -CommandName Test-SqlDscIsRole -ParameterFilter {
347+
$ServerObject.Equals($mockServerObject) -and $Name -eq 'TestPrincipal'
348+
} -Exactly -Times 1
345349
}
346350
}
347351
}

0 commit comments

Comments
 (0)