Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add IntegrationTests project (#111)
Leendert de Borst533362210b88
diff --git a/aliasvault.sln b/aliasvault.slnindex 067939259..1f437f45c 100644--- a/aliasvault.sln+++ b/aliasvault.sln@@ -39,6 +39,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Services", "Services", "{8AEndProjectProject("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AliasVault.SmtpService", "src\Services\AliasVault.SmtpService\AliasVault.SmtpService.csproj", "{B095A174-E528-4D38-BEC1-D1D38B3B30C0}"EndProject+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AliasVault.IntegrationTests", "src\Tests\AliasVault.IntegrationTests\AliasVault.IntegrationTests.csproj", "{1C7C8DE9-5F2A-43DB-A25E-33319E80A509}"+EndProjectGlobalGlobalSection(SolutionConfigurationPlatforms) = preSolutionDebug|Any CPU = Debug|Any CPU@@ -97,6 +99,10 @@ Global{B095A174-E528-4D38-BEC1-D1D38B3B30C0}.Debug|Any CPU.Build.0 = Debug|Any CPU{B095A174-E528-4D38-BEC1-D1D38B3B30C0}.Release|Any CPU.ActiveCfg = Release|Any CPU{B095A174-E528-4D38-BEC1-D1D38B3B30C0}.Release|Any CPU.Build.0 = Release|Any CPU+ {1C7C8DE9-5F2A-43DB-A25E-33319E80A509}.Debug|Any CPU.ActiveCfg = Debug|Any CPU+ {1C7C8DE9-5F2A-43DB-A25E-33319E80A509}.Debug|Any CPU.Build.0 = Debug|Any CPU+ {1C7C8DE9-5F2A-43DB-A25E-33319E80A509}.Release|Any CPU.ActiveCfg = Release|Any CPU+ {1C7C8DE9-5F2A-43DB-A25E-33319E80A509}.Release|Any CPU.Build.0 = Release|Any CPUEndGlobalSectionGlobalSection(SolutionProperties) = preSolutionHideSolutionNode = FALSE@@ -112,6 +118,7 @@ Global{607945F3-9896-4544-99EC-F3496CF4D36B} = {29DE523D-EEF2-41E9-AC12-F20D8D02BEBB}{A9C9A606-C87E-4298-AB32-09B1884D7487} = {01AB9389-2F89-4F8E-A688-BF4BF1FC42C8}{B095A174-E528-4D38-BEC1-D1D38B3B30C0} = {8A477241-B96C-4174-968D-D40CB77F1ECD}+ {1C7C8DE9-5F2A-43DB-A25E-33319E80A509} = {29DE523D-EEF2-41E9-AC12-F20D8D02BEBB}EndGlobalSectionGlobalSection(ExtensibilityGlobals) = postSolutionSolutionGuid = {FEE82475-C009-4762-8113-A6563D9DC49E}diff --git a/src/Tests/AliasVault.IntegrationTests/AliasVault.IntegrationTests.csproj b/src/Tests/AliasVault.IntegrationTests/AliasVault.IntegrationTests.csprojnew file mode 100644index 000000000..3c5af2523--- /dev/null+++ b/src/Tests/AliasVault.IntegrationTests/AliasVault.IntegrationTests.csproj@@ -0,0 +1,35 @@+<Project Sdk="Microsoft.NET.Sdk">++ <PropertyGroup>+ <TargetFramework>net8.0</TargetFramework>+ <ImplicitUsings>enable</ImplicitUsings>+ <Nullable>enable</Nullable>++ <IsPackable>false</IsPackable>+ <IsTestProject>true</IsTestProject>+ </PropertyGroup>++ <ItemGroup>+ <PackageReference Include="coverlet.collector" Version="6.0.0"/>+ <PackageReference Include="MailKit" Version="4.7.1.1" />+ <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>+ <PackageReference Include="NUnit" Version="3.14.0"/>+ <PackageReference Include="NUnit.Analyzers" Version="3.9.0"/>+ <PackageReference Include="NUnit3TestAdapter" Version="4.5.0"/>+ </ItemGroup>++ <ItemGroup>+ <Using Include="NUnit.Framework"/>+ </ItemGroup>++ <ItemGroup>+ <AdditionalFiles Include="..\..\stylecop.json">+ <Link>stylecop.json</Link>+ </AdditionalFiles>+ </ItemGroup>++ <ItemGroup>+ <ProjectReference Include="..\..\Services\AliasVault.SmtpService\AliasVault.SmtpService.csproj" />+ </ItemGroup>++</Project>diff --git a/src/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.cs b/src/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.csnew file mode 100644index 000000000..7ffb27837--- /dev/null+++ b/src/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.cs@@ -0,0 +1,99 @@+//-----------------------------------------------------------------------+// <copyright file="SmtpServerTests.cs" company="lanedirt">+// Copyright (c) lanedirt. All rights reserved.+// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.+// </copyright>+//-----------------------------------------------------------------------++namespace AliasVault.IntegrationTests.SmtpServer;++using AliasServerDb;++using MailKit.Security;+using MailKit.Net.Smtp;+using Microsoft.EntityFrameworkCore;+using Microsoft.Extensions.DependencyInjection;+using Microsoft.Extensions.Hosting;+using MimeKit;++[TestFixture]+public class SmtpServerTests+{+ private IHost _testHost;++ [SetUp]+ public async Task Setup()+ {+ _testHost = new TestHostBuilder().Build(services =>+ {+ // Here you can override services or add mocks as needed+ });++ await _testHost.StartAsync();+ }++ [TearDown]+ public async Task TearDown()+ {+ if (_testHost != null)+ {+ await _testHost.StopAsync();+ _testHost.Dispose();+ }+ }++ [Test]+ public async Task TestWorkerProcessesEmails()+ {+ // Arrange+ // Simulate sending an email to your SMTP server+ // You might need to implement a method to do this in your test SMTP server+ // Arrange+ var message = new MimeMessage();+ 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.Body = new TextPart("plain")+ {+ Text = "This is a test email."+ };++ using var client = new SmtpClient();++ // Send message to SMTP server+ await client.ConnectAsync("localhost", 25, SecureSocketOptions.None);+ try+ {+ await client.SendAsync(message);+ }+ catch (Exception ex)+ {+ throw;+ // Show failure message indicating that message was not sent: SMTP server issue?+ Assert.Fail($"Failed to send email, check SMTP server receive logs: {ex}");+ }+ finally+ {+ 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+ }++ }+}diff --git a/src/Tests/AliasVault.IntegrationTests/SmtpServer/TestHostBuilder.cs b/src/Tests/AliasVault.IntegrationTests/SmtpServer/TestHostBuilder.csnew file mode 100644index 000000000..49687f57b--- /dev/null+++ b/src/Tests/AliasVault.IntegrationTests/SmtpServer/TestHostBuilder.cs@@ -0,0 +1,86 @@+// -----------------------------------------------------------------------+// <copyright file="TestHostBuilder.cs" company="lanedirt">+// Copyright (c) lanedirt. All rights reserved.+// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.+// </copyright>+// -----------------------------------------------------------------------++namespace AliasVault.IntegrationTests.SmtpServer;++using System.Data.Common;+using AliasServerDb;+using AliasVault.SmtpService;+using Microsoft.Extensions.Hosting;+using Microsoft.Extensions.DependencyInjection;+using Microsoft.Data.Sqlite;+using Microsoft.EntityFrameworkCore;+using global::SmtpServer;+using global::SmtpServer.Storage;++public class TestHostBuilder+{+ public IHost Build(Action<IServiceCollection> configureServices = null)+ {+ 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.AddDbContextFactory<AliasServerDbContext>((sp, options) =>+ {+ var connection = sp.GetRequiredService<DbConnection>();+ options.UseSqlite(connection).UseLazyLoadingProxies();+ });++ services.AddTransient<IMessageStore, DatabaseMessageStore>();+ services.AddTransient<IMailboxFilter, AllowedDomainsFilter>();++ services.AddSingleton<global::SmtpServer.SmtpServer>(+ provider =>+ {+ var options = new SmtpServerOptionsBuilder()+ .ServerName("aliasvault");++ // No TLS+ options.Endpoint(serverBuilder =>+ serverBuilder+ .Port(25, false))+ .Endpoint(serverBuilder =>+ serverBuilder+ .Port(587, false)+ );++ return new SmtpServer(options.Build(), provider.GetRequiredService<IServiceProvider>());+ }+ );++ services.AddHostedService<Worker>();++ // Ensure the in-memory database is populated with tables+ var serviceProvider = services.BuildServiceProvider();+ using (var scope = serviceProvider.CreateScope())+ {+ var dbContextFactory = scope.ServiceProvider.GetRequiredService<IDbContextFactory<AliasServerDbContext>>();+ var dbContext = dbContextFactory.CreateDbContext();+ dbContext.Database.Migrate();+ }++ // Allow additional service configuration from the test+ configureServices.Invoke(services);+ });++ return builder.Build();+ }+}