From 0d553f714796706b4007b2415b1e5d7f2af2e29d Mon Sep 17 00:00:00 2001 From: Javier Calvarro Nelson Date: Fri, 26 Jul 2019 19:37:13 +0200 Subject: [PATCH] Fix small issue in Identity UI (#12607) The logic for selecting the views assembly had a small bug. Added an E2E test to cover the scenario. --- .../UI/src/IdentityBuilderUIExtensions.cs | 26 +++++----- .../UIFramewrokAttributeTest.cs | 47 +++++++++++++++++++ .../Identity.DefaultUI.WebSite.csproj | 7 +++ 3 files changed, 67 insertions(+), 13 deletions(-) create mode 100644 src/Identity/test/Identity.FunctionalTests/Bootstrap3Tests/UIFramewrokAttributeTest.cs diff --git a/src/Identity/UI/src/IdentityBuilderUIExtensions.cs b/src/Identity/UI/src/IdentityBuilderUIExtensions.cs index 6cf1ed958f..b83c37ca8e 100644 --- a/src/Identity/UI/src/IdentityBuilderUIExtensions.cs +++ b/src/Identity/UI/src/IdentityBuilderUIExtensions.cs @@ -53,22 +53,22 @@ namespace Microsoft.AspNetCore.Identity private static void AddRelatedParts(IdentityBuilder builder) { - // We try to resolve the UI framework that was used by looking at the entry assembly. - // When an app runs, the entry assembly will point to the built app. In some rare cases - // (functional testing) the app assembly will be different, and we'll try to locate it through - // the same mechanism that MVC uses today. - // Finally, if for some reason we aren't able to find the assembly, we'll use our default value - // (Bootstrap4) - if (!TryResolveUIFramework(Assembly.GetEntryAssembly(), out var framework) || - !TryResolveUIFramework(GetApplicationAssembly(builder), out framework)) - { - framework = default; - } - var mvcBuilder = builder.Services .AddMvc() .ConfigureApplicationPartManager(partManager => { + // We try to resolve the UI framework that was used by looking at the entry assembly. + // When an app runs, the entry assembly will point to the built app. In some rare cases + // (functional testing) the app assembly will be different, and we'll try to locate it through + // the same mechanism that MVC uses today. + // Finally, if for some reason we aren't able to find the assembly, we'll use our default value + // (Bootstrap4) + if (!TryResolveUIFramework(Assembly.GetEntryAssembly(), out var framework) && + !TryResolveUIFramework(GetApplicationAssembly(builder), out framework)) + { + framework = default; + } + var thisAssembly = typeof(IdentityBuilderUIExtensions).Assembly; var relatedAssemblies = RelatedAssemblyAttribute.GetRelatedAssemblies(thisAssembly, throwOnError: true); var relatedParts = relatedAssemblies.ToDictionary( @@ -149,7 +149,7 @@ namespace Microsoft.AspNetCore.Identity } // If we find the metadata there must be a valid framework here. - if (!Enum.TryParse(metadata, ignoreCase: true, out var uIFramework)) + if (!Enum.TryParse(metadata, ignoreCase: true, out uiFramework)) { var enumValues = string.Join(", ", Enum.GetNames(typeof(UIFramework)).Select(v => $"'{v}'")); throw new InvalidOperationException( diff --git a/src/Identity/test/Identity.FunctionalTests/Bootstrap3Tests/UIFramewrokAttributeTest.cs b/src/Identity/test/Identity.FunctionalTests/Bootstrap3Tests/UIFramewrokAttributeTest.cs new file mode 100644 index 0000000000..c771f8becf --- /dev/null +++ b/src/Identity/test/Identity.FunctionalTests/Bootstrap3Tests/UIFramewrokAttributeTest.cs @@ -0,0 +1,47 @@ +// 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.Linq; +using Identity.DefaultUI.WebSite; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc.ApplicationParts; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.Extensions.DependencyInjection; +using Xunit; + +namespace Microsoft.AspNetCore.Identity.FunctionalTests.Bootstrap3Tests +{ + public class UIFramewrokAttributeTest : IClassFixture> + { + public UIFramewrokAttributeTest(WebApplicationFactory factory) + { + Factory = factory; + } + + public WebApplicationFactory Factory { get; } + + [Fact] + public void DefaultWebSite_DefaultsToBootstrap3() + { + var hasV3Part = false; + var hasV4Part = false; + var factory = Factory.WithWebHostBuilder( + whb => whb.UseStartup().ConfigureServices( + services => services.AddMvc().ConfigureApplicationPartManager( + apm => (hasV3Part, hasV4Part) = (HasPart(apm, "V3"), HasPart(apm, "V4"))))); + + // Act + var client = factory.CreateClient(); + + // Assert + Assert.True(hasV3Part); + Assert.False(hasV4Part); + } + + private static bool HasPart(ApplicationPartManager apm, string name) + { + return apm.ApplicationParts + .Any(p => p is CompiledRazorAssemblyPart cp && cp.Assembly.GetName().Name == "Microsoft.AspNetCore.Identity.UI.Views." + name); + } + } +} diff --git a/src/Identity/testassets/Identity.DefaultUI.WebSite/Identity.DefaultUI.WebSite.csproj b/src/Identity/testassets/Identity.DefaultUI.WebSite/Identity.DefaultUI.WebSite.csproj index ad7f35bb53..4309f2248c 100644 --- a/src/Identity/testassets/Identity.DefaultUI.WebSite/Identity.DefaultUI.WebSite.csproj +++ b/src/Identity/testassets/Identity.DefaultUI.WebSite/Identity.DefaultUI.WebSite.csproj @@ -48,6 +48,13 @@ _SetBootstrapFrameworkVersion + + + <_Parameter1>$(IdentityDefaultUIFramework) + + + + <_StaticWebAssetsProjectReference Condition="'%(FileName)' == 'Microsoft.AspNetCore.Identity.UI'">