Skip to content

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

Add TaskRunner maintenance time tests (#221)

Leendert de Borst0923936f7c1f
diff --git a/src/Services/AliasVault.TaskRunner/Tasks/IMaintenanceTask.cs b/src/Services/AliasVault.TaskRunner/Tasks/Interfaces/IMaintenanceTask.cs
similarity index 100%
rename from src/Services/AliasVault.TaskRunner/Tasks/IMaintenanceTask.cs
rename to src/Services/AliasVault.TaskRunner/Tasks/Interfaces/IMaintenanceTask.cs
diff --git a/src/Tests/AliasVault.IntegrationTests/TaskRunner/SeedData.cs b/src/Tests/AliasVault.IntegrationTests/TaskRunner/SeedData.cs
new file mode 100644
index 000000000..d0beacc33
--- /dev/null
+++ b/src/Tests/AliasVault.IntegrationTests/TaskRunner/SeedData.cs
@@ -0,0 +1,213 @@
+//-----------------------------------------------------------------------
+// <copyright file="SeedData.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.TaskRunner;
+
+using AliasServerDb;
+using AliasVault.Shared.Models.Enums;
+
+/// <summary>
+/// Helper class for seeding the database with test data.
+/// </summary>
+public static class SeedData
+{
+ /// <summary>
+ /// Seeds the database with test data.
+ /// </summary>
+ /// <param name="dbContext">The database context.</param>
+ /// <returns>Task.</returns>
+ public static async Task SeedDatabase(AliasServerDbContext dbContext)
+ {
+ // Seed the database with settings
+ var settings = new List<ServerSetting>
+ {
+ new() { Key = "EmailRetentionDays", Value = "30" },
+ new() { Key = "GeneralLogRetentionDays", Value = "45" },
+ new() { Key = "AuthLogRetentionDays", Value = "60" },
+ new() { Key = "MaxEmailsPerUser", Value = "100" },
+ new() { Key = "MaintenanceTime", Value = "00:00" },
+ new() { Key = "TaskRunnerDays", Value = "1,2,3,4,5,6,7" },
+ };
+
+ await dbContext.ServerSettings.AddRangeAsync(settings);
+
+ // Create test user
+ var user = new AliasVaultUser
+ {
+ UserName = "testuser",
+ Email = "[email protected]",
+ };
+ dbContext.AliasVaultUsers.Add(user);
+ await dbContext.SaveChangesAsync();
+
+ // Create encryption key for the user
+ var encryptionKey = new UserEncryptionKey
+ {
+ Id = Guid.NewGuid(),
+ UserId = user.Id,
+ PublicKey = "test-encryption-key",
+ CreatedAt = DateTime.UtcNow,
+ UpdatedAt = DateTime.UtcNow,
+ };
+ dbContext.UserEncryptionKeys.Add(encryptionKey);
+ await dbContext.SaveChangesAsync();
+
+ await SeedEmails(dbContext, encryptionKey.Id);
+ await SeedLogs(dbContext);
+ await SeedAuthLogs(dbContext);
+
+ await dbContext.SaveChangesAsync();
+ }
+
+ /// <summary>
+ /// Seeds the database with test emails.
+ /// </summary>
+ /// <param name="dbContext">The database context.</param>
+ /// <param name="encryptionKeyId">The encryption key ID.</param>
+ /// <returns>Task.</returns>
+ private static async Task SeedEmails(AliasServerDbContext dbContext, Guid encryptionKeyId)
+ {
+ // Seed old emails (older than 30 days)
+ var oldEmails = new List<Email>();
+ for (int i = 0; i < 50; i++)
+ {
+ oldEmails.Add(CreateTestEmail(i, -45, encryptionKeyId, "Old Email"));
+ }
+
+ await dbContext.Emails.AddRangeAsync(oldEmails);
+
+ // Seed recent emails (within 30 days)
+ var recentEmails = new List<Email>();
+ for (int i = 0; i < 50; i++)
+ {
+ recentEmails.Add(CreateTestEmail(i, -1, encryptionKeyId, "Recent Email"));
+ }
+
+ await dbContext.Emails.AddRangeAsync(recentEmails);
+ }
+
+ /// <summary>
+ /// Seeds the database with test logs.
+ /// </summary>
+ /// <param name="dbContext">The database context.</param>
+ /// <returns>Task.</returns>
+ private static async Task SeedLogs(AliasServerDbContext dbContext)
+ {
+ // Add old general logs (older than 45 days)
+ var oldLogs = new List<Log>();
+ for (int i = 0; i < 50; i++)
+ {
+ oldLogs.Add(CreateTestLog(i, -60, "Old Log"));
+ }
+
+ await dbContext.Logs.AddRangeAsync(oldLogs);
+
+ // Add recent logs (within 45 days)
+ var recentLogs = new List<Log>();
+ for (int i = 0; i < 50; i++)
+ {
+ recentLogs.Add(CreateTestLog(i, -1, "Recent Log"));
+ }
+
+ await dbContext.Logs.AddRangeAsync(recentLogs);
+ }
+
+ /// <summary>
+ /// Seeds the database with test auth logs.
+ /// </summary>
+ /// <param name="dbContext">The database context.</param>
+ /// <returns>Task.</returns>
+ private static async Task SeedAuthLogs(AliasServerDbContext dbContext)
+ {
+ // Add old auth logs (older than 60 days)
+ var oldAuthLogs = new List<AuthLog>();
+ for (int i = 0; i < 50; i++)
+ {
+ oldAuthLogs.Add(CreateTestAuthLog(i, -70));
+ }
+
+ await dbContext.AuthLogs.AddRangeAsync(oldAuthLogs);
+
+ // Add recent auth logs (within 60 days)
+ var recentAuthLogs = new List<AuthLog>();
+ for (int i = 0; i < 50; i++)
+ {
+ recentAuthLogs.Add(CreateTestAuthLog(i, -1));
+ }
+
+ await dbContext.AuthLogs.AddRangeAsync(recentAuthLogs);
+ }
+
+ /// <summary>
+ /// Creates a test email.
+ /// </summary>
+ /// <param name="index">The index.</param>
+ /// <param name="daysOffset">The days offset.</param>
+ /// <param name="encryptionKeyId">The encryption key ID.</param>
+ /// <param name="prefix">The prefix.</param>
+ /// <returns>Email.</returns>
+ private static Email CreateTestEmail(int index, int daysOffset, Guid encryptionKeyId, string prefix)
+ {
+ return new Email
+ {
+ Subject = $"{prefix} {index}",
+ From = "[email protected]",
+ FromLocal = "sender",
+ FromDomain = "example.com",
+ ToLocal = "testuser",
+ ToDomain = "example.tld",
+ Date = DateTime.UtcNow.AddDays(daysOffset),
+ DateSystem = DateTime.UtcNow.AddDays(daysOffset),
+ MessagePlain = "Test message",
+ MessagePreview = "Test message",
+ MessageSource = "Test source",
+ EncryptedSymmetricKey = "dummy-key",
+ UserEncryptionKeyId = encryptionKeyId,
+ };
+ }
+
+ /// <summary>
+ /// Creates a test log.
+ /// </summary>
+ /// <param name="index">The index.</param>
+ /// <param name="daysOffset">The days offset.</param>
+ /// <param name="prefix">The prefix.</param>
+ /// <returns>Log.</returns>
+ private static Log CreateTestLog(int index, int daysOffset, string prefix)
+ {
+ return new Log
+ {
+ Application = "TestApp",
+ SourceContext = "TestContext",
+ Message = $"{prefix} {index}",
+ MessageTemplate = $"{prefix} {index}",
+ Level = "Information",
+ TimeStamp = DateTime.UtcNow.AddDays(daysOffset),
+ Exception = string.Empty,
+ Properties = "{}",
+ LogEvent = "{}",
+ };
+ }
+
+ /// <summary>
+ /// Creates a test auth log.
+ /// </summary>
+ /// <param name="index">The index.</param>
+ /// <param name="daysOffset">The days offset.</param>
+ /// <returns>AuthLog.</returns>
+ private static AuthLog CreateTestAuthLog(int index, int daysOffset)
+ {
+ return new AuthLog
+ {
+ Username = "testuser",
+ EventType = AuthEventType.Login,
+ IsSuccess = true,
+ Timestamp = DateTime.UtcNow.AddDays(daysOffset),
+ };
+ }
+}
diff --git a/src/Tests/AliasVault.IntegrationTests/TaskRunner/TaskRunnerTests.cs b/src/Tests/AliasVault.IntegrationTests/TaskRunner/TaskRunnerTests.cs
index 011f50d91..d8e956a31 100644
--- a/src/Tests/AliasVault.IntegrationTests/TaskRunner/TaskRunnerTests.cs
+++ b/src/Tests/AliasVault.IntegrationTests/TaskRunner/TaskRunnerTests.cs
@@ -7,8 +7,6 @@
namespace AliasVault.IntegrationTests.TaskRunner;
-using AliasServerDb;
-using AliasVault.Shared.Models.Enums;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;
@@ -31,18 +29,11 @@ public class TaskRunnerTests
/// <summary>
/// Setup logic for every test.
/// </summary>
- /// <returns>Task.</returns>
[SetUp]
- public async Task Setup()
+ public void Setup()
{
_testHostBuilder = new TestHostBuilder();
_testHost = _testHostBuilder.Build();
-
- // Seed the database before starting the service.
- await SeedDatabase();
-
- // Start the service so it can run the tasks on the test data.
- await _testHost.StartAsync();
}
/// <summary>
@@ -63,6 +54,9 @@ public class TaskRunnerTests
[Test]
public async Task EmailCleanup()
{
+ // Arrange
+ await InitializeWithTestData();
+
// Assert
var dbContext = _testHostBuilder.GetDbContext();
var emails = await dbContext.Emails.ToListAsync();
@@ -76,10 +70,11 @@ public class TaskRunnerTests
[Test]
public async Task LogCleanup()
{
+ // Arrange
+ await InitializeWithTestData();
+
// Assert
var dbContext = _testHostBuilder.GetDbContext();
-
- // Check general logs
var generalLogs = await dbContext.Logs.ToListAsync();
Assert.That(generalLogs.Count, Is.EqualTo(50), "Only recent general logs should remain");
}
@@ -91,6 +86,9 @@ public class TaskRunnerTests
[Test]
public async Task AuthLogCleanup()
{
+ // Arrange
+ await InitializeWithTestData();
+
// Assert
var dbContext = _testHostBuilder.GetDbContext();
@@ -99,167 +97,99 @@ public class TaskRunnerTests
Assert.That(authLogs.Count, Is.EqualTo(50), "Only recent auth logs should remain");
}
- /// <summary>
- /// Seeds the database with test data.
+ /// <summary>
+ /// Tests that the TaskRunner does not run tasks before the maintenance time.
/// </summary>
- private async Task SeedDatabase()
+ /// <returns>Task.</returns>
+ [Test]
+ public async Task MaintenanceTimeInFutureDoesNotRun()
{
- // Seed the database with settings
- var dbContext = _testHostBuilder.GetDbContext();
+ // Seed database with generic test data.
+ await SeedData.SeedDatabase(_testHostBuilder.GetDbContext());
- // Configure maintenance settings
- var settings = new List<ServerSetting>
- {
- new() { Key = "EmailRetentionDays", Value = "30" },
- new() { Key = "GeneralLogRetentionDays", Value = "45" },
- new() { Key = "AuthLogRetentionDays", Value = "60" },
- new() { Key = "MaxEmailsPerUser", Value = "100" },
- new() { Key = "MaintenanceTime", Value = "03:30" },
- new() { Key = "TaskRunnerDays", Value = "1,2,3,4,5,6,7" },
- };
+ // Update maintenance time in database to future to ensure the task runner doesn't execute yet.
- await dbContext.ServerSettings.AddRangeAsync(settings);
+ // Get current time and set maintenance time to 2 hours in the future
+ var now = DateTime.Now;
+ var futureTime = now.AddHours(2);
- // Create test user
- var user = new AliasVaultUser
+ // Make sure we don't exceed midnight
+ if (futureTime.Day != now.Day)
{
- UserName = "testuser",
- Email = "[email protected]",
- };
- dbContext.AliasVaultUsers.Add(user);
- await dbContext.SaveChangesAsync();
+ futureTime = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59);
+ }
- // Create encryption key for the user
- var encryptionKey = new UserEncryptionKey
- {
- Id = Guid.NewGuid(),
- UserId = user.Id,
- PublicKey = "test-encryption-key",
- CreatedAt = DateTime.UtcNow,
- UpdatedAt = DateTime.UtcNow,
- };
- dbContext.UserEncryptionKeys.Add(encryptionKey);
+ // Update maintenance time in database
+ var dbContext = _testHostBuilder.GetDbContext();
+ var maintenanceTimeSetting = await dbContext.ServerSettings
+ .FirstAsync(s => s.Key == "MaintenanceTime");
+ maintenanceTimeSetting.Value = futureTime.ToString("HH:mm");
await dbContext.SaveChangesAsync();
- // Seed old emails (older than 30 days)
- var oldEmails = new List<Email>();
- for (int i = 0; i < 50; i++)
- {
- oldEmails.Add(new Email
- {
- Subject = $"Old Email {i}",
- From = "[email protected]",
- FromLocal = "sender",
- FromDomain = "example.com",
- ToLocal = "testuser",
- ToDomain = "example.tld",
- Date = DateTime.UtcNow.AddDays(-45),
- DateSystem = DateTime.UtcNow.AddDays(-45),
- MessagePlain = "Test message",
- MessagePreview = "Test message",
- MessageSource = "Test source",
- EncryptedSymmetricKey = "dummy-key",
- UserEncryptionKeyId = encryptionKey.Id,
- });
- }
+ // Get initial email count
+ var initialEmailCount = await dbContext.Emails.CountAsync();
- await dbContext.Emails.AddRangeAsync(oldEmails);
+ // Start the service.
+ await _testHost.StartAsync();
- // Seed recent emails (within 30 days)
- var recentEmails = new List<Email>();
- for (int i = 0; i < 50; i++)
- {
- recentEmails.Add(new Email
- {
- Subject = $"Recent Email {i}",
- From = "[email protected]",
- FromLocal = "sender",
- FromDomain = "example.com",
- ToLocal = "testuser",
- ToDomain = "example.tld",
- Date = DateTime.UtcNow.AddDays(-1),
- DateSystem = DateTime.UtcNow.AddDays(-1),
- MessagePlain = "Test message",
- MessagePreview = "Test message",
- MessageSource = "Test source",
- EncryptedSymmetricKey = "dummy-key",
- UserEncryptionKeyId = encryptionKey.Id,
- });
- }
+ // Verify email count hasn't changed (tasks haven't run)
+ var currentEmailCount = await dbContext.Emails.CountAsync();
+ Assert.That(currentEmailCount, Is.EqualTo(initialEmailCount), "Email count changed despite maintenance time being in the future. Check if TaskRunner is respecting the maintenance time setting.");
+ }
- await dbContext.Emails.AddRangeAsync(recentEmails);
+ /// <summary>
+ /// Tests that the TaskRunner does not run tasks when the current day is excluded.
+ /// </summary>
+ /// <returns>Task.</returns>
+ [Test]
+ public async Task MaintenanceTimeExcludedDayDoesNotRun()
+ {
+ // Seed database with generic test data.
+ await SeedData.SeedDatabase(_testHostBuilder.GetDbContext());
- // Add old general logs (older than 45 days)
- var oldLogs = new List<Log>();
- for (int i = 0; i < 50; i++)
+ // Get current day of week (1-7, Monday = 1, Sunday = 7)
+ var currentDay = (int)DateTime.Now.DayOfWeek;
+ if (currentDay == 0)
{
- oldLogs.Add(new Log
- {
- Application = "TestApp",
- SourceContext = "TestContext",
- Message = $"Old Log {i}",
- MessageTemplate = $"Old Log {i}",
- Level = "Information",
- TimeStamp = DateTime.UtcNow.AddDays(-60),
- Exception = string.Empty,
- Properties = "{}",
- LogEvent = "{}",
- });
+ currentDay = 7; // Convert Sunday from 0 to 7
}
- await dbContext.Logs.AddRangeAsync(oldLogs);
+ // Update maintenance settings in database to exclude current day
+ var dbContext = _testHostBuilder.GetDbContext();
- // Add recent logs (within 45 days)
- var recentLogs = new List<Log>();
- for (int i = 0; i < 50; i++)
- {
- recentLogs.Add(new Log
- {
- Application = "TestApp",
- SourceContext = "TestContext",
- Message = $"Recent Log {i}",
- MessageTemplate = $"Recent Log {i}",
- Level = "Information",
- TimeStamp = DateTime.UtcNow.AddDays(-1),
- Exception = string.Empty,
- Properties = "{}",
- LogEvent = "{}",
- });
- }
+ // Set maintenance time to midnight
+ var maintenanceTimeSetting = await dbContext.ServerSettings
+ .FirstAsync(s => s.Key == "MaintenanceTime");
+ maintenanceTimeSetting.Value = "00:00";
- await dbContext.Logs.AddRangeAsync(recentLogs);
+ // Set task runner days to all days except current day
+ var taskRunnerDays = Enumerable.Range(1, 7)
+ .Where(d => d != currentDay)
+ .ToList();
+ var taskRunnerDaysSetting = await dbContext.ServerSettings
+ .FirstAsync(s => s.Key == "TaskRunnerDays");
+ taskRunnerDaysSetting.Value = string.Join(",", taskRunnerDays);
- // Add old auth logs (older than 60 days)
- var oldAuthLogs = new List<AuthLog>();
- for (int i = 0; i < 50; i++)
- {
- oldAuthLogs.Add(new AuthLog
- {
- Username = "testuser",
- EventType = AuthEventType.Login,
- IsSuccess = true,
- Timestamp = DateTime.UtcNow.AddDays(-70),
- });
- }
+ await dbContext.SaveChangesAsync();
- await dbContext.AuthLogs.AddRangeAsync(oldAuthLogs);
+ // Get initial email count
+ var initialEmailCount = await dbContext.Emails.CountAsync();
- var recentAuthLogs = new List<AuthLog>();
- for (int i = 0; i < 50; i++)
- {
- recentAuthLogs.Add(new AuthLog
- {
- Username = "testuser",
- EventType = AuthEventType.Login,
- IsSuccess = true,
- Timestamp = DateTime.UtcNow.AddDays(-1),
- });
- }
+ // Start the service
+ await _testHost.StartAsync();
- await dbContext.AuthLogs.AddRangeAsync(recentAuthLogs);
+ // Verify email count hasn't changed (tasks haven't run)
+ var currentEmailCount = await dbContext.Emails.CountAsync();
+ Assert.That(currentEmailCount, Is.EqualTo(initialEmailCount), "Email count changed despite current day being excluded from maintenance days. Check if TaskRunner is respecting the task runner days setting.");
+ }
- await dbContext.SaveChangesAsync();
+ /// <summary>
+ /// Initializes the test with test data.
+ /// </summary>
+ /// <returns>Task.</returns>
+ protected async Task InitializeWithTestData()
+ {
+ await SeedData.SeedDatabase(_testHostBuilder.GetDbContext());
+ await _testHost.StartAsync();
}
}