Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Update test infrastructure to use built-in Kestrel test methods (#1353)

Leendert de Borst8e35b2f89539
diff --git a/apps/server/Tests/AliasVault.E2ETests/Common/AdminPlaywrightTest.cs b/apps/server/Tests/AliasVault.E2ETests/Common/AdminPlaywrightTest.cs
index 73d6835e6..3fe346b2c 100644
--- a/apps/server/Tests/AliasVault.E2ETests/Common/AdminPlaywrightTest.cs
+++ b/apps/server/Tests/AliasVault.E2ETests/Common/AdminPlaywrightTest.cs
@@ -62,6 +62,7 @@ public abstract class AdminPlaywrightTest : PlaywrightTest
// Start Admin project in-memory.
_webAppFactory.Port = appPort;
+ _webAppFactory.InitializeKestrel();
_webAppFactory.CreateDefaultClient();
await SetupPlaywrightBrowserAndContext();
diff --git a/apps/server/Tests/AliasVault.E2ETests/Common/ClientPlaywrightTest.cs b/apps/server/Tests/AliasVault.E2ETests/Common/ClientPlaywrightTest.cs
index a2cceecba..f305ec22c 100644
--- a/apps/server/Tests/AliasVault.E2ETests/Common/ClientPlaywrightTest.cs
+++ b/apps/server/Tests/AliasVault.E2ETests/Common/ClientPlaywrightTest.cs
@@ -101,10 +101,12 @@ public abstract class ClientPlaywrightTest : PlaywrightTest
// Start WebAPI in-memory.
_apiFactory.Port = apiPort;
+ _apiFactory.InitializeKestrel();
_apiFactory.CreateDefaultClient();
// Start Blazor WASM in-memory.
_clientFactory.Port = appPort;
+ _clientFactory.InitializeKestrel();
_clientFactory.CreateDefaultClient();
await SetupPlaywrightBrowserAndContext();
diff --git a/apps/server/Tests/AliasVault.E2ETests/Infrastructure/Abstracts/WebApplicationFactoryFixture.cs b/apps/server/Tests/AliasVault.E2ETests/Infrastructure/Abstracts/WebApplicationFactoryFixture.cs
index f1b2d7a34..1ad6cf992 100644
--- a/apps/server/Tests/AliasVault.E2ETests/Infrastructure/Abstracts/WebApplicationFactoryFixture.cs
+++ b/apps/server/Tests/AliasVault.E2ETests/Infrastructure/Abstracts/WebApplicationFactoryFixture.cs
@@ -11,7 +11,6 @@ using AliasServerDb;
using AliasVault.Shared.Providers.Time;
using AliasVault.Shared.Server.Services;
using Microsoft.AspNetCore.Hosting;
-using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
@@ -44,6 +43,11 @@ public abstract class WebApplicationFactoryFixture<TEntryPoint> : WebApplication
/// </summary>
private string? _tempDbName;
+ /// <summary>
+ /// Whether UseKestrel has been called.
+ /// </summary>
+ private bool _kestrelConfigured;
+
/// <summary>
/// Gets or sets the port the web application kestrel host will listen on.
/// </summary>
@@ -120,15 +124,22 @@ public abstract class WebApplicationFactoryFixture<TEntryPoint> : WebApplication
await base.DisposeAsync();
}
- /// <inheritdoc />
- protected override IHost CreateHost(IHostBuilder builder)
+ /// <summary>
+ /// Initializes the factory with Kestrel on the specified port.
+ /// Must be called before CreateDefaultClient() in tests.
+ /// </summary>
+ public void InitializeKestrel()
{
- builder.ConfigureWebHost(webHostBuilder =>
+ if (!_kestrelConfigured)
{
- webHostBuilder.UseKestrel(opt => opt.ListenLocalhost(Port));
- webHostBuilder.ConfigureServices(s => s.AddSingleton<IServer, KestrelTestServer>());
- });
+ UseKestrel(Port);
+ _kestrelConfigured = true;
+ }
+ }
+ /// <inheritdoc />
+ protected override IHost CreateHost(IHostBuilder builder)
+ {
var host = base.CreateHost(builder);
// Get the DbContextFactory instance and store it for later use during tests.
diff --git a/apps/server/Tests/AliasVault.E2ETests/Infrastructure/KestrelTestServer.cs b/apps/server/Tests/AliasVault.E2ETests/Infrastructure/KestrelTestServer.cs
deleted file mode 100644
index 90dbc3ed7..000000000
--- a/apps/server/Tests/AliasVault.E2ETests/Infrastructure/KestrelTestServer.cs
+++ /dev/null
@@ -1,66 +0,0 @@
-//-----------------------------------------------------------------------
-// <copyright file="KestrelTestServer.cs" company="aliasvault">
-// Copyright (c) aliasvault. All rights reserved.
-// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
-// </copyright>
-//-----------------------------------------------------------------------
-
-namespace AliasVault.E2ETests.Infrastructure;
-
-using Microsoft.AspNetCore.Connections;
-using Microsoft.AspNetCore.Hosting.Server;
-using Microsoft.AspNetCore.Server.Kestrel.Core;
-using Microsoft.AspNetCore.TestHost;
-using Microsoft.Extensions.DependencyInjection;
-using Microsoft.Extensions.Logging;
-using Microsoft.Extensions.Options;
-
-/// <summary>
-/// A <see cref="TestServer"/> that uses Kestrel as the server.
-/// </summary>
-public class KestrelTestServer : TestServer, IServer
-{
- private readonly KestrelServer _server;
-
- /// <summary>
- /// Initializes a new instance of the <see cref="KestrelTestServer"/> class.
- /// </summary>
- /// <param name="serviceProvider">The <see cref="IServiceProvider"/> to use.</param>
- public KestrelTestServer(IServiceProvider serviceProvider)
- : base(serviceProvider)
- {
- // We get all the transport factories registered, and the first one is the correct one
- // Getting the IConnectionListenerFactory directly from the service provider does not work
- var transportFactory = serviceProvider.GetRequiredService<IEnumerable<IConnectionListenerFactory>>().First();
-
- var kestrelOptions = serviceProvider.GetRequiredService<IOptions<KestrelServerOptions>>();
- var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
- _server = new KestrelServer(kestrelOptions, transportFactory, loggerFactory);
- }
-
- /// <inheritdoc />
- async Task IServer.StartAsync<TContext>(IHttpApplication<TContext> application, CancellationToken cancellationToken)
- {
- // We need to also invoke the TestServer's StartAsync method to ensure that the test server is started
- // Because the TestServer's StartAsync method is implemented explicitly, we need to use reflection to invoke it
- await InvokeExplicitInterfaceMethod(nameof(IServer.StartAsync), typeof(TContext), [application, cancellationToken]);
-
- // We also start the Kestrel server in order for localhost to work
- await _server.StartAsync(application, cancellationToken);
- }
-
- /// <inheritdoc />
- async Task IServer.StopAsync(CancellationToken cancellationToken)
- {
- await InvokeExplicitInterfaceMethod(nameof(IServer.StopAsync), null, [cancellationToken]);
- await _server.StopAsync(cancellationToken);
- }
-
- private Task InvokeExplicitInterfaceMethod(string methodName, Type? genericParameter, object[] args)
- {
- var baseMethod = typeof(TestServer).GetInterfaceMap(typeof(IServer)).TargetMethods.First(m => m.Name.EndsWith(methodName));
- var method = genericParameter == null ? baseMethod : baseMethod.MakeGenericMethod(genericParameter);
- var task = method.Invoke(this, args) as Task ?? throw new InvalidOperationException("Task not returned");
- return task;
- }
-}
diff --git a/apps/server/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationClientFactoryFixture.cs b/apps/server/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationClientFactoryFixture.cs
index 93970e5a0..3cf56df49 100644
--- a/apps/server/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationClientFactoryFixture.cs
+++ b/apps/server/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationClientFactoryFixture.cs
@@ -7,11 +7,7 @@
namespace AliasVault.E2ETests.Infrastructure;
-using Microsoft.AspNetCore.Hosting;
-using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Mvc.Testing;
-using Microsoft.Extensions.DependencyInjection;
-using Microsoft.Extensions.Hosting;
/// <summary>
/// Client web application factory fixture for integration tests.
@@ -20,20 +16,26 @@ using Microsoft.Extensions.Hosting;
public class WebApplicationClientFactoryFixture<TEntryPoint> : WebApplicationFactory<TEntryPoint>
where TEntryPoint : class
{
+ /// <summary>
+ /// Whether UseKestrel has been called.
+ /// </summary>
+ private bool _kestrelConfigured;
+
/// <summary>
/// Gets or sets the port the web application kestrel host will listen on.
/// </summary>
public int Port { get; set; } = 5002;
- /// <inheritdoc />
- protected override IHost CreateHost(IHostBuilder builder)
+ /// <summary>
+ /// Initializes the factory with Kestrel on the specified port.
+ /// Must be called before CreateDefaultClient() in tests.
+ /// </summary>
+ public void InitializeKestrel()
{
- builder.ConfigureWebHost(webHostBuilder =>
+ if (!_kestrelConfigured)
{
- webHostBuilder.UseKestrel(opt => opt.ListenLocalhost(Port));
- webHostBuilder.ConfigureServices(s => s.AddSingleton<IServer, KestrelTestServer>());
- });
-
- return base.CreateHost(builder);
+ UseKestrel(Port);
+ _kestrelConfigured = true;
+ }
}
}
diff --git a/apps/server/Tests/AliasVault.E2ETests/Tests/Client/Shard1/ApiTests.cs b/apps/server/Tests/AliasVault.E2ETests/Tests/Client/Shard1/ApiTests.cs
index b07779210..bf8416014 100644
--- a/apps/server/Tests/AliasVault.E2ETests/Tests/Client/Shard1/ApiTests.cs
+++ b/apps/server/Tests/AliasVault.E2ETests/Tests/Client/Shard1/ApiTests.cs
@@ -66,7 +66,9 @@ public class ApiTests : ClientPlaywrightTest
{
// Check if the IP address is not anonymized as we enabled IP logging for this test file, see OneTimeSetUp().
var authLogEntry = await ApiDbContext.AuthLogs.FirstAsync(x => x.Username == TestUserUsername && x.EventType == AuthEventType.Register);
- Assert.That(authLogEntry.IpAddress, Is.EqualTo("::1"), "IP address is anonymized while IP logging should be enabled. Check test configuration.");
+
+ bool isValidLocalhostIp = authLogEntry.IpAddress == "::1" || authLogEntry.IpAddress == "127.0.0.1";
+ Assert.That(isValidLocalhostIp, Is.True, $"IP address '{authLogEntry.IpAddress}' is anonymized while IP logging should be enabled. Check test configuration.");
}
/// <summary>