Move identity functional test website to generic host (#10974)

This commit is contained in:
Javier Calvarro Nelson 2019-06-06 22:01:33 -07:00 committed by GitHub
parent f7f80fdbaa
commit 5d091df908
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 104 additions and 65 deletions

View File

@ -1,8 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved.
// 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 System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Identity.DefaultUI.WebSite;
@ -12,16 +14,45 @@ using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.AspNetCore.Identity.FunctionalTests
{
public static class FunctionalTestsServiceCollectionExtensions
{
public static IServiceCollection SetupTestDatabase<TContext>(this IServiceCollection services, DbConnection connection) where TContext : DbContext =>
services.AddDbContext<TContext>(options =>
options.ConfigureWarnings(b => b.Log(CoreEventId.ManyServiceProvidersCreatedWarning))
.UseSqlite(connection));
public static IServiceCollection SetupTestDatabase<TContext>(this IServiceCollection services, DbConnection connection) where TContext : DbContext
{
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<TContext>));
if (descriptor != null)
{
services.Remove(descriptor);
}
services.AddScoped(p =>
DbContextOptionsFactory<TContext>(
p,
(sp, options) => options
.ConfigureWarnings(b => b.Log(CoreEventId.ManyServiceProvidersCreatedWarning))
.UseSqlite(connection)));
return services;
}
private static DbContextOptions<TContext> DbContextOptionsFactory<TContext>(
IServiceProvider applicationServiceProvider,
Action<IServiceProvider, DbContextOptionsBuilder> optionsAction)
where TContext : DbContext
{
var builder = new DbContextOptionsBuilder<TContext>(
new DbContextOptions<TContext>(new Dictionary<Type, IDbContextOptionsExtension>()));
builder.UseApplicationServiceProvider(applicationServiceProvider);
optionsAction?.Invoke(applicationServiceProvider, builder);
return builder.Options;
}
public static IServiceCollection SetupTestThirdPartyLogin(this IServiceCollection services) =>
services.AddAuthentication()

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Identity.DefaultUI.WebSite;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
@ -9,6 +10,7 @@ using Microsoft.AspNetCore.TestHost;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Microsoft.AspNetCore.Identity.FunctionalTests
{
@ -27,6 +29,12 @@ namespace Microsoft.AspNetCore.Identity.FunctionalTests
ClientOptions.BaseAddress = new Uri("https://localhost");
}
protected override IHostBuilder CreateHostBuilder()
{
Program.UseStartup = false;
return base.CreateHostBuilder();
}
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
base.ConfigureWebHost(builder);
@ -42,18 +50,25 @@ namespace Microsoft.AspNetCore.Identity.FunctionalTests
});
}
protected override TestServer CreateServer(IWebHostBuilder builder)
protected override IHost CreateHost(IHostBuilder builder)
{
var result = base.CreateServer(builder);
EnsureDatabaseCreated(result);
var result = base.CreateHost(builder);
EnsureDatabaseCreated(result.Services);
return result;
}
public void EnsureDatabaseCreated(TestServer server)
protected override TestServer CreateServer(IWebHostBuilder builder)
{
using (var scope = server.Host.Services.CreateScope())
var result = base.CreateServer(builder);
EnsureDatabaseCreated(result.Host.Services);
return result;
}
public void EnsureDatabaseCreated(IServiceProvider services)
{
using (var scope = services.CreateScope())
{
scope.ServiceProvider.GetService<TContext>().Database.EnsureCreated();
scope.ServiceProvider.GetService<TContext>()?.Database?.EnsureCreated();
}
}

View File

@ -11,6 +11,7 @@
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.AspNetCore" />
<Reference Include="Microsoft.AspNetCore.Authentication.Cookies" />
<Reference Include="Microsoft.AspNetCore.Authentication.Facebook" />
<Reference Include="Microsoft.AspNetCore.Authentication.Google" />
@ -36,6 +37,7 @@
<Reference Include="Microsoft.Extensions.Configuration.CommandLine" />
<Reference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
<Reference Include="Microsoft.Extensions.Configuration.UserSecrets" />
<Reference Include="Microsoft.Extensions.Hosting" />
<Reference Include="Microsoft.Extensions.Logging.Configuration" />
<Reference Include="Microsoft.Extensions.Logging.Console" />
<Reference Include="Microsoft.Extensions.Logging.Debug" />

View File

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// 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 Microsoft.AspNetCore.Builder;

View File

@ -14,60 +14,19 @@ namespace Identity.DefaultUI.WebSite
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
CreateHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
var builder = new WebHostBuilder()
.UseKestrel((builderContext, options) =>
{
options.Configure(builderContext.Configuration.GetSection("Kestrel"));
})
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;
public static bool UseStartup { get; set; } = true;
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: false);
if (env.IsDevelopment())
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
if (UseStartup)
{
var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
if (appAssembly != null)
{
config.AddUserSecrets(appAssembly, optional: true);
}
webBuilder.UseStartup<Startup>();
}
config.AddEnvironmentVariables();
if (args != null)
{
config.AddCommandLine(args);
}
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.AddDebug();
})
.UseIISIntegration()
.UseDefaultServiceProvider((context, options) =>
{
options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
});
if (args != null)
{
builder.UseConfiguration(new ConfigurationBuilder().AddCommandLine(args).Build());
}
builder.UseStartup<Startup>();
return builder;
}
}
}

View File

@ -1,6 +1,8 @@
// 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 System.Collections.Generic;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
@ -49,9 +51,8 @@ namespace Identity.DefaultUI.WebSite
.AddDefaultUI(Framework)
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<TContext>();
services.AddMvc();
services.AddMvc();
services.AddSingleton<IFileVersionProvider, FileVersionProvider>();
}
@ -59,7 +60,7 @@ namespace Identity.DefaultUI.WebSite
public virtual void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// This prevents running out of file watchers on some linux machines
((PhysicalFileProvider)env.WebRootFileProvider).UseActivePolling = false;
DisableFilePolling(env);
if (env.IsDevelopment())
{
@ -87,5 +88,36 @@ namespace Identity.DefaultUI.WebSite
endpoints.MapRazorPages();
});
}
protected static void DisableFilePolling(IWebHostEnvironment env)
{
var pendingProviders = new Stack<IFileProvider>();
pendingProviders.Push(env.WebRootFileProvider);
while (pendingProviders.TryPop(out var currentProvider))
{
switch (currentProvider)
{
case PhysicalFileProvider physical:
physical.UseActivePolling = false;
break;
case IFileProvider staticWebAssets when staticWebAssets.GetType().Name == "StaticWebAssetsFileProvider":
GetUnderlyingProvider(staticWebAssets).UseActivePolling = false;
break;
case CompositeFileProvider composite:
foreach (var childFileProvider in composite.FileProviders)
{
pendingProviders.Push(childFileProvider);
}
break;
default:
throw new InvalidOperationException("Unknown provider");
}
}
}
private static PhysicalFileProvider GetUnderlyingProvider(IFileProvider staticWebAssets)
{
return (PhysicalFileProvider) staticWebAssets.GetType().GetProperty("InnerProvider").GetValue(staticWebAssets);
}
}
}

View File

@ -28,7 +28,7 @@ namespace Identity.DefaultUI.WebSite
public override void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// This prevents running out of file watchers on some linux machines
((PhysicalFileProvider)env.WebRootFileProvider).UseActivePolling = false;
DisableFilePolling(env);
if (env.IsDevelopment())
{