From 31bcc45f3a1f23d39fcb53a3801ffb73229510fe Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Fri, 26 Jun 2020 16:18:27 +0200 Subject: [PATCH] SqlServerDsc: Supports authenticate using both NetBIOS and FQDN --- CHANGELOG.md | 13 ++ .../DSC_SqlWindowsFirewall.psm1 | 4 +- .../SqlServerDsc.Common.psm1 | 6 +- tests/Unit/DSC_SqlWindowsFirewall.Tests.ps1 | 127 ++++++++++---- tests/Unit/SqlServerDsc.Common.Tests.ps1 | 160 ++++++++++++++---- 5 files changed, 240 insertions(+), 70 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3673bd073..e8774bba9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Update resource parameter documentation ([issue #1568](https://github.com/dsccommunity/SqlServerDsc/issues/1568)). - Documentation is now published to the GitHub Wiki. - Deploy task was updated with the correct name. +- SqlServerDsc.Common + - Connect-UncPath + - Now support to authenticate using both NetBIOS domain and Fully Qualified + Domain Name (FQDN) ([issue #1223](https://github.com/dsccommunity/SqlServerDsc/issues/1223)). + - Connect-SQL + - Now support to authenticate using both NetBIOS domain and Fully Qualified + Domain Name (FQDN) ([issue #1223](https://github.com/dsccommunity/SqlServerDsc/issues/1223)). + - Connect-SQLAnalysis + - Now support to authenticate using both NetBIOS domain and Fully Qualified + Domain Name (FQDN) ([issue #1223](https://github.com/dsccommunity/SqlServerDsc/issues/1223)). +- SqlWindowsFirewall + - Now support to authenticate using both NetBIOS domain and Fully Qualified + Domain Name (FQDN) ([issue #1223](https://github.com/dsccommunity/SqlServerDsc/issues/1223)). ## [14.0.0] - 2020-06-12 diff --git a/source/DSCResources/DSC_SqlWindowsFirewall/DSC_SqlWindowsFirewall.psm1 b/source/DSCResources/DSC_SqlWindowsFirewall/DSC_SqlWindowsFirewall.psm1 index 454d63c67..9609f09b0 100644 --- a/source/DSCResources/DSC_SqlWindowsFirewall/DSC_SqlWindowsFirewall.psm1 +++ b/source/DSCResources/DSC_SqlWindowsFirewall/DSC_SqlWindowsFirewall.psm1 @@ -55,7 +55,7 @@ function Get-TargetResource if ($SourceCredential) { - $userName = "$($SourceCredential.GetNetworkCredential().Domain)\$($SourceCredential.GetNetworkCredential().UserName)" + $userName = $SourceCredential.UserName Write-Verbose -Message ( $script:localizedData.ConnectUsingCredential -f $SourcePath, $userName @@ -375,7 +375,7 @@ function Set-TargetResource if ($SourceCredential) { - $userName = "$($SourceCredential.GetNetworkCredential().Domain)\$($SourceCredential.GetNetworkCredential().UserName)" + $userName = $SourceCredential.UserName Write-Verbose -Message ( $script:localizedData.ConnectUsingCredential -f $SourcePath, $userName diff --git a/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 b/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 index a97749401..51ad8d1a5 100644 --- a/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 +++ b/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 @@ -314,7 +314,7 @@ function Connect-UncPath if ($PSBoundParameters.ContainsKey('SourceCredential')) { - $newSmbMappingParameters['UserName'] = "$($SourceCredential.GetNetworkCredential().Domain)\$($SourceCredential.GetNetworkCredential().UserName)" + $newSmbMappingParameters['UserName'] = $SourceCredential.UserName $newSmbMappingParameters['Password'] = $SourceCredential.GetNetworkCredential().Password } @@ -527,7 +527,7 @@ function Connect-SQL } else { - $connectUserName = $SetupCredential.GetNetworkCredential().UserName + $connectUserName = $SetupCredential.UserName Write-Verbose -Message ( $script:localizedData.ConnectingUsingImpersonation -f $connectUsername, $LoginType @@ -633,7 +633,7 @@ function Connect-SQLAnalysis if ($SetupCredential) { - $userName = $SetupCredential.GetNetworkCredential().UserName + $userName = $SetupCredential.UserName $password = $SetupCredential.GetNetworkCredential().Password $analysisServicesDataSource = "Data Source=$analysisServiceInstance;User ID=$userName;Password=$password" diff --git a/tests/Unit/DSC_SqlWindowsFirewall.Tests.ps1 b/tests/Unit/DSC_SqlWindowsFirewall.Tests.ps1 index e1fc0b5f1..bbc9f411e 100644 --- a/tests/Unit/DSC_SqlWindowsFirewall.Tests.ps1 +++ b/tests/Unit/DSC_SqlWindowsFirewall.Tests.ps1 @@ -93,9 +93,13 @@ try $mockNamedInstance_IntegrationServiceName = $mockSqlIntegrationName $mockNamedInstance_AnalysisServiceName = "$($mockSqlAnalysisName)`$$($mockNamedInstance_InstanceName)" - $mockmockSourceCredentialUserName = "COMPANY\sqladmin" - $mockmockSourceCredentialPassword = "dummyPassw0rd" | ConvertTo-SecureString -asPlainText -Force - $mockSourceCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @($mockmockSourceCredentialUserName, $mockmockSourceCredentialPassword) + $mockSourceCredentialUserName = "COMPANY\sqladmin" + $mockSourceCredentialPassword = "dummyPassw0rd" | ConvertTo-SecureString -asPlainText -Force + $mockSourceCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @($mockSourceCredentialUserName, $mockSourceCredentialPassword) + + $mockFqdnSourceCredentialUserName = "sqladmin@company.local" + $mockFqdnSourceCredentialPassword = "dummyPassw0rd" | ConvertTo-SecureString -asPlainText -Force + $mockFqdnSourceCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @($mockFqdnSourceCredentialUserName, $mockFqdnSourceCredentialPassword) $mockDynamicSQLEngineFirewallRulePresent = $true $mockDynamicSQLBrowserFirewallRulePresent = $true @@ -525,7 +529,7 @@ try } Context "When SQL Server version is $mockCurrentSqlMajorVersion and there are no components installed" { - BeforeEach { + BeforeAll { $testParameters = $mockDefaultParameters.Clone() $testParameters += @{ InstanceName = $mockCurrentInstanceName @@ -559,19 +563,50 @@ try $result.Features | Should -BeNullOrEmpty } - It 'Should call the correct functions exact number of times' { - $result = Get-TargetResource @testParameters - Assert-MockCalled -CommandName New-SmbMapping -Exactly -Times 1 -Scope It - Assert-MockCalled -CommandName Remove-SmbMapping -Exactly -Times 1 -Scope It - Assert-MockCalled -CommandName Get-Service -Exactly -Times 1 -Scope It - Assert-MockCalled -CommandName Test-IsFirewallRuleInDesiredState -Exactly -Times 0 -Scope It - Assert-MockCalled -CommandName New-NetFirewallRule -Exactly -Times 0 -Scope It - Assert-MockCalled -CommandName Set-NetFirewallRule -Exactly -Times 0 -Scope It - Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_SqlInstanceId_ParameterFilter -Exactly -Times 0 -Scope It - Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_AnalysisServicesInstanceId_ParameterFilter -Exactly -Times 0 -Scope It - Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_DatabaseEngineSqlBinRoot_ParameterFilter -Exactly -Times 0 -Scope It - Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_AnalysisServicesSqlBinRoot_ParameterFilter -Exactly -Times 0 -Scope It - Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_IntegrationsServicesSqlPath_ParameterFilter -Exactly -Times 0 -Scope It + Context 'When authenticating using NetBIOS domain' { + It 'Should call the correct functions exact number of times' { + $result = Get-TargetResource @testParameters + + Assert-MockCalled -CommandName Remove-SmbMapping -Exactly -Times 1 -Scope It + Assert-MockCalled -CommandName Get-Service -Exactly -Times 1 -Scope It + Assert-MockCalled -CommandName Test-IsFirewallRuleInDesiredState -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName New-NetFirewallRule -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName Set-NetFirewallRule -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_SqlInstanceId_ParameterFilter -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_AnalysisServicesInstanceId_ParameterFilter -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_DatabaseEngineSqlBinRoot_ParameterFilter -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_AnalysisServicesSqlBinRoot_ParameterFilter -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_IntegrationsServicesSqlPath_ParameterFilter -Exactly -Times 0 -Scope It + + Assert-MockCalled -CommandName New-SmbMapping -ParameterFilter { + $UserName -eq $mockSourceCredentialUserName + } -Exactly -Times 1 -Scope It + } + } + + Context 'When authenticating using Fully Qualified Domain Name (FQDN)' { + BeforeAll { + $testParameters['SourceCredential'] = $mockFqdnSourceCredential + } + + It 'Should call the correct functions exact number of times' { + $result = Get-TargetResource @testParameters + + Assert-MockCalled -CommandName Remove-SmbMapping -Exactly -Times 1 -Scope It + Assert-MockCalled -CommandName Get-Service -Exactly -Times 1 -Scope It + Assert-MockCalled -CommandName Test-IsFirewallRuleInDesiredState -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName New-NetFirewallRule -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName Set-NetFirewallRule -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_SqlInstanceId_ParameterFilter -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_AnalysisServicesInstanceId_ParameterFilter -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_DatabaseEngineSqlBinRoot_ParameterFilter -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_AnalysisServicesSqlBinRoot_ParameterFilter -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_IntegrationsServicesSqlPath_ParameterFilter -Exactly -Times 0 -Scope It + + Assert-MockCalled -CommandName New-SmbMapping -ParameterFilter { + $UserName -eq $mockFqdnSourceCredentialUserName + } -Exactly -Times 1 -Scope It + } } } @@ -1054,7 +1089,7 @@ try Mock -CommandName Test-TargetResource -MockWith { return $false } Context "When SQL Server version is $mockCurrentSqlMajorVersion and there are no components installed" { - BeforeEach { + BeforeAll { $testParameters = $mockDefaultParameters.Clone() $testParameters += @{ InstanceName = $mockCurrentInstanceName @@ -1069,20 +1104,50 @@ try Mock -CommandName Set-NetFirewallRule -Verifiable } - It 'Should throw the correct error when Set-TargetResource verifies result with Test-TargetResource' { - { Set-TargetResource @testParameters } | Should -Throw $script:localizedData.TestFailedAfterSet + Context 'When authenticating using NetBIOS domain' { + It 'Should throw the correct error when Set-TargetResource verifies result with Test-TargetResource' { + { Set-TargetResource @testParameters } | Should -Throw $script:localizedData.TestFailedAfterSet - Assert-MockCalled -CommandName New-SmbMapping -Exactly -Times 1 -Scope It - Assert-MockCalled -CommandName Remove-SmbMapping -Exactly -Times 1 -Scope It - Assert-MockCalled -CommandName Get-Service -Exactly -Times 1 -Scope It - Assert-MockCalled -CommandName Test-IsFirewallRuleInDesiredState -Exactly -Times 0 -Scope It - Assert-MockCalled -CommandName New-NetFirewallRule -Exactly -Times 0 -Scope It - Assert-MockCalled -CommandName Set-NetFirewallRule -Exactly -Times 0 -Scope It - Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_SqlInstanceId_ParameterFilter -Exactly -Times 0 -Scope It - Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_AnalysisServicesInstanceId_ParameterFilter -Exactly -Times 0 -Scope It - Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_DatabaseEngineSqlBinRoot_ParameterFilter -Exactly -Times 0 -Scope It - Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_AnalysisServicesSqlBinRoot_ParameterFilter -Exactly -Times 0 -Scope It - Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_IntegrationsServicesSqlPath_ParameterFilter -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName Remove-SmbMapping -Exactly -Times 1 -Scope It + Assert-MockCalled -CommandName Get-Service -Exactly -Times 1 -Scope It + Assert-MockCalled -CommandName Test-IsFirewallRuleInDesiredState -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName New-NetFirewallRule -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName Set-NetFirewallRule -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_SqlInstanceId_ParameterFilter -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_AnalysisServicesInstanceId_ParameterFilter -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_DatabaseEngineSqlBinRoot_ParameterFilter -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_AnalysisServicesSqlBinRoot_ParameterFilter -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_IntegrationsServicesSqlPath_ParameterFilter -Exactly -Times 0 -Scope It + + Assert-MockCalled -CommandName New-SmbMapping -ParameterFilter { + $UserName -eq $mockSourceCredentialUserName + } -Exactly -Times 1 -Scope It + } + } + + Context 'When authenticating using Fully Qualified Domain Name (FQDN)' { + BeforeAll { + $testParameters['SourceCredential'] = $mockFqdnSourceCredential + } + + It 'Should throw the correct error when Set-TargetResource verifies result with Test-TargetResource' { + { Set-TargetResource @testParameters } | Should -Throw $script:localizedData.TestFailedAfterSet + + Assert-MockCalled -CommandName Remove-SmbMapping -Exactly -Times 1 -Scope It + Assert-MockCalled -CommandName Get-Service -Exactly -Times 1 -Scope It + Assert-MockCalled -CommandName Test-IsFirewallRuleInDesiredState -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName New-NetFirewallRule -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName Set-NetFirewallRule -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_SqlInstanceId_ParameterFilter -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_AnalysisServicesInstanceId_ParameterFilter -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_DatabaseEngineSqlBinRoot_ParameterFilter -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_AnalysisServicesSqlBinRoot_ParameterFilter -Exactly -Times 0 -Scope It + Assert-MockCalled -CommandName Get-ItemProperty -ParameterFilter $mockGetItemProperty_IntegrationsServicesSqlPath_ParameterFilter -Exactly -Times 0 -Scope It + + Assert-MockCalled -CommandName New-SmbMapping -ParameterFilter { + $UserName -eq $mockFqdnSourceCredentialUserName + } -Exactly -Times 1 -Scope It + } } } diff --git a/tests/Unit/SqlServerDsc.Common.Tests.ps1 b/tests/Unit/SqlServerDsc.Common.Tests.ps1 index 5140bd733..b4c172f9b 100644 --- a/tests/Unit/SqlServerDsc.Common.Tests.ps1 +++ b/tests/Unit/SqlServerDsc.Common.Tests.ps1 @@ -577,6 +577,13 @@ InModuleScope $script:subModuleName { ($mockShareCredentialPassword | ConvertTo-SecureString -AsPlainText -Force) ) + $mockFqdnShareCredentialUserName = 'SqlAdmin@company.local' + $mockFqdnShareCredentialPassword = 'dummyPassW0rd' + $mockFqdnShareCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @( + $mockFqdnShareCredentialUserName, + ($mockFqdnShareCredentialPassword | ConvertTo-SecureString -AsPlainText -Force) + ) + Mock -CommandName New-SmbMapping -MockWith { return @{ RemotePath = $mockSourcePathUNC @@ -602,20 +609,40 @@ InModuleScope $script:subModuleName { } Context 'When connecting to a UNC path with specific credentials' { - It 'Should call the correct mocks' { - { - $connectUncPathParameters = @{ - RemotePath = $mockSourcePathUNC - SourceCredential = $mockShareCredential - } + Context 'When connecting using NetBIOS domain' { + It 'Should call the correct mocks' { + { + $connectUncPathParameters = @{ + RemotePath = $mockSourcePathUNC + SourceCredential = $mockShareCredential + } - Connect-UncPath @connectUncPathParameters - } | Should -Not -Throw + Connect-UncPath @connectUncPathParameters + } | Should -Not -Throw - Assert-MockCalled -CommandName New-SmbMapping -ParameterFilter { - $RemotePath -eq $mockSourcePathUNC ` - -and $UserName -eq $mockShareCredentialUserName - } -Exactly -Times 1 -Scope It + Assert-MockCalled -CommandName New-SmbMapping -ParameterFilter { + $RemotePath -eq $mockSourcePathUNC ` + -and $UserName -eq $mockShareCredentialUserName + } -Exactly -Times 1 -Scope It + } + } + + Context 'When connecting using Fully Qualified Domain Name (FQDN)' { + It 'Should call the correct mocks' { + { + $connectUncPathParameters = @{ + RemotePath = $mockSourcePathUNC + SourceCredential = $mockFqdnShareCredential + } + + Connect-UncPath @connectUncPathParameters + } | Should -Not -Throw + + Assert-MockCalled -CommandName New-SmbMapping -ParameterFilter { + $RemotePath -eq $mockSourcePathUNC ` + -and $UserName -eq $mockFqdnShareCredentialUserName + } -Exactly -Times 1 -Scope It + } } } @@ -1407,6 +1434,16 @@ InModuleScope $script:subModuleName { $mockSqlCredentialSecurePassword = ConvertTo-SecureString -String $mockSqlCredentialPassword -AsPlainText -Force $mockSqlCredential = New-Object -TypeName PSCredential -ArgumentList ($mockSqlCredentialUserName, $mockSqlCredentialSecurePassword) + $mockNetBiosSqlCredentialUserName = 'DOMAIN\TestUserName12345' + $mockNetBiosSqlCredentialPassword = 'StrongOne7.' + $mockNetBiosSqlCredentialSecurePassword = ConvertTo-SecureString -String $mockNetBiosSqlCredentialPassword -AsPlainText -Force + $mockNetBiosSqlCredential = New-Object -TypeName PSCredential -ArgumentList ($mockNetBiosSqlCredentialUserName, $mockNetBiosSqlCredentialSecurePassword) + + $mockFqdnSqlCredentialUserName = 'TestUserName12345@domain.local' + $mockFqdnSqlCredentialPassword = 'StrongOne7.' + $mockFqdnSqlCredentialSecurePassword = ConvertTo-SecureString -String $mockFqdnSqlCredentialPassword -AsPlainText -Force + $mockFqdnSqlCredential = New-Object -TypeName PSCredential -ArgumentList ($mockFqdnSqlCredentialUserName, $mockFqdnSqlCredentialSecurePassword) + Mock -CommandName Import-Assembly } @@ -1440,13 +1477,37 @@ InModuleScope $script:subModuleName { } Context 'When connecting to the named instance using Windows Authentication impersonation' { - It 'Should not throw when connecting' { - $mockExpectedDataSource = "Data Source=$env:COMPUTERNAME\$mockInstanceName;User ID=$mockSqlCredentialUserName;Password=$mockSqlCredentialPassword" + Context 'When authentication without NetBIOS domain and Fully Qualified Domain Name (FQDN)' { + It 'Should not throw when connecting' { + $mockExpectedDataSource = "Data Source=$env:COMPUTERNAME\$mockInstanceName;User ID=$mockSqlCredentialUserName;Password=$mockSqlCredentialPassword" - { Connect-SQLAnalysis -InstanceName $mockInstanceName -SetupCredential $mockSqlCredential } | Should -Not -Throw + { Connect-SQLAnalysis -InstanceName $mockInstanceName -SetupCredential $mockSqlCredential } | Should -Not -Throw - Assert-MockCalled -CommandName New-Object -Exactly -Times 1 -Scope It ` - -ParameterFilter $mockNewObject_MicrosoftAnalysisServicesServer_ParameterFilter + Assert-MockCalled -CommandName New-Object -Exactly -Times 1 -Scope It ` + -ParameterFilter $mockNewObject_MicrosoftAnalysisServicesServer_ParameterFilter + } + } + + Context 'When authentication using NetBIOS domain' { + It 'Should not throw when connecting' { + $mockExpectedDataSource = "Data Source=$env:COMPUTERNAME\$mockInstanceName;User ID=$mockNetBiosSqlCredentialUserName;Password=$mockNetBiosSqlCredentialPassword" + + { Connect-SQLAnalysis -InstanceName $mockInstanceName -SetupCredential $mockNetBiosSqlCredential } | Should -Not -Throw + + Assert-MockCalled -CommandName New-Object -Exactly -Times 1 -Scope It ` + -ParameterFilter $mockNewObject_MicrosoftAnalysisServicesServer_ParameterFilter + } + } + + Context 'When authentication using Fully Qualified Domain Name (FQDN)' { + It 'Should not throw when connecting' { + $mockExpectedDataSource = "Data Source=$env:COMPUTERNAME\$mockInstanceName;User ID=$mockFqdnSqlCredentialUserName;Password=$mockFqdnSqlCredentialPassword" + + { Connect-SQLAnalysis -InstanceName $mockInstanceName -SetupCredential $mockFqdnSqlCredential } | Should -Not -Throw + + Assert-MockCalled -CommandName New-Object -Exactly -Times 1 -Scope It ` + -ParameterFilter $mockNewObject_MicrosoftAnalysisServicesServer_ParameterFilter + } } } @@ -2674,6 +2735,11 @@ InModuleScope $script:subModuleName { $mockWinCredentialPassword = 'StrongerOne7.' $mockWinCredentialSecurePassword = ConvertTo-SecureString -String $mockWinCredentialPassword -AsPlainText -Force $mockWinCredential = New-Object -TypeName PSCredential -ArgumentList ($mockWinCredentialUserName, $mockWinCredentialSecurePassword) + + $mockWinFqdnCredentialUserName = 'TestUserName12345@domain.local' + $mockWinFqdnCredentialPassword = 'StrongerOne7.' + $mockWinFqdnCredentialSecurePassword = ConvertTo-SecureString -String $mockWinFqdnCredentialPassword -AsPlainText -Force + $mockWinFqdnCredential = New-Object -TypeName PSCredential -ArgumentList ($mockWinFqdnCredentialUserName, $mockWinFqdnCredentialSecurePassword) } BeforeEach { @@ -2777,7 +2843,7 @@ InModuleScope $script:subModuleName { $databaseEngineServerObject.ConnectionContext.ServerInstance | Should -BeExactly "$mockExpectedDatabaseEngineServer\$mockExpectedDatabaseEngineInstance" $databaseEngineServerObject.ConnectionContext.ConnectAsUser | Should -Be $true $databaseEngineServerObject.ConnectionContext.ConnectAsUserPassword | Should -BeExactly $mockWinCredential.GetNetworkCredential().Password - $databaseEngineServerObject.ConnectionContext.ConnectAsUserName | Should -BeExactly $mockWinCredential.GetNetworkCredential().UserName + $databaseEngineServerObject.ConnectionContext.ConnectAsUserName | Should -BeExactly $mockWinCredential.UserName $databaseEngineServerObject.ConnectionContext.ConnectAsUser | Should -Be $true $databaseEngineServerObject.ConnectionContext.LoginSecure | Should -Be $true @@ -2787,26 +2853,52 @@ InModuleScope $script:subModuleName { } Context 'When using the default login type' { - BeforeAll { - $testParameters = @{ - ServerName = $mockExpectedDatabaseEngineServer - InstanceName = $mockExpectedDatabaseEngineInstance - SetupCredential = $mockWinCredential - LoginType = 'WindowsUser' + Context 'When authenticating using NetBIOS domain' { + BeforeAll { + $testParameters = @{ + ServerName = $mockExpectedDatabaseEngineServer + InstanceName = $mockExpectedDatabaseEngineInstance + SetupCredential = $mockWinCredential + LoginType = 'WindowsUser' + } + } + + It 'Should return the correct service instance' { + $databaseEngineServerObject = Connect-SQL @testParameters + $databaseEngineServerObject.ConnectionContext.ServerInstance | Should -BeExactly "$mockExpectedDatabaseEngineServer\$mockExpectedDatabaseEngineInstance" + $databaseEngineServerObject.ConnectionContext.ConnectAsUser | Should -Be $true + $databaseEngineServerObject.ConnectionContext.ConnectAsUserPassword | Should -BeExactly $mockWinCredential.GetNetworkCredential().Password + $databaseEngineServerObject.ConnectionContext.ConnectAsUserName | Should -BeExactly $mockWinCredential.UserName + $databaseEngineServerObject.ConnectionContext.ConnectAsUser | Should -Be $true + $databaseEngineServerObject.ConnectionContext.LoginSecure | Should -Be $true + + Assert-MockCalled -CommandName New-Object -Exactly -Times 1 -Scope It ` + -ParameterFilter $mockNewObject_MicrosoftDatabaseEngine_ParameterFilter } } - It 'Should return the correct service instance' { - $databaseEngineServerObject = Connect-SQL @testParameters - $databaseEngineServerObject.ConnectionContext.ServerInstance | Should -BeExactly "$mockExpectedDatabaseEngineServer\$mockExpectedDatabaseEngineInstance" - $databaseEngineServerObject.ConnectionContext.ConnectAsUser | Should -Be $true - $databaseEngineServerObject.ConnectionContext.ConnectAsUserPassword | Should -BeExactly $mockWinCredential.GetNetworkCredential().Password - $databaseEngineServerObject.ConnectionContext.ConnectAsUserName | Should -BeExactly $mockWinCredential.GetNetworkCredential().UserName - $databaseEngineServerObject.ConnectionContext.ConnectAsUser | Should -Be $true - $databaseEngineServerObject.ConnectionContext.LoginSecure | Should -Be $true + Context 'When authenticating using Fully Qualified Domain Name (FQDN)' { + BeforeAll { + $testParameters = @{ + ServerName = $mockExpectedDatabaseEngineServer + InstanceName = $mockExpectedDatabaseEngineInstance + SetupCredential = $mockWinFqdnCredential + LoginType = 'WindowsUser' + } + } - Assert-MockCalled -CommandName New-Object -Exactly -Times 1 -Scope It ` - -ParameterFilter $mockNewObject_MicrosoftDatabaseEngine_ParameterFilter + It 'Should return the correct service instance' { + $databaseEngineServerObject = Connect-SQL @testParameters + $databaseEngineServerObject.ConnectionContext.ServerInstance | Should -BeExactly "$mockExpectedDatabaseEngineServer\$mockExpectedDatabaseEngineInstance" + $databaseEngineServerObject.ConnectionContext.ConnectAsUser | Should -Be $true + $databaseEngineServerObject.ConnectionContext.ConnectAsUserPassword | Should -BeExactly $mockWinFqdnCredential.GetNetworkCredential().Password + $databaseEngineServerObject.ConnectionContext.ConnectAsUserName | Should -BeExactly $mockWinFqdnCredential.UserName + $databaseEngineServerObject.ConnectionContext.ConnectAsUser | Should -Be $true + $databaseEngineServerObject.ConnectionContext.LoginSecure | Should -Be $true + + Assert-MockCalled -CommandName New-Object -Exactly -Times 1 -Scope It ` + -ParameterFilter $mockNewObject_MicrosoftDatabaseEngine_ParameterFilter + } } } }