Skip to content

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

Make basic SMTP service integration test work (#111)

Leendert de Borst0e4d0b0f8474
diff --git a/src/Services/AliasVault.SmtpService/Program.cs b/src/Services/AliasVault.SmtpService/Program.cs
index 925f465f2..1198f726f 100644
--- a/src/Services/AliasVault.SmtpService/Program.cs
+++ b/src/Services/AliasVault.SmtpService/Program.cs
@@ -43,7 +43,7 @@ builder.Services.AddSingleton<DbConnection>(container =>
builder.Services.AddDbContextFactory<AliasServerDbContext>((container, options) =>
{
var connection = container.GetRequiredService<DbConnection>();
- options.UseSqlite(connection).UseLazyLoadingProxies();
+ options.UseSqlite(connection);
});
builder.Services.AddTransient<IMessageStore, DatabaseMessageStore>();
diff --git a/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationApiFactoryFixture.cs b/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationApiFactoryFixture.cs
index 0433173f1..9c4b4f297 100644
--- a/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationApiFactoryFixture.cs
+++ b/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationApiFactoryFixture.cs
@@ -25,14 +25,14 @@ public class WebApplicationApiFactoryFixture<TEntryPoint> : WebApplicationFactor
where TEntryPoint : class
{
/// <summary>
- /// The DbContext instance that is created for the test.
+ /// The DbConnection instance that is created for the test.
/// </summary>
- private AliasServerDbContext? _dbContext;
+ private DbConnection? _dbConnection;
/// <summary>
- /// The DbConnection instance that is created for the test.
+ /// The DbContext instance that is created for the test.
/// </summary>
- private DbConnection? _dbConnection;
+ private AliasServerDbContext? _dbContext;
/// <summary>
/// Gets or sets the URL the web application host will listen on.
diff --git a/src/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.cs b/src/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.cs
index 7ffb27837..1e343a709 100644
--- a/src/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.cs
+++ b/src/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.cs
@@ -21,13 +21,13 @@ public class SmtpServerTests
{
private IHost _testHost;
+ private TestHostBuilder _testHostBuilder;
+
[SetUp]
public async Task Setup()
{
- _testHost = new TestHostBuilder().Build(services =>
- {
- // Here you can override services or add mocks as needed
- });
+ _testHostBuilder = new TestHostBuilder();
+ _testHost = _testHostBuilder.Build();
await _testHost.StartAsync();
}
@@ -53,7 +53,7 @@ public class SmtpServerTests
message.From.Add(new MailboxAddress("Test Sender", "[email protected]"));
message.To.Add(new MailboxAddress("Test Recipient", "[email protected]"));
message.Cc.Add(new MailboxAddress("Test Recipient 2", "[email protected]"));
- message.Subject = "Test Email with multiple recipients.";
+ message.Subject = "Test Email";
message.Body = new TextPart("plain")
{
Text = "This is a test email."
@@ -78,22 +78,10 @@ public class SmtpServerTests
await client.DisconnectAsync(true);
}
- // Act
- // Wait for the worker to process the email
- await Task.Delay(1000); // Adjust as needed
-
- // Assert
- using (var scope = _testHost.Services.CreateScope())
- {
- var dbContextFactory = scope.ServiceProvider.GetRequiredService<IDbContextFactory<AliasServerDbContext>>();
- var dbContext = await dbContextFactory.CreateDbContextAsync();
-
- // Check the database for the expected results
- var processedEmail = await dbContext.Emails.FirstOrDefaultAsync(e => e.Subject == "Test Email");
- Assert.That(processedEmail, Is.Not.Null);
- Assert.That(processedEmail.To, Is.EqualTo("[email protected]"));
- // Add more assertions as needed
- }
+ var dbContext = _testHostBuilder.GetDbContext();
+ var processedEmail = await dbContext.Emails.FirstOrDefaultAsync(e => e.Subject == "Test Email");
+ Assert.That(processedEmail, Is.Not.Null);
+ Assert.That(processedEmail.To, Is.EqualTo("[email protected]"));
}
}
diff --git a/src/Tests/AliasVault.IntegrationTests/SmtpServer/TestHostBuilder.cs b/src/Tests/AliasVault.IntegrationTests/SmtpServer/TestHostBuilder.cs
index 49687f57b..e5a97c353 100644
--- a/src/Tests/AliasVault.IntegrationTests/SmtpServer/TestHostBuilder.cs
+++ b/src/Tests/AliasVault.IntegrationTests/SmtpServer/TestHostBuilder.cs
@@ -19,29 +19,59 @@ using global::SmtpServer.Storage;
public class TestHostBuilder
{
- public IHost Build(Action<IServiceCollection> configureServices = null)
+ /// <summary>
+ /// The DbConnection instance that is created for the test.
+ /// </summary>
+ private DbConnection? _dbConnection;
+
+ /// <summary>
+ /// The DbContext instance that is created for the test.
+ /// </summary>
+ private AliasServerDbContext? _dbContext;
+
+ /// <summary>
+ /// Returns the DbContext instance for the test. This can be used to seed the database with test data.
+ /// </summary>
+ /// <returns>AliasServerDbContext instance.</returns>
+ public AliasServerDbContext GetDbContext()
{
+ if (_dbContext == null)
+ {
+ var options = new DbContextOptionsBuilder<AliasServerDbContext>()
+ .UseSqlite(_dbConnection!)
+ .Options;
+
+ _dbContext = new AliasServerDbContext(options);
+ }
+
+ return _dbContext;
+ }
+
+ /// <summary>
+ /// Builds the SmtpService test host.
+ /// </summary>
+ /// <returns></returns>
+ public IHost Build()
+ {
+ // Create a persistent in-memory database for the duration of the test.
+ _dbConnection = new SqliteConnection("DataSource=:memory:");
+ _dbConnection.Open();
+
var builder = Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
- // Add your services here, similar to your Program.cs
services.AddSingleton(new Config
{
AllowedToDomains = new List<string> { "example.tld" },
SmtpTlsEnabled = "false"
});
- services.AddSingleton<DbConnection>(sp =>
- {
- var connection = new SqliteConnection("DataSource=:memory:");
- connection.Open();
- return connection;
- });
+ services.AddSingleton(_dbConnection);
services.AddDbContextFactory<AliasServerDbContext>((sp, options) =>
{
var connection = sp.GetRequiredService<DbConnection>();
- options.UseSqlite(connection).UseLazyLoadingProxies();
+ options.UseSqlite(connection);
});
services.AddTransient<IMessageStore, DatabaseMessageStore>();
@@ -76,9 +106,6 @@ public class TestHostBuilder
var dbContext = dbContextFactory.CreateDbContext();
dbContext.Database.Migrate();
}
-
- // Allow additional service configuration from the test
- configureServices.Invoke(services);
});
return builder.Build();