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(container => builder.Services.AddDbContextFactory((container, options) => { var connection = container.GetRequiredService(); - options.UseSqlite(connection).UseLazyLoadingProxies(); + options.UseSqlite(connection); }); builder.Services.AddTransient(); 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 @@ -24,16 +24,16 @@ using Microsoft.Extensions.Hosting; public class WebApplicationApiFactoryFixture : WebApplicationFactory where TEntryPoint : class { - /// - /// The DbContext instance that is created for the test. - /// - private AliasServerDbContext? _dbContext; - /// /// The DbConnection instance that is created for the test. /// private DbConnection? _dbConnection; + /// + /// The DbContext instance that is created for the test. + /// + private AliasServerDbContext? _dbContext; + /// /// 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", "sender@example.com")); message.To.Add(new MailboxAddress("Test Recipient", "recipient.to@example.tld")); message.Cc.Add(new MailboxAddress("Test Recipient 2", "recipient.cc@example.tld")); - 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>(); - 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("test@test.com")); - // 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("recipient.to@example.tld")); } } 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 configureServices = null) + /// + /// The DbConnection instance that is created for the test. + /// + private DbConnection? _dbConnection; + + /// + /// The DbContext instance that is created for the test. + /// + private AliasServerDbContext? _dbContext; + + /// + /// Returns the DbContext instance for the test. This can be used to seed the database with test data. + /// + /// AliasServerDbContext instance. + public AliasServerDbContext GetDbContext() { + if (_dbContext == null) + { + var options = new DbContextOptionsBuilder() + .UseSqlite(_dbConnection!) + .Options; + + _dbContext = new AliasServerDbContext(options); + } + + return _dbContext; + } + + /// + /// Builds the SmtpService test host. + /// + /// + 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 { "example.tld" }, SmtpTlsEnabled = "false" }); - services.AddSingleton(sp => - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - return connection; - }); + services.AddSingleton(_dbConnection); services.AddDbContextFactory((sp, options) => { var connection = sp.GetRequiredService(); - options.UseSqlite(connection).UseLazyLoadingProxies(); + options.UseSqlite(connection); }); services.AddTransient(); @@ -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();