Skip to content

Commit

Permalink
[dsccommunity#1474] Added unit tests for SqlServerLogin.DefaultDatabase
Browse files Browse the repository at this point in the history
  • Loading branch information
bozho committed Mar 31, 2020
1 parent 12cc084 commit 502df1a
Showing 1 changed file with 67 additions and 19 deletions.
86 changes: 67 additions & 19 deletions tests/Unit/MSFT_SqlServerLogin.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 |
Expand All @@ -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
)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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' )
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 502df1a

Please sign in to comment.