Skip to content

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

Add test helpers and move to separate namespace (#2144)

Leendert de Borst301aed70345e
diff --git a/apps/server/Tests/AliasVault.E2ETests/Tests/Client/Shard1/EmailDecryptionTests.cs b/apps/server/Tests/AliasVault.E2ETests/Tests/Client/Shard1/EmailDecryptionTests.cs
index faf5f0819..bc7a3ca37 100644
--- a/apps/server/Tests/AliasVault.E2ETests/Tests/Client/Shard1/EmailDecryptionTests.cs
+++ b/apps/server/Tests/AliasVault.E2ETests/Tests/Client/Shard1/EmailDecryptionTests.cs
@@ -8,7 +8,7 @@
namespace AliasVault.E2ETests.Tests.Client.Shard1;
using System.Text;
-using AliasVault.IntegrationTests.SmtpServer;
+using AliasVault.IntegrationTests.SmtpServer.Helpers;
using MailKit.Net.Smtp;
using MailKit.Security;
using Microsoft.EntityFrameworkCore;
diff --git a/apps/server/Tests/AliasVault.IntegrationTests/SmtpServer/TestHostBuilder.cs b/apps/server/Tests/AliasVault.IntegrationTests/SmtpServer/Helpers/TestHostBuilder.cs
similarity index 98%
rename from apps/server/Tests/AliasVault.IntegrationTests/SmtpServer/TestHostBuilder.cs
rename to apps/server/Tests/AliasVault.IntegrationTests/SmtpServer/Helpers/TestHostBuilder.cs
index e999fe674..6898a0890 100644
--- a/apps/server/Tests/AliasVault.IntegrationTests/SmtpServer/TestHostBuilder.cs
+++ b/apps/server/Tests/AliasVault.IntegrationTests/SmtpServer/Helpers/TestHostBuilder.cs
@@ -5,7 +5,7 @@
// </copyright>
// -----------------------------------------------------------------------
-namespace AliasVault.IntegrationTests.SmtpServer;
+namespace AliasVault.IntegrationTests.SmtpServer.Helpers;
using System.Data.Common;
using System.Net;
diff --git a/apps/server/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.cs b/apps/server/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.cs
index ca6d579e2..538f058de 100644
--- a/apps/server/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.cs
+++ b/apps/server/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.cs
@@ -11,6 +11,7 @@ using System.Net.Sockets;
using System.Text;
using AliasServerDb;
using AliasVault.Cryptography.Server;
+using AliasVault.IntegrationTests.SmtpServer.Helpers;
using MailKit.Net.Smtp;
using MailKit.Security;
using Microsoft.EntityFrameworkCore;
diff --git a/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/Helpers/ExceptionThrowingController.cs b/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/Helpers/ExceptionThrowingController.cs
new file mode 100644
index 000000000..2c2ec42ad
--- /dev/null
+++ b/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/Helpers/ExceptionThrowingController.cs
@@ -0,0 +1,39 @@
+//-----------------------------------------------------------------------
+// <copyright file="ExceptionThrowingController.cs" company="aliasvault">
+// Copyright (c) aliasvault. All rights reserved.
+// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace AliasVault.IntegrationTests.StatusHostedService.Helpers;
+
+/// <summary>
+/// Controls the throwing of exceptions inside the StatusWorker.
+/// </summary>
+public sealed class ExceptionThrowingController
+{
+ private readonly HashSet<int> _throwOnCalls;
+ private int _saveCount;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="ExceptionThrowingController"/> class.
+ /// </summary>
+ /// <param name="throwOnCalls">The one-based save-call indexes on which to throw.</param>
+ public ExceptionThrowingController(params int[] throwOnCalls)
+ {
+ _throwOnCalls = [.. throwOnCalls];
+ }
+
+ /// <summary>
+ /// Records a save attempt and throws a <see cref="TaskCanceledException"/> when the current attempt
+ /// index is one of the configured fault indexes.
+ /// </summary>
+ public void ThrowIfScheduled()
+ {
+ var current = Interlocked.Increment(ref _saveCount);
+ if (_throwOnCalls.Contains(current))
+ {
+ throw new TaskCanceledException("Simulated transient database cancellation.");
+ }
+ }
+}
diff --git a/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/Helpers/ExceptionThrowingWorkerStatusDbContext.cs b/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/Helpers/ExceptionThrowingWorkerStatusDbContext.cs
new file mode 100644
index 000000000..19f53cf27
--- /dev/null
+++ b/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/Helpers/ExceptionThrowingWorkerStatusDbContext.cs
@@ -0,0 +1,50 @@
+//-----------------------------------------------------------------------
+// <copyright file="ExceptionThrowingWorkerStatusDbContext.cs" company="aliasvault">
+// Copyright (c) aliasvault. All rights reserved.
+// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace AliasVault.IntegrationTests.StatusHostedService.Helpers;
+
+using AliasServerDb;
+using AliasVault.WorkerStatus.Database;
+using Microsoft.EntityFrameworkCore;
+
+/// <summary>
+/// An <see cref="IWorkerStatusDbContext"/> decorator that wraps a real context but throws a transient
+/// <see cref="TaskCanceledException"/> on selected save operations (driven by a shared
+/// <see cref="ExceptionThrowingController"/>). Used to verify the StatusWorker keeps running and does not
+/// persist a "Stopped" state when a database call is cancelled while the host is still alive.
+/// </summary>
+/// <param name="inner">The real wrapped context used for all queries and successful saves.</param>
+/// <param name="controller">The shared controller that decides when to inject a fault.</param>
+public sealed class ExceptionThrowingWorkerStatusDbContext(AliasServerDbContext inner, ExceptionThrowingController controller) : IWorkerStatusDbContext
+{
+ /// <inheritdoc/>
+ public DbSet<WorkerServiceStatus> WorkerServiceStatuses
+ {
+ get => inner.WorkerServiceStatuses;
+ set => inner.WorkerServiceStatuses = value;
+ }
+
+ /// <inheritdoc/>
+ public int SaveChanges()
+ {
+ controller.ThrowIfScheduled();
+ return inner.SaveChanges();
+ }
+
+ /// <inheritdoc/>
+ public async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
+ {
+ controller.ThrowIfScheduled();
+ return await inner.SaveChangesAsync(cancellationToken);
+ }
+
+ /// <inheritdoc/>
+ public void Dispose()
+ {
+ inner.Dispose();
+ }
+}
diff --git a/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/Helpers/StatusWorkerResilienceTestHostBuilder.cs b/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/Helpers/StatusWorkerResilienceTestHostBuilder.cs
new file mode 100644
index 000000000..780b46af3
--- /dev/null
+++ b/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/Helpers/StatusWorkerResilienceTestHostBuilder.cs
@@ -0,0 +1,57 @@
+// -----------------------------------------------------------------------
+// <copyright file="StatusWorkerResilienceTestHostBuilder.cs" company="aliasvault">
+// Copyright (c) aliasvault. All rights reserved.
+// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+// -----------------------------------------------------------------------
+
+namespace AliasVault.IntegrationTests.StatusHostedService.Helpers;
+
+using System.Reflection;
+using AliasServerDb;
+using AliasVault.WorkerStatus.Database;
+using AliasVault.WorkerStatus.ServiceExtensions;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Hosting;
+
+/// <summary>
+/// Builds a test host whose StatusWorker is backed by a database context that injects a transient
+/// <see cref="TaskCanceledException"/> on its first few save operations to test if the StatusWorker
+/// recovers itself instead of permanently soft-stopping the service.
+/// </summary>
+public class StatusWorkerResilienceTestHostBuilder : AbstractTestHostBuilder
+{
+ /// <summary>
+ /// Gets the controller that decides when the wrapped database context throws. It is configured to
+ /// fault the first couple of save operations performed inside the StatusWorker monitoring loop.
+ /// </summary>
+ public ExceptionThrowingController ExceptionThrowingController { get; } = new(2, 3);
+
+ /// <summary>
+ /// Builds the test host for the StatusWorker resilience test.
+ /// </summary>
+ /// <returns>IHost.</returns>
+ public IHost Build()
+ {
+ var builder = CreateBuilder();
+
+ builder.ConfigureServices((context, services) =>
+ {
+ services.AddSingleton(ExceptionThrowingController);
+
+ // Register the status DbContext factory override before AddStatusHostedService so the
+ // TryAddSingleton inside it keeps our faulting wrapper instead of the default one.
+ services.AddSingleton<Func<IWorkerStatusDbContext>>(sp =>
+ {
+ var factory = sp.GetRequiredService<IDbContextFactory<AliasServerDbContext>>();
+ var exceptionThrowingController = sp.GetRequiredService<ExceptionThrowingController>();
+ return () => new ExceptionThrowingWorkerStatusDbContext(factory.CreateDbContext(), exceptionThrowingController);
+ });
+
+ services.AddStatusHostedService<TestNoopWorker, AliasServerDbContext>(Assembly.GetExecutingAssembly().GetName().Name!);
+ });
+
+ return builder.Build();
+ }
+}
diff --git a/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/TestExceptionWorker.cs b/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/Helpers/TestExceptionWorker.cs
similarity index 92%
rename from apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/TestExceptionWorker.cs
rename to apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/Helpers/TestExceptionWorker.cs
index 945dc0732..3f377c55b 100644
--- a/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/TestExceptionWorker.cs
+++ b/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/Helpers/TestExceptionWorker.cs
@@ -5,7 +5,7 @@
// </copyright>
//-----------------------------------------------------------------------
-namespace AliasVault.IntegrationTests.StatusHostedService;
+namespace AliasVault.IntegrationTests.StatusHostedService.Helpers;
using Microsoft.Extensions.Hosting;
diff --git a/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/TestHostBuilder.cs b/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/Helpers/TestHostBuilder.cs
similarity index 96%
rename from apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/TestHostBuilder.cs
rename to apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/Helpers/TestHostBuilder.cs
index 013dbf7dc..86b9c2eef 100644
--- a/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/TestHostBuilder.cs
+++ b/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/Helpers/TestHostBuilder.cs
@@ -4,7 +4,7 @@
// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
// </copyright>
// -----------------------------------------------------------------------
-namespace AliasVault.IntegrationTests.StatusHostedService;
+namespace AliasVault.IntegrationTests.StatusHostedService.Helpers;
using System.Reflection;
using AliasServerDb;
diff --git a/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/Helpers/TestNoopWorker.cs b/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/Helpers/TestNoopWorker.cs
new file mode 100644
index 000000000..4b9aa782b
--- /dev/null
+++ b/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/Helpers/TestNoopWorker.cs
@@ -0,0 +1,30 @@
+//-----------------------------------------------------------------------
+// <copyright file="TestNoopWorker.cs" company="aliasvault">
+// Copyright (c) aliasvault. All rights reserved.
+// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace AliasVault.IntegrationTests.StatusHostedService.Helpers;
+
+using Microsoft.Extensions.Hosting;
+
+/// <summary>
+/// A worker that does nothing but stay running until it is cancelled. This is used to test that the
+/// StatusWorker reaches and maintains the "Started" state under healthy worker conditions.
+/// </summary>
+public sealed class TestNoopWorker : BackgroundService
+{
+ /// <inheritdoc/>
+ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
+ {
+ try
+ {
+ await Task.Delay(Timeout.Infinite, stoppingToken);
+ }
+ catch (OperationCanceledException)
+ {
+ // Expected when the worker is stopped.
+ }
+ }
+}
diff --git a/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/StatusHostedServiceTests.cs b/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/StatusHostedServiceTests.cs
index 960047ceb..537e39bf3 100644
--- a/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/StatusHostedServiceTests.cs
+++ b/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/StatusHostedServiceTests.cs
@@ -7,6 +7,7 @@
namespace AliasVault.IntegrationTests.StatusHostedService;
+using AliasVault.IntegrationTests.StatusHostedService.Helpers;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;
diff --git a/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/StatusWorkerResilienceTests.cs b/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/StatusWorkerResilienceTests.cs
new file mode 100644
index 000000000..fdec963c3
--- /dev/null
+++ b/apps/server/Tests/AliasVault.IntegrationTests/StatusHostedService/StatusWorkerResilienceTests.cs
@@ -0,0 +1,83 @@
+//-----------------------------------------------------------------------
+// <copyright file="StatusWorkerResilienceTests.cs" company="aliasvault">
+// Copyright (c) aliasvault. All rights reserved.
+// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace AliasVault.IntegrationTests.StatusHostedService;
+
+using System.Reflection;
+using AliasVault.IntegrationTests.StatusHostedService.Helpers;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.Hosting;
+
+/// <summary>
+/// Regression tests ensuring the StatusWorker recovers itself when a exception is thrown
+/// inside the worker logic.
+/// </summary>
+[TestFixture]
+public class StatusWorkerResilienceTests
+{
+ /// <summary>
+ /// The test host instance.
+ /// </summary>
+ private IHost _testHost = null!;
+
+ /// <summary>
+ /// The test host builder instance.
+ /// </summary>
+ private StatusWorkerResilienceTestHostBuilder _testHostBuilder = null!;
+
+ /// <summary>
+ /// Setup logic for every test.
+ /// </summary>
+ [SetUp]
+ public void Setup()
+ {
+ _testHostBuilder = new StatusWorkerResilienceTestHostBuilder();
+ _testHost = _testHostBuilder.Build();
+ }
+
+ /// <summary>
+ /// Tear down logic for every test.
+ /// </summary>
+ /// <returns>Task.</returns>
+ [TearDown]
+ public async Task TearDown()
+ {
+ await _testHost.StopAsync();
+ _testHost.Dispose();
+ await _testHostBuilder.DisposeAsync();
+ }
+
+ /// <summary>
+ /// Verifies that the StatusWorker recovers itself when a exception is thrown inside the worker logic.
+ /// </summary>
+ /// <returns>Task.</returns>
+ [Test]
+ public async Task ExceptionThrownInsideWorkerLogicDoesNotSoftStopService()
+ {
+ await _testHost.StartAsync();
+
+ var serviceName = Assembly.GetExecutingAssembly().GetName().Name!;
+ string? currentStatus = null;
+ var deadline = DateTime.UtcNow.AddSeconds(40);
+
+ while (DateTime.UtcNow < deadline)
+ {
+ await using var dbContext = await _testHostBuilder.GetDbContextAsync();
+ var entry = await dbContext.WorkerServiceStatuses.AsNoTracking().FirstOrDefaultAsync(x => x.ServiceName == serviceName);
+ currentStatus = entry?.CurrentStatus;
+
+ if (currentStatus == "Started")
+ {
+ break;
+ }
+
+ await Task.Delay(1000);
+ }
+
+ Assert.That(currentStatus, Is.EqualTo("Started"), "Service should recover to Started after a exception is thrown inside the worker logic instead of remaining soft-stopped.");
+ }
+}
diff --git a/apps/server/Tests/AliasVault.IntegrationTests/TaskRunner/SeedData.cs b/apps/server/Tests/AliasVault.IntegrationTests/TaskRunner/Helpers/SeedData.cs
similarity index 99%
rename from apps/server/Tests/AliasVault.IntegrationTests/TaskRunner/SeedData.cs
rename to apps/server/Tests/AliasVault.IntegrationTests/TaskRunner/Helpers/SeedData.cs
index 04b32c061..129a3512a 100644
--- a/apps/server/Tests/AliasVault.IntegrationTests/TaskRunner/SeedData.cs
+++ b/apps/server/Tests/AliasVault.IntegrationTests/TaskRunner/Helpers/SeedData.cs
@@ -5,7 +5,7 @@
// </copyright>
//-----------------------------------------------------------------------
-namespace AliasVault.IntegrationTests.TaskRunner;
+namespace AliasVault.IntegrationTests.TaskRunner.Helpers;
using AliasServerDb;
using AliasVault.Shared.Models.Enums;
diff --git a/apps/server/Tests/AliasVault.IntegrationTests/TaskRunner/TestHostBuilder.cs b/apps/server/Tests/AliasVault.IntegrationTests/TaskRunner/Helpers/TestHostBuilder.cs
similarity index 96%
rename from apps/server/Tests/AliasVault.IntegrationTests/TaskRunner/TestHostBuilder.cs
rename to apps/server/Tests/AliasVault.IntegrationTests/TaskRunner/Helpers/TestHostBuilder.cs
index 495097640..38da4d953 100644
--- a/apps/server/Tests/AliasVault.IntegrationTests/TaskRunner/TestHostBuilder.cs
+++ b/apps/server/Tests/AliasVault.IntegrationTests/TaskRunner/Helpers/TestHostBuilder.cs
@@ -5,7 +5,7 @@
// </copyright>
//-----------------------------------------------------------------------
-namespace AliasVault.IntegrationTests.TaskRunner;
+namespace AliasVault.IntegrationTests.TaskRunner.Helpers;
using AliasVault.Shared.Server.Services;
using AliasVault.TaskRunner.Tasks;
diff --git a/apps/server/Tests/AliasVault.IntegrationTests/TaskRunner/TaskRunnerTests.cs b/apps/server/Tests/AliasVault.IntegrationTests/TaskRunner/TaskRunnerTests.cs
index dbb3980f3..fd6271e87 100644
--- a/apps/server/Tests/AliasVault.IntegrationTests/TaskRunner/TaskRunnerTests.cs
+++ b/apps/server/Tests/AliasVault.IntegrationTests/TaskRunner/TaskRunnerTests.cs
@@ -8,6 +8,7 @@
namespace AliasVault.IntegrationTests.TaskRunner;
using AliasServerDb;
+using AliasVault.IntegrationTests.TaskRunner.Helpers;
using AliasVault.Shared.Models.Enums;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;