Skip to content

Commit 0865063

Browse files
committed
Add integration tests for Set-SqlDscServerPermission to cover invalid permissions and non-existent principals
1 parent 59c83ab commit 0865063

File tree

2 files changed

+73
-10
lines changed

2 files changed

+73
-10
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
### Added
99

10+
- `Set-SqlDscServerPermission`
11+
- Added integration tests for negative test scenarios including invalid
12+
permission names and non-existent principals.
1013
- `New-SqlDscDatabase`
1114
- Added comprehensive set of settable database properties that were previously
1215
only available in `Set-SqlDscDatabaseProperty`

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

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,16 @@ Describe 'Set-SqlDscServerPermission' -Tag @('Integration_SQL2017', 'Integration
100100
}
101101

102102
It 'Should set combined Grant, GrantWithGrant, and Deny permissions' {
103-
Set-SqlDscServerPermission -Login $script:loginObject `
104-
-Grant 'ViewServerState' `
105-
-GrantWithGrant 'CreateAnyDatabase' `
106-
-Deny 'ViewAnyDefinition' `
107-
-Force -ErrorAction 'Stop'
103+
$setPermissionParams = @{
104+
Login = $script:loginObject
105+
Grant = 'ViewServerState'
106+
GrantWithGrant = 'CreateAnyDatabase'
107+
Deny = 'ViewAnyDefinition'
108+
Force = $true
109+
ErrorAction = 'Stop'
110+
}
111+
112+
Set-SqlDscServerPermission @setPermissionParams
108113

109114
# Verify Grant permission
110115
$grantResult = Test-SqlDscServerPermission -Login $script:loginObject -Grant -Permission 'ViewServerState' -ErrorAction 'Stop'
@@ -235,11 +240,16 @@ Describe 'Set-SqlDscServerPermission' -Tag @('Integration_SQL2017', 'Integration
235240
}
236241

237242
It 'Should set combined Grant, GrantWithGrant, and Deny permissions for role' {
238-
Set-SqlDscServerPermission -ServerRole $script:roleObject `
239-
-Grant 'ViewServerState' `
240-
-GrantWithGrant 'CreateAnyDatabase' `
241-
-Deny 'ViewAnyDefinition' `
242-
-Force -ErrorAction 'Stop'
243+
$setPermissionParams = @{
244+
ServerRole = $script:roleObject
245+
Grant = 'ViewServerState'
246+
GrantWithGrant = 'CreateAnyDatabase'
247+
Deny = 'ViewAnyDefinition'
248+
Force = $true
249+
ErrorAction = 'Stop'
250+
}
251+
252+
Set-SqlDscServerPermission @setPermissionParams
243253

244254
# Verify Grant permission
245255
$grantResult = Test-SqlDscServerPermission -ServerRole $script:roleObject -Grant -Permission 'ViewServerState' -ErrorAction 'Stop'
@@ -344,4 +354,54 @@ Describe 'Set-SqlDscServerPermission' -Tag @('Integration_SQL2017', 'Integration
344354
$result | Should -BeTrue
345355
}
346356
}
357+
358+
Context 'When specifying invalid permission values' {
359+
BeforeEach {
360+
# Get the login object for testing
361+
$script:loginObject = Get-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -ErrorAction 'Stop'
362+
}
363+
364+
It 'Should throw when specifying an invalid permission name' {
365+
{
366+
Set-SqlDscServerPermission -Login $script:loginObject -Grant 'InvalidPermissionName' -Force -ErrorAction 'Stop'
367+
} | Should -Throw
368+
}
369+
}
370+
371+
Context 'When specifying a non-existent principal' {
372+
It 'Should throw when using a login object that no longer exists' {
373+
# Create a temporary login
374+
$tempLoginName = 'TempLoginForErrorTest'
375+
$mockPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
376+
377+
New-SqlDscLogin -ServerObject $script:serverObject -Name $tempLoginName -LoginType 'SqlLogin' -SecureString $mockPassword -Force -ErrorAction 'Stop'
378+
379+
$tempLoginObject = Get-SqlDscLogin -ServerObject $script:serverObject -Name $tempLoginName -ErrorAction 'Stop'
380+
381+
# Remove the login
382+
Remove-SqlDscLogin -LoginObject $tempLoginObject -Force -ErrorAction 'Stop'
383+
384+
# Attempt to set permissions on the removed login should throw
385+
{
386+
Set-SqlDscServerPermission -Login $tempLoginObject -Grant 'ViewServerState' -Force -ErrorAction 'Stop'
387+
} | Should -Throw
388+
}
389+
390+
It 'Should throw when using a server role object that no longer exists' {
391+
# Create a temporary role
392+
$tempRoleName = 'TempRoleForErrorTest'
393+
394+
New-SqlDscRole -ServerObject $script:serverObject -Name $tempRoleName -Force -ErrorAction 'Stop'
395+
396+
$tempRoleObject = Get-SqlDscRole -ServerObject $script:serverObject -Name $tempRoleName -ErrorAction 'Stop'
397+
398+
# Remove the role
399+
Remove-SqlDscRole -RoleObject $tempRoleObject -Force -ErrorAction 'Stop'
400+
401+
# Attempt to set permissions on the removed role should throw
402+
{
403+
Set-SqlDscServerPermission -ServerRole $tempRoleObject -Grant 'ViewServerState' -Force -ErrorAction 'Stop'
404+
} | Should -Throw
405+
}
406+
}
347407
}

0 commit comments

Comments
 (0)