net6conversion (#642)

* Converting to net6 startup and integration test formats

* Update to minimal api and using pagetitle

* Adjust functional test fixtures
Update ApiEndpoints package version

* Finish migration to minimal api startup
This commit is contained in:
Steve Smith
2021-12-06 15:14:43 -05:00
committed by GitHub
parent 20b060aeb3
commit ce63e38a23
34 changed files with 499 additions and 728 deletions

View File

@@ -1,78 +1,43 @@
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopWeb.Infrastructure.Data;
using Microsoft.eShopWeb.Infrastructure.Identity;
using Microsoft.eShopWeb.PublicApi;
using Microsoft.eShopWeb.PublicApi.AuthEndpoints;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Hosting;
namespace Microsoft.eShopWeb.FunctionalTests.PublicApi;
public class ApiTestFixture : WebApplicationFactory<Startup>
public class TestApiApplication : WebApplicationFactory<Authenticate>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseEnvironment("Testing");
private readonly string _environment = "Testing";
protected override IHost CreateHost(IHostBuilder builder)
{
builder.UseEnvironment(_environment);
// Add mock/test services to the builder here
builder.ConfigureServices(services =>
{
services.AddEntityFrameworkInMemoryDatabase();
// Create a new service provider.
var provider = services
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
// Add a database context (ApplicationDbContext) using an in-memory
// database for testing.
services.AddDbContext<CatalogContext>(options =>
services.AddScoped(sp =>
{
options.UseInMemoryDatabase("InMemoryDbForTesting");
options.UseInternalServiceProvider(provider);
// Replace SQLite with in-memory database for tests
return new DbContextOptionsBuilder<CatalogContext>()
.UseInMemoryDatabase("DbForPublicApi")
.UseApplicationServiceProvider(sp)
.Options;
});
services.AddDbContext<AppIdentityDbContext>(options =>
services.AddScoped(sp =>
{
options.UseInMemoryDatabase("Identity");
options.UseInternalServiceProvider(provider);
// Replace SQLite with in-memory database for tests
return new DbContextOptionsBuilder<AppIdentityDbContext>()
.UseInMemoryDatabase("IdentityDbForPublicApi")
.UseApplicationServiceProvider(sp)
.Options;
});
// Build the service provider.
var sp = services.BuildServiceProvider();
// Create a scope to obtain a reference to the database
// context (ApplicationDbContext).
using (var scope = sp.CreateScope())
{
var scopedServices = scope.ServiceProvider;
var db = scopedServices.GetRequiredService<CatalogContext>();
var loggerFactory = scopedServices.GetRequiredService<ILoggerFactory>();
var logger = scopedServices
.GetRequiredService<ILogger<ApiTestFixture>>();
// Ensure the database is created.
db.Database.EnsureCreated();
try
{
// Seed the database with test data.
CatalogContextSeed.SeedAsync(db, loggerFactory).Wait();
// seed sample user data
var userManager = scopedServices.GetRequiredService<UserManager<ApplicationUser>>();
var roleManager = scopedServices.GetRequiredService<RoleManager<IdentityRole>>();
AppIdentityDbContextSeed.SeedAsync(userManager, roleManager).Wait();
}
catch (Exception ex)
{
logger.LogError(ex, $"An error occurred seeding the " +
"database with test messages. Error: {ex.Message}");
}
}
});
return base.CreateHost(builder);
}
}

View File

@@ -10,11 +10,11 @@ using Xunit;
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers;
[Collection("Sequential")]
public class AuthenticateEndpoint : IClassFixture<ApiTestFixture>
public class AuthenticateEndpoint : IClassFixture<TestApiApplication>
{
JsonSerializerOptions _jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
public AuthenticateEndpoint(ApiTestFixture factory)
public AuthenticateEndpoint(TestApiApplication factory)
{
Client = factory.CreateClient();
}

View File

@@ -8,9 +8,9 @@ using Xunit;
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers;
[Collection("Sequential")]
public class ApiCatalogControllerList : IClassFixture<ApiTestFixture>
public class ApiCatalogControllerList : IClassFixture<TestApiApplication>
{
public ApiCatalogControllerList(ApiTestFixture factory)
public ApiCatalogControllerList(TestApiApplication factory)
{
Client = factory.CreateClient();
}

View File

@@ -12,7 +12,7 @@ using Xunit;
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers;
[Collection("Sequential")]
public class CreateEndpoint : IClassFixture<ApiTestFixture>
public class CreateEndpoint : IClassFixture<TestApiApplication>
{
JsonSerializerOptions _jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
private int _testBrandId = 1;
@@ -21,7 +21,7 @@ public class CreateEndpoint : IClassFixture<ApiTestFixture>
private string _testName = "test name";
private decimal _testPrice = 1.23m;
public CreateEndpoint(ApiTestFixture factory)
public CreateEndpoint(TestApiApplication factory)
{
Client = factory.CreateClient();
}

View File

@@ -11,11 +11,11 @@ using Xunit;
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers;
[Collection("Sequential")]
public class DeleteEndpoint : IClassFixture<ApiTestFixture>
public class DeleteEndpoint : IClassFixture<TestApiApplication>
{
JsonSerializerOptions _jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
public DeleteEndpoint(ApiTestFixture factory)
public DeleteEndpoint(TestApiApplication factory)
{
Client = factory.CreateClient();
}

View File

@@ -9,11 +9,11 @@ using Xunit;
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers;
[Collection("Sequential")]
public class GetByIdEndpoint : IClassFixture<ApiTestFixture>
public class GetByIdEndpoint : IClassFixture<TestApiApplication>
{
JsonSerializerOptions _jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
public GetByIdEndpoint(ApiTestFixture factory)
public GetByIdEndpoint(TestApiApplication factory)
{
Client = factory.CreateClient();
}

View File

@@ -11,9 +11,9 @@ using Xunit;
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers;
[Collection("Sequential")]
public class AccountControllerSignIn : IClassFixture<WebTestFixture>
public class AccountControllerSignIn : IClassFixture<TestApplication>
{
public AccountControllerSignIn(WebTestFixture factory)
public AccountControllerSignIn(TestApplication factory)
{
Client = factory.CreateClient(new WebApplicationFactoryClientOptions
{

View File

@@ -5,9 +5,9 @@ using Xunit;
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers;
[Collection("Sequential")]
public class CatalogControllerIndex : IClassFixture<WebTestFixture>
public class CatalogControllerIndex : IClassFixture<TestApplication>
{
public CatalogControllerIndex(WebTestFixture factory)
public CatalogControllerIndex(TestApplication factory)
{
Client = factory.CreateClient();
}

View File

@@ -7,9 +7,9 @@ using Xunit;
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers;
[Collection("Sequential")]
public class OrderIndexOnGet : IClassFixture<WebTestFixture>
public class OrderIndexOnGet : IClassFixture<TestApplication>
{
public OrderIndexOnGet(WebTestFixture factory)
public OrderIndexOnGet(TestApplication factory)
{
Client = factory.CreateClient(new WebApplicationFactoryClientOptions
{

View File

@@ -10,9 +10,9 @@ using Xunit;
namespace Microsoft.eShopWeb.FunctionalTests.WebRazorPages;
[Collection("Sequential")]
public class BasketPageCheckout : IClassFixture<WebTestFixture>
public class BasketPageCheckout : IClassFixture<TestApplication>
{
public BasketPageCheckout(WebTestFixture factory)
public BasketPageCheckout(TestApplication factory)
{
Client = factory.CreateClient(new WebApplicationFactoryClientOptions
{

View File

@@ -6,9 +6,9 @@ using Xunit;
namespace Microsoft.eShopWeb.FunctionalTests.WebRazorPages;
[Collection("Sequential")]
public class HomePageOnGet : IClassFixture<WebTestFixture>
public class HomePageOnGet : IClassFixture<TestApplication>
{
public HomePageOnGet(WebTestFixture factory)
public HomePageOnGet(TestApplication factory)
{
Client = factory.CreateClient();
}

View File

@@ -5,74 +5,42 @@ using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopWeb.Infrastructure.Data;
using Microsoft.eShopWeb.Infrastructure.Identity;
using Microsoft.eShopWeb.Web;
using Microsoft.eShopWeb.Web.Interfaces;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Microsoft.eShopWeb.FunctionalTests.Web;
public class WebTestFixture : WebApplicationFactory<Startup>
public class TestApplication : WebApplicationFactory<IBasketViewModelService>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseEnvironment("Testing");
private readonly string _environment = "Development";
protected override IHost CreateHost(IHostBuilder builder)
{
builder.UseEnvironment(_environment);
// Add mock/test services to the builder here
builder.ConfigureServices(services =>
{
services.AddEntityFrameworkInMemoryDatabase();
// Create a new service provider.
var provider = services
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
// Add a database context (ApplicationDbContext) using an in-memory
// database for testing.
services.AddDbContext<CatalogContext>(options =>
services.AddScoped(sp =>
{
options.UseInMemoryDatabase("InMemoryDbForTesting");
options.UseInternalServiceProvider(provider);
// Replace SQLite with in-memory database for tests
return new DbContextOptionsBuilder<CatalogContext>()
.UseInMemoryDatabase("InMemoryDbForTesting")
.UseApplicationServiceProvider(sp)
.Options;
});
services.AddDbContext<AppIdentityDbContext>(options =>
services.AddScoped(sp =>
{
options.UseInMemoryDatabase("Identity");
options.UseInternalServiceProvider(provider);
// Replace SQLite with in-memory database for tests
return new DbContextOptionsBuilder<AppIdentityDbContext>()
.UseInMemoryDatabase("Identity")
.UseApplicationServiceProvider(sp)
.Options;
});
// Build the service provider.
var sp = services.BuildServiceProvider();
// Create a scope to obtain a reference to the database
// context (ApplicationDbContext).
using (var scope = sp.CreateScope())
{
var scopedServices = scope.ServiceProvider;
var db = scopedServices.GetRequiredService<CatalogContext>();
var loggerFactory = scopedServices.GetRequiredService<ILoggerFactory>();
var logger = scopedServices
.GetRequiredService<ILogger<WebTestFixture>>();
// Ensure the database is created.
db.Database.EnsureCreated();
try
{
// Seed the database with test data.
CatalogContextSeed.SeedAsync(db, loggerFactory).Wait();
// seed sample user data
var userManager = scopedServices.GetRequiredService<UserManager<ApplicationUser>>();
var roleManager = scopedServices.GetRequiredService<RoleManager<IdentityRole>>();
AppIdentityDbContextSeed.SeedAsync(userManager, roleManager).Wait();
}
catch (Exception ex)
{
logger.LogError(ex, $"An error occurred seeding the " +
"database with test messages. Error: {ex.Message}");
}
}
});
return base.CreateHost(builder);
}
}