Files
yavsc/test/yavscTests/WebServerFixture.cs

159 lines
5.7 KiB
C#
Raw Normal View History

2025-07-13 18:13:04 +01:00
using System.Net;
using Microsoft.AspNetCore;
2025-07-14 18:58:04 +01:00
using Microsoft.AspNetCore.Builder;
2025-07-13 18:13:04 +01:00
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;
2025-07-14 18:58:04 +01:00
using Yavsc.Extensions;
using Microsoft.EntityFrameworkCore;
2025-07-15 15:42:30 +01:00
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
using Yavsc.Models.Auth;
2025-07-13 18:13:04 +01:00
namespace isnd.tests
{
[CollectionDefinition("Web server collection")]
public class WebServerFixture : IDisposable
{
public List<string> Addresses { get; private set; } = new List<string>();
public Microsoft.Extensions.Logging.ILogger Logger { get; internal set; }
private SiteSettings siteSettings;
2025-07-14 18:58:04 +01:00
public IConfigurationRoot Configuration { get; private set; }
private WebApplication app;
public string TestClientId { get; private set; }
2025-07-13 18:13:04 +01:00
2025-07-15 15:42:30 +01:00
public IServiceProvider Services { get; private set; }
2025-07-13 18:13:04 +01:00
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";
2025-07-13 18:13:04 +01:00
public WebServerFixture()
{
2025-07-14 18:58:04 +01:00
SetupHost().Wait();
2025-07-13 18:13:04 +01:00
}
public void Dispose()
{
2025-07-14 18:58:04 +01:00
if (app!=null)
app.StopAsync().Wait();
2025-07-13 18:13:04 +01:00
}
2025-07-14 18:58:04 +01:00
public async Task SetupHost()
2025-07-13 18:13:04 +01:00
{
2025-07-14 18:58:04 +01:00
var builder = WebApplication.CreateBuilder();
2025-07-14 18:58:04 +01:00
Configuration = builder.Configuration
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables()
.Build();
this.app = builder.ConfigureWebAppServices();
2025-07-15 15:42:30 +01:00
Services = app.Services;
2025-07-14 18:58:04 +01:00
using (var migrationScope = app.Services.CreateScope())
2025-07-13 18:13:04 +01:00
{
2025-07-14 18:58:04 +01:00
var db = migrationScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
await db.Database.MigrateAsync();
}
await app.ConfigurePipeline();
app.UseSession();
await app.StartAsync();
2025-07-13 18:13:04 +01:00
2025-07-14 18:58:04 +01:00
var logFactory = app.Services.GetRequiredService<ILoggerFactory>();
2025-07-13 18:13:04 +01:00
Logger = logFactory.CreateLogger<WebServerFixture>();
2025-07-14 18:58:04 +01:00
var server = app.Services.GetRequiredService<IServer>();
2025-07-13 18:13:04 +01:00
var addressFeatures = server.Features.Get<IServerAddressesFeature>();
foreach (var address in addressFeatures.Addresses)
{
Addresses.Add(address);
}
2025-07-14 18:58:04 +01:00
SiteSettings = app.Services.GetRequiredService<IOptions<SiteSettings>>().Value;
2025-07-13 18:13:04 +01:00
2025-07-14 18:58:04 +01:00
using IServiceScope scope = app.Services.CreateScope();
2025-07-13 18:13:04 +01:00
ApplicationDbContext dbContext =
scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
2025-07-14 18:58:04 +01:00
//dbContext.Database.EnsureCreated();
dbContext.Database.Migrate();
2025-07-13 18:13:04 +01:00
TestingUserName = "Tester";
TestClientId = "testClientId";
TestingUser = await dbContext.Users.FirstOrDefaultAsync(u => u.UserName == TestingUserName);
2025-07-13 18:13:04 +01:00
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();
}
2025-07-16 00:47:50 +01:00
else
{
testClient.DisplayName = "Testing Client";
testClient.Secret = TestClientSecret;
testClient.Active = true;
testClient.Type = ApplicationTypes.NativeConfidential;
testClient.AccessTokenLifetime = 900;
testClient.RefreshTokenLifeTime = 1500;
}
2025-07-13 18:13:04 +01:00
}
public void EnsureUser(string testingUserName)
{
if (TestingUser == null)
{
2025-07-14 18:58:04 +01:00
using IServiceScope scope = app.Services.CreateScope();
2025-07-13 18:13:04 +01:00
var userManager =
scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
TestingUser = new ApplicationUser
{
2025-07-15 15:42:30 +01:00
UserName = testingUserName,
Email = testingUserName + "@example.com",
EmailConfirmed = true
2025-07-13 18:13:04 +01:00
};
2025-07-15 17:35:14 +01:00
var result = userManager.CreateAsync(TestingUser,"test").Result;
2025-07-13 18:13:04 +01:00
Assert.True(result.Succeeded);
ApplicationDbContext dbContext =
scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
TestingUser = dbContext.Users.FirstOrDefault(u => u.UserName == testingUserName);
}
}
}
}