using System.Net; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting.Server; using Microsoft.AspNetCore.Hosting.Server.Features; using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Xunit; using Yavsc; using Yavsc.Models; using Yavsc.Services; using yavscTests.Settings; using Yavsc.Extensions; using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Mvc.ModelBinding.Binders; using Yavsc.Models.Auth; namespace isnd.tests { [CollectionDefinition("Web server collection")] public class WebServerFixture : IDisposable { public List Addresses { get; private set; } = new List(); public Microsoft.Extensions.Logging.ILogger Logger { get; internal set; } private SiteSettings siteSettings; public IConfigurationRoot Configuration { get; private set; } private WebApplication app; public string TestClientId { get; private set; } public IServiceProvider Services { get; private set; } public string TestingUserName { get; private set; } public string ProtectedTestingApiKey { get; internal set; } public ApplicationUser TestingUser { get; private set; } public bool DbCreated { get; internal set; } public SiteSettings SiteSettings { get => siteSettings; set => siteSettings = value; } public TestingSetup? TestingSetup { get; internal set; } public string TestClientSecret { get; private set; } = "TestClientSecret"; public WebServerFixture() { SetupHost().Wait(); } public void Dispose() { if (app!=null) app.StopAsync().Wait(); } public async Task SetupHost() { var builder = WebApplication.CreateBuilder(); Configuration = builder.Configuration .AddJsonFile("appsettings.json") .AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true) .AddEnvironmentVariables() .Build(); this.app = builder.ConfigureWebAppServices(); Services = app.Services; using (var migrationScope = app.Services.CreateScope()) { var db = migrationScope.ServiceProvider.GetRequiredService(); await db.Database.MigrateAsync(); } await app.ConfigurePipeline(); app.UseSession(); await app.StartAsync(); var logFactory = app.Services.GetRequiredService(); Logger = logFactory.CreateLogger(); var server = app.Services.GetRequiredService(); var addressFeatures = server.Features.Get(); foreach (var address in addressFeatures.Addresses) { Addresses.Add(address); } SiteSettings = app.Services.GetRequiredService>().Value; using IServiceScope scope = app.Services.CreateScope(); ApplicationDbContext dbContext = scope.ServiceProvider.GetRequiredService(); //dbContext.Database.EnsureCreated(); dbContext.Database.Migrate(); TestingUserName = "Tester"; TestClientId = "testClientId"; TestingUser = await dbContext.Users.FirstOrDefaultAsync(u => u.UserName == TestingUserName); EnsureUser(TestingUserName); // ensure a client var testClient = await dbContext.Client.FirstOrDefaultAsync((c) => c.Id == TestClientId); if (testClient == null) { testClient = new Yavsc.Models.Auth.Client { Id = TestClientId, DisplayName = "Testing Client", Secret = TestClientSecret, Active = true, Type = ApplicationTypes.NativeConfidential, AccessTokenLifetime = 900, RefreshTokenLifeTime = 15000 }; dbContext.Client.Add(testClient); dbContext.SaveChanges(); } else { testClient.DisplayName = "Testing Client"; testClient.Secret = TestClientSecret; testClient.Active = true; testClient.Type = ApplicationTypes.NativeConfidential; testClient.AccessTokenLifetime = 900; testClient.RefreshTokenLifeTime = 1500; } } public void EnsureUser(string testingUserName) { if (TestingUser == null) { using IServiceScope scope = app.Services.CreateScope(); var userManager = scope.ServiceProvider.GetRequiredService>(); TestingUser = new ApplicationUser { UserName = testingUserName, Email = testingUserName + "@example.com", EmailConfirmed = true }; var result = userManager.CreateAsync(TestingUser,"test").Result; Assert.True(result.Succeeded); ApplicationDbContext dbContext = scope.ServiceProvider.GetRequiredService(); TestingUser = dbContext.Users.FirstOrDefault(u => u.UserName == testingUserName); } } } }