Skip to content

Commit 8688166

Browse files
Copilotjohlju
andcommitted
Fix newlines and add comprehensive integration tests for login commands
Co-authored-by: johlju <[email protected]>
1 parent 60e4aa4 commit 8688166

12 files changed

+519
-6
lines changed

azure-pipelines.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,10 @@ stages:
282282
'tests/Integration/Commands/Connect-SqlDscDatabaseEngine.Integration.Tests.ps1'
283283
# Group 2
284284
'tests/Integration/Commands/Assert-SqlDscLogin.Integration.Tests.ps1'
285+
'tests/Integration/Commands/Disable-SqlDscLogin.Integration.Tests.ps1'
286+
'tests/Integration/Commands/Enable-SqlDscLogin.Integration.Tests.ps1'
285287
'tests/Integration/Commands/Get-SqlDscLogin.Integration.Tests.ps1'
288+
'tests/Integration/Commands/Test-SqlDscIsLoginEnabled.Integration.Tests.ps1'
286289
# Group 9
287290
'tests/Integration/Commands/Uninstall-SqlDscServer.Integration.Tests.ps1'
288291
)

source/Public/Disable-SqlDscLogin.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,4 @@ function Disable-SqlDscLogin
100100
$LoginObject.Disable()
101101
}
102102
}
103-
}
103+
}

source/Public/Enable-SqlDscLogin.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,4 @@ function Enable-SqlDscLogin
100100
$LoginObject.Enable()
101101
}
102102
}
103-
}
103+
}

source/Public/Test-SqlDscIsLoginEnabled.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,4 @@ function Test-SqlDscIsLoginEnabled
8383

8484
return $loginEnabled
8585
}
86-
}
86+
}

source/en-US/SqlServerDsc.strings.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,4 @@ ConvertFrom-StringData @'
246246
Assert_Login_LoginMissing = The principal '{0}' does not exist as a login on the instance '{1}'.
247247
Assert_Login_LoginExists = The principal '{0}' exists as a login.
248248
'@
249+
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'Suppressing this rule because Script Analyzer does not understand Pester syntax.')]
2+
param ()
3+
4+
BeforeDiscovery {
5+
try
6+
{
7+
if (-not (Get-Module -Name 'DscResource.Test'))
8+
{
9+
# Assumes dependencies have been resolved, so if this module is not available, run 'noop' task.
10+
if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable))
11+
{
12+
# Redirect all streams to $null, except the error stream (stream 2)
13+
& "$PSScriptRoot/../../../build.ps1" -Tasks 'noop' 3>&1 4>&1 5>&1 6>&1 > $null
14+
}
15+
16+
# If the dependencies have not been resolved, this will throw an error.
17+
Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop'
18+
}
19+
}
20+
catch [System.IO.FileNotFoundException]
21+
{
22+
throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks build" first.'
23+
}
24+
}
25+
26+
BeforeAll {
27+
$script:dscModuleName = 'SqlServerDsc'
28+
29+
Import-Module -Name $script:dscModuleName -Force -ErrorAction 'Stop'
30+
}
31+
32+
Describe 'Disable-SqlDscLogin' -Tag @('Integration_SQL2016', 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
33+
BeforeAll {
34+
# Starting the named instance SQL Server service prior to running tests.
35+
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
36+
37+
$script:mockInstanceName = 'DSCSQLTEST'
38+
$script:mockComputerName = Get-ComputerName
39+
40+
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
41+
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
42+
43+
$script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword)
44+
45+
$script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential
46+
47+
# Create a test login for testing
48+
$script:testLoginName = 'TestLogin_Disable'
49+
$testLoginPassword = ConvertTo-SecureString -String 'P@ssw0rd123!' -AsPlainText -Force
50+
51+
# Create the login if it doesn't exist
52+
$existingLogin = $script:serverObject.Logins[$script:testLoginName]
53+
if (-not $existingLogin)
54+
{
55+
$newLogin = [Microsoft.SqlServer.Management.Smo.Login]::new($script:serverObject, $script:testLoginName)
56+
$newLogin.LoginType = 'SqlLogin'
57+
$newLogin.Create($testLoginPassword)
58+
}
59+
60+
# Ensure the login is initially enabled for testing
61+
$testLogin = $script:serverObject.Logins[$script:testLoginName]
62+
if ($testLogin.IsDisabled -eq $true)
63+
{
64+
$testLogin.Enable()
65+
}
66+
}
67+
68+
AfterAll {
69+
# Clean up test login
70+
$testLogin = $script:serverObject.Logins[$script:testLoginName]
71+
if ($testLogin)
72+
{
73+
$testLogin.Drop()
74+
}
75+
76+
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject
77+
78+
# Stop the named instance SQL Server service to save memory on the build worker.
79+
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
80+
}
81+
82+
Context 'When disabling a login using ServerObject parameter set' {
83+
It 'Should disable the specified login' {
84+
# Verify login is initially enabled
85+
$loginBefore = Get-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName
86+
$loginBefore.IsDisabled | Should -BeFalse
87+
88+
# Disable the login
89+
Disable-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -Force
90+
91+
# Verify login is now disabled
92+
$loginAfter = Get-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -Refresh
93+
$loginAfter.IsDisabled | Should -BeTrue
94+
}
95+
96+
It 'Should disable the login with Refresh parameter' {
97+
# First enable the login
98+
Enable-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -Force
99+
100+
# Disable with Refresh parameter
101+
Disable-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -Refresh -Force
102+
103+
# Verify login is disabled
104+
$loginAfter = Get-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -Refresh
105+
$loginAfter.IsDisabled | Should -BeTrue
106+
}
107+
108+
It 'Should accept ServerObject from pipeline' {
109+
# First enable the login
110+
Enable-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -Force
111+
112+
# Disable using pipeline
113+
$script:serverObject | Disable-SqlDscLogin -Name $script:testLoginName -Force
114+
115+
# Verify login is disabled
116+
$loginAfter = Get-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -Refresh
117+
$loginAfter.IsDisabled | Should -BeTrue
118+
}
119+
}
120+
121+
Context 'When disabling a login using LoginObject parameter set' {
122+
It 'Should disable the specified login object' {
123+
# First enable the login
124+
Enable-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -Force
125+
126+
# Get the login object and disable it
127+
$loginObject = Get-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName
128+
Disable-SqlDscLogin -LoginObject $loginObject -Force
129+
130+
# Verify login is disabled
131+
$loginAfter = Get-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -Refresh
132+
$loginAfter.IsDisabled | Should -BeTrue
133+
}
134+
135+
It 'Should accept LoginObject from pipeline' {
136+
# First enable the login
137+
Enable-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -Force
138+
139+
# Disable using pipeline
140+
$loginObject = Get-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName
141+
$loginObject | Disable-SqlDscLogin -Force
142+
143+
# Verify login is disabled
144+
$loginAfter = Get-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -Refresh
145+
$loginAfter.IsDisabled | Should -BeTrue
146+
}
147+
}
148+
149+
Context 'When disabling a non-existent login' {
150+
It 'Should throw an error for non-existent login' {
151+
{ Disable-SqlDscLogin -ServerObject $script:serverObject -Name 'NonExistentLogin' -Force -ErrorAction 'Stop' } |
152+
Should -Throw -ExpectedMessage 'There is no login with the name ''NonExistentLogin''.'
153+
}
154+
}
155+
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'Suppressing this rule because Script Analyzer does not understand Pester syntax.')]
2+
param ()
3+
4+
BeforeDiscovery {
5+
try
6+
{
7+
if (-not (Get-Module -Name 'DscResource.Test'))
8+
{
9+
# Assumes dependencies have been resolved, so if this module is not available, run 'noop' task.
10+
if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable))
11+
{
12+
# Redirect all streams to $null, except the error stream (stream 2)
13+
& "$PSScriptRoot/../../../build.ps1" -Tasks 'noop' 3>&1 4>&1 5>&1 6>&1 > $null
14+
}
15+
16+
# If the dependencies have not been resolved, this will throw an error.
17+
Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop'
18+
}
19+
}
20+
catch [System.IO.FileNotFoundException]
21+
{
22+
throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks build" first.'
23+
}
24+
}
25+
26+
BeforeAll {
27+
$script:dscModuleName = 'SqlServerDsc'
28+
29+
Import-Module -Name $script:dscModuleName -Force -ErrorAction 'Stop'
30+
}
31+
32+
Describe 'Enable-SqlDscLogin' -Tag @('Integration_SQL2016', 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
33+
BeforeAll {
34+
# Starting the named instance SQL Server service prior to running tests.
35+
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
36+
37+
$script:mockInstanceName = 'DSCSQLTEST'
38+
$script:mockComputerName = Get-ComputerName
39+
40+
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
41+
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
42+
43+
$script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword)
44+
45+
$script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential
46+
47+
# Create a test login for testing
48+
$script:testLoginName = 'TestLogin_Enable'
49+
$testLoginPassword = ConvertTo-SecureString -String 'P@ssw0rd123!' -AsPlainText -Force
50+
51+
# Create the login if it doesn't exist
52+
$existingLogin = $script:serverObject.Logins[$script:testLoginName]
53+
if (-not $existingLogin)
54+
{
55+
$newLogin = [Microsoft.SqlServer.Management.Smo.Login]::new($script:serverObject, $script:testLoginName)
56+
$newLogin.LoginType = 'SqlLogin'
57+
$newLogin.Create($testLoginPassword)
58+
}
59+
60+
# Ensure the login is initially disabled for testing
61+
$testLogin = $script:serverObject.Logins[$script:testLoginName]
62+
if ($testLogin.IsDisabled -eq $false)
63+
{
64+
$testLogin.Disable()
65+
}
66+
}
67+
68+
AfterAll {
69+
# Clean up test login
70+
$testLogin = $script:serverObject.Logins[$script:testLoginName]
71+
if ($testLogin)
72+
{
73+
$testLogin.Drop()
74+
}
75+
76+
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject
77+
78+
# Stop the named instance SQL Server service to save memory on the build worker.
79+
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
80+
}
81+
82+
Context 'When enabling a login using ServerObject parameter set' {
83+
It 'Should enable the specified login' {
84+
# Verify login is initially disabled
85+
$loginBefore = Get-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName
86+
$loginBefore.IsDisabled | Should -BeTrue
87+
88+
# Enable the login
89+
Enable-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -Force
90+
91+
# Verify login is now enabled
92+
$loginAfter = Get-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -Refresh
93+
$loginAfter.IsDisabled | Should -BeFalse
94+
}
95+
96+
It 'Should enable the login with Refresh parameter' {
97+
# First disable the login
98+
Disable-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -Force
99+
100+
# Enable with Refresh parameter
101+
Enable-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -Refresh -Force
102+
103+
# Verify login is enabled
104+
$loginAfter = Get-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -Refresh
105+
$loginAfter.IsDisabled | Should -BeFalse
106+
}
107+
108+
It 'Should accept ServerObject from pipeline' {
109+
# First disable the login
110+
Disable-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -Force
111+
112+
# Enable using pipeline
113+
$script:serverObject | Enable-SqlDscLogin -Name $script:testLoginName -Force
114+
115+
# Verify login is enabled
116+
$loginAfter = Get-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -Refresh
117+
$loginAfter.IsDisabled | Should -BeFalse
118+
}
119+
}
120+
121+
Context 'When enabling a login using LoginObject parameter set' {
122+
It 'Should enable the specified login object' {
123+
# First disable the login
124+
Disable-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -Force
125+
126+
# Get the login object and enable it
127+
$loginObject = Get-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName
128+
Enable-SqlDscLogin -LoginObject $loginObject -Force
129+
130+
# Verify login is enabled
131+
$loginAfter = Get-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -Refresh
132+
$loginAfter.IsDisabled | Should -BeFalse
133+
}
134+
135+
It 'Should accept LoginObject from pipeline' {
136+
# First disable the login
137+
Disable-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -Force
138+
139+
# Enable using pipeline
140+
$loginObject = Get-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName
141+
$loginObject | Enable-SqlDscLogin -Force
142+
143+
# Verify login is enabled
144+
$loginAfter = Get-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -Refresh
145+
$loginAfter.IsDisabled | Should -BeFalse
146+
}
147+
}
148+
149+
Context 'When enabling a non-existent login' {
150+
It 'Should throw an error for non-existent login' {
151+
{ Enable-SqlDscLogin -ServerObject $script:serverObject -Name 'NonExistentLogin' -Force -ErrorAction 'Stop' } |
152+
Should -Throw -ExpectedMessage 'There is no login with the name ''NonExistentLogin''.'
153+
}
154+
}
155+
}

0 commit comments

Comments
 (0)