Skip to content

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

Update SmtpServer TestHostBuilder to be compatible with integration and E2E tests (#512)

Leendert de Borst202151e4f1f6
diff --git a/src/Tests/AliasVault.IntegrationTests/SmtpServer/TestHostBuilder.cs b/src/Tests/AliasVault.IntegrationTests/SmtpServer/TestHostBuilder.cs
index e53a0bb65..2f63a0671 100644
--- a/src/Tests/AliasVault.IntegrationTests/SmtpServer/TestHostBuilder.cs
+++ b/src/Tests/AliasVault.IntegrationTests/SmtpServer/TestHostBuilder.cs
@@ -7,11 +7,13 @@
namespace AliasVault.IntegrationTests.SmtpServer;
+using System.Data.Common;
using AliasVault.SmtpService;
using AliasVault.SmtpService.Handlers;
using AliasVault.SmtpService.Workers;
using global::SmtpServer;
using global::SmtpServer.Storage;
+using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
@@ -23,8 +25,9 @@ public class TestHostBuilder : AbstractTestHostBuilder
/// <summary>
/// Builds the SmtpService test host with a provided database connection.
/// </summary>
+ /// <param name="dbConnection">The database connection to use for the test.</param>
/// <returns>IHost.</returns>
- public IHost Build()
+ public IHost Build(DbConnection dbConnection)
{
// Get base builder with database connection already configured.
var builder = CreateBuilder();
@@ -32,35 +35,75 @@ public class TestHostBuilder : AbstractTestHostBuilder
// Add specific services for the TestExceptionWorker.
builder.ConfigureServices((context, services) =>
{
- services.AddSingleton(new Config
- {
- AllowedToDomains = new List<string> { "example.tld" },
- SmtpTlsEnabled = "false",
- });
-
- services.AddTransient<IMessageStore, DatabaseMessageStore>();
- services.AddSingleton<SmtpServer>(
- provider =>
+ // Override database connection with provided connection.
+ services.Remove(services.First(x => x.ServiceType == typeof(IConfiguration)));
+ var configuration = new ConfigurationBuilder()
+ .AddJsonFile("appsettings.json", optional: true)
+ .AddInMemoryCollection(new Dictionary<string, string?>
{
- var options = new SmtpServerOptionsBuilder()
- .ServerName("aliasvault");
+ ["DatabaseProvider"] = "postgresql",
+ ["ConnectionStrings:AliasServerDbContext"] = dbConnection.ConnectionString,
+ })
+ .Build();
- // Note: port 25 doesn't work in GitHub actions so we use these instead for the integration tests:
- // - 2525 for the SMTP server
- // - 5870 for the submission server
- options.Endpoint(serverBuilder =>
- serverBuilder
- .Port(2525, false))
- .Endpoint(serverBuilder =>
- serverBuilder
- .Port(5870, false));
+ services.AddSingleton<IConfiguration>(configuration);
+
+ ConfigureSmtpServices(services);
+ });
+
+ return builder.Build();
+ }
- return new SmtpServer(options.Build(), provider.GetRequiredService<IServiceProvider>());
- });
+ /// <summary>
+ /// Builds the SmtpService test host with a new database connection.
+ /// </summary>
+ /// <returns>IHost.</returns>
+ public IHost Build()
+ {
+ // Get base builder with database connection already configured.
+ var builder = CreateBuilder();
- services.AddHostedService<SmtpServerWorker>();
+ // Add specific services for the TestExceptionWorker.
+ builder.ConfigureServices((context, services) =>
+ {
+ ConfigureSmtpServices(services);
});
return builder.Build();
}
+
+ /// <summary>
+ /// Configures the SMTP services for the test host.
+ /// </summary>
+ /// <param name="services">The service collection to configure.</param>
+ private void ConfigureSmtpServices(IServiceCollection services)
+ {
+ services.AddSingleton(new Config
+ {
+ AllowedToDomains = new List<string> { "example.tld" },
+ SmtpTlsEnabled = "false",
+ });
+
+ services.AddTransient<IMessageStore, DatabaseMessageStore>();
+ services.AddSingleton<SmtpServer>(
+ provider =>
+ {
+ var options = new SmtpServerOptionsBuilder()
+ .ServerName("aliasvault");
+
+ // Note: port 25 doesn't work in GitHub actions so we use these instead for the integration tests:
+ // - 2525 for the SMTP server
+ // - 5870 for the submission server
+ options.Endpoint(serverBuilder =>
+ serverBuilder
+ .Port(2525, false))
+ .Endpoint(serverBuilder =>
+ serverBuilder
+ .Port(5870, false));
+
+ return new SmtpServer(options.Build(), provider.GetRequiredService<IServiceProvider>());
+ });
+
+ services.AddHostedService<SmtpServerWorker>();
+ }
}