Skip to content

Commit 5d091df

Browse files
authored
Move identity functional test website to generic host (#10974)
1 parent f7f80fd commit 5d091df

File tree

7 files changed

+102
-63
lines changed

7 files changed

+102
-63
lines changed

src/Identity/test/Identity.FunctionalTests/Infrastructure/FunctionalTestsServiceCollectionExtensions.cs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.Data.Common;
7+
using System.Linq;
68
using System.Security.Claims;
79
using System.Threading.Tasks;
810
using Identity.DefaultUI.WebSite;
@@ -12,16 +14,45 @@
1214
using Microsoft.AspNetCore.Mvc.Authorization;
1315
using Microsoft.EntityFrameworkCore;
1416
using Microsoft.EntityFrameworkCore.Diagnostics;
17+
using Microsoft.EntityFrameworkCore.Infrastructure;
1518
using Microsoft.Extensions.DependencyInjection;
1619

1720
namespace Microsoft.AspNetCore.Identity.FunctionalTests
1821
{
1922
public static class FunctionalTestsServiceCollectionExtensions
2023
{
21-
public static IServiceCollection SetupTestDatabase<TContext>(this IServiceCollection services, DbConnection connection) where TContext : DbContext =>
22-
services.AddDbContext<TContext>(options =>
23-
options.ConfigureWarnings(b => b.Log(CoreEventId.ManyServiceProvidersCreatedWarning))
24-
.UseSqlite(connection));
24+
public static IServiceCollection SetupTestDatabase<TContext>(this IServiceCollection services, DbConnection connection) where TContext : DbContext
25+
{
26+
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<TContext>));
27+
if (descriptor != null)
28+
{
29+
services.Remove(descriptor);
30+
}
31+
32+
services.AddScoped(p =>
33+
DbContextOptionsFactory<TContext>(
34+
p,
35+
(sp, options) => options
36+
.ConfigureWarnings(b => b.Log(CoreEventId.ManyServiceProvidersCreatedWarning))
37+
.UseSqlite(connection)));
38+
39+
return services;
40+
}
41+
42+
private static DbContextOptions<TContext> DbContextOptionsFactory<TContext>(
43+
IServiceProvider applicationServiceProvider,
44+
Action<IServiceProvider, DbContextOptionsBuilder> optionsAction)
45+
where TContext : DbContext
46+
{
47+
var builder = new DbContextOptionsBuilder<TContext>(
48+
new DbContextOptions<TContext>(new Dictionary<Type, IDbContextOptionsExtension>()));
49+
50+
builder.UseApplicationServiceProvider(applicationServiceProvider);
51+
52+
optionsAction?.Invoke(applicationServiceProvider, builder);
53+
54+
return builder.Options;
55+
}
2556

2657
public static IServiceCollection SetupTestThirdPartyLogin(this IServiceCollection services) =>
2758
services.AddAuthentication()

src/Identity/test/Identity.FunctionalTests/Infrastructure/ServerFactory.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using Identity.DefaultUI.WebSite;
56
using Microsoft.AspNetCore.Authentication;
67
using Microsoft.AspNetCore.Hosting;
78
using Microsoft.AspNetCore.Mvc.Testing;
89
using Microsoft.AspNetCore.TestHost;
910
using Microsoft.Data.Sqlite;
1011
using Microsoft.EntityFrameworkCore;
1112
using Microsoft.Extensions.DependencyInjection;
13+
using Microsoft.Extensions.Hosting;
1214

1315
namespace Microsoft.AspNetCore.Identity.FunctionalTests
1416
{
@@ -27,6 +29,12 @@ public ServerFactory()
2729
ClientOptions.BaseAddress = new Uri("https://localhost");
2830
}
2931

32+
protected override IHostBuilder CreateHostBuilder()
33+
{
34+
Program.UseStartup = false;
35+
return base.CreateHostBuilder();
36+
}
37+
3038
protected override void ConfigureWebHost(IWebHostBuilder builder)
3139
{
3240
base.ConfigureWebHost(builder);
@@ -42,18 +50,25 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
4250
});
4351
}
4452

53+
protected override IHost CreateHost(IHostBuilder builder)
54+
{
55+
var result = base.CreateHost(builder);
56+
EnsureDatabaseCreated(result.Services);
57+
return result;
58+
}
59+
4560
protected override TestServer CreateServer(IWebHostBuilder builder)
4661
{
4762
var result = base.CreateServer(builder);
48-
EnsureDatabaseCreated(result);
63+
EnsureDatabaseCreated(result.Host.Services);
4964
return result;
5065
}
5166

52-
public void EnsureDatabaseCreated(TestServer server)
67+
public void EnsureDatabaseCreated(IServiceProvider services)
5368
{
54-
using (var scope = server.Host.Services.CreateScope())
69+
using (var scope = services.CreateScope())
5570
{
56-
scope.ServiceProvider.GetService<TContext>().Database.EnsureCreated();
71+
scope.ServiceProvider.GetService<TContext>()?.Database?.EnsureCreated();
5772
}
5873
}
5974

src/Identity/testassets/Identity.DefaultUI.WebSite/Identity.DefaultUI.WebSite.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
</ItemGroup>
1212

1313
<ItemGroup>
14+
<Reference Include="Microsoft.AspNetCore" />
1415
<Reference Include="Microsoft.AspNetCore.Authentication.Cookies" />
1516
<Reference Include="Microsoft.AspNetCore.Authentication.Facebook" />
1617
<Reference Include="Microsoft.AspNetCore.Authentication.Google" />
@@ -36,6 +37,7 @@
3637
<Reference Include="Microsoft.Extensions.Configuration.CommandLine" />
3738
<Reference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
3839
<Reference Include="Microsoft.Extensions.Configuration.UserSecrets" />
40+
<Reference Include="Microsoft.Extensions.Hosting" />
3941
<Reference Include="Microsoft.Extensions.Logging.Configuration" />
4042
<Reference Include="Microsoft.Extensions.Logging.Console" />
4143
<Reference Include="Microsoft.Extensions.Logging.Debug" />

src/Identity/testassets/Identity.DefaultUI.WebSite/PocoUserStartup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using Microsoft.AspNetCore.Builder;

src/Identity/testassets/Identity.DefaultUI.WebSite/Program.cs

Lines changed: 8 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,60 +14,19 @@ public class Program
1414
{
1515
public static void Main(string[] args)
1616
{
17-
CreateWebHostBuilder(args).Build().Run();
17+
CreateHostBuilder(args).Build().Run();
1818
}
1919

20-
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
21-
{
22-
var builder = new WebHostBuilder()
23-
.UseKestrel((builderContext, options) =>
24-
{
25-
options.Configure(builderContext.Configuration.GetSection("Kestrel"));
26-
})
27-
.UseContentRoot(Directory.GetCurrentDirectory())
28-
.ConfigureAppConfiguration((hostingContext, config) =>
29-
{
30-
var env = hostingContext.HostingEnvironment;
31-
32-
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
33-
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: false);
34-
35-
if (env.IsDevelopment())
36-
{
37-
var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
38-
if (appAssembly != null)
39-
{
40-
config.AddUserSecrets(appAssembly, optional: true);
41-
}
42-
}
43-
44-
config.AddEnvironmentVariables();
20+
public static bool UseStartup { get; set; } = true;
4521

46-
if (args != null)
22+
public static IHostBuilder CreateHostBuilder(string[] args) =>
23+
Host.CreateDefaultBuilder(args)
24+
.ConfigureWebHostDefaults(webBuilder =>
25+
{
26+
if (UseStartup)
4727
{
48-
config.AddCommandLine(args);
28+
webBuilder.UseStartup<Startup>();
4929
}
50-
})
51-
.ConfigureLogging((hostingContext, logging) =>
52-
{
53-
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
54-
logging.AddConsole();
55-
logging.AddDebug();
56-
})
57-
.UseIISIntegration()
58-
.UseDefaultServiceProvider((context, options) =>
59-
{
60-
options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
6130
});
62-
63-
if (args != null)
64-
{
65-
builder.UseConfiguration(new ConfigurationBuilder().AddCommandLine(args).Build());
66-
}
67-
68-
builder.UseStartup<Startup>();
69-
70-
return builder;
71-
}
7231
}
7332
}

src/Identity/testassets/Identity.DefaultUI.WebSite/StartupBase.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
5+
using System.Collections.Generic;
46
using Microsoft.AspNetCore.Builder;
57
using Microsoft.AspNetCore.Hosting;
68
using Microsoft.AspNetCore.Identity;
@@ -49,17 +51,16 @@ public virtual void ConfigureServices(IServiceCollection services)
4951
.AddDefaultUI(Framework)
5052
.AddRoles<IdentityRole>()
5153
.AddEntityFrameworkStores<TContext>();
52-
53-
services.AddMvc();
5454

55+
services.AddMvc();
5556
services.AddSingleton<IFileVersionProvider, FileVersionProvider>();
5657
}
5758

5859
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
5960
public virtual void Configure(IApplicationBuilder app, IWebHostEnvironment env)
6061
{
6162
// This prevents running out of file watchers on some linux machines
62-
((PhysicalFileProvider)env.WebRootFileProvider).UseActivePolling = false;
63+
DisableFilePolling(env);
6364

6465
if (env.IsDevelopment())
6566
{
@@ -87,5 +88,36 @@ public virtual void Configure(IApplicationBuilder app, IWebHostEnvironment env)
8788
endpoints.MapRazorPages();
8889
});
8990
}
91+
92+
protected static void DisableFilePolling(IWebHostEnvironment env)
93+
{
94+
var pendingProviders = new Stack<IFileProvider>();
95+
pendingProviders.Push(env.WebRootFileProvider);
96+
while (pendingProviders.TryPop(out var currentProvider))
97+
{
98+
switch (currentProvider)
99+
{
100+
case PhysicalFileProvider physical:
101+
physical.UseActivePolling = false;
102+
break;
103+
case IFileProvider staticWebAssets when staticWebAssets.GetType().Name == "StaticWebAssetsFileProvider":
104+
GetUnderlyingProvider(staticWebAssets).UseActivePolling = false;
105+
break;
106+
case CompositeFileProvider composite:
107+
foreach (var childFileProvider in composite.FileProviders)
108+
{
109+
pendingProviders.Push(childFileProvider);
110+
}
111+
break;
112+
default:
113+
throw new InvalidOperationException("Unknown provider");
114+
}
115+
}
116+
}
117+
118+
private static PhysicalFileProvider GetUnderlyingProvider(IFileProvider staticWebAssets)
119+
{
120+
return (PhysicalFileProvider) staticWebAssets.GetType().GetProperty("InnerProvider").GetValue(staticWebAssets);
121+
}
90122
}
91123
}

src/Identity/testassets/Identity.DefaultUI.WebSite/StartupWithoutEndpointRouting.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public override void ConfigureServices(IServiceCollection services)
2828
public override void Configure(IApplicationBuilder app, IWebHostEnvironment env)
2929
{
3030
// This prevents running out of file watchers on some linux machines
31-
((PhysicalFileProvider)env.WebRootFileProvider).UseActivePolling = false;
31+
DisableFilePolling(env);
3232

3333
if (env.IsDevelopment())
3434
{

0 commit comments

Comments
 (0)