Files
isn/test/isnd.tests/WebServerFixture.cs
2025-07-07 15:26:19 +01:00

137 lines
4.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using isn;
using isnd.Data;
using isnd.Entities;
using isnd.Interfaces;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Identity;
using Microsoft.CodeAnalysis.Options;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Xunit;
namespace isnd.tests
{
[CollectionDefinition("Web server collection")]
public class WebServerFixture : IDisposable
{
public IWebHost Host { get; private set;}
public List<string> Addresses { get; private set; } = new List<string>();
public Microsoft.Extensions.Logging.ILogger Logger { get; internal set; }
private IsndSettings siteSettings;
public IDataProtector DataProtector { get; private set; }
public string TestingUserName { get; private set; }
public string ProtectedTestingApiKey { get; internal set; }
public ApplicationUser TestingUser { get; private set; }
public WebServerFixture()
{
SetupHost();
}
public void Dispose()
{
Host.StopAsync().Wait();
Host.Dispose();
}
public void SetupHost()
{
var builder = WebHost.CreateDefaultBuilder(new string[0]);
// .UseContentRoot("../../../../../src/isnd")
builder.UseStartup(typeof(Startup))
.ConfigureAppConfiguration((builderContext, config) =>
{
config.AddJsonFile("appsettings.json", true);
config.AddJsonFile("appsettings.Development.json", false);
});
Host = builder.Build();
var logFactory = Host.Services.GetRequiredService<ILoggerFactory>();
Logger = logFactory.CreateLogger<WebServerFixture>();
Host.Start(); //Starts listening on the configured addresses.
var server = Host.Services.GetRequiredService<IServer>();
var addressFeatures = server.Features.Get<IServerAddressesFeature>();
foreach (var address in addressFeatures.Addresses)
{
Addresses.Add(address);
}
siteSettings = Host.Services.GetRequiredService<IOptions<IsndSettings>>().Value;
DataProtector = Host.Services.GetRequiredService<IDataProtectionProvider>()
.CreateProtector(siteSettings.ProtectionTitle);
using IServiceScope scope = Host.Services.CreateScope();
ApplicationDbContext dbContext =
scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
TestingUserName = "Tester";
TestingUser = dbContext.Users.FirstOrDefault(u => u.UserName == TestingUserName);
EnsureUser(TestingUserName);
var testKey = dbContext.ApiKey.FirstOrDefault(k => k.UserId == TestingUser.Id);
if (testKey == null)
{
var keyProvider = scope.ServiceProvider.GetService<IApiKeyProvider>();
var apiKeyQuery = new Data.ApiKeys.CreateModel
{
Name = "Testing Key",
UserId = TestingUser.Id,
ValidityPeriodInDays = 1
};
testKey = keyProvider.CreateApiKeyAsync(apiKeyQuery).Result;
}
ProtectedTestingApiKey = DataProtector.Protect(testKey.Id);
ServicePointManager.ServerCertificateValidationCallback =
(sender, cert, chain, sslPolicyErrors) => true;
}
public void EnsureUser(string testingUserName)
{
if (TestingUser == null)
{
using IServiceScope scope = Host.Services.CreateScope();
var userManager =
scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
TestingUser = new ApplicationUser
{
UserName = testingUserName
};
var result = userManager.CreateAsync(TestingUser).Result;
Assert.True(result.Succeeded);
ApplicationDbContext dbContext =
scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
TestingUser = dbContext.Users.FirstOrDefault(u => u.UserName == testingUserName);
}
}
}
}