Skip to content

Commit

Permalink
SqlDatabaseUser: Pass parameter ServerName to Assert-SqlLogin (#1681)
Browse files Browse the repository at this point in the history
- SqlDatabaseUser
  - Added parameter `ServerName` to the call of `Assert-SqlLogin`.
    `@PSBoundParameters` doesn't capture the default value of `ServerName`
    when it is not explicitly set by the caller (issue #1647).
  • Loading branch information
bschapendonk authored Feb 1, 2021
1 parent db3c76f commit f7676b0
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 33 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added/corrected `InstallSharedDir`, property output when using SQL Server 2019.
- SqlTraceFlag
- Fixed Assembly not loaded error ([issue #1680](https://github.com/dsccommunity/SqlServerDsc/issues/1680)).
- SqlDatabaseUser
- Added parameter `ServerName` to the call of `Assert-SqlLogin`.
`@PSBoundParameters` doesn't capture the default value of `ServerName`
when it is not explicitly set by the caller ([issue #1647](https://github.com/dsccommunity/SqlServerDsc/issues/1647)).

## [15.0.1] - 2021-01-09

Expand Down
26 changes: 17 additions & 9 deletions source/DSCResources/DSC_SqlDatabaseUser/DSC_SqlDatabaseUser.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function Get-TargetResource
#>
function Set-TargetResource
{
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification = 'The command Connect-Sql is called when Get-TargetResource is called')]
[CmdletBinding()]
param
(
Expand Down Expand Up @@ -243,8 +243,14 @@ function Set-TargetResource
$script:localizedData.ChangingLoginName -f $Name, $LoginName
)

$assertSqlLoginParameters = @{
ServerName = $ServerName
InstanceName = $InstanceName
LoginName = $LoginName
}

# Assert that the login exist.
Assert-SqlLogin @PSBoundParameters
Assert-SqlLogin @assertSqlLoginParameters

try
{
Expand Down Expand Up @@ -359,8 +365,14 @@ function Set-TargetResource
{
'Login'
{
$assertSqlLoginParameters = @{
ServerName = $ServerName
InstanceName = $InstanceName
LoginName = $LoginName
}

# Assert that the login exist.
Assert-SqlLogin @PSBoundParameters
Assert-SqlLogin @assertSqlLoginParameters

Invoke-Query @invokeQueryParameters -Query (
'CREATE USER [{0}] FOR LOGIN [{1}];' -f $Name, $LoginName
Expand Down Expand Up @@ -444,7 +456,7 @@ function Set-TargetResource
#>
function Test-TargetResource
{
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification = 'The command Connect-Sql is called when Get-TargetResource is called')]
[CmdletBinding()]
[OutputType([System.Boolean])]
param
Expand Down Expand Up @@ -735,11 +747,7 @@ function Assert-SqlLogin

[Parameter(Mandatory = $true)]
[System.String]
$LoginName,

# Catch all other splatted parameters from $PSBoundParameters
[Parameter(ValueFromRemainingArguments = $true)]
$RemainingArguments
$LoginName
)

$sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName
Expand Down
144 changes: 120 additions & 24 deletions tests/Unit/DSC_SqlDatabaseUser.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -523,19 +523,60 @@ try
}

Context 'When creating a database user with a login' {
BeforeAll {
$setTargetResourceParameters = $mockDefaultParameters.Clone()
$setTargetResourceParameters['LoginName'] = $mockLoginName
$setTargetResourceParameters['UserType'] = 'Login'
Context 'When calling an using the default ServerName' {
BeforeAll {
$setTargetResourceParameters = $mockDefaultParameters.Clone()
$setTargetResourceParameters['LoginName'] = $mockLoginName
$setTargetResourceParameters['UserType'] = 'Login'

<#
Make sure to use the default value for ServerName.
Regression test for issue #1647.
#>
$setTargetResourceParameters.Remove('ServerName')
}

It 'Should not throw and should call the correct mocks' {
{ Set-TargetResource @setTargetResourceParameters } | Should -Not -Throw

Assert-MockCalled -CommandName Get-TargetResource -Exactly -Times 1 -Scope It
Assert-MockCalled -CommandName Invoke-Query -ParameterFilter {
$Query -eq ('CREATE USER [{0}] FOR LOGIN [{1}];' -f $mockName, $mockLoginName)
} -Exactly -Times 1 -Scope It

Assert-MockCalled -CommandName Assert-SqlLogin -ParameterFilter {
$ServerName -eq (Get-ComputerName)
} -Exactly -Times 1 -Scope It
}
}

It 'Should not throw and should call the correct mocks' {
{ Set-TargetResource @setTargetResourceParameters } | Should -Not -Throw
Context 'When calling with an explicit ServerName' {
BeforeAll {
$expectedServerName = 'host.company.local'

Assert-MockCalled -CommandName Get-TargetResource -Exactly -Times 1 -Scope It
Assert-MockCalled -CommandName Invoke-Query -ParameterFilter {
$Query -eq ('CREATE USER [{0}] FOR LOGIN [{1}];' -f $mockName, $mockLoginName)
} -Exactly -Times 1 -Scope It
$setTargetResourceParameters = $mockDefaultParameters.Clone()
$setTargetResourceParameters['LoginName'] = $mockLoginName
$setTargetResourceParameters['UserType'] = 'Login'

<#
Set an explicit value for ServerName.
Regression test for issue #1647.
#>
$setTargetResourceParameters['ServerName'] = $expectedServerName
}

It 'Should not throw and should call the correct mocks' {
{ Set-TargetResource @setTargetResourceParameters } | Should -Not -Throw

Assert-MockCalled -CommandName Get-TargetResource -Exactly -Times 1 -Scope It
Assert-MockCalled -CommandName Invoke-Query -ParameterFilter {
$Query -eq ('CREATE USER [{0}] FOR LOGIN [{1}];' -f $mockName, $mockLoginName)
} -Exactly -Times 1 -Scope It

Assert-MockCalled -CommandName Assert-SqlLogin -ParameterFilter {
$ServerName -eq $expectedServerName
} -Exactly -Times 1 -Scope It
}
}
}

Expand Down Expand Up @@ -628,23 +669,70 @@ try
LoginType = 'WindowsUser'
}
}
}

$setTargetResourceParameters = $mockDefaultParameters.Clone()
$setTargetResourceParameters['LoginName'] = 'OtherLogin1'
$setTargetResourceParameters['UserType'] = 'Login'
Context 'When calling an using the default ServerName' {
BeforeAll {
$setTargetResourceParameters = $mockDefaultParameters.Clone()
$setTargetResourceParameters['LoginName'] = 'OtherLogin1'
$setTargetResourceParameters['UserType'] = 'Login'

<#
Make sure to use the default value for ServerName.
Regression test for issue #1647.
#>
$setTargetResourceParameters.Remove('ServerName')
}

It 'Should not throw and should call the correct mocks' {
{ Set-TargetResource @setTargetResourceParameters } | Should -Not -Throw

Assert-MockCalled -CommandName Get-TargetResource -Exactly -Times 1 -Scope It
Assert-MockCalled -CommandName Invoke-Query -ParameterFilter {
$Query -eq ('ALTER USER [{0}] WITH NAME = [{1}], LOGIN = [{2}];' -f $mockName, $mockName, 'OtherLogin1')
} -Exactly -Times 1 -Scope It

Assert-MockCalled -CommandName Assert-SqlLogin -ParameterFilter {
$ServerName -eq (Get-ComputerName)
} -Exactly -Times 1 -Scope It
}
}

It 'Should not throw and should call the correct mocks' {
{ Set-TargetResource @setTargetResourceParameters } | Should -Not -Throw
Context 'When calling with an explicit ServerName' {
BeforeAll {
$expectedServerName = 'host.company.local'

Assert-MockCalled -CommandName Get-TargetResource -Exactly -Times 1 -Scope It
Assert-MockCalled -CommandName Invoke-Query -ParameterFilter {
$Query -eq ('ALTER USER [{0}] WITH NAME = [{1}], LOGIN = [{2}];' -f $mockName, $mockName, 'OtherLogin1')
} -Exactly -Times 1 -Scope It
$setTargetResourceParameters = $mockDefaultParameters.Clone()
$setTargetResourceParameters['LoginName'] = 'OtherLogin1'
$setTargetResourceParameters['UserType'] = 'Login'

<#
Set an explicit value for ServerName.
Regression test for issue #1647.
#>
$setTargetResourceParameters['ServerName'] = $expectedServerName
}

It 'Should not throw and should call the correct mocks' {
{ Set-TargetResource @setTargetResourceParameters } | Should -Not -Throw

Assert-MockCalled -CommandName Get-TargetResource -Exactly -Times 1 -Scope It
Assert-MockCalled -CommandName Invoke-Query -ParameterFilter {
$Query -eq ('ALTER USER [{0}] WITH NAME = [{1}], LOGIN = [{2}];' -f $mockName, $mockName, 'OtherLogin1')
} -Exactly -Times 1 -Scope It

Assert-MockCalled -CommandName Assert-SqlLogin -ParameterFilter {
$ServerName -eq $expectedServerName
} -Exactly -Times 1 -Scope It
}
}

Context 'When trying to alter the login name but Invoke-Query fails' {
BeforeAll {
$setTargetResourceParameters = $mockDefaultParameters.Clone()
$setTargetResourceParameters['LoginName'] = 'OtherLogin1'
$setTargetResourceParameters['UserType'] = 'Login'

Mock -CommandName Invoke-Query -MockWith {
throw
}
Expand All @@ -657,7 +745,7 @@ try

Assert-MockCalled -CommandName Get-TargetResource -Exactly -Times 1 -Scope It
Assert-MockCalled -CommandName Invoke-Query -Exactly -Times 1 -Scope It
}
}
}
}

Expand Down Expand Up @@ -959,8 +1047,12 @@ try

Context 'When the SQL login exist' {
BeforeAll {
$assertSqlLoginParameters = $mockDefaultParameters.Clone()
$assertSqlLoginParameters['LoginName'] = $mockLoginName
$assertSqlLoginParameters = @{
InstanceName = $mockInstanceName
ServerName = $mockServerName
LoginName = $mockLoginName
Verbose = $true
}
}

It 'Should not throw any error' {
Expand All @@ -970,8 +1062,12 @@ try

Context 'When the SQL login does not exist' {
BeforeAll {
$assertSqlLoginParameters = $mockDefaultParameters.Clone()
$assertSqlLoginParameters['LoginName'] = 'AnyValue'
$assertSqlLoginParameters = @{
InstanceName = $mockInstanceName
ServerName = $mockServerName
LoginName = 'AnyValue'
Verbose = $true
}
}

It 'Should throw the correct error' {
Expand Down

0 comments on commit f7676b0

Please sign in to comment.