@@ -1147,7 +1147,16 @@ Describe 'SqlPermission\Modify()' -Tag 'Modify' {
11471147 return $true
11481148 }
11491149
1150- Mock - CommandName Set-SqlDscServerPermission
1150+ Mock - CommandName Get-SqlDscLogin - MockWith {
1151+ return New-Object - TypeName ' Microsoft.SqlServer.Management.Smo.Login' - ArgumentList @ (
1152+ (New-Object - TypeName ' Microsoft.SqlServer.Management.Smo.Server' ),
1153+ ' MockUserName'
1154+ )
1155+ }
1156+
1157+ Mock - CommandName Grant-SqlDscServerPermission
1158+ Mock - CommandName Deny-SqlDscServerPermission
1159+ Mock - CommandName Revoke-SqlDscServerPermission
11511160 }
11521161
11531162 It ' Should call the correct mock with the correct parameter values' {
@@ -1171,13 +1180,13 @@ Describe 'SqlPermission\Modify()' -Tag 'Modify' {
11711180 }
11721181
11731182 # Grants
1174- Should - Invoke - CommandName Set -SqlDscServerPermission - ParameterFilter {
1175- $State -eq ' Grant ' -and $Permission .ConnectSql -eq $true
1183+ Should - Invoke - CommandName Grant -SqlDscServerPermission - ParameterFilter {
1184+ $Permission -contains ' ConnectSql ' -and -not $PSBoundParameters .ContainsKey ( ' WithGrant ' )
11761185 } - Exactly - Times 1 - Scope It
11771186
11781187 # GrantWithGrants
1179- Should - Invoke - CommandName Set -SqlDscServerPermission - ParameterFilter {
1180- $State -eq ' Grant ' -and $Permission .AlterAnyEndpoint -eq $true
1188+ Should - Invoke - CommandName Grant -SqlDscServerPermission - ParameterFilter {
1189+ $Permission -contains ' AlterAnyEndpoint ' -and $WithGrant -eq $true
11811190 } - Exactly - Times 1 - Scope It
11821191 }
11831192 }
@@ -1234,7 +1243,16 @@ Describe 'SqlPermission\Modify()' -Tag 'Modify' {
12341243 return $true
12351244 }
12361245
1237- Mock - CommandName Set-SqlDscServerPermission
1246+ Mock - CommandName Get-SqlDscLogin - MockWith {
1247+ return New-Object - TypeName ' Microsoft.SqlServer.Management.Smo.Login' - ArgumentList @ (
1248+ (New-Object - TypeName ' Microsoft.SqlServer.Management.Smo.Server' ),
1249+ ' MockUserName'
1250+ )
1251+ }
1252+
1253+ Mock - CommandName Grant-SqlDscServerPermission
1254+ Mock - CommandName Deny-SqlDscServerPermission
1255+ Mock - CommandName Revoke-SqlDscServerPermission
12381256 }
12391257
12401258 It ' Should call the correct mock with the correct parameter values' {
@@ -1258,23 +1276,23 @@ Describe 'SqlPermission\Modify()' -Tag 'Modify' {
12581276 }
12591277
12601278 # Revoking Grants
1261- Should - Invoke - CommandName Set -SqlDscServerPermission - ParameterFilter {
1262- $State -eq ' Revoke ' -and $Permission.AlterAnyAvailabilityGroup -eq $true -and $Permission .ViewServerState -eq $true
1279+ Should - Invoke - CommandName Revoke -SqlDscServerPermission - ParameterFilter {
1280+ $Permission -contains ' AlterAnyAvailabilityGroup ' -and $Permission -contains ' ViewServerState ' -and -not $PSBoundParameters .ContainsKey ( ' WithGrant ' )
12631281 } - Exactly - Times 1 - Scope It
12641282
12651283 # Revoking GrantWithGrants
1266- Should - Invoke - CommandName Set -SqlDscServerPermission - ParameterFilter {
1267- $State -eq ' Revoke ' -and $Permission .ControlServer -eq $true
1284+ Should - Invoke - CommandName Revoke -SqlDscServerPermission - ParameterFilter {
1285+ $Permission -contains ' ControlServer ' -and $WithGrant -eq $true
12681286 } - Exactly - Times 1 - Scope It
12691287
12701288 # Revoking Denies
1271- Should - Invoke - CommandName Set -SqlDscServerPermission - ParameterFilter {
1272- $State -eq ' Revoke ' -and $Permission .CreateEndpoint -eq $true
1289+ Should - Invoke - CommandName Revoke -SqlDscServerPermission - ParameterFilter {
1290+ $Permission -contains ' CreateEndpoint ' -and -not $PSBoundParameters .ContainsKey ( ' WithGrant ' )
12731291 } - Exactly - Times 1 - Scope It
12741292
12751293 # Adding new Grant
1276- Should - Invoke - CommandName Set -SqlDscServerPermission - ParameterFilter {
1277- $State -eq ' Grant ' -and $Permission .ConnectSql -eq $true
1294+ Should - Invoke - CommandName Grant -SqlDscServerPermission - ParameterFilter {
1295+ $Permission -contains ' ConnectSql ' -and -not $PSBoundParameters .ContainsKey ( ' WithGrant ' )
12781296 } - Exactly - Times 1 - Scope It
12791297 }
12801298 }
@@ -1333,7 +1351,16 @@ Describe 'SqlPermission\Modify()' -Tag 'Modify' {
13331351 return $true
13341352 }
13351353
1336- Mock - CommandName Set-SqlDscServerPermission
1354+ Mock - CommandName Get-SqlDscLogin - MockWith {
1355+ return New-Object - TypeName ' Microsoft.SqlServer.Management.Smo.Login' - ArgumentList @ (
1356+ (New-Object - TypeName ' Microsoft.SqlServer.Management.Smo.Server' ),
1357+ ' MockUserName'
1358+ )
1359+ }
1360+
1361+ Mock - CommandName Grant-SqlDscServerPermission
1362+ Mock - CommandName Deny-SqlDscServerPermission
1363+ Mock - CommandName Revoke-SqlDscServerPermission
13371364 }
13381365
13391366 It ' Should call the correct mock with the correct parameter values' {
@@ -1357,13 +1384,13 @@ Describe 'SqlPermission\Modify()' -Tag 'Modify' {
13571384 }
13581385
13591386 # Grants
1360- Should - Invoke - CommandName Set -SqlDscServerPermission - ParameterFilter {
1361- $State -eq ' Grant ' -and $Permission .ConnectSql -eq $true
1387+ Should - Invoke - CommandName Grant -SqlDscServerPermission - ParameterFilter {
1388+ $Permission -contains ' ConnectSql ' -and -not $PSBoundParameters .ContainsKey ( ' WithGrant ' )
13621389 } - Exactly - Times 1 - Scope It
13631390
13641391 # GrantWithGrants
1365- Should - Invoke - CommandName Set -SqlDscServerPermission - ParameterFilter {
1366- $State -eq ' Grant ' -and $Permission .AlterAnyEndpoint -eq $true
1392+ Should - Invoke - CommandName Grant -SqlDscServerPermission - ParameterFilter {
1393+ $Permission -contains ' AlterAnyEndpoint ' -and $WithGrant -eq $true
13671394 } - Exactly - Times 1 - Scope It
13681395 }
13691396 }
@@ -1422,7 +1449,16 @@ Describe 'SqlPermission\Modify()' -Tag 'Modify' {
14221449 return $true
14231450 }
14241451
1425- Mock - CommandName Set-SqlDscServerPermission
1452+ Mock - CommandName Get-SqlDscLogin - MockWith {
1453+ return New-Object - TypeName ' Microsoft.SqlServer.Management.Smo.Login' - ArgumentList @ (
1454+ (New-Object - TypeName ' Microsoft.SqlServer.Management.Smo.Server' ),
1455+ ' MockUserName'
1456+ )
1457+ }
1458+
1459+ Mock - CommandName Grant-SqlDscServerPermission
1460+ Mock - CommandName Deny-SqlDscServerPermission
1461+ Mock - CommandName Revoke-SqlDscServerPermission
14261462 }
14271463
14281464 It ' Should call the correct mock with the correct parameter values' {
@@ -1446,19 +1482,19 @@ Describe 'SqlPermission\Modify()' -Tag 'Modify' {
14461482 }
14471483
14481484 # Revoking Grants
1449- Should - Invoke - CommandName Set -SqlDscServerPermission - ParameterFilter {
1450- $State -eq ' Revoke ' -and $Permission .ConnectSql -eq $true
1485+ Should - Invoke - CommandName Revoke -SqlDscServerPermission - ParameterFilter {
1486+ $Permission -contains ' ConnectSql ' -and -not $PSBoundParameters .ContainsKey ( ' WithGrant ' )
14511487 } - Exactly - Times 1 - Scope It
14521488
14531489 # Revoking GrantWithGrants
1454- Should - Invoke - CommandName Set -SqlDscServerPermission - ParameterFilter {
1455- $State -eq ' Revoke ' -and $Permission .AlterAnyEndpoint -eq $true
1490+ Should - Invoke - CommandName Revoke -SqlDscServerPermission - ParameterFilter {
1491+ $Permission -contains ' AlterAnyEndpoint ' -and $WithGrant -eq $true
14561492 } - Exactly - Times 1 - Scope It
14571493 }
14581494 }
14591495 }
14601496
1461- Context ' When Set-SqlDscServerPermission fails to change permission' {
1497+ Context ' When Grant/Deny/Revoke commands fail to change permission' {
14621498 Context ' When granting permissions' {
14631499 BeforeAll {
14641500 InModuleScope - ScriptBlock {
@@ -1511,7 +1547,20 @@ Describe 'SqlPermission\Modify()' -Tag 'Modify' {
15111547 return $true
15121548 }
15131549
1514- Mock - CommandName Set-SqlDscServerPermission - MockWith {
1550+ Mock - CommandName Get-SqlDscLogin - MockWith {
1551+ return New-Object - TypeName ' Microsoft.SqlServer.Management.Smo.Login' - ArgumentList @ (
1552+ (New-Object - TypeName ' Microsoft.SqlServer.Management.Smo.Server' ),
1553+ ' MockUserName'
1554+ )
1555+ }
1556+
1557+ Mock - CommandName Grant-SqlDscServerPermission - MockWith {
1558+ throw ' Mocked error'
1559+ }
1560+ Mock - CommandName Deny-SqlDscServerPermission - MockWith {
1561+ throw ' Mocked error'
1562+ }
1563+ Mock - CommandName Revoke-SqlDscServerPermission - MockWith {
15151564 throw ' Mocked error'
15161565 }
15171566 }
@@ -1602,7 +1651,20 @@ Describe 'SqlPermission\Modify()' -Tag 'Modify' {
16021651 return $true
16031652 }
16041653
1605- Mock - CommandName Set-SqlDscServerPermission - MockWith {
1654+ Mock - CommandName Get-SqlDscLogin - MockWith {
1655+ return New-Object - TypeName ' Microsoft.SqlServer.Management.Smo.Login' - ArgumentList @ (
1656+ (New-Object - TypeName ' Microsoft.SqlServer.Management.Smo.Server' ),
1657+ ' MockUserName'
1658+ )
1659+ }
1660+
1661+ Mock - CommandName Grant-SqlDscServerPermission - MockWith {
1662+ throw ' Mocked error'
1663+ }
1664+ Mock - CommandName Deny-SqlDscServerPermission - MockWith {
1665+ throw ' Mocked error'
1666+ }
1667+ Mock - CommandName Revoke-SqlDscServerPermission - MockWith {
16061668 throw ' Mocked error'
16071669 }
16081670 }
0 commit comments