From 0b5973e5014f17e7bbdbe57c28b90d2b9b1d2347 Mon Sep 17 00:00:00 2001 From: Pavel Krymets <pavel@krymets.com> Date: Sat, 13 Oct 2018 10:54:20 -0700 Subject: [PATCH] Enable auth tests on IIS (#1511) * Enable auth tests on IIS * FB --- .../IISDeploymentParameterExtensions.cs | 46 ++++++++++++++++--- .../BasicAuthTests.cs | 5 +- .../RequiresEnvironmentVariableAttribute.cs | 24 ++++++++++ .../WindowsAuthTests.cs | 0 test/IIS.FunctionalTests/ServicesTests.cs | 6 +-- tools/SetupTestEnvironment.ps1 | 4 +- 6 files changed, 69 insertions(+), 16 deletions(-) rename test/{IISExpress.FunctionalTests => Common.FunctionalTests}/BasicAuthTests.cs (92%) create mode 100644 test/Common.FunctionalTests/Utilities/RequiresEnvironmentVariableAttribute.cs rename test/{IISExpress.FunctionalTests => Common.FunctionalTests}/WindowsAuthTests.cs (100%) diff --git a/src/Microsoft.AspNetCore.Server.IntegrationTesting.IIS/IISDeploymentParameterExtensions.cs b/src/Microsoft.AspNetCore.Server.IntegrationTesting.IIS/IISDeploymentParameterExtensions.cs index 8534add2540..90f76fd8c29 100644 --- a/src/Microsoft.AspNetCore.Server.IntegrationTesting.IIS/IISDeploymentParameterExtensions.cs +++ b/src/Microsoft.AspNetCore.Server.IntegrationTesting.IIS/IISDeploymentParameterExtensions.cs @@ -43,14 +43,22 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting.IIS public static void SetWindowsAuth(this IISDeploymentParameters parameters, bool enabled = true) { + parameters.EnsureSection("windowsAuthentication", "system.webServer", "security", "windowsAuthentication"); parameters.EnableModule("WindowsAuthenticationModule", "%IIS_BIN%\\authsspi.dll"); parameters.AddServerConfigAction( element => { - element.Descendants("windowsAuthentication") - .Single() - .SetAttributeValue("enabled", enabled); + var windowsAuthentication = element + .RequiredElement("system.webServer") + .RequiredElement("security") + .RequiredElement("authentication") + .GetOrAdd("windowsAuthentication"); + + windowsAuthentication.SetAttributeValue("enabled", enabled); + var providers = windowsAuthentication.GetOrAdd("providers"); + providers.GetOrAdd("add", "value", "Negotiate"); + providers.GetOrAdd("add", "value", "NTLM"); }); } @@ -59,8 +67,11 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting.IIS parameters.AddServerConfigAction( element => { - element.Descendants("anonymousAuthentication") - .Single() + element + .RequiredElement("system.webServer") + .RequiredElement("security") + .RequiredElement("authentication") + .GetOrAdd("anonymousAuthentication") .SetAttributeValue("enabled", enabled); }); } @@ -72,12 +83,33 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting.IIS parameters.AddServerConfigAction( element => { - element.Descendants("basicAuthentication") - .Single() + element + .RequiredElement("system.webServer") + .RequiredElement("security") + .RequiredElement("authentication") + .GetOrAdd("basicAuthentication") .SetAttributeValue("enabled", enabled); }); } + public static void EnsureSection(this IISDeploymentParameters parameters, string name, params string[] path) + { + parameters.ServerConfigActionList.Add( + (config, _) => { + + var element = config + .RequiredElement("configSections"); + + foreach (var s in path) + { + element = element.GetOrAdd("sectionGroup", "name", s); + } + + element.GetOrAdd("section", "name", "applicationInitialization") + .SetAttributeValue("overrideModeDefault", "Allow"); + }); + } + public static void EnableLogging(this IISDeploymentParameters deploymentParameters, string path) { deploymentParameters.WebConfigActionList.Add( diff --git a/test/IISExpress.FunctionalTests/BasicAuthTests.cs b/test/Common.FunctionalTests/BasicAuthTests.cs similarity index 92% rename from test/IISExpress.FunctionalTests/BasicAuthTests.cs rename to test/Common.FunctionalTests/BasicAuthTests.cs index 1011b678ed7..1c7faf00b4d 100644 --- a/test/IISExpress.FunctionalTests/BasicAuthTests.cs +++ b/test/Common.FunctionalTests/BasicAuthTests.cs @@ -32,7 +32,8 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.FunctionalTests .WithAllAncmVersions() .WithAllHostingModels(); - [ConditionalTheory(Skip = "This test is manual. To run it set ASPNETCORE_MODULE_TEST_USER and ASPNETCORE_MODULE_TEST_PASSWORD environment variables to existing user")] + [ConditionalTheory] + [RequiresEnvironmentVariable("ASPNETCORE_MODULE_TEST_USER")] [RequiresIIS(IISCapability.BasicAuthentication)] [MemberData(nameof(TestVariants))] public async Task BasicAuthTest(TestVariant variant) @@ -63,7 +64,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.FunctionalTests else { // We expect out-of-proc not allowing basic auth - Assert.Equal("Windows:", responseText); + Assert.Equal("Windows", responseText); } } } diff --git a/test/Common.FunctionalTests/Utilities/RequiresEnvironmentVariableAttribute.cs b/test/Common.FunctionalTests/Utilities/RequiresEnvironmentVariableAttribute.cs new file mode 100644 index 00000000000..d2749db547d --- /dev/null +++ b/test/Common.FunctionalTests/Utilities/RequiresEnvironmentVariableAttribute.cs @@ -0,0 +1,24 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using Microsoft.AspNetCore.Server.IntegrationTesting; +using Microsoft.AspNetCore.Testing.xunit; + +namespace Microsoft.AspNetCore.Server.IISIntegration.FunctionalTests +{ + [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)] + public sealed class RequiresEnvironmentVariableAttribute : Attribute, ITestCondition + { + private readonly string _name; + + public RequiresEnvironmentVariableAttribute(string name) + { + _name = name; + } + + public bool IsMet => !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(_name)); + + public string SkipReason => $"Environment variable {_name} is required to run this test."; + } +} diff --git a/test/IISExpress.FunctionalTests/WindowsAuthTests.cs b/test/Common.FunctionalTests/WindowsAuthTests.cs similarity index 100% rename from test/IISExpress.FunctionalTests/WindowsAuthTests.cs rename to test/Common.FunctionalTests/WindowsAuthTests.cs diff --git a/test/IIS.FunctionalTests/ServicesTests.cs b/test/IIS.FunctionalTests/ServicesTests.cs index 47e53b8d404..875b5b13be8 100644 --- a/test/IIS.FunctionalTests/ServicesTests.cs +++ b/test/IIS.FunctionalTests/ServicesTests.cs @@ -78,13 +78,9 @@ namespace IIS.FunctionalTests private static void EnablePreload(IISDeploymentParameters baseDeploymentParameters) { + baseDeploymentParameters.EnsureSection("applicationInitialization", "system.webServer"); baseDeploymentParameters.ServerConfigActionList.Add( (config, _) => { - config - .RequiredElement("configSections") - .GetOrAdd("sectionGroup", "name", "system.webServer") - .GetOrAdd("section", "name", "applicationInitialization") - .SetAttributeValue("overrideModeDefault", "Allow"); config .RequiredElement("system.applicationHost") diff --git a/tools/SetupTestEnvironment.ps1 b/tools/SetupTestEnvironment.ps1 index 625b040e39f..3adaf790451 100644 --- a/tools/SetupTestEnvironment.ps1 +++ b/tools/SetupTestEnvironment.ps1 @@ -57,8 +57,8 @@ function Setup-appverif($application) function Shutdown-appverif($application) { - setx APPVERIFIER_ENABLED_CODES "`"`""; - setx APPVERIFIER_LEVEL "`"`""; + setx APPVERIFIER_ENABLED_CODES "NONE"; + setx APPVERIFIER_LEVEL "NONE"; appverif.exe -disable * -for $application } -- GitLab