From 2c1b07a03e233c490ef08d810c578b8cbd04a31b Mon Sep 17 00:00:00 2001 From: Marko Bozikovic Date: Thu, 26 Mar 2020 16:35:13 +0100 Subject: [PATCH 1/6] [#1474] SqlServerLogin: Added DefaultDatabase parameter --- CHANGELOG.md | 5 ++ .../MSFT_SqlServerLogin.psm1 | 53 ++++++++++++++++--- .../MSFT_SqlServerLogin.schema.mof | 1 + .../en-US/MSFT_SqlServerLogin.strings.psd1 | 1 + .../MSFT_SqlServerLogin.Integration.Tests.ps1 | 1 + .../MSFT_SqlServerLogin.config.ps1 | 16 ++++++ tests/Unit/Stubs/SMO.cs | 1 + 7 files changed, 70 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a13c9951..70b197084 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md) ## [Unreleased] +### Added + +- SqlServerLogin + - Added `DefaultDatabase` parameter ([issue #1474](https://github.com/dsccommunity/SqlServerDsc/issues/1474).) + ### Changed - SqlServerDsc diff --git a/source/DSCResources/MSFT_SqlServerLogin/MSFT_SqlServerLogin.psm1 b/source/DSCResources/MSFT_SqlServerLogin/MSFT_SqlServerLogin.psm1 index dd5d6f71c..6b8fb8d6e 100644 --- a/source/DSCResources/MSFT_SqlServerLogin/MSFT_SqlServerLogin.psm1 +++ b/source/DSCResources/MSFT_SqlServerLogin/MSFT_SqlServerLogin.psm1 @@ -62,12 +62,13 @@ function Get-TargetResource ) $returnValue = @{ - Ensure = $ensure - Name = $Name - LoginType = $login.LoginType - ServerName = $ServerName - InstanceName = $InstanceName - Disabled = $login.IsDisabled + Ensure = $ensure + Name = $Name + LoginType = $login.LoginType + ServerName = $ServerName + InstanceName = $InstanceName + Disabled = $login.IsDisabled + DefaultDatabase = $login.DefaultDatabase } if ($login.LoginType -eq 'SqlLogin') @@ -113,6 +114,9 @@ function Get-TargetResource .PARAMETER Disabled Specifies if the login is disabled. Default is $false. + + .PARAMETER DefaultDatabase + Specifies the default database for the login. #> function Set-TargetResource { @@ -167,7 +171,11 @@ function Set-TargetResource [Parameter()] [System.Boolean] - $Disabled + $Disabled, + + [Parameter()] + [System.String] + $DefaultDatabase ) $serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName @@ -232,6 +240,12 @@ function Set-TargetResource $login.Enable() } } + + if ( $PSBoundParameers.ContainsKey('DefaultDatabase') -and ($login.DefaultDatabase -ne $DefaultDatabase) ) + { + $login.DefaultDatabase = $DefaultDatabase + Update-SQLServerLogin -Login $login + } } else { @@ -295,6 +309,13 @@ function Set-TargetResource $login.Disable() } + + # set the default database if specified + if ( $PSBoundParameers.ContainsKey('DefaultDatabase') ) + { + $login.DefaultDatabase = $DefaultDatabase + Update-SQLServerLogin -Login $login + } } } @@ -345,6 +366,9 @@ function Set-TargetResource .PARAMETER Disabled Specifies if the login is disabled. Default is $false. + + .PARAMETER DefaultDatabase + Specifies the default database for the login. #> function Test-TargetResource { @@ -399,7 +423,11 @@ function Test-TargetResource [Parameter()] [System.Boolean] - $Disabled + $Disabled, + + [Parameter()] + [System.String] + $DefaultDatabase ) Write-Verbose -Message ( @@ -455,6 +483,15 @@ function Test-TargetResource $testPassed = $false } + if ( $PSBoundParameters.ContainsKey('DefaultDatabase') -and ($loginInfo.DefaultDatabase -ne $DefaultDatabase) ) + { + Write-Verbose -Message ( + $script:localizedData.WrongDefaultDatabase -f $Name, $loginInfo.DefaultDatabase, $DefaultDatabase + ) + + $testPassed = $false + } + if ( $LoginType -eq 'SqlLogin' ) { if ( $LoginPasswordExpirationEnabled -ne $loginInfo.LoginPasswordExpirationEnabled ) diff --git a/source/DSCResources/MSFT_SqlServerLogin/MSFT_SqlServerLogin.schema.mof b/source/DSCResources/MSFT_SqlServerLogin/MSFT_SqlServerLogin.schema.mof index f687e0963..bc336633a 100644 --- a/source/DSCResources/MSFT_SqlServerLogin/MSFT_SqlServerLogin.schema.mof +++ b/source/DSCResources/MSFT_SqlServerLogin/MSFT_SqlServerLogin.schema.mof @@ -13,4 +13,5 @@ class MSFT_SqlServerLogin : OMI_BaseResource [Write, Description("Specifies if the login password is required to expire in accordance to the operating system security policy. Only applies to SQL Logins. Default is $true.")] Boolean LoginPasswordExpirationEnabled; [Write, Description("Specifies if the login password is required to conform to the password policy specified in the system security policy. Only applies to SQL Logins. Default is $true.")] Boolean LoginPasswordPolicyEnforced; [Write, Description("Specifies if the login is disabled. Default is $false.")] Boolean Disabled; + [Write, Description("Default database name.")] String DefaultDatabase; }; diff --git a/source/DSCResources/MSFT_SqlServerLogin/en-US/MSFT_SqlServerLogin.strings.psd1 b/source/DSCResources/MSFT_SqlServerLogin/en-US/MSFT_SqlServerLogin.strings.psd1 index a5d61cd0d..f11465e91 100644 --- a/source/DSCResources/MSFT_SqlServerLogin/en-US/MSFT_SqlServerLogin.strings.psd1 +++ b/source/DSCResources/MSFT_SqlServerLogin/en-US/MSFT_SqlServerLogin.strings.psd1 @@ -18,6 +18,7 @@ ConvertFrom-StringData @' WrongLoginType = The login '{0}' has the login type '{1}', but expected it to have the login type '{2}'. ExpectedDisabled = Expected the login '{0}' to be disabled, but it is enabled. ExpectedEnabled = Expected the login '{0}' to be enabled, but it is disabled. + WrongDefaultDatabase = The login '{0}' has the default database '{1}', but expected it to have the default database '{2}'. ExpectedLoginPasswordExpirationDisabled = The login '{0}' has the password expiration enabled, but expected it to be disabled. ExpectedLoginPasswordExpirationEnabled = The login '{0}' has the password expiration disabled, but expected it to be enabled. ExpectedLoginPasswordPolicyEnforcedDisabled = The login '{0}' has the password policy enforced enabled, but expected it to be disabled. diff --git a/tests/Integration/MSFT_SqlServerLogin.Integration.Tests.ps1 b/tests/Integration/MSFT_SqlServerLogin.Integration.Tests.ps1 index a8daec2de..39d338870 100644 --- a/tests/Integration/MSFT_SqlServerLogin.Integration.Tests.ps1 +++ b/tests/Integration/MSFT_SqlServerLogin.Integration.Tests.ps1 @@ -156,6 +156,7 @@ try $resourceCurrentState.Ensure | Should -Be 'Present' $resourceCurrentState.Name | Should -Be $ConfigurationData.AllNodes.DscUser2Name $resourceCurrentState.LoginType | Should -Be $ConfigurationData.AllNodes.DscUser2Type + $resourceCurrentState.DefaultDatabase | Should -Be $ConfigurationData.AllNodes.DefaultDbName $resourceCurrentState.Disabled | Should -Be $false } diff --git a/tests/Integration/MSFT_SqlServerLogin.config.ps1 b/tests/Integration/MSFT_SqlServerLogin.config.ps1 index a4d837b4d..8cadca465 100644 --- a/tests/Integration/MSFT_SqlServerLogin.config.ps1 +++ b/tests/Integration/MSFT_SqlServerLogin.config.ps1 @@ -24,6 +24,8 @@ else ServerName = $env:COMPUTERNAME InstanceName = 'DSCSQLTEST' + DefaultDbName = 'DefaultDb' + DscUser1Name = ('{0}\{1}' -f $env:COMPUTERNAME, 'DscUser1') DscUser1Type = 'WindowsUser' @@ -52,6 +54,7 @@ else Configuration MSFT_SqlServerLogin_CreateDependencies_Config { Import-DscResource -ModuleName 'PSDscResources' -ModuleVersion '2.12.0.0' + Import-DscResource -ModuleName 'SqlServerDsc' node $AllNodes.NodeName { @@ -99,6 +102,18 @@ Configuration MSFT_SqlServerLogin_CreateDependencies_Config '[User]CreateDscUser2' ) } + + SqlDatabase 'DefaultDb_Test' + { + Ensure = 'Present' + ServerName = $Node.ServerName + InstanceName = $Node.InstanceName + Name = $Node.DefaultDbName + + PsDscRunAsCredential = New-Object ` + -TypeName System.Management.Automation.PSCredential ` + -ArgumentList @($Node.Admin_UserName, (ConvertTo-SecureString -String $Node.Admin_Password -AsPlainText -Force)) + } } } @@ -143,6 +158,7 @@ Configuration MSFT_SqlServerLogin_AddLoginDscUser2_Config Ensure = 'Present' Name = $Node.DscUser2Name LoginType = $Node.DscUser2Type + DefaultDatabase = $Node.DefaultDbName ServerName = $Node.ServerName InstanceName = $Node.InstanceName diff --git a/tests/Unit/Stubs/SMO.cs b/tests/Unit/Stubs/SMO.cs index 02ba17e86..10bca2608 100644 --- a/tests/Unit/Stubs/SMO.cs +++ b/tests/Unit/Stubs/SMO.cs @@ -351,6 +351,7 @@ public class Login public bool PasswordPolicyEnforced = false; public bool PasswordExpirationEnabled = false; public bool IsDisabled = false; + public string DefaultDatabase; public string MockName; public LoginType MockLoginType; From f83a913f2180286a5dc1ca50679fa5cbc8c61188 Mon Sep 17 00:00:00 2001 From: Marko Bozikovic Date: Fri, 27 Mar 2020 18:08:41 +0100 Subject: [PATCH 2/6] [#1474] Fixed two typos --- .../DSCResources/MSFT_SqlServerLogin/MSFT_SqlServerLogin.psm1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/DSCResources/MSFT_SqlServerLogin/MSFT_SqlServerLogin.psm1 b/source/DSCResources/MSFT_SqlServerLogin/MSFT_SqlServerLogin.psm1 index 6b8fb8d6e..ee7f91a6e 100644 --- a/source/DSCResources/MSFT_SqlServerLogin/MSFT_SqlServerLogin.psm1 +++ b/source/DSCResources/MSFT_SqlServerLogin/MSFT_SqlServerLogin.psm1 @@ -241,7 +241,7 @@ function Set-TargetResource } } - if ( $PSBoundParameers.ContainsKey('DefaultDatabase') -and ($login.DefaultDatabase -ne $DefaultDatabase) ) + if ( $PSBoundParameters.ContainsKey('DefaultDatabase') -and ($login.DefaultDatabase -ne $DefaultDatabase) ) { $login.DefaultDatabase = $DefaultDatabase Update-SQLServerLogin -Login $login @@ -311,7 +311,7 @@ function Set-TargetResource } # set the default database if specified - if ( $PSBoundParameers.ContainsKey('DefaultDatabase') ) + if ( $PSBoundParameters.ContainsKey('DefaultDatabase') ) { $login.DefaultDatabase = $DefaultDatabase Update-SQLServerLogin -Login $login From 12cc08494b2c354aab2061ea871b93fdae657070 Mon Sep 17 00:00:00 2001 From: Marko Bozikovic Date: Fri, 27 Mar 2020 18:23:01 +0100 Subject: [PATCH 3/6] [#1474] Updated README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index a0a03d281..5516515d9 100644 --- a/README.md +++ b/README.md @@ -1414,6 +1414,8 @@ No description. policy. Only applies to SQL Logins. Default is $true. * **`[Boolean]` Disabled** _(Write)_: Specifies if the login is disabled. Default is $false. +* **`[String]` DefaultDatabase** _(Write)_: Default database name. If not specified, + default database is not changed. #### Examples From 502df1a5fb79486e66958258af84a0e34464ec8a Mon Sep 17 00:00:00 2001 From: Marko Bozikovic Date: Tue, 31 Mar 2020 11:23:23 +0200 Subject: [PATCH 4/6] [#1474] Added unit tests for SqlServerLogin.DefaultDatabase --- tests/Unit/MSFT_SqlServerLogin.Tests.ps1 | 86 ++++++++++++++++++------ 1 file changed, 67 insertions(+), 19 deletions(-) diff --git a/tests/Unit/MSFT_SqlServerLogin.Tests.ps1 b/tests/Unit/MSFT_SqlServerLogin.Tests.ps1 index ac563bd54..33f71ab89 100644 --- a/tests/Unit/MSFT_SqlServerLogin.Tests.ps1 +++ b/tests/Unit/MSFT_SqlServerLogin.Tests.ps1 @@ -185,24 +185,28 @@ try $script:mockWasLoginClassMethodEnableCalled = $false $script:mockWasLoginClassMethodDisabledCalled = $false - $mockConnectSql = { + $mockConnectSQL = { $windowsUser = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login -ArgumentList @('Server', 'Windows\User1') $windowsUser.LoginType = 'WindowsUser' $windowsUser = $windowsUser | Add-Member -Name 'Disable' -MemberType ScriptMethod -Value { $script:mockWasLoginClassMethodDisabledCalled = $true } -PassThru -Force + $windowsUser.DefaultDatabase = "master" $windowsGroup = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login -ArgumentList ('Server', 'Windows\Group1') $windowsGroup.LoginType = 'windowsGroup' + $windowsGroup.DefaultDatabase = "master" $sqlLogin = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login -ArgumentList @('Server', 'SqlLogin1') $sqlLogin.LoginType = 'SqlLogin' $sqlLogin.MustChangePassword = $false + $sqlLogin.DefaultDatabase = "master" $sqlLogin.PasswordPolicyEnforced = $true $sqlLogin.PasswordExpirationEnabled = $true $sqlLoginDisabled = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login -ArgumentList @('Server', 'Windows\UserDisabled') $sqlLoginDisabled.LoginType = 'WindowsUser' + $sqlLoginDisabled.DefaultDatabase = "master" $sqlLoginDisabled.IsDisabled = $true $sqlLoginDisabled = $sqlLoginDisabled | Add-Member -Name 'Enable' -MemberType ScriptMethod -Value { $script:mockWasLoginClassMethodEnableCalled = $true @@ -228,12 +232,14 @@ try 'Windows\User1' = ( New-Object -TypeName Object | Add-Member -MemberType NoteProperty -Name 'Name' -Value 'Windows\User1' -PassThru | Add-Member -MemberType NoteProperty -Name 'LoginType' -Value 'WindowsUser' -PassThru | + Add-Member -MemberType NoteProperty -Name 'DefaultDatabase' -Value 'master' -PassThru | Add-Member -MemberType ScriptMethod -Name Alter -Value {} -PassThru | Add-Member -MemberType ScriptMethod -Name Drop -Value {} -PassThru -Force ) 'SqlLogin1' = ( New-Object -TypeName Object | Add-Member -MemberType NoteProperty -Name 'Name' -Value 'SqlLogin1' -PassThru | Add-Member -MemberType NoteProperty -Name 'LoginType' -Value 'SqlLogin' -PassThru | + Add-Member -MemberType NoteProperty -Name 'DefaultDatabase' -Value 'master' -PassThru | Add-Member -MemberType NoteProperty -Name 'MustChangePassword' -Value $false -PassThru | Add-Member -MemberType NoteProperty -Name 'PasswordExpirationEnabled' -Value $true -PassThru | Add-Member -MemberType NoteProperty -Name 'PasswordPolicyEnforced' -Value $true -PassThru | @@ -243,6 +249,7 @@ try 'Windows\Group1' = ( New-Object -TypeName Object | Add-Member -MemberType NoteProperty -Name 'Name' -Value 'Windows\Group1' -PassThru | Add-Member -MemberType NoteProperty -Name 'LoginType' -Value 'WindowsGroup' -PassThru | + Add-Member -MemberType NoteProperty -Name 'DefaultDatabase' -Value 'master' -PassThru | Add-Member -MemberType ScriptMethod -Name Alter -Value {} -PassThru | Add-Member -MemberType ScriptMethod -Name Drop -Value {} -PassThru -Force ) @@ -284,6 +291,7 @@ try $result.Ensure | Should -Be 'Present' $result.LoginType | Should -Be 'SqlLogin' + $result.DefaultDatabase | Should -Not -BeNullOrEmpty $result.LoginMustChangePassword | Should -Not -BeNullOrEmpty $result.LoginPasswordExpirationEnabled | Should -Not -BeNullOrEmpty $result.LoginPasswordPolicyEnforced | Should -Not -BeNullOrEmpty @@ -296,6 +304,7 @@ try $result.Ensure | Should -Be 'Present' $result.LoginType | Should -Be 'WindowsUser' + $result.DefaultDatabase | Should -Not -BeNullOrEmpty $result.LoginMustChangePassword | Should -BeNullOrEmpty $result.LoginPasswordExpirationEnabled | Should -BeNullOrEmpty $result.LoginPasswordPolicyEnforced | Should -BeNullOrEmpty @@ -308,6 +317,7 @@ try $result.Ensure | Should -Be 'Present' $result.LoginType | Should -Be 'WindowsGroup' + $result.DefaultDatabase | Should -Not -BeNullOrEmpty $result.LoginMustChangePassword | Should -BeNullOrEmpty $result.LoginPasswordExpirationEnabled | Should -BeNullOrEmpty $result.LoginPasswordPolicyEnforced | Should -BeNullOrEmpty @@ -322,6 +332,7 @@ try $result.Ensure | Should -Be 'Present' $result.LoginType | Should -Be 'WindowsUser' + $result.DefaultDatabase | Should -Not -BeNullOrEmpty $result.LoginMustChangePassword | Should -BeNullOrEmpty $result.LoginPasswordExpirationEnabled | Should -BeNullOrEmpty $result.LoginPasswordPolicyEnforced | Should -BeNullOrEmpty @@ -426,12 +437,13 @@ try # Override Get-TargetResource Mock -CommandName Get-TargetResource {return New-Object PSObject -Property @{ - Ensure = 'Present' - Name = $mockTestTargetResourceParameters.Name - LoginType = $mockTestTargetResourceParameters.LoginType - ServerName = 'Server1' - InstanceName = 'MSSQLERVER' - Disabled = $true + Ensure = 'Present' + Name = $mockTestTargetResourceParameters.Name + LoginType = $mockTestTargetResourceParameters.LoginType + ServerName = 'Server1' + InstanceName = 'MSSQLERVER' + Disabled = $true + DefaultDatabase = 'master' LoginMustChangePassword = $false LoginPasswordPolicyEnforced = $true LoginPasswordExpirationEnabled = $true @@ -460,12 +472,13 @@ try # Override Get-TargetResource Mock -CommandName Get-TargetResource {return New-Object PSObject -Property @{ - Ensure = 'Present' - Name = $mockTestTargetResourceParameters.Name - LoginType = $mockTestTargetResourceParameters.LoginType - ServerName = 'Server1' - InstanceName = 'MSSQLERVER' - Disabled = $true + Ensure = 'Present' + Name = $mockTestTargetResourceParameters.Name + LoginType = $mockTestTargetResourceParameters.LoginType + ServerName = 'Server1' + InstanceName = 'MSSQLERVER' + Disabled = $true + DefaultDatabase = 'master' LoginMustChangePassword = $false LoginPasswordPolicyEnforced = $true LoginPasswordExpirationEnabled = $true @@ -494,12 +507,13 @@ try # Override Get-TargetResource Mock -CommandName Get-TargetResource {return New-Object PSObject -Property @{ - Ensure = 'Present' - Name = $mockTestTargetResourceParameters.Name - LoginType = $mockTestTargetResourceParameters.LoginType - ServerName = 'Server1' - InstanceName = 'MSSQLERVER' - Disabled = $true + Ensure = 'Present' + Name = $mockTestTargetResourceParameters.Name + LoginType = $mockTestTargetResourceParameters.LoginType + ServerName = 'Server1' + InstanceName = 'MSSQLERVER' + Disabled = $true + DefaultDatabase = 'master' LoginMustChangePassword = $false LoginPasswordPolicyEnforced = $true LoginPasswordExpirationEnabled = $true @@ -603,6 +617,26 @@ try Assert-MockCalled -CommandName Connect-SQL -Scope It -Times 1 -Exactly } + It 'Should return $true when the specified SQL Login is Present and DefaultDatabase is "master"' { + $testTargetResource_SqlLoginPresentWithDefaultDatabaseMaster_EnsurePresent = $testTargetResource_SqlLoginPresentWithDefaultValues.Clone() + $testTargetResource_SqlLoginPresentWithDefaultDatabaseMaster_EnsurePresent.Add( 'Ensure', 'Present' ) + $testTargetResource_SqlLoginPresentWithDefaultDatabaseMaster_EnsurePresent.Add( 'DefaultDatabase', 'master' ) + + ( Test-TargetResource @testTargetResource_SqlLoginPresentWithDefaultDatabaseMaster_EnsurePresent ) | Should -Be $true + + Assert-MockCalled -CommandName Connect-SQL -Scope It -Times 1 -Exactly + } + + It 'Should return $false when the specified SQL Login is Present and DefaultDatabase is not "master"' { + $testTargetResource_SqlLoginPresentWithDefaultDatabaseNotMaster_EnsurePresent = $testTargetResource_SqlLoginPresentWithDefaultValues.Clone() + $testTargetResource_SqlLoginPresentWithDefaultDatabaseNotMaster_EnsurePresent.Add( 'Ensure', 'Present' ) + $testTargetResource_SqlLoginPresentWithDefaultDatabaseNotMaster_EnsurePresent.Add( 'DefaultDatabase', 'notmaster' ) + + ( Test-TargetResource @testTargetResource_SqlLoginPresentWithDefaultDatabaseNotMaster_EnsurePresent ) | Should -Be $false + + Assert-MockCalled -CommandName Connect-SQL -Scope It -Times 1 -Exactly + } + It 'Should return $true when the specified SQL Login is Present and PasswordExpirationEnabled is $true' { $testTargetResource_SqlLoginPresentWithPasswordExpirationEnabledTrue_EnsurePresent = $testTargetResource_SqlLoginPresentWithDefaultValues.Clone() $testTargetResource_SqlLoginPresentWithPasswordExpirationEnabledTrue_EnsurePresent.Add( 'Ensure', 'Present' ) @@ -1029,6 +1063,20 @@ try Assert-MockCalled -CommandName Set-SQLServerLoginPassword -Scope It -Times 1 -Exactly } + It 'Should set DefaultDatabase on the specified SQL Login if it does not match the DefaultDatabase parameter' { + Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable + + $setTargetResource_SqlLoginPresent_EnsurePresent_LoginDefaultDatabase = $setTargetResource_SqlLoginPresent.Clone() + $setTargetResource_SqlLoginPresent_EnsurePresent_LoginDefaultDatabase.Add( 'Ensure', 'Present' ) + $setTargetResource_SqlLoginPresent_EnsurePresent_LoginDefaultDatabase.Add( 'LoginCredential', $mockSqlLoginCredential ) + $setTargetResource_SqlLoginPresent_EnsurePresent_LoginDefaultDatabase.Add( 'DefaultDatabase', 'notmaster' ) + + Set-TargetResource @setTargetResource_SqlLoginPresent_EnsurePresent_LoginDefaultDatabase + + Assert-MockCalled -CommandName Connect-SQL -Scope It -Times 1 -Exactly + Assert-MockCalled -CommandName Update-SQLServerLogin -Scope It -Times 1 -Exactly + } + It 'Should set PasswordExpirationEnabled on the specified SQL Login if it does not match the LoginPasswordExpirationEnabled parameter' { Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable From ab3e5d0f9d504f52c044796503961f5d49df916b Mon Sep 17 00:00:00 2001 From: Marko Bozikovic Date: Wed, 8 Apr 2020 10:30:32 +0200 Subject: [PATCH 5/6] [#1474] Resolved CR comments --- CHANGELOG.md | 2 +- .../Resources/SqlServerLogin/1-AddLogin.ps1 | 11 + tests/Unit/MSFT_SqlServerLogin.Tests.ps1 | 320 +++++++++--------- 3 files changed, 172 insertions(+), 161 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 70b197084..cb783d810 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md) ### Added - SqlServerLogin - - Added `DefaultDatabase` parameter ([issue #1474](https://github.com/dsccommunity/SqlServerDsc/issues/1474).) + - Added `DefaultDatabase` parameter ([issue #1474](https://github.com/dsccommunity/SqlServerDsc/issues/1474)). ### Changed diff --git a/source/Examples/Resources/SqlServerLogin/1-AddLogin.ps1 b/source/Examples/Resources/SqlServerLogin/1-AddLogin.ps1 index 422192277..c8fde6c0c 100644 --- a/source/Examples/Resources/SqlServerLogin/1-AddLogin.ps1 +++ b/source/Examples/Resources/SqlServerLogin/1-AddLogin.ps1 @@ -42,6 +42,17 @@ Configuration Example Disabled = $true } + SqlServerLogin 'Add_WindowsUser_Set_Default_Database' + { + Ensure = 'Present' + Name = 'CONTOSO\WindowsUser3' + LoginType = 'WindowsUser' + ServerName = 'TestServer.company.local' + InstanceName = 'DSC' + DefaultDatabase = 'contoso' + PsDscRunAsCredential = $SqlAdministratorCredential + } + SqlServerLogin 'Add_WindowsGroup' { Ensure = 'Present' diff --git a/tests/Unit/MSFT_SqlServerLogin.Tests.ps1 b/tests/Unit/MSFT_SqlServerLogin.Tests.ps1 index 33f71ab89..bcfecde8d 100644 --- a/tests/Unit/MSFT_SqlServerLogin.Tests.ps1 +++ b/tests/Unit/MSFT_SqlServerLogin.Tests.ps1 @@ -76,107 +76,107 @@ try } $getTargetResource_UnknownSqlLogin = $instanceParameters.Clone() - $getTargetResource_UnknownSqlLogin.Add( 'Name', 'UnknownSqlLogin' ) + $getTargetResource_UnknownSqlLogin[ 'Name' ] = 'UnknownSqlLogin' $getTargetResource_UnknownWindows = $instanceParameters.Clone() - $getTargetResource_UnknownWindows.Add( 'Name', 'Windows\UserOrGroup' ) + $getTargetResource_UnknownWindows[ 'Name' ] = 'Windows\UserOrGroup' $getTargetResource_KnownSqlLogin = $instanceParameters.Clone() - $getTargetResource_KnownSqlLogin.Add( 'Name', 'SqlLogin1' ) + $getTargetResource_KnownSqlLogin[ 'Name' ] = 'SqlLogin1' $getTargetResource_KnownWindowsUser = $instanceParameters.Clone() - $getTargetResource_KnownWindowsUser.Add( 'Name', 'Windows\User1' ) + $getTargetResource_KnownWindowsUser[ 'Name' ] = 'Windows\User1' $getTargetResource_KnownWindowsGroup = $instanceParameters.Clone() - $getTargetResource_KnownWindowsGroup.Add( 'Name', 'Windows\Group1' ) + $getTargetResource_KnownWindowsGroup[ 'Name' ] = 'Windows\Group1' $testTargetResource_WindowsUserAbsent = $instanceParameters.Clone() - $testTargetResource_WindowsUserAbsent.Add( 'Name', 'Windows\UserAbsent' ) - $testTargetResource_WindowsUserAbsent.Add( 'LoginType', 'WindowsUser' ) + $testTargetResource_WindowsUserAbsent[ 'Name' ] = 'Windows\UserAbsent' + $testTargetResource_WindowsUserAbsent[ 'LoginType' ] = 'WindowsUser' $testTargetResource_WindowsGroupAbsent = $instanceParameters.Clone() - $testTargetResource_WindowsGroupAbsent.Add( 'Name', 'Windows\GroupAbsent' ) - $testTargetResource_WindowsGroupAbsent.Add( 'LoginType', 'WindowsGroup' ) + $testTargetResource_WindowsGroupAbsent[ 'Name' ] = 'Windows\GroupAbsent' + $testTargetResource_WindowsGroupAbsent[ 'LoginType' ] = 'WindowsGroup' $testTargetResource_SqlLoginAbsent = $instanceParameters.Clone() - $testTargetResource_SqlLoginAbsent.Add( 'Name', 'SqlLoginAbsent' ) - $testTargetResource_SqlLoginAbsent.Add( 'LoginType', 'SqlLogin' ) + $testTargetResource_SqlLoginAbsent[ 'Name' ] = 'SqlLoginAbsent' + $testTargetResource_SqlLoginAbsent[ 'LoginType' ] = 'SqlLogin' $testTargetResource_WindowsUserPresent = $instanceParameters.Clone() - $testTargetResource_WindowsUserPresent.Add( 'Name', 'Windows\User1' ) - $testTargetResource_WindowsUserPresent.Add( 'LoginType', 'WindowsUser' ) + $testTargetResource_WindowsUserPresent[ 'Name' ] = 'Windows\User1' + $testTargetResource_WindowsUserPresent[ 'LoginType' ] = 'WindowsUser' $testTargetResource_WindowsGroupPresent = $instanceParameters.Clone() - $testTargetResource_WindowsGroupPresent.Add( 'Name', 'Windows\Group1' ) - $testTargetResource_WindowsGroupPresent.Add( 'LoginType', 'WindowsGroup' ) + $testTargetResource_WindowsGroupPresent[ 'Name' ] = 'Windows\Group1' + $testTargetResource_WindowsGroupPresent[ 'LoginType' ] = 'WindowsGroup' $testTargetResource_SqlLoginPresentWithDefaultValues = $instanceParameters.Clone() - $testTargetResource_SqlLoginPresentWithDefaultValues.Add( 'Name', 'SqlLogin1' ) - $testTargetResource_SqlLoginPresentWithDefaultValues.Add( 'LoginType', 'SqlLogin' ) + $testTargetResource_SqlLoginPresentWithDefaultValues[ 'Name' ] = 'SqlLogin1' + $testTargetResource_SqlLoginPresentWithDefaultValues[ 'LoginType' ] = 'SqlLogin' $setTargetResource_CertificateAbsent = $instanceParameters.Clone() - $setTargetResource_CertificateAbsent.Add( 'Name', 'Certificate' ) - $setTargetResource_CertificateAbsent.Add( 'LoginType', 'Certificate' ) + $setTargetResource_CertificateAbsent[ 'Name' ] = 'Certificate' + $setTargetResource_CertificateAbsent[ 'LoginType' ] = 'Certificate' $setTargetResource_WindowsUserAbsent = $instanceParameters.Clone() - $setTargetResource_WindowsUserAbsent.Add( 'Name', 'Windows\UserAbsent' ) - $setTargetResource_WindowsUserAbsent.Add( 'LoginType', 'WindowsUser' ) + $setTargetResource_WindowsUserAbsent[ 'Name' ] = 'Windows\UserAbsent' + $setTargetResource_WindowsUserAbsent[ 'LoginType' ] = 'WindowsUser' $setTargetResource_WindowsGroupAbsent = $instanceParameters.Clone() - $setTargetResource_WindowsGroupAbsent.Add( 'Name', 'Windows\GroupAbsent' ) - $setTargetResource_WindowsGroupAbsent.Add( 'LoginType', 'WindowsGroup' ) + $setTargetResource_WindowsGroupAbsent[ 'Name' ] = 'Windows\GroupAbsent' + $setTargetResource_WindowsGroupAbsent[ 'LoginType' ] = 'WindowsGroup' $setTargetResource_SqlLoginAbsent = $instanceParameters.Clone() - $setTargetResource_SqlLoginAbsent.Add( 'Name', 'SqlLoginAbsent' ) - $setTargetResource_SqlLoginAbsent.Add( 'LoginType', 'SqlLogin' ) + $setTargetResource_SqlLoginAbsent[ 'Name' ] = 'SqlLoginAbsent' + $setTargetResource_SqlLoginAbsent[ 'LoginType' ] = 'SqlLogin' $setTargetResource_SqlLoginAbsentExisting = $instanceParameters.Clone() - $setTargetResource_SqlLoginAbsentExisting.Add( 'Name', 'Existing' ) - $setTargetResource_SqlLoginAbsentExisting.Add( 'LoginType', 'SqlLogin' ) + $setTargetResource_SqlLoginAbsentExisting[ 'Name' ] = 'Existing' + $setTargetResource_SqlLoginAbsentExisting[ 'LoginType' ] = 'SqlLogin' $setTargetResource_SqlLoginAbsentUnknown = $instanceParameters.Clone() - $setTargetResource_SqlLoginAbsentUnknown.Add( 'Name', 'Unknown' ) - $setTargetResource_SqlLoginAbsentUnknown.Add( 'LoginType', 'SqlLogin' ) + $setTargetResource_SqlLoginAbsentUnknown[ 'Name' ] = 'Unknown' + $setTargetResource_SqlLoginAbsentUnknown[ 'LoginType' ] = 'SqlLogin' $setTargetResource_WindowsUserPresent = $instanceParameters.Clone() - $setTargetResource_WindowsUserPresent.Add( 'Name', 'Windows\User1' ) - $setTargetResource_WindowsUserPresent.Add( 'LoginType', 'WindowsUser' ) + $setTargetResource_WindowsUserPresent[ 'Name' ] = 'Windows\User1' + $setTargetResource_WindowsUserPresent[ 'LoginType' ] = 'WindowsUser' $setTargetResource_CertificateAbsent = $instanceParameters.Clone() - $setTargetResource_CertificateAbsent.Add( 'Name', 'Certificate' ) - $setTargetResource_CertificateAbsent.Add( 'LoginType', 'Certificate' ) + $setTargetResource_CertificateAbsent[ 'Name' ] = 'Certificate' + $setTargetResource_CertificateAbsent[ 'LoginType' ] = 'Certificate' $setTargetResource_WindowsUserAbsent = $instanceParameters.Clone() - $setTargetResource_WindowsUserAbsent.Add( 'Name', 'Windows\UserAbsent' ) - $setTargetResource_WindowsUserAbsent.Add( 'LoginType', 'WindowsUser' ) + $setTargetResource_WindowsUserAbsent[ 'Name' ] = 'Windows\UserAbsent' + $setTargetResource_WindowsUserAbsent[ 'LoginType' ] = 'WindowsUser' $setTargetResource_WindowsGroupAbsent = $instanceParameters.Clone() - $setTargetResource_WindowsGroupAbsent.Add( 'Name', 'Windows\GroupAbsent' ) - $setTargetResource_WindowsGroupAbsent.Add( 'LoginType', 'WindowsGroup' ) + $setTargetResource_WindowsGroupAbsent[ 'Name' ] = 'Windows\GroupAbsent' + $setTargetResource_WindowsGroupAbsent[ 'LoginType' ] = 'WindowsGroup' $setTargetResource_SqlLoginAbsent = $instanceParameters.Clone() - $setTargetResource_SqlLoginAbsent.Add( 'Name', 'SqlLoginAbsent' ) - $setTargetResource_SqlLoginAbsent.Add( 'LoginType', 'SqlLogin' ) + $setTargetResource_SqlLoginAbsent[ 'Name' ] = 'SqlLoginAbsent' + $setTargetResource_SqlLoginAbsent[ 'LoginType' ] = 'SqlLogin' $setTargetResource_SqlLoginAbsentExisting = $instanceParameters.Clone() - $setTargetResource_SqlLoginAbsentExisting.Add( 'Name', 'Existing' ) - $setTargetResource_SqlLoginAbsentExisting.Add( 'LoginType', 'SqlLogin' ) + $setTargetResource_SqlLoginAbsentExisting[ 'Name' ] = 'Existing' + $setTargetResource_SqlLoginAbsentExisting[ 'LoginType' ] = 'SqlLogin' $setTargetResource_SqlLoginAbsentUnknown = $instanceParameters.Clone() - $setTargetResource_SqlLoginAbsentUnknown.Add( 'Name', 'Unknown' ) - $setTargetResource_SqlLoginAbsentUnknown.Add( 'LoginType', 'SqlLogin' ) + $setTargetResource_SqlLoginAbsentUnknown[ 'Name' ] = 'Unknown' + $setTargetResource_SqlLoginAbsentUnknown[ 'LoginType' ] = 'SqlLogin' $setTargetResource_WindowsUserPresent = $instanceParameters.Clone() - $setTargetResource_WindowsUserPresent.Add( 'Name', 'Windows\User1' ) - $setTargetResource_WindowsUserPresent.Add( 'LoginType', 'WindowsUser' ) + $setTargetResource_WindowsUserPresent[ 'Name' ] = 'Windows\User1' + $setTargetResource_WindowsUserPresent[ 'LoginType' ] = 'WindowsUser' $setTargetResource_WindowsGroupPresent = $instanceParameters.Clone() - $setTargetResource_WindowsGroupPresent.Add( 'Name', 'Windows\Group1' ) - $setTargetResource_WindowsGroupPresent.Add( 'LoginType', 'WindowsGroup' ) + $setTargetResource_WindowsGroupPresent[ 'Name' ] = 'Windows\Group1' + $setTargetResource_WindowsGroupPresent[ 'LoginType' ] = 'WindowsGroup' $setTargetResource_SqlLoginPresent = $instanceParameters.Clone() - $setTargetResource_SqlLoginPresent.Add( 'Name', 'SqlLogin1' ) - $setTargetResource_SqlLoginPresent.Add( 'LoginType', 'SqlLogin' ) + $setTargetResource_SqlLoginPresent[ 'Name' ] = 'SqlLogin1' + $setTargetResource_SqlLoginPresent[ 'LoginType' ] = 'SqlLogin' <# These are set when the mocked methods Enable() and Disabled() are called. @@ -191,22 +191,22 @@ try $windowsUser = $windowsUser | Add-Member -Name 'Disable' -MemberType ScriptMethod -Value { $script:mockWasLoginClassMethodDisabledCalled = $true } -PassThru -Force - $windowsUser.DefaultDatabase = "master" + $windowsUser.DefaultDatabase = 'master' $windowsGroup = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login -ArgumentList ('Server', 'Windows\Group1') $windowsGroup.LoginType = 'windowsGroup' - $windowsGroup.DefaultDatabase = "master" + $windowsGroup.DefaultDatabase = 'master' $sqlLogin = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login -ArgumentList @('Server', 'SqlLogin1') $sqlLogin.LoginType = 'SqlLogin' $sqlLogin.MustChangePassword = $false - $sqlLogin.DefaultDatabase = "master" + $sqlLogin.DefaultDatabase = 'master' $sqlLogin.PasswordPolicyEnforced = $true $sqlLogin.PasswordExpirationEnabled = $true $sqlLoginDisabled = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login -ArgumentList @('Server', 'Windows\UserDisabled') $sqlLoginDisabled.LoginType = 'WindowsUser' - $sqlLoginDisabled.DefaultDatabase = "master" + $sqlLoginDisabled.DefaultDatabase = 'master' $sqlLoginDisabled.IsDisabled = $true $sqlLoginDisabled = $sqlLoginDisabled | Add-Member -Name 'Enable' -MemberType ScriptMethod -Value { $script:mockWasLoginClassMethodEnableCalled = $true @@ -291,7 +291,7 @@ try $result.Ensure | Should -Be 'Present' $result.LoginType | Should -Be 'SqlLogin' - $result.DefaultDatabase | Should -Not -BeNullOrEmpty + $result.DefaultDatabase | Should -Be 'master' $result.LoginMustChangePassword | Should -Not -BeNullOrEmpty $result.LoginPasswordExpirationEnabled | Should -Not -BeNullOrEmpty $result.LoginPasswordPolicyEnforced | Should -Not -BeNullOrEmpty @@ -304,7 +304,7 @@ try $result.Ensure | Should -Be 'Present' $result.LoginType | Should -Be 'WindowsUser' - $result.DefaultDatabase | Should -Not -BeNullOrEmpty + $result.DefaultDatabase | Should -Be 'master' $result.LoginMustChangePassword | Should -BeNullOrEmpty $result.LoginPasswordExpirationEnabled | Should -BeNullOrEmpty $result.LoginPasswordPolicyEnforced | Should -BeNullOrEmpty @@ -317,7 +317,7 @@ try $result.Ensure | Should -Be 'Present' $result.LoginType | Should -Be 'WindowsGroup' - $result.DefaultDatabase | Should -Not -BeNullOrEmpty + $result.DefaultDatabase | Should -Be 'master' $result.LoginMustChangePassword | Should -BeNullOrEmpty $result.LoginPasswordExpirationEnabled | Should -BeNullOrEmpty $result.LoginPasswordPolicyEnforced | Should -BeNullOrEmpty @@ -327,12 +327,12 @@ try It 'Should be return the correct values when a login is disabled' { $mockGetTargetResourceParameters = $instanceParameters.Clone() - $mockGetTargetResourceParameters.Add( 'Name', 'Windows\UserDisabled' ) + $mockGetTargetResourceParameters[ 'Name' ] = 'Windows\UserDisabled' $result = Get-TargetResource @mockGetTargetResourceParameters $result.Ensure | Should -Be 'Present' $result.LoginType | Should -Be 'WindowsUser' - $result.DefaultDatabase | Should -Not -BeNullOrEmpty + $result.DefaultDatabase | Should -Be 'master' $result.LoginMustChangePassword | Should -BeNullOrEmpty $result.LoginPasswordExpirationEnabled | Should -BeNullOrEmpty $result.LoginPasswordPolicyEnforced | Should -BeNullOrEmpty @@ -349,7 +349,7 @@ try Context 'When the desired state is Absent' { It 'Should return $true when the specified Windows user is Absent' { $testTargetResource_WindowsUserAbsent_EnsureAbsent = $testTargetResource_WindowsUserAbsent.Clone() - $testTargetResource_WindowsUserAbsent_EnsureAbsent.Add( 'Ensure', 'Absent' ) + $testTargetResource_WindowsUserAbsent_EnsureAbsent[ 'Ensure' ] = 'Absent' ( Test-TargetResource @testTargetResource_WindowsUserAbsent_EnsureAbsent ) | Should -Be $true @@ -358,7 +358,7 @@ try It 'Should return $true when the specified Windows group is Absent' { $testTargetResource_WindowsGroupAbsent_EnsureAbsent = $testTargetResource_WindowsGroupAbsent.Clone() - $testTargetResource_WindowsGroupAbsent_EnsureAbsent.Add( 'Ensure', 'Absent' ) + $testTargetResource_WindowsGroupAbsent_EnsureAbsent[ 'Ensure' ] = 'Absent' ( Test-TargetResource @testTargetResource_WindowsGroupAbsent_EnsureAbsent ) | Should -Be $true @@ -367,7 +367,7 @@ try It 'Should return $true when the specified SQL Login is Absent' { $testTargetResource_SqlLoginAbsent_EnsureAbsent = $testTargetResource_SqlLoginAbsent.Clone() - $testTargetResource_SqlLoginAbsent_EnsureAbsent.Add( 'Ensure', 'Absent' ) + $testTargetResource_SqlLoginAbsent_EnsureAbsent[ 'Ensure' ] = 'Absent' ( Test-TargetResource @testTargetResource_SqlLoginAbsent_EnsureAbsent ) | Should -Be $true @@ -376,7 +376,7 @@ try It 'Should return $false when the specified Windows user is Present' { $testTargetResource_WindowsUserPresent_EnsureAbsent = $testTargetResource_WindowsUserPresent.Clone() - $testTargetResource_WindowsUserPresent_EnsureAbsent.Add( 'Ensure', 'Absent' ) + $testTargetResource_WindowsUserPresent_EnsureAbsent[ 'Ensure' ] = 'Absent' ( Test-TargetResource @testTargetResource_WindowsUserPresent_EnsureAbsent ) | Should -Be $false @@ -385,7 +385,7 @@ try It 'Should return $false when the specified Windows group is Present' { $testTargetResource_WindowsGroupPresent_EnsureAbsent = $testTargetResource_WindowsGroupPresent.Clone() - $testTargetResource_WindowsGroupPresent_EnsureAbsent.Add( 'Ensure', 'Absent' ) + $testTargetResource_WindowsGroupPresent_EnsureAbsent[ 'Ensure' ] = 'Absent' ( Test-TargetResource @testTargetResource_WindowsGroupPresent_EnsureAbsent ) | Should -Be $false @@ -394,7 +394,7 @@ try It 'Should return $false when the specified SQL Login is Present' { $testTargetResource_SqlLoginPresentWithDefaultValues_EnsureAbsent = $testTargetResource_SqlLoginPresentWithDefaultValues.Clone() - $testTargetResource_SqlLoginPresentWithDefaultValues_EnsureAbsent.Add( 'Ensure', 'Absent' ) + $testTargetResource_SqlLoginPresentWithDefaultValues_EnsureAbsent[ 'Ensure' ] = 'Absent' ( Test-TargetResource @testTargetResource_SqlLoginPresentWithDefaultValues_EnsureAbsent ) | Should -Be $false @@ -403,9 +403,9 @@ try It 'Should be return $false when a login should be disabled but are enabled' { $mockTestTargetResourceParameters = $instanceParameters.Clone() - $mockTestTargetResourceParameters.Add( 'Ensure', 'Present' ) - $mockTestTargetResourceParameters.Add( 'Name', 'Windows\User1' ) - $mockTestTargetResourceParameters.Add( 'Disabled', $true ) + $mockTestTargetResourceParameters[ 'Ensure' ] = 'Present' + $mockTestTargetResourceParameters[ 'Name' ] = 'Windows\User1' + $mockTestTargetResourceParameters[ 'Disabled' ] = $true $result = Test-TargetResource @mockTestTargetResourceParameters $result | Should -Be $false @@ -415,9 +415,9 @@ try It 'Should be return $false when a login should be enabled but are disabled' { $mockTestTargetResourceParameters = $instanceParameters.Clone() - $mockTestTargetResourceParameters.Add( 'Ensure', 'Present' ) - $mockTestTargetResourceParameters.Add( 'Name', 'Windows\UserDisabled' ) - $mockTestTargetResourceParameters.Add( 'Disabled', $false ) + $mockTestTargetResourceParameters[ 'Ensure' ] = 'Present' + $mockTestTargetResourceParameters[ 'Name' ] = 'Windows\UserDisabled' + $mockTestTargetResourceParameters[ 'Disabled' ] = $false $result = Test-TargetResource @mockTestTargetResourceParameters $result | Should -Be $false @@ -427,10 +427,10 @@ try It 'Should be return $true when a login should be present but disabled' { $mockTestTargetResourceParameters = $getTargetResource_KnownSqlLogin.Clone() - $mockTestTargetResourceParameters.Add('Ensure', 'Present') - $mockTestTargetResourceParameters.Add('Disabled', $true) - $mockTestTargetResourceParameters.Add('LoginType', 'SqlLogin') - $mockTestTargetResourceParameters.Add('LoginCredential', (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @($mockTestTargetResourceParameters.Name, $mockSqlLoginPassword))) + $mockTestTargetResourceParameters[ 'Ensure' ] = 'Present' + $mockTestTargetResourceParameters[ 'Disabled' ] = $true + $mockTestTargetResourceParameters[ 'LoginType' ] = 'SqlLogin' + $mockTestTargetResourceParameters[ 'LoginCredential' ] = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @($mockTestTargetResourceParameters.Name, $mockSqlLoginPassword) # Override mock declaration Mock -CommandName Connect-SQL -MockWith {throw $mockAccountDisabledException} @@ -462,10 +462,10 @@ try It 'Should be return $false when a login should be present but disabled and password incorrect' { $mockTestTargetResourceParameters = $getTargetResource_KnownSqlLogin.Clone() - $mockTestTargetResourceParameters.Add('Ensure', 'Present') - $mockTestTargetResourceParameters.Add('Disabled', $true) - $mockTestTargetResourceParameters.Add('LoginType', 'SqlLogin') - $mockTestTargetResourceParameters.Add('LoginCredential', (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @($mockTestTargetResourceParameters.Name, $mockSqlLoginPassword))) + $mockTestTargetResourceParameters[ 'Ensure' ] = 'Present' + $mockTestTargetResourceParameters[ 'Disabled' ] = $true + $mockTestTargetResourceParameters[ 'LoginType' ] = 'SqlLogin' + $mockTestTargetResourceParameters[ 'LoginCredential' ] = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @($mockTestTargetResourceParameters.Name, $mockSqlLoginPassword) # Override mock declaration Mock -CommandName Connect-SQL -MockWith {throw $mockLoginFailedException} @@ -497,10 +497,10 @@ try It 'Should throw exception when unknown error occurred and account is disabled' { $mockTestTargetResourceParameters = $getTargetResource_KnownSqlLogin.Clone() - $mockTestTargetResourceParameters.Add('Ensure', 'Present') - $mockTestTargetResourceParameters.Add('Disabled', $true) - $mockTestTargetResourceParameters.Add('LoginType', 'SqlLogin') - $mockTestTargetResourceParameters.Add('LoginCredential', (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @($mockTestTargetResourceParameters.Name, $mockSqlLoginPassword))) + $mockTestTargetResourceParameters[ 'Ensure' ] = 'Present' + $mockTestTargetResourceParameters[ 'Disabled' ] = $true + $mockTestTargetResourceParameters[ 'LoginType' ] = 'SqlLogin' + $mockTestTargetResourceParameters[ 'LoginCredential' ] = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @($mockTestTargetResourceParameters.Name, $mockSqlLoginPassword) # Override mock declaration Mock -CommandName Connect-SQL -MockWith {throw $mockException} @@ -532,7 +532,7 @@ try Context 'When the desired state is Present' { It 'Should return $false when the specified Windows user is Absent' { $testTargetResource_WindowsUserAbsent_EnsurePresent = $testTargetResource_WindowsUserAbsent.Clone() - $testTargetResource_WindowsUserAbsent_EnsurePresent.Add( 'Ensure', 'Present' ) + $testTargetResource_WindowsUserAbsent_EnsurePresent[ 'Ensure' ] = 'Present' ( Test-TargetResource @testTargetResource_WindowsUserAbsent_EnsurePresent ) | Should -Be $false @@ -543,7 +543,7 @@ try Mock -CommandName Write-Verbose -ParameterFilter {$message.contains('rather than WindowsUser')} $testTargetResource_WindowsUserAbsent_EnsurePresent = $testTargetResource_WindowsUserAbsent.Clone() - $testTargetResource_WindowsUserAbsent_EnsurePresent.Add( 'Ensure', 'Present' ) + $testTargetResource_WindowsUserAbsent_EnsurePresent[ 'Ensure' ] = 'Present' ( Test-TargetResource @testTargetResource_WindowsUserAbsent_EnsurePresent ) | Should -Be $false @@ -552,7 +552,7 @@ try It 'Should return $false when the specified Windows group is Absent' { $testTargetResource_WindowsGroupAbsent_EnsurePresent = $testTargetResource_WindowsGroupAbsent.Clone() - $testTargetResource_WindowsGroupAbsent_EnsurePresent.Add( 'Ensure', 'Present' ) + $testTargetResource_WindowsGroupAbsent_EnsurePresent[ 'Ensure' ] = 'Present' ( Test-TargetResource @testTargetResource_WindowsGroupAbsent_EnsurePresent ) | Should -Be $false @@ -563,7 +563,7 @@ try Mock -CommandName Write-Verbose -ParameterFilter {$message.contains('rather than WindowsGroup')} $testTargetResource_WindowsGroupAbsent_EnsurePresent = $testTargetResource_WindowsGroupAbsent.Clone() - $testTargetResource_WindowsGroupAbsent_EnsurePresent.Add( 'Ensure', 'Present' ) + $testTargetResource_WindowsGroupAbsent_EnsurePresent[ 'Ensure' ] = 'Present' ( Test-TargetResource @testTargetResource_WindowsGroupAbsent_EnsurePresent ) | Should -Be $false @@ -572,7 +572,7 @@ try It 'Should return $false when the specified SQL Login is Absent' { $testTargetResource_SqlLoginAbsent_EnsurePresent = $testTargetResource_SqlLoginAbsent.Clone() - $testTargetResource_SqlLoginAbsent_EnsurePresent.Add( 'Ensure', 'Present' ) + $testTargetResource_SqlLoginAbsent_EnsurePresent[ 'Ensure' ] = 'Present' ( Test-TargetResource @testTargetResource_SqlLoginAbsent_EnsurePresent ) | Should -Be $false @@ -583,7 +583,7 @@ try Mock -CommandName Write-Verbose -ParameterFilter {$message.contains('rather than SqlLogin')} $testTargetResource_SqlLoginAbsent_EnsurePresent = $testTargetResource_SqlLoginAbsent.Clone() - $testTargetResource_SqlLoginAbsent_EnsurePresent.Add( 'Ensure', 'Present' ) + $testTargetResource_SqlLoginAbsent_EnsurePresent[ 'Ensure' ] = 'Present' ( Test-TargetResource @testTargetResource_SqlLoginAbsent_EnsurePresent ) | Should -Be $false @@ -592,7 +592,7 @@ try It 'Should return $true when the specified Windows user is Present' { $testTargetResource_WindowsUserPresent_EnsurePresent = $testTargetResource_WindowsUserPresent.Clone() - $testTargetResource_WindowsUserPresent_EnsurePresent.Add( 'Ensure', 'Present' ) + $testTargetResource_WindowsUserPresent_EnsurePresent[ 'Ensure' ] = 'Present' ( Test-TargetResource @testTargetResource_WindowsUserPresent_EnsurePresent ) | Should -Be $true @@ -601,7 +601,7 @@ try It 'Should return $true when the specified Windows group is Present' { $testTargetResource_WindowsGroupPresent_EnsurePresent = $testTargetResource_WindowsGroupPresent.Clone() - $testTargetResource_WindowsGroupPresent_EnsurePresent.Add( 'Ensure', 'Present' ) + $testTargetResource_WindowsGroupPresent_EnsurePresent[ 'Ensure' ] = 'Present' ( Test-TargetResource @testTargetResource_WindowsGroupPresent_EnsurePresent ) | Should -Be $true @@ -610,7 +610,7 @@ try It 'Should return $true when the specified SQL Login is Present using default parameter values' { $testTargetResource_SqlLoginPresentWithDefaultValues_EnsurePresent = $testTargetResource_SqlLoginPresentWithDefaultValues.Clone() - $testTargetResource_SqlLoginPresentWithDefaultValues_EnsurePresent.Add( 'Ensure', 'Present' ) + $testTargetResource_SqlLoginPresentWithDefaultValues_EnsurePresent[ 'Ensure' ] = 'Present' ( Test-TargetResource @testTargetResource_SqlLoginPresentWithDefaultValues_EnsurePresent ) | Should -Be $true @@ -619,8 +619,8 @@ try It 'Should return $true when the specified SQL Login is Present and DefaultDatabase is "master"' { $testTargetResource_SqlLoginPresentWithDefaultDatabaseMaster_EnsurePresent = $testTargetResource_SqlLoginPresentWithDefaultValues.Clone() - $testTargetResource_SqlLoginPresentWithDefaultDatabaseMaster_EnsurePresent.Add( 'Ensure', 'Present' ) - $testTargetResource_SqlLoginPresentWithDefaultDatabaseMaster_EnsurePresent.Add( 'DefaultDatabase', 'master' ) + $testTargetResource_SqlLoginPresentWithDefaultDatabaseMaster_EnsurePresent[ 'Ensure' ] = 'Present' + $testTargetResource_SqlLoginPresentWithDefaultDatabaseMaster_EnsurePresent[ 'DefaultDatabase' ] = 'master' ( Test-TargetResource @testTargetResource_SqlLoginPresentWithDefaultDatabaseMaster_EnsurePresent ) | Should -Be $true @@ -629,8 +629,8 @@ try It 'Should return $false when the specified SQL Login is Present and DefaultDatabase is not "master"' { $testTargetResource_SqlLoginPresentWithDefaultDatabaseNotMaster_EnsurePresent = $testTargetResource_SqlLoginPresentWithDefaultValues.Clone() - $testTargetResource_SqlLoginPresentWithDefaultDatabaseNotMaster_EnsurePresent.Add( 'Ensure', 'Present' ) - $testTargetResource_SqlLoginPresentWithDefaultDatabaseNotMaster_EnsurePresent.Add( 'DefaultDatabase', 'notmaster' ) + $testTargetResource_SqlLoginPresentWithDefaultDatabaseNotMaster_EnsurePresent[ 'Ensure' ] = 'Present' + $testTargetResource_SqlLoginPresentWithDefaultDatabaseNotMaster_EnsurePresent[ 'DefaultDatabase' ] = 'notmaster' ( Test-TargetResource @testTargetResource_SqlLoginPresentWithDefaultDatabaseNotMaster_EnsurePresent ) | Should -Be $false @@ -639,8 +639,8 @@ try It 'Should return $true when the specified SQL Login is Present and PasswordExpirationEnabled is $true' { $testTargetResource_SqlLoginPresentWithPasswordExpirationEnabledTrue_EnsurePresent = $testTargetResource_SqlLoginPresentWithDefaultValues.Clone() - $testTargetResource_SqlLoginPresentWithPasswordExpirationEnabledTrue_EnsurePresent.Add( 'Ensure', 'Present' ) - $testTargetResource_SqlLoginPresentWithPasswordExpirationEnabledTrue_EnsurePresent.Add( 'LoginPasswordExpirationEnabled', $true ) + $testTargetResource_SqlLoginPresentWithPasswordExpirationEnabledTrue_EnsurePresent[ 'Ensure' ] = 'Present' + $testTargetResource_SqlLoginPresentWithPasswordExpirationEnabledTrue_EnsurePresent[ 'LoginPasswordExpirationEnabled' ] = $true ( Test-TargetResource @testTargetResource_SqlLoginPresentWithPasswordExpirationEnabledTrue_EnsurePresent ) | Should -Be $true @@ -649,8 +649,8 @@ try It 'Should return $false when the specified SQL Login is Present and PasswordExpirationEnabled is $false' { $testTargetResource_SqlLoginPresentWithPasswordExpirationEnabledFalse_EnsurePresent = $testTargetResource_SqlLoginPresentWithDefaultValues.Clone() - $testTargetResource_SqlLoginPresentWithPasswordExpirationEnabledFalse_EnsurePresent.Add( 'Ensure', 'Present' ) - $testTargetResource_SqlLoginPresentWithPasswordExpirationEnabledFalse_EnsurePresent.Add( 'LoginPasswordExpirationEnabled', $false ) + $testTargetResource_SqlLoginPresentWithPasswordExpirationEnabledFalse_EnsurePresent[ 'Ensure' ] = 'Present' + $testTargetResource_SqlLoginPresentWithPasswordExpirationEnabledFalse_EnsurePresent[ 'LoginPasswordExpirationEnabled' ] = $false ( Test-TargetResource @testTargetResource_SqlLoginPresentWithPasswordExpirationEnabledFalse_EnsurePresent ) | Should -Be $false @@ -659,8 +659,8 @@ try It 'Should return $true when the specified SQL Login is Present and PasswordPolicyEnforced is $true' { $testTargetResource_SqlLoginPresentWithPasswordPolicyEnforcedTrue_EnsurePresent = $testTargetResource_SqlLoginPresentWithDefaultValues.Clone() - $testTargetResource_SqlLoginPresentWithPasswordPolicyEnforcedTrue_EnsurePresent.Add( 'Ensure', 'Present' ) - $testTargetResource_SqlLoginPresentWithPasswordPolicyEnforcedTrue_EnsurePresent.Add( 'LoginPasswordPolicyEnforced', $true ) + $testTargetResource_SqlLoginPresentWithPasswordPolicyEnforcedTrue_EnsurePresent[ 'Ensure' ] = 'Present' + $testTargetResource_SqlLoginPresentWithPasswordPolicyEnforcedTrue_EnsurePresent[ 'LoginPasswordPolicyEnforced' ] = $true ( Test-TargetResource @testTargetResource_SqlLoginPresentWithPasswordPolicyEnforcedTrue_EnsurePresent ) | Should -Be $true @@ -669,8 +669,8 @@ try It 'Should return $false when the specified SQL Login is Present and PasswordPolicyEnforced is $false' { $testTargetResource_SqlLoginPresentWithPasswordPolicyEnforcedFalse_EnsurePresent = $testTargetResource_SqlLoginPresentWithDefaultValues.Clone() - $testTargetResource_SqlLoginPresentWithPasswordPolicyEnforcedFalse_EnsurePresent.Add( 'Ensure', 'Present' ) - $testTargetResource_SqlLoginPresentWithPasswordPolicyEnforcedFalse_EnsurePresent.Add( 'LoginPasswordPolicyEnforced', $false ) + $testTargetResource_SqlLoginPresentWithPasswordPolicyEnforcedFalse_EnsurePresent[ 'Ensure' ] = 'Present' + $testTargetResource_SqlLoginPresentWithPasswordPolicyEnforcedFalse_EnsurePresent[ 'LoginPasswordPolicyEnforced' ] = $false ( Test-TargetResource @testTargetResource_SqlLoginPresentWithPasswordPolicyEnforcedFalse_EnsurePresent ) | Should -Be $false @@ -679,8 +679,8 @@ try It 'Should return $true when the specified SQL Login is Present using default parameter values and the password is properly configured.' { $testTargetResource_SqlLoginPresentWithDefaultValuesGoodPw_EnsurePresent = $testTargetResource_SqlLoginPresentWithDefaultValues.Clone() - $testTargetResource_SqlLoginPresentWithDefaultValuesGoodPw_EnsurePresent.Add( 'Ensure', 'Present' ) - $testTargetResource_SqlLoginPresentWithDefaultValuesGoodPw_EnsurePresent.Add( 'LoginCredential', $mockSqlLoginCredential ) + $testTargetResource_SqlLoginPresentWithDefaultValuesGoodPw_EnsurePresent[ 'Ensure' ] = 'Present' + $testTargetResource_SqlLoginPresentWithDefaultValuesGoodPw_EnsurePresent[ 'LoginCredential' ] = $mockSqlLoginCredential ( Test-TargetResource @testTargetResource_SqlLoginPresentWithDefaultValuesGoodPw_EnsurePresent ) | Should -Be $true @@ -691,8 +691,8 @@ try Mock -CommandName Connect-SQL -MockWith { throw } -Verifiable -ParameterFilter { $SetupCredential } $testTargetResource_SqlLoginPresentWithDefaultValuesBadPw_EnsurePresent = $testTargetResource_SqlLoginPresentWithDefaultValues.Clone() - $testTargetResource_SqlLoginPresentWithDefaultValuesBadPw_EnsurePresent.Add( 'Ensure', 'Present' ) - $testTargetResource_SqlLoginPresentWithDefaultValuesBadPw_EnsurePresent.Add( 'LoginCredential', $mockSqlLoginCredentialBadPassword ) + $testTargetResource_SqlLoginPresentWithDefaultValuesBadPw_EnsurePresent[ 'Ensure' ] = 'Present' + $testTargetResource_SqlLoginPresentWithDefaultValuesBadPw_EnsurePresent[ 'LoginCredential' ] = $mockSqlLoginCredentialBadPassword ( Test-TargetResource @testTargetResource_SqlLoginPresentWithDefaultValuesBadPw_EnsurePresent ) | Should -Be $false @@ -701,9 +701,9 @@ try It 'Should be return $true when a login is enabled' { $mockTestTargetResourceParameters = $instanceParameters.Clone() - $mockTestTargetResourceParameters.Add( 'Ensure', 'Present' ) - $mockTestTargetResourceParameters.Add( 'Name', 'Windows\User1' ) - $mockTestTargetResourceParameters.Add( 'Disabled', $false ) + $mockTestTargetResourceParameters[ 'Ensure' ] = 'Present' + $mockTestTargetResourceParameters[ 'Name' ] = 'Windows\User1' + $mockTestTargetResourceParameters[ 'Disabled' ] = $false $result = Test-TargetResource @mockTestTargetResourceParameters $result | Should -Be $true @@ -713,9 +713,9 @@ try It 'Should be return $true when a login is disabled' { $mockTestTargetResourceParameters = $instanceParameters.Clone() - $mockTestTargetResourceParameters.Add( 'Ensure', 'Present' ) - $mockTestTargetResourceParameters.Add( 'Name', 'Windows\UserDisabled' ) - $mockTestTargetResourceParameters.Add( 'Disabled', $true ) + $mockTestTargetResourceParameters[ 'Ensure' ] = 'Present' + $mockTestTargetResourceParameters[ 'Name' ] = 'Windows\UserDisabled' + $mockTestTargetResourceParameters[ 'Disabled' ] = $true $result = Test-TargetResource @mockTestTargetResourceParameters $result | Should -Be $true @@ -725,14 +725,14 @@ try It 'Should be return $false when a login has the wrong login type' { $mockTestTargetResourceParameters = $instanceParameters.Clone() - $mockTestTargetResourceParameters.Add( 'Ensure', 'Present' ) + $mockTestTargetResourceParameters[ 'Ensure' ] = 'Present' <# Use WindowsLogin format here to be able to test the specific property LoginType. #> - $mockTestTargetResourceParameters.Add( 'Name', 'Windows\UserDisabled' ) - $mockTestTargetResourceParameters.Add( 'LoginType', 'SqlLogin' ) - $mockTestTargetResourceParameters.Add( 'Disabled', $true ) + $mockTestTargetResourceParameters[ 'Name' ] = 'Windows\UserDisabled' + $mockTestTargetResourceParameters[ 'LoginType' ] = 'SqlLogin' + $mockTestTargetResourceParameters[ 'Disabled' ] = $true $result = Test-TargetResource @mockTestTargetResourceParameters $result | Should -Be $false @@ -758,7 +758,7 @@ try Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable $setTargetResource_WindowsUserPresent_EnsureAbsent = $setTargetResource_WindowsUserPresent.Clone() - $setTargetResource_WindowsUserPresent_EnsureAbsent.Add( 'Ensure', 'Absent' ) + $setTargetResource_WindowsUserPresent_EnsureAbsent[ 'Ensure' ] = 'Absent' Set-TargetResource @setTargetResource_WindowsUserPresent_EnsureAbsent @@ -773,9 +773,9 @@ try Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable $mockSetTargetResourceParameters = $instanceParameters.Clone() - $mockSetTargetResourceParameters.Add( 'Ensure', 'Present' ) - $mockSetTargetResourceParameters.Add( 'Name', 'Windows\UserDisabled' ) - $mockSetTargetResourceParameters.Add( 'Disabled', $false ) + $mockSetTargetResourceParameters[ 'Ensure' ] = 'Present' + $mockSetTargetResourceParameters[ 'Name' ] = 'Windows\UserDisabled' + $mockSetTargetResourceParameters[ 'Disabled' ] = $false Set-TargetResource @mockSetTargetResourceParameters $script:mockWasLoginClassMethodEnableCalled | Should -Be $true @@ -792,9 +792,9 @@ try Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable $mockSetTargetResourceParameters = $instanceParameters.Clone() - $mockSetTargetResourceParameters.Add( 'Ensure', 'Present' ) - $mockSetTargetResourceParameters.Add( 'Name', 'Windows\User1' ) - $mockSetTargetResourceParameters.Add( 'Disabled', $true ) + $mockSetTargetResourceParameters[ 'Ensure' ] = 'Present' + $mockSetTargetResourceParameters[ 'Name' ] = 'Windows\User1' + $mockSetTargetResourceParameters[ 'Disabled' ] = $true Set-TargetResource @mockSetTargetResourceParameters $script:mockWasLoginClassMethodEnableCalled | Should -Be $false @@ -811,7 +811,7 @@ try Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable $setTargetResource_WindowsGroupPresent_EnsureAbsent = $setTargetResource_WindowsGroupPresent.Clone() - $setTargetResource_WindowsGroupPresent_EnsureAbsent.Add( 'Ensure', 'Absent' ) + $setTargetResource_WindowsGroupPresent_EnsureAbsent[ 'Ensure' ] = 'Absent' Set-TargetResource @setTargetResource_WindowsGroupPresent_EnsureAbsent @@ -826,7 +826,7 @@ try Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable $setTargetResource_SqlLoginPresent_EnsureAbsent = $setTargetResource_SqlLoginPresent.Clone() - $setTargetResource_SqlLoginPresent_EnsureAbsent.Add( 'Ensure', 'Absent' ) + $setTargetResource_SqlLoginPresent_EnsureAbsent[ 'Ensure' ] = 'Absent' Set-TargetResource @setTargetResource_SqlLoginPresent_EnsureAbsent @@ -841,7 +841,7 @@ try Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable $setTargetResource_WindowsUserAbsent_EnsureAbsent = $setTargetResource_WindowsUserAbsent.Clone() - $setTargetResource_WindowsUserAbsent_EnsureAbsent.Add( 'Ensure', 'Absent' ) + $setTargetResource_WindowsUserAbsent_EnsureAbsent[ 'Ensure' ] = 'Absent' Set-TargetResource @setTargetResource_WindowsUserAbsent_EnsureAbsent @@ -856,7 +856,7 @@ try Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable $setTargetResource_WindowsGroupAbsent_EnsureAbsent = $setTargetResource_WindowsGroupAbsent.Clone() - $setTargetResource_WindowsGroupAbsent_EnsureAbsent.Add( 'Ensure', 'Absent' ) + $setTargetResource_WindowsGroupAbsent_EnsureAbsent[ 'Ensure' ] = 'Absent' Set-TargetResource @setTargetResource_WindowsGroupAbsent_EnsureAbsent @@ -871,7 +871,7 @@ try Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable $setTargetResource_SqlLoginAbsent_EnsureAbsent = $setTargetResource_SqlLoginAbsent.Clone() - $setTargetResource_SqlLoginAbsent_EnsureAbsent.Add( 'Ensure', 'Absent' ) + $setTargetResource_SqlLoginAbsent_EnsureAbsent[ 'Ensure' ] = 'Absent' Set-TargetResource @setTargetResource_SqlLoginAbsent_EnsureAbsent @@ -893,7 +893,7 @@ try Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable $setTargetResource_WindowsUserAbsent_EnsurePresent = $setTargetResource_WindowsUserAbsent.Clone() - $setTargetResource_WindowsUserAbsent_EnsurePresent.Add( 'Ensure', 'Present' ) + $setTargetResource_WindowsUserAbsent_EnsurePresent[ 'Ensure' ] = 'Present' Set-TargetResource @setTargetResource_WindowsUserAbsent_EnsurePresent @@ -923,9 +923,9 @@ try }-Verifiable $mockSetTargetResourceParameters = $instanceParameters.Clone() - $mockSetTargetResourceParameters.Add( 'Ensure', 'Present' ) - $mockSetTargetResourceParameters.Add( 'Name', 'Windows\UserAbsent' ) - $mockSetTargetResourceParameters.Add( 'Disabled', $true ) + $mockSetTargetResourceParameters[ 'Ensure' ] = 'Present' + $mockSetTargetResourceParameters[ 'Name' ] = 'Windows\UserAbsent' + $mockSetTargetResourceParameters[ 'Disabled' ] = $true Set-TargetResource @mockSetTargetResourceParameters $script:mockWasLoginClassMethodDisabledCalled | Should -Be $true @@ -941,7 +941,7 @@ try Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable $setTargetResource_WindowsGroupAbsent_EnsurePresent = $setTargetResource_WindowsGroupAbsent.Clone() - $setTargetResource_WindowsGroupAbsent_EnsurePresent.Add( 'Ensure', 'Present' ) + $setTargetResource_WindowsGroupAbsent_EnsurePresent[ 'Ensure' ] = 'Present' Set-TargetResource @setTargetResource_WindowsGroupAbsent_EnsurePresent @@ -956,8 +956,8 @@ try Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable $setTargetResource_SqlLoginAbsent_EnsurePresent = $setTargetResource_SqlLoginAbsent.Clone() - $setTargetResource_SqlLoginAbsent_EnsurePresent.Add( 'Ensure', 'Present' ) - $setTargetResource_SqlLoginAbsent_EnsurePresent.Add( 'LoginCredential', $mockSqlLoginCredential ) + $setTargetResource_SqlLoginAbsent_EnsurePresent[ 'Ensure' ] = 'Present' + $setTargetResource_SqlLoginAbsent_EnsurePresent[ 'LoginCredential' ] = $mockSqlLoginCredential Set-TargetResource @setTargetResource_SqlLoginAbsent_EnsurePresent @@ -972,9 +972,9 @@ try Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable $setTargetResource_SqlLoginAbsent_EnsurePresent = $setTargetResource_SqlLoginAbsent.Clone() - $setTargetResource_SqlLoginAbsent_EnsurePresent.Add( 'Ensure', 'Present' ) - $setTargetResource_SqlLoginAbsent_EnsurePresent.Add( 'LoginCredential', $mockSqlLoginCredential ) - $setTargetResource_SqlLoginAbsent_EnsurePresent.Add( 'LoginMustChangePassword', $false ) + $setTargetResource_SqlLoginAbsent_EnsurePresent[ 'Ensure' ] = 'Present' + $setTargetResource_SqlLoginAbsent_EnsurePresent[ 'LoginCredential' ] = $mockSqlLoginCredential + $setTargetResource_SqlLoginAbsent_EnsurePresent[ 'LoginMustChangePassword' ] = $false Set-TargetResource @setTargetResource_SqlLoginAbsent_EnsurePresent @@ -989,7 +989,7 @@ try Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable $setTargetResource_CertificateAbsent_EnsurePresent = $setTargetResource_CertificateAbsent.Clone() - $setTargetResource_CertificateAbsent_EnsurePresent.Add( 'Ensure', 'Present' ) + $setTargetResource_CertificateAbsent_EnsurePresent[ 'Ensure' ] = 'Present' $errorMessage = $script:localizedData.LoginTypeNotImplemented -f $setTargetResource_CertificateAbsent_EnsurePresent.LoginType { Set-TargetResource @setTargetResource_CertificateAbsent_EnsurePresent } | Should -Throw $errorMessage @@ -1005,7 +1005,7 @@ try Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable $setTargetResource_SqlLoginAbsent_EnsurePresent_NoCred = $setTargetResource_SqlLoginAbsent.Clone() - $setTargetResource_SqlLoginAbsent_EnsurePresent_NoCred.Add( 'Ensure', 'Present' ) + $setTargetResource_SqlLoginAbsent_EnsurePresent_NoCred[ 'Ensure' ] = 'Present' $errorMessage = $script:localizedData.LoginCredentialNotFound -f $setTargetResource_SqlLoginAbsent_EnsurePresent_NoCred.Name { Set-TargetResource @setTargetResource_SqlLoginAbsent_EnsurePresent_NoCred } | Should -Throw $errorMessage @@ -1021,7 +1021,7 @@ try Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable $setTargetResource_WindowsUserPresent_EnsurePresent = $setTargetResource_WindowsUserPresent.Clone() - $setTargetResource_WindowsUserPresent_EnsurePresent.Add( 'Ensure', 'Present' ) + $setTargetResource_WindowsUserPresent_EnsurePresent[ 'Ensure' ] = 'Present' Set-TargetResource @setTargetResource_WindowsUserPresent_EnsurePresent @@ -1036,7 +1036,7 @@ try Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable $setTargetResource_WindowsGroupPresent_EnsurePresent = $setTargetResource_WindowsGroupPresent.Clone() - $setTargetResource_WindowsGroupPresent_EnsurePresent.Add( 'Ensure', 'Present' ) + $setTargetResource_WindowsGroupPresent_EnsurePresent[ 'Ensure' ] = 'Present' Set-TargetResource @setTargetResource_WindowsGroupPresent_EnsurePresent @@ -1051,8 +1051,8 @@ try Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable $setTargetResource_SqlLoginPresent_EnsurePresent = $setTargetResource_SqlLoginPresent.Clone() - $setTargetResource_SqlLoginPresent_EnsurePresent.Add( 'Ensure', 'Present' ) - $setTargetResource_SqlLoginPresent_EnsurePresent.Add( 'LoginCredential', $mockSqlLoginCredential ) + $setTargetResource_SqlLoginPresent_EnsurePresent[ 'Ensure' ] = 'Present' + $setTargetResource_SqlLoginPresent_EnsurePresent[ 'LoginCredential' ] = $mockSqlLoginCredential Set-TargetResource @setTargetResource_SqlLoginPresent_EnsurePresent @@ -1067,9 +1067,9 @@ try Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable $setTargetResource_SqlLoginPresent_EnsurePresent_LoginDefaultDatabase = $setTargetResource_SqlLoginPresent.Clone() - $setTargetResource_SqlLoginPresent_EnsurePresent_LoginDefaultDatabase.Add( 'Ensure', 'Present' ) - $setTargetResource_SqlLoginPresent_EnsurePresent_LoginDefaultDatabase.Add( 'LoginCredential', $mockSqlLoginCredential ) - $setTargetResource_SqlLoginPresent_EnsurePresent_LoginDefaultDatabase.Add( 'DefaultDatabase', 'notmaster' ) + $setTargetResource_SqlLoginPresent_EnsurePresent_LoginDefaultDatabase[ 'Ensure' ] = 'Present' + $setTargetResource_SqlLoginPresent_EnsurePresent_LoginDefaultDatabase[ 'LoginCredential' ] = $mockSqlLoginCredential + $setTargetResource_SqlLoginPresent_EnsurePresent_LoginDefaultDatabase[ 'DefaultDatabase' ] = 'notmaster' Set-TargetResource @setTargetResource_SqlLoginPresent_EnsurePresent_LoginDefaultDatabase @@ -1081,9 +1081,9 @@ try Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable $setTargetResource_SqlLoginPresent_EnsurePresent_LoginPasswordExpirationEnabled = $setTargetResource_SqlLoginPresent.Clone() - $setTargetResource_SqlLoginPresent_EnsurePresent_LoginPasswordExpirationEnabled.Add( 'Ensure', 'Present' ) - $setTargetResource_SqlLoginPresent_EnsurePresent_LoginPasswordExpirationEnabled.Add( 'LoginCredential', $mockSqlLoginCredential ) - $setTargetResource_SqlLoginPresent_EnsurePresent_LoginPasswordExpirationEnabled.Add( 'LoginPasswordExpirationEnabled', $false ) + $setTargetResource_SqlLoginPresent_EnsurePresent_LoginPasswordExpirationEnabled[ 'Ensure' ] = 'Present' + $setTargetResource_SqlLoginPresent_EnsurePresent_LoginPasswordExpirationEnabled[ 'LoginCredential' ] = $mockSqlLoginCredential + $setTargetResource_SqlLoginPresent_EnsurePresent_LoginPasswordExpirationEnabled[ 'LoginPasswordExpirationEnabled' ] = $false Set-TargetResource @setTargetResource_SqlLoginPresent_EnsurePresent_LoginPasswordExpirationEnabled @@ -1098,9 +1098,9 @@ try Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable $setTargetResource_SqlLoginPresent_EnsurePresent_LoginPasswordPolicyEnforced = $setTargetResource_SqlLoginPresent.Clone() - $setTargetResource_SqlLoginPresent_EnsurePresent_LoginPasswordPolicyEnforced.Add( 'Ensure', 'Present' ) - $setTargetResource_SqlLoginPresent_EnsurePresent_LoginPasswordPolicyEnforced.Add( 'LoginCredential', $mockSqlLoginCredential ) - $setTargetResource_SqlLoginPresent_EnsurePresent_LoginPasswordPolicyEnforced.Add( 'LoginPasswordPolicyEnforced', $false ) + $setTargetResource_SqlLoginPresent_EnsurePresent_LoginPasswordPolicyEnforced[ 'Ensure' ] = 'Present' + $setTargetResource_SqlLoginPresent_EnsurePresent_LoginPasswordPolicyEnforced[ 'LoginCredential' ] = $mockSqlLoginCredential + $setTargetResource_SqlLoginPresent_EnsurePresent_LoginPasswordPolicyEnforced[ 'LoginPasswordPolicyEnforced' ] = $false Set-TargetResource @setTargetResource_SqlLoginPresent_EnsurePresent_LoginPasswordPolicyEnforced @@ -1117,8 +1117,8 @@ try Mock -CommandName Connect-SQL -MockWith $mockConnectSQL_LoginMode -Verifiable $setTargetResource_SqlLoginAbsent_EnsurePresent = $setTargetResource_SqlLoginAbsent.Clone() - $setTargetResource_SqlLoginAbsent_EnsurePresent.Add( 'Ensure', 'Present' ) - $setTargetResource_SqlLoginAbsent_EnsurePresent.Add( 'LoginCredential', $mockSqlLoginCredential ) + $setTargetResource_SqlLoginAbsent_EnsurePresent[ 'Ensure' ] = 'Present' + $setTargetResource_SqlLoginAbsent_EnsurePresent[ 'LoginCredential' ] = $mockSqlLoginCredential $errorMessage = $script:localizedData.IncorrectLoginMode -f $setTargetResource_SqlLoginAbsent_EnsurePresent.ServerName, @@ -1141,8 +1141,8 @@ try Mock -CommandName Connect-SQL -MockWith $mockConnectSQL_LoginMode -Verifiable $setTargetResource_SqlLoginAbsent_EnsurePresent = $setTargetResource_SqlLoginAbsent.Clone() - $setTargetResource_SqlLoginAbsent_EnsurePresent.Add( 'Ensure', 'Present' ) - $setTargetResource_SqlLoginAbsent_EnsurePresent.Add( 'LoginCredential', $mockSqlLoginCredential ) + $setTargetResource_SqlLoginAbsent_EnsurePresent[ 'Ensure' ] = 'Present' + $setTargetResource_SqlLoginAbsent_EnsurePresent[ 'LoginCredential' ] = $mockSqlLoginCredential { Set-TargetResource @setTargetResource_SqlLoginAbsent_EnsurePresent } | Should -Not -Throw @@ -1159,8 +1159,8 @@ try Mock -CommandName Connect-SQL -MockWith $mockConnectSQL_LoginMode -Verifiable $setTargetResource_SqlLoginAbsent_EnsurePresent = $setTargetResource_SqlLoginAbsent.Clone() - $setTargetResource_SqlLoginAbsent_EnsurePresent.Add( 'Ensure', 'Present' ) - $setTargetResource_SqlLoginAbsent_EnsurePresent.Add( 'LoginCredential', $mockSqlLoginCredential ) + $setTargetResource_SqlLoginAbsent_EnsurePresent[ 'Ensure' ] = 'Present' + $setTargetResource_SqlLoginAbsent_EnsurePresent[ 'LoginCredential' ] = $mockSqlLoginCredential { Set-TargetResource @setTargetResource_SqlLoginAbsent_EnsurePresent } | Should -Not -Throw From 1779587648af0364c27c655bfeff0a2b8eea9f3c Mon Sep 17 00:00:00 2001 From: Marko Bozikovic Date: Fri, 10 Apr 2020 15:26:13 +0200 Subject: [PATCH 6/6] [#1474] Added SqlSetup integration tests cleanup. --- .../MSFT_SqlServerLogin.Integration.Tests.ps1 | 27 +++++++++++++++++++ .../MSFT_SqlServerLogin.config.ps1 | 25 +++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/tests/Integration/MSFT_SqlServerLogin.Integration.Tests.ps1 b/tests/Integration/MSFT_SqlServerLogin.Integration.Tests.ps1 index 39d338870..d63b77cb9 100644 --- a/tests/Integration/MSFT_SqlServerLogin.Integration.Tests.ps1 +++ b/tests/Integration/MSFT_SqlServerLogin.Integration.Tests.ps1 @@ -359,6 +359,33 @@ try Test-DscConfiguration -Verbose | Should -Be 'True' } } + + $configurationName = "$($script:dscResourceName)_CleanupDependencies_Config" + + Context ('When using configuration {0}' -f $configurationName) { + It 'Should compile and apply the MOF without throwing' { + { + $configurationParameters = @{ + OutputPath = $TestDrive + # The variable $ConfigurationData was dot-sourced above. + ConfigurationData = $ConfigurationData + } + + & $configurationName @configurationParameters + + $startDscConfigurationParameters = @{ + Path = $TestDrive + ComputerName = 'localhost' + Wait = $true + Verbose = $true + Force = $true + ErrorAction = 'Stop' + } + + Start-DscConfiguration @startDscConfigurationParameters + } | Should -Not -Throw + } + } } } finally diff --git a/tests/Integration/MSFT_SqlServerLogin.config.ps1 b/tests/Integration/MSFT_SqlServerLogin.config.ps1 index 8cadca465..cdc163069 100644 --- a/tests/Integration/MSFT_SqlServerLogin.config.ps1 +++ b/tests/Integration/MSFT_SqlServerLogin.config.ps1 @@ -280,3 +280,28 @@ Configuration MSFT_SqlServerLogin_RemoveLoginDscUser3_Config } } } + +<# + .SYNOPSIS + Clean up test resources so they are not interfering with + the other integration tests. +#> +Configuration MSFT_SqlServerLogin_CleanupDependencies_Config +{ + Import-DscResource -ModuleName 'SqlServerDsc' + + node $AllNodes.NodeName + { + SqlDatabase 'Remove_DefaultDb_Test' + { + Ensure = 'Absent' + ServerName = $Node.ServerName + InstanceName = $Node.InstanceName + Name = $Node.DefaultDbName + + PsDscRunAsCredential = New-Object ` + -TypeName System.Management.Automation.PSCredential ` + -ArgumentList @($Node.Admin_UserName, (ConvertTo-SecureString -String $Node.Admin_Password -AsPlainText -Force)) + } + } +}