From 37edee8ff2609b343c6dd8f20666b9f7914da371 Mon Sep 17 00:00:00 2001 From: Cezary Piatek Date: Sat, 15 Dec 2018 16:44:18 +0100 Subject: [PATCH 1/9] #241: Add support for anonymouse authentication credentials in xWebsite module --- DSCResources/MSFT_xWebsite/MSFT_xWebsite.psm1 | 94 +++++++++++++++++++ .../MSFT_xWebsite/MSFT_xWebsite.schema.mof | 8 ++ 2 files changed, 102 insertions(+) diff --git a/DSCResources/MSFT_xWebsite/MSFT_xWebsite.psm1 b/DSCResources/MSFT_xWebsite/MSFT_xWebsite.psm1 index 7f6ac410c..41e54d95e 100644 --- a/DSCResources/MSFT_xWebsite/MSFT_xWebsite.psm1 +++ b/DSCResources/MSFT_xWebsite/MSFT_xWebsite.psm1 @@ -40,6 +40,7 @@ data LocalizedData VerboseSetTargetWebsiteStarted = Successfully started website "{0}". VerboseSetTargetWebsiteRemoved = Successfully removed website "{0}". VerboseSetTargetAuthenticationInfoUpdated = Successfully updated AuthenticationInfo on website "{0}". + VerboseSetTargetAnonymousCredentialsUpdated = Successfully updated AnonymousCredentials on website "{0}". VerboseSetTargetWebsitePreloadUpdated = Successfully updated Preload on website "{0}". VerboseSetTargetWebsiteAutoStartUpdated = Successfully updated AutoStart on website "{0}". VerboseSetTargetWebsiteAutoStartProviderUpdated = Successfully updated AutoStartProvider on website "{0}". @@ -65,6 +66,7 @@ data LocalizedData VerboseTestTargetFalsePreload = Preload for website "{0}" do not match the desired state. VerboseTestTargetFalseAutoStart = AutoStart for website "{0}" do not match the desired state. VerboseTestTargetFalseAuthenticationInfo = AuthenticationInfo for website "{0}" is not in the desired state. + VerboseTestTargetFalseAnonymousCredentials = AnonymousCredentials for website "{0}" is not in the desired state. VerboseTestTargetFalseIISAutoStartProvider = AutoStartProvider for IIS is not in the desired state VerboseTestTargetFalseWebsiteAutoStartProvider = AutoStartProvider for website "{0}" is not in the desired state VerboseTestTargetFalseLogPath = LogPath does not match desired state on Website "{0}". @@ -138,6 +140,7 @@ function Get-TargetResource Select-Object Name,Type [Array] $cimLogCustomFields = ConvertTo-CimLogCustomFields -InputObject $website.logFile.customFields.Collection + $anonymousCredentials = Get-AnonymousCredentials -Site $Name } # Multiple websites with the same name exist. This is not supported and is an error else @@ -160,6 +163,7 @@ function Get-TargetResource DefaultPage = $allDefaultPages EnabledProtocols = $website.EnabledProtocols AuthenticationInfo = $cimAuthentication + AnonymousCredentials = $anonymousCredentials PreloadEnabled = $website.applicationDefaults.preloadEnabled ServiceAutoStartProvider = $website.applicationDefaults.serviceAutoStartProvider ServiceAutoStartEnabled = $website.applicationDefaults.serviceAutoStartEnabled @@ -229,6 +233,9 @@ function Set-TargetResource [Microsoft.Management.Infrastructure.CimInstance] $AuthenticationInfo, + [Microsoft.Management.Infrastructure.CimInstance] + $AnonymousCredentials, + [Boolean] $PreloadEnabled, @@ -526,6 +533,15 @@ function Set-TargetResource -f $Name) } + if($PSBoundParameters.ContainsKey('AnonymousCredentials') -and ` + (Test-AuthenticationEnabled -Site $Name -Type 'Anonymous' ) -and ` + ( -not (Test-AnonymousCredentials -Site $Name -Credentials $AnonymousCredentials))) + { + Set-AnonymousAuthenticationCredentials -Site $Site -Credentials $AnonymousCredentials -ErrorAction Stop + Write-Verbose -Message ($LocalizedData.VerboseSetTargetAnonymousCredentialsUpdated ` + -f $Name) + } + # Update Preload if required if ($PSBoundParameters.ContainsKey('preloadEnabled') -and ` ($website.applicationDefaults.preloadEnabled -ne $PreloadEnabled)) @@ -747,6 +763,9 @@ function Test-TargetResource [Microsoft.Management.Infrastructure.CimInstance] $AuthenticationInfo, + [Microsoft.Management.Infrastructure.CimInstance] + $AnonymousCredentials, + [Boolean] $PreloadEnabled, @@ -894,6 +913,13 @@ function Test-TargetResource Write-Verbose -Message ($LocalizedData.VerboseTestTargetFalseAuthenticationInfo) } + if($PSBoundParameters.ContainsKey('AnonymousCredentials') -and ` + (-not (Test-AnonymousCredentials -Site $Name -Credentials $AnonymousCredentials))) + { + $inDesiredState = $false + Write-Verbose -Message ($LocalizedData.VerboseTestTargetFalseAnonymousCredentials -f $Name) + } + #Check Preload if($PSBoundParameters.ContainsKey('preloadEnabled') -and ` $website.applicationDefaults.preloadEnabled -ne $PreloadEnabled) @@ -2234,6 +2260,74 @@ function Update-WebsiteBinding } } + +function Test-AnonymousCredentials +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $true)] + [String]$Site, + [Microsoft.Management.Infrastructure.CimInstance] + $Credentials + ) + + $currentCredentials = Get-AnonymousCredentials -Site $Site + + if($currentCredentials -eq $null) + { + return $false + } + + ($currentCredentials.UserName -eq $Credentials.User) -and ($currentCredentials.Password -eq $Credentials.Password) +} + <# + .SYNOPSIS + Helper function to extract credentials for anonymous authentication + + .PARAMETER Site + Specifies the name of the Website. +#> +function Get-AnonymousCredentials +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $true)] + [String]$Site + ) + $anonymousAuthentication = Get-WebConfiguration -Filter 'system.webServer/security/authentication/anonymousAuthentication' -PSPath "IIS:\Sites\$Site" + if($anonymousAuthentication.enabled) + { + New-CimInstance -ClassName MSFT_xWebAnonymousAuthenticationCredentials ` + -ClientOnly ` + -Property @{ UserName = $anonymousAuthentication.userName; Password = $anonymousAuthentication.password } + } +} + <# + .SYNOPSIS + Helper function used to set credentials for anonymous authentication + .PARAMETER Site + Specifies the name of the Website. + .PARAMETER Credentials + A CimInstance of what state the Credentials should be +#> +function Set-AnonymousAuthenticationCredentials +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $true)] + [String]$Site, + + [Microsoft.Management.Infrastructure.CimInstance] + $Credentials + ) + $anonymousAuthenticationConfigurationPath = 'system.webServer/security/authentication/anonymousAuthentication' + Set-WebConfigurationProperty -Location $Site -Filter $anonymousAuthenticationConfigurationPath -Name 'userName' $Credentials.User + Set-WebConfigurationProperty -Location $Site -Filter $anonymousAuthenticationConfigurationPath -Name 'password' $Credentials.Password +} + #endregion Export-ModuleMember -Function *-TargetResource diff --git a/DSCResources/MSFT_xWebsite/MSFT_xWebsite.schema.mof b/DSCResources/MSFT_xWebsite/MSFT_xWebsite.schema.mof index 43175d3a1..dcb6b15b0 100644 --- a/DSCResources/MSFT_xWebsite/MSFT_xWebsite.schema.mof +++ b/DSCResources/MSFT_xWebsite/MSFT_xWebsite.schema.mof @@ -21,6 +21,13 @@ class MSFT_xWebAuthenticationInformation [Write] Boolean Windows; }; +[ClassVersion("1.0.0")] +class MSFT_xWebAnonymousAuthenticationCredentials +{ + [Write] String UserName; + [Write] String Password; +}; + [ClassVersion("1.0.0")] class MSFT_xLogCustomFieldInformation { @@ -42,6 +49,7 @@ class MSFT_xWebsite : OMI_BaseResource [Write] String DefaultPage[]; [Write] String EnabledProtocols; [write, EmbeddedInstance("MSFT_xWebAuthenticationInformation"), Description("Hashtable containing authentication information (Anonymous, Basic, Digest, Windows)")] String AuthenticationInfo; + [write, EmbeddedInstance("MSFT_xWebAnonymousAuthenticationCredentials"), Description("Credentials for Anonymous authentication")] String AnonymousCredentials; [Write, Description ("Allows the Website to automatically start without a request")] Boolean PreloadEnabled; [Write, Description ("Enables Autostart on a Website.")] Boolean ServiceAutoStartEnabled; [Write, Description ("Adds a AutostartProvider")] String ServiceAutoStartProvider; From 688a095cf74f0c3228b811b017e31c8e3a717711 Mon Sep 17 00:00:00 2001 From: Cezary Piatek Date: Sat, 15 Dec 2018 20:11:20 +0100 Subject: [PATCH 2/9] #241: Add UT for Anonymous authentication --- Tests/Unit/MSFT_xWebsite.Tests.ps1 | 66 ++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 16 deletions(-) diff --git a/Tests/Unit/MSFT_xWebsite.Tests.ps1 b/Tests/Unit/MSFT_xWebsite.Tests.ps1 index 1f401ea71..32985be9a 100644 --- a/Tests/Unit/MSFT_xWebsite.Tests.ps1 +++ b/Tests/Unit/MSFT_xWebsite.Tests.ps1 @@ -3,6 +3,9 @@ $script:DSCModuleName = 'xWebAdministration' $script:DSCResourceName = 'MSFT_xWebsite' #region HEADER +$culture = [System.Globalization.CultureInfo]::GetCultureInfo('en-US') +[System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture +[System.Threading.Thread]::CurrentThread.CurrentCulture = $culture $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or ` (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) ) @@ -86,6 +89,11 @@ try -ClientOnly ) + $MockAnonymousCredentials = @{ + enabled = $true + userName = "TestUser" + password = "secret" + } $mockLogCustomFields = @( @{ LogFieldName = 'LogField1' @@ -172,6 +180,10 @@ try Mock -CommandName Get-WebConfigurationProperty ` -MockWith {return $MockAuthenticationInfo} + Mock -CommandName Get-WebConfiguration ` + -ParameterFilter {$filter -eq 'system.webServer/security/authentication/anonymousAuthentication'} ` + -MockWith { return $MockAnonymousCredentials} + Mock -CommandName Test-AuthenticationEnabled { return $true } ` -ParameterFilter { ($Type -eq 'Anonymous') } @@ -186,14 +198,6 @@ try $Result = Get-TargetResource -Name $MockWebsite.Name - It 'should call Get-Website once' { - Assert-MockCalled -CommandName Get-Website -Exactly 1 - } - - It 'should call Get-WebConfiguration twice' { - Assert-MockCalled -CommandName Get-WebConfiguration -Exactly 2 - } - It 'should return Ensure' { $Result.Ensure | Should Be 'Present' } @@ -244,6 +248,11 @@ try $Result.AuthenticationInfo.CimInstanceProperties['Windows'].Value | Should Be 'true' } + It 'should return AnonymousCredentials' { + $Result.AnonymousCredentials.CimInstanceProperties['UserName'].Value | Should Be 'TestUser' + $Result.AnonymousCredentials.CimInstanceProperties['Password'].Value | Should Be 'secret' + } + It 'should return Preload' { $Result.PreloadEnabled | Should Be $MockWebsite.ApplicationDefaults.PreloadEnabled } @@ -555,6 +564,32 @@ try } } + Context 'Check AnonymousCredentials is different' { + Mock -CommandName Get-Website -MockWith {return $MockWebsite} + Mock -CommandName Get-WebConfiguration ` + -ParameterFilter {$filter -eq 'system.webServer/security/authentication/anonymousAuthentication'} ` + -MockWith { return @{ + enabled = $true + userName = "fake" + password = "none" + }} + + $MockAnonymousCredentials = New-CimInstance ` + -ClassName MSFT_xWebAnonymousAuthenticationCredentials ` + -ClientOnly ` + -Property @{ UserName="TestUser"; Password="secret" } + + $Result = Test-TargetResource -Ensure $MockParameters.Ensure ` + -Name $MockParameters.Name ` + -PhysicalPath $MockParameters.PhysicalPath ` + -AnonymousCredentials $MockAnonymousCredentials ` + -Verbose:$VerbosePreference + + It 'Current AnonymousCredentials should be different than expected' { + $Result | Should Be $false + } + } + Context 'Check Preload is different' { Mock -CommandName Get-Website -MockWith {return $MockWebsite} @@ -916,6 +951,11 @@ try -ClientOnly ` -Property @{ Anonymous=$true; Basic=$false; Digest=$false; Windows=$true } + $MockAnonymousCredentials = New-CimInstance ` + -ClassName MSFT_xWebAnonymousAuthenticationCredentials ` + -ClientOnly ` + -Property @{ UserName="TestUser"; Password="secret" } + $MockBindingInfo = @( New-CimInstance -ClassName MSFT_xWebBindingInformation ` -Namespace root/microsoft/Windows/DesiredStateConfiguration ` @@ -956,6 +996,7 @@ try ServiceAutoStartEnabled = $True ApplicationType = 'MockApplicationType' AuthenticationInfo = $MockAuthenticationInfo + AnonymousCredentials = $MockAnonymousCredentials LogPath = 'C:\MockLogLocation' LogFlags = 'Date','Time','ClientIP','UserName','ServerIP' LogPeriod = 'Hourly' @@ -1074,19 +1115,12 @@ try It 'Should call all the mocks' { Assert-MockCalled -CommandName Add-WebConfiguration -Exactly 1 - Assert-MockCalled -CommandName Confirm-UniqueBinding -Exactly 1 - Assert-MockCalled -CommandName Confirm-UniqueServiceAutoStartProviders -Exactly 1 - Assert-MockCalled -CommandName Test-AuthenticationEnabled -Exactly 4 - Assert-MockCalled -CommandName Test-WebsiteBinding -Exactly 1 - Assert-MockCalled -CommandName Update-WebsiteBinding -Exactly 1 - Assert-MockCalled -CommandName Update-DefaultPage -Exactly 1 Assert-MockCalled -CommandName Set-Authentication -Exactly 4 Assert-MockCalled -CommandName Get-Item -Exactly 3 Assert-MockCalled -CommandName Set-Item -Exactly 3 Assert-MockCalled -CommandName Set-ItemProperty -Exactly 10 Assert-MockCalled -CommandName Start-Website -Exactly 1 - Assert-MockCalled -CommandName Set-WebConfigurationProperty -Exactly 2 - Assert-MockCalled -CommandName Test-LogCustomField -Exactly 1 + Assert-MockCalled -CommandName Set-WebConfigurationProperty -Exactly 4 } } From 75a2cdab95ef1f99d43a8f48ee01a526431c48f0 Mon Sep 17 00:00:00 2001 From: Cezary Piatek Date: Sat, 15 Dec 2018 17:34:57 +0100 Subject: [PATCH 3/9] #241: Add missing message formatting --- DSCResources/MSFT_xWebsite/MSFT_xWebsite.psm1 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/DSCResources/MSFT_xWebsite/MSFT_xWebsite.psm1 b/DSCResources/MSFT_xWebsite/MSFT_xWebsite.psm1 index 41e54d95e..d77b43480 100644 --- a/DSCResources/MSFT_xWebsite/MSFT_xWebsite.psm1 +++ b/DSCResources/MSFT_xWebsite/MSFT_xWebsite.psm1 @@ -537,7 +537,7 @@ function Set-TargetResource (Test-AuthenticationEnabled -Site $Name -Type 'Anonymous' ) -and ` ( -not (Test-AnonymousCredentials -Site $Name -Credentials $AnonymousCredentials))) { - Set-AnonymousAuthenticationCredentials -Site $Site -Credentials $AnonymousCredentials -ErrorAction Stop + Set-AnonymousAuthenticationCredentials -Site $Name -Credentials $AnonymousCredentials -ErrorAction Stop Write-Verbose -Message ($LocalizedData.VerboseSetTargetAnonymousCredentialsUpdated ` -f $Name) } @@ -910,7 +910,7 @@ function Test-TargetResource -AuthenticationInfo $AuthenticationInfo))) { $inDesiredState = $false - Write-Verbose -Message ($LocalizedData.VerboseTestTargetFalseAuthenticationInfo) + Write-Verbose -Message ($LocalizedData.VerboseTestTargetFalseAuthenticationInfo -f $Name) } if($PSBoundParameters.ContainsKey('AnonymousCredentials') -and ` @@ -2279,7 +2279,7 @@ function Test-AnonymousCredentials return $false } - ($currentCredentials.UserName -eq $Credentials.User) -and ($currentCredentials.Password -eq $Credentials.Password) + ($currentCredentials.UserName -eq $Credentials.UserName) -and ($currentCredentials.Password -eq $Credentials.Password) } <# .SYNOPSIS @@ -2324,8 +2324,8 @@ function Set-AnonymousAuthenticationCredentials $Credentials ) $anonymousAuthenticationConfigurationPath = 'system.webServer/security/authentication/anonymousAuthentication' - Set-WebConfigurationProperty -Location $Site -Filter $anonymousAuthenticationConfigurationPath -Name 'userName' $Credentials.User - Set-WebConfigurationProperty -Location $Site -Filter $anonymousAuthenticationConfigurationPath -Name 'password' $Credentials.Password + Set-WebConfigurationProperty -Location $Site -Filter $anonymousAuthenticationConfigurationPath -Name 'userName' -Value $Credentials.UserName + Set-WebConfigurationProperty -Location $Site -Filter $anonymousAuthenticationConfigurationPath -Name 'password' -Value $Credentials.Password } #endregion From 4ee636fd42fd94d25191bd25d55bed8425812a04 Mon Sep 17 00:00:00 2001 From: Cezary Piatek Date: Sun, 14 Apr 2019 22:26:14 +0200 Subject: [PATCH 4/9] #241: Remove redundant mock data --- Tests/Unit/MSFT_xWebsite.Tests.ps1 | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/Tests/Unit/MSFT_xWebsite.Tests.ps1 b/Tests/Unit/MSFT_xWebsite.Tests.ps1 index 32985be9a..7f60c341d 100644 --- a/Tests/Unit/MSFT_xWebsite.Tests.ps1 +++ b/Tests/Unit/MSFT_xWebsite.Tests.ps1 @@ -2662,22 +2662,13 @@ try } Describe "$script:DSCResourceName\Get-AuthenticationInfo" { - $MockWebsite = @{ - Name = 'MockName' - PhysicalPath = 'C:\NonExistent' - State = 'Started' - ApplicationPool = 'MockPool' - Bindings = @{Collection = @($MockWebBinding)} - EnabledProtocols = 'http' - ApplicationDefaults = @{Collection = @($MockPreloadAndAutostartProviders)} - Count = 1 - } + $MockWebsiteName ='MockName' Context 'Expected behavior' { Mock -CommandName Get-WebConfigurationProperty -MockWith { return 'False'} It 'should not throw an error' { - { Get-AuthenticationInfo -site $MockWebsite.Name } | Should Not Throw + { Get-AuthenticationInfo -site $MockWebsiteName } | Should Not Throw } It 'should call Get-WebConfigurationProperty four times' { @@ -2695,7 +2686,7 @@ try Mock -CommandName Get-WebConfigurationProperty -MockWith { $MockWebConfiguration } It 'should all be false' { - $result = Get-AuthenticationInfo -site $MockWebsite.Name + $result = Get-AuthenticationInfo -site $MockWebsiteName $result.Anonymous | Should be $false $result.Digest | Should be $false $result.Basic | Should be $false @@ -2717,7 +2708,7 @@ try Mock -CommandName Get-WebConfigurationProperty -MockWith { $MockWebConfiguration } It 'should all be true' { - $result = Get-AuthenticationInfo -site $MockWebsite.Name + $result = Get-AuthenticationInfo -site $MockWebsiteName $result.Anonymous | Should be True $result.Digest | Should be True $result.Basic | Should be True From 607338a4394542690c2fd46d134d22e2bb0bbdd7 Mon Sep 17 00:00:00 2001 From: Cezary Piatek Date: Sun, 14 Apr 2019 23:01:39 +0200 Subject: [PATCH 5/9] #241: Add UT for Get-AnonymousCredentials --- DSCResources/MSFT_xWebsite/MSFT_xWebsite.psm1 | 18 ++++++- Tests/Unit/MSFT_xWebsite.Tests.ps1 | 51 +++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/DSCResources/MSFT_xWebsite/MSFT_xWebsite.psm1 b/DSCResources/MSFT_xWebsite/MSFT_xWebsite.psm1 index d77b43480..f47a946ac 100644 --- a/DSCResources/MSFT_xWebsite/MSFT_xWebsite.psm1 +++ b/DSCResources/MSFT_xWebsite/MSFT_xWebsite.psm1 @@ -2301,7 +2301,10 @@ function Get-AnonymousCredentials { New-CimInstance -ClassName MSFT_xWebAnonymousAuthenticationCredentials ` -ClientOnly ` - -Property @{ UserName = $anonymousAuthentication.userName; Password = $anonymousAuthentication.password } + -Property @{ + UserName = ConvertTo-NotNullString -Value $anonymousAuthentication.userName + Password = ConvertTo-NotNullString -Value $anonymousAuthentication.password + } } } <# @@ -2328,6 +2331,19 @@ function Set-AnonymousAuthenticationCredentials Set-WebConfigurationProperty -Location $Site -Filter $anonymousAuthenticationConfigurationPath -Name 'password' -Value $Credentials.Password } +function ConvertTo-NotNullString + { + param ( + [string] $Value + ) + if([string]::IsNullOrEmpty($Value)) + { + return [string]::Empty + }else{ + return $Value + } +} + #endregion Export-ModuleMember -Function *-TargetResource diff --git a/Tests/Unit/MSFT_xWebsite.Tests.ps1 b/Tests/Unit/MSFT_xWebsite.Tests.ps1 index 7f60c341d..7a05beadc 100644 --- a/Tests/Unit/MSFT_xWebsite.Tests.ps1 +++ b/Tests/Unit/MSFT_xWebsite.Tests.ps1 @@ -2740,6 +2740,57 @@ try } } + Describe "$script:DSCResourceName\Get-AnonymousCredentials" { + $MockWebsiteName ='MockName' + + Context 'When anonymous authentication is disabled' { + Mock -CommandName Get-WebConfiguration -MockWith { return @{ + enabled = $false + }} + + It 'should return expected values' { + Get-AnonymousCredentials -site $MockWebsiteName | Should -Be $null + } + + It 'should call Get-WebConfigurationProperty one time' { + Assert-MockCalled -CommandName Get-WebConfiguration -Exactly 1 + } + } + + Context 'When anonymous authentication is enabled without credentials' { + Mock -CommandName Get-WebConfiguration -MockWith { return @{ + enabled = $true + }} + + It 'should return expected values' { + $result = Get-AnonymousCredentials -site $MockWebsiteName + $result.UserName | Should -Be "" + $result.Password | Should -Be "" + } + + It 'should call Get-WebConfigurationProperty one time' { + Assert-MockCalled -CommandName Get-WebConfiguration -Exactly 1 + } + } + + Context 'When anonymous authentication is enabled along with credentials' { + Mock -CommandName Get-WebConfiguration -MockWith { return @{ + enabled = $true + userName = "TestUser" + password = "Secret" + }} + + It 'should return expected values' { + $result = Get-AnonymousCredentials -site $MockWebsiteName + $result.UserName | Should -Be "TestUser" + $result.Password | Should -Be "Secret" + } + + It 'should call Get-WebConfigurationProperty one time' { + Assert-MockCalled -CommandName Get-WebConfiguration -Exactly 1 + } + } + } Describe "$script:DSCResourceName\Set-Authentication" { Context 'Expected behavior' { $MockWebsite = @{ From 967f81e8eb658c191472bb62e2bb4700479ce225 Mon Sep 17 00:00:00 2001 From: Cezary Piatek Date: Sun, 14 Apr 2019 23:06:29 +0200 Subject: [PATCH 6/9] #241: Remove redundant mock data --- Tests/Unit/MSFT_xWebsite.Tests.ps1 | 43 ++++++------------------------ 1 file changed, 8 insertions(+), 35 deletions(-) diff --git a/Tests/Unit/MSFT_xWebsite.Tests.ps1 b/Tests/Unit/MSFT_xWebsite.Tests.ps1 index 7a05beadc..b74a2e5c7 100644 --- a/Tests/Unit/MSFT_xWebsite.Tests.ps1 +++ b/Tests/Unit/MSFT_xWebsite.Tests.ps1 @@ -2793,22 +2793,13 @@ try } Describe "$script:DSCResourceName\Set-Authentication" { Context 'Expected behavior' { - $MockWebsite = @{ - Name = 'MockName' - PhysicalPath = 'C:\NonExistent' - State = 'Started' - ApplicationPool = 'MockPool' - Bindings = @{Collection = @($MockWebBinding)} - EnabledProtocols = 'http' - ApplicationDefaults = @{Collection = @($MockPreloadAndAutostartProviders)} - Count = 1 - } + $MockWebsiteName = 'MockName' Mock -CommandName Set-WebConfigurationProperty It 'should not throw an error' { { Set-Authentication ` - -Site $MockWebsite.Name ` + -Site $MockWebsiteName ` -Type Basic ` -Enabled $true } | Should Not Throw } @@ -2821,16 +2812,7 @@ try Describe "$script:DSCResourceName\Set-AuthenticationInfo" { Context 'Expected behavior' { - $MockWebsite = @{ - Name = 'MockName' - PhysicalPath = 'C:\NonExistent' - State = 'Started' - ApplicationPool = 'MockPool' - Bindings = @{Collection = @($MockWebBinding)} - EnabledProtocols = 'http' - ApplicationDefaults = @{Collection = @($MockPreloadAndAutostartProviders)} - Count = 1 - } + $MockWebsiteName = 'MockName' Mock -CommandName Set-WebConfigurationProperty @@ -2841,7 +2823,7 @@ try It 'should not throw an error' { { Set-AuthenticationInfo ` - -Site $MockWebsite.Name ` + -Site $MockWebsiteName ` -AuthenticationInfo $AuthenticationInfo } | Should Not Throw } @@ -2852,16 +2834,7 @@ try } Describe "$script:DSCResourceName\Test-AuthenticationEnabled" { - $MockWebsite = @{ - Name = 'MockName' - PhysicalPath = 'C:\NonExistent' - State = 'Started' - ApplicationPool = 'MockPool' - Bindings = @{Collection = @($MockWebBinding)} - EnabledProtocols = 'http' - ApplicationDefaults = @{Collection = @($MockPreloadAndAutostartProviders)} - Count = 1 - } + $MockWebsiteName = 'MockName' Context 'Expected behavior' { $MockWebConfiguration = @( @@ -2874,7 +2847,7 @@ try It 'should not throw an error' { { Test-AuthenticationEnabled ` - -Site $MockWebsite.Name ` + -Site $MockWebsiteName ` -Type 'Basic'} | Should Not Throw } @@ -2893,7 +2866,7 @@ try Mock -CommandName Get-WebConfigurationProperty -MockWith { $MockWebConfiguration } It 'should return false' { - Test-AuthenticationEnabled -Site $MockWebsite.Name -Type 'Basic' | Should be False + Test-AuthenticationEnabled -Site $MockWebsiteName -Type 'Basic' | Should be False } It 'should call expected mocks' { @@ -2911,7 +2884,7 @@ try Mock -CommandName Get-WebConfigurationProperty -MockWith { $MockWebConfiguration} It 'should all be true' { - Test-AuthenticationEnabled -Site $MockWebsite.Name -Type 'Basic' | Should be True + Test-AuthenticationEnabled -Site $MockWebsiteName -Type 'Basic' | Should be True } It 'should call expected mocks' { From d341cb1a7db77320c96df764ae170a9b2651531a Mon Sep 17 00:00:00 2001 From: Cezary Piatek Date: Sun, 14 Apr 2019 23:14:42 +0200 Subject: [PATCH 7/9] #241: Add UT for Set-AnonymousAuthenticationCredentials --- Tests/Unit/MSFT_xWebsite.Tests.ps1 | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Tests/Unit/MSFT_xWebsite.Tests.ps1 b/Tests/Unit/MSFT_xWebsite.Tests.ps1 index b74a2e5c7..885eefa91 100644 --- a/Tests/Unit/MSFT_xWebsite.Tests.ps1 +++ b/Tests/Unit/MSFT_xWebsite.Tests.ps1 @@ -2833,6 +2833,29 @@ try } } + Describe "$script:DSCResourceName\Set-AnonymousAuthenticationCredentials" { + Context 'Expected behavior' { + $MockWebsiteName = 'MockName' + + Mock -CommandName Set-WebConfigurationProperty + + $AnonymousCredentials = New-CimInstance ` + -ClassName MSFT_xWebAnonymousAuthenticationCredentials ` + -ClientOnly ` + -Property @{UserName='SampleUser'; Password='Secret'} + + It 'should not throw an error' { + { Set-AnonymousAuthenticationCredentials ` + -Site $MockWebsiteName ` + -Credentials $AnonymousCredentials } | Should Not Throw + } + + It 'should call should call expected mocks' { + Assert-MockCalled -CommandName Set-WebConfigurationProperty -Exactly 2 + } + } + } + Describe "$script:DSCResourceName\Test-AuthenticationEnabled" { $MockWebsiteName = 'MockName' From ad7c493e17b3ee1fc140f5453888917426e5bd17 Mon Sep 17 00:00:00 2001 From: Cezary Piatek Date: Sun, 14 Apr 2019 23:17:01 +0200 Subject: [PATCH 8/9] #241: Remove redundant mock data --- Tests/Unit/MSFT_xWebsite.Tests.ps1 | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/Tests/Unit/MSFT_xWebsite.Tests.ps1 b/Tests/Unit/MSFT_xWebsite.Tests.ps1 index 885eefa91..ac08ff5bd 100644 --- a/Tests/Unit/MSFT_xWebsite.Tests.ps1 +++ b/Tests/Unit/MSFT_xWebsite.Tests.ps1 @@ -2919,16 +2919,7 @@ try Describe "$script:DSCResourceName\Test-AuthenticationInfo" { Mock -CommandName Get-WebConfigurationProperty -MockWith {$MockWebConfiguration} - $MockWebsite = @{ - Name = 'MockName' - PhysicalPath = 'C:\NonExistent' - State = 'Started' - ApplicationPool = 'MockPool' - Bindings = @{Collection = @($MockWebBinding)} - EnabledProtocols = 'http' - ApplicationDefaults = @{Collection = @($MockPreloadAndAutostartProviders)} - Count = 1 - } + $MockWebsiteName = 'MockName' $MockWebConfiguration = @( @{ @@ -2944,7 +2935,7 @@ try Context 'Expected behavior' { It 'should not throw an error' { { Test-AuthenticationInfo ` - -Site $MockWebsite.Name ` + -Site $MockWebsiteName ` -AuthenticationInfo $AuthenticationInfo } | Should Not Throw } @@ -2957,7 +2948,7 @@ try Mock -CommandName Get-WebConfigurationProperty -MockWith { $MockWebConfiguration} It 'should return false' { - Test-AuthenticationInfo -Site $MockWebsite.Name -AuthenticationInfo $AuthenticationInfo | Should be False + Test-AuthenticationInfo -Site $MockWebsiteName -AuthenticationInfo $AuthenticationInfo | Should be False } It 'should call expected mocks' { @@ -2981,7 +2972,7 @@ try It 'should return true' { Test-AuthenticationInfo ` - -Site $MockWebsite.Name ` + -Site $MockWebsiteName ` -AuthenticationInfo $AuthenticationInfo | Should be True } From e665489a92fa64b8e50f31d7f529f39efe504bfd Mon Sep 17 00:00:00 2001 From: Cezary Piatek Date: Sun, 14 Apr 2019 23:50:33 +0200 Subject: [PATCH 9/9] #241: Add UT for Test-AnonymousCredentials --- Tests/Unit/MSFT_xWebsite.Tests.ps1 | 76 ++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/Tests/Unit/MSFT_xWebsite.Tests.ps1 b/Tests/Unit/MSFT_xWebsite.Tests.ps1 index ac08ff5bd..45bd8039f 100644 --- a/Tests/Unit/MSFT_xWebsite.Tests.ps1 +++ b/Tests/Unit/MSFT_xWebsite.Tests.ps1 @@ -2981,6 +2981,82 @@ try } } } + Describe "$script:DSCResourceName\Test-AnonymousCredentials" { + $MockWebsiteName = 'MockName' + + Context "When expecting given username and password but anonymous authentication is disabled" { + $expectedCredentials = New-CimInstance ` + -ClassName MSFT_xWebAnonymousAuthenticationCredentials ` + -ClientOnly ` + -Property @{UserName='SampleUser'; Password='Secret'} + + Mock -CommandName Get-WebConfiguration -MockWith { @{enabled = $false}} + + It "Should return false" { + Test-AnonymousCredentials -Site $MockWebsiteName -Credentials $expectedCredentials | Should -Be $false + } + } + + Context "When expecting given username and password but anonymous credentials are different" { + $expectedCredentials = New-CimInstance ` + -ClassName MSFT_xWebAnonymousAuthenticationCredentials ` + -ClientOnly ` + -Property @{UserName='SampleUser'; Password='Secret'} + + Mock -CommandName Get-WebConfiguration -MockWith { @{ enabled = $true; userName = "OtherUser"; password = "NonSecret"; } } + + It "Should return false" { + Test-AnonymousCredentials -Site $MockWebsiteName -Credentials $expectedCredentials | Should -Be $false + } + } + + Context "When expecting given username and password but login is different" { + $expectedCredentials = New-CimInstance ` + -ClassName MSFT_xWebAnonymousAuthenticationCredentials ` + -ClientOnly ` + -Property @{UserName='SampleUser'; Password='Secret'} + + Mock -CommandName Get-WebConfiguration -MockWith { @{ enabled = $true; userName = "OtherUser"; password = "Secret" } } + + It "Should return false" { + Test-AnonymousCredentials -Site $MockWebsiteName -Credentials $expectedCredentials | Should -Be $false + } + } + + Context "When expecting given username and password but password is different" { + $expectedCredentials = New-CimInstance ` + -ClassName MSFT_xWebAnonymousAuthenticationCredentials ` + -ClientOnly ` + -Property @{UserName='SampleUser'; Password='Secret'} + + Mock -CommandName Get-WebConfiguration -MockWith { @{ enabled = $true; userName = "SampleUser"; password = "NoSecret" } } + + It "Should return false" { + Test-AnonymousCredentials -Site $MockWebsiteName -Credentials $expectedCredentials | Should -Be $false + } + } + + Context "When current anonymous credentials are the same as expected" { + $expectedCredentials = New-CimInstance ` + -ClassName MSFT_xWebAnonymousAuthenticationCredentials ` + -ClientOnly ` + -Property @{UserName='SampleUser'; Password='Secret'} + + Mock -CommandName Get-WebConfiguration -MockWith { @{ enabled = $true; userName = "SampleUser"; password = "Secret" } } + + It "Should return true" { + Test-AnonymousCredentials -Site $MockWebsiteName -Credentials $expectedCredentials | Should -Be $true + } + } + + Context "When expecting no credentials but there are any" { + Mock -CommandName Get-WebConfiguration -MockWith { @{ enabled = $true; userName = "SampleUser"; password = "Secret"; } } + + It "Should return false" { + Test-AnonymousCredentials -Site $MockWebsiteName | Should -Be $false + } + } + } Describe "$script:DSCResourceName\Test-BindingInfo" { Context 'BindingInfo is valid' {