mirror of
https://github.com/aliasvault/aliasvault.git
synced 2026-05-07 14:56:02 -04:00
Make basic SMTP service integration test work (#111)
This commit is contained in:
@@ -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>();
|
||||
|
||||
@@ -24,16 +24,16 @@ using Microsoft.Extensions.Hosting;
|
||||
public class WebApplicationApiFactoryFixture<TEntryPoint> : WebApplicationFactory<TEntryPoint>
|
||||
where TEntryPoint : class
|
||||
{
|
||||
/// <summary>
|
||||
/// The DbContext instance that is created for the test.
|
||||
/// </summary>
|
||||
private AliasServerDbContext? _dbContext;
|
||||
|
||||
/// <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>
|
||||
/// Gets or sets the URL the web application host will listen on.
|
||||
/// </summary>
|
||||
|
||||
@@ -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<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("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"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user