Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Update email integration tests (#2159)
Leendert de Borst4327934ced47
diff --git a/apps/server/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.cs b/apps/server/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.csindex 5f0fa1e76..2f4c0cf89 100644--- a/apps/server/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.cs+++ b/apps/server/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.cs@@ -58,54 +58,16 @@ public class SmtpServerTestsawait _testHost.StartAsync();- // Create an AliasVault user, public key and an email claim.+ // Create an AliasVault user with personal group, manifest and primary delivery key (public key).var dbContext = _testHostBuilder.GetDbContext();- var user = new AliasVaultUser- {- UserName = "testuser",- Email = "[email protected]",- };- dbContext.AliasVaultUsers.Add(user);- await dbContext.SaveChangesAsync();+ var testUser = await TestUserSeeder.CreateTestUserAsync(dbContext, "testuser", "[email protected]", PublicKey);- // Create email claims.- var emailClaim = new UserEmailClaim- {- UserId = user.Id,- Address = "[email protected]",- AddressLocal = "claimed",- AddressDomain = "example.tld",- };- dbContext.UserEmailClaims.Add(emailClaim);-- var emailClaim2 = new UserEmailClaim- {- UserId = user.Id,- Address = "[email protected]",- AddressLocal = "claimed.cc",- AddressDomain = "example.tld",- };- dbContext.UserEmailClaims.Add(emailClaim2);+ // Create email claims linked to the user's personal manifest so delivery can resolve the primary delivery key.+ dbContext.EmailClaims.Add(TestUserSeeder.CreateEmailClaim(testUser.Manifest.ManifestId, "[email protected]"));+ dbContext.EmailClaims.Add(TestUserSeeder.CreateEmailClaim(testUser.Manifest.ManifestId, "[email protected]"));// Create disabled email claim.- var emailClaimDisabled = new UserEmailClaim- {- UserId = user.Id,- Address = "[email protected]",- AddressLocal = "disabled",- AddressDomain = "example.tld",- Disabled = true,- };- dbContext.UserEmailClaims.Add(emailClaimDisabled);-- // Create public key.- var encryptionKey = new UserEncryptionKey- {- UserId = user.Id,- PublicKey = PublicKey,- IsPrimary = true,- };- dbContext.UserEncryptionKeys.Add(encryptionKey);+ dbContext.EmailClaims.Add(TestUserSeeder.CreateEmailClaim(testUser.Manifest.ManifestId, "[email protected]", disabled: true));await dbContext.SaveChangesAsync();}@@ -215,7 +177,7 @@ public class SmtpServerTestsawait SendMessageToSmtpServer(message);// Check if the email is in the database.- var processedEmail = await _testHostBuilder.GetDbContext().Emails.FirstAsync();+ var processedEmail = await _testHostBuilder.GetDbContext().Emails.Include(x => x.Wraps).FirstAsync();// Test non-encrypted field.Assert.That(processedEmail.To, Is.EqualTo("[email protected]"));@@ -251,7 +213,7 @@ public class SmtpServerTestsawait SendMessageToSmtpServer(message);// Check if the email is in the database.- var processedEmail = await _testHostBuilder.GetDbContext().Emails.FirstAsync();+ var processedEmail = await _testHostBuilder.GetDbContext().Emails.Include(x => x.Wraps).FirstAsync();// Test non-encrypted field.Assert.That(processedEmail.To, Is.EqualTo("[email protected]"));@@ -285,7 +247,7 @@ public class SmtpServerTestsawait SendMessageToSmtpServer(message);// Check if the email is in the database.- var processedEmail = await _testHostBuilder.GetDbContext().Emails.FirstAsync();+ var processedEmail = await _testHostBuilder.GetDbContext().Emails.Include(x => x.Wraps).FirstAsync();// Test non-encrypted field.Assert.That(processedEmail.To, Is.EqualTo("[email protected]"));@@ -319,7 +281,7 @@ public class SmtpServerTestsawait SendMessageToSmtpServer(message);// Check if the email is in the database.- var processedEmail = await _testHostBuilder.GetDbContext().Emails.FirstAsync();+ var processedEmail = await _testHostBuilder.GetDbContext().Emails.Include(x => x.Wraps).FirstAsync();// Decrypt the email and verify the accented characters survived into the preview.processedEmail = EmailEncryption.DecryptEmail(processedEmail, PrivateKey);diff --git a/apps/server/Tests/AliasVault.IntegrationTests/TaskRunner/Helpers/SeedData.cs b/apps/server/Tests/AliasVault.IntegrationTests/TaskRunner/Helpers/SeedData.csindex 129a3512a..22660931a 100644--- a/apps/server/Tests/AliasVault.IntegrationTests/TaskRunner/Helpers/SeedData.cs+++ b/apps/server/Tests/AliasVault.IntegrationTests/TaskRunner/Helpers/SeedData.cs@@ -36,28 +36,10 @@ public static class SeedDataawait 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();+ // Create test user with personal group, manifest and primary delivery key.+ var testUser = await TestUserSeeder.CreateTestUserAsync(dbContext, "testuser", "[email protected]");- await SeedEmails(dbContext, encryptionKey.Id);+ await SeedEmails(dbContext, testUser.DeliveryKey.Id);await SeedLogs(dbContext);await SeedAuthLogs(dbContext);@@ -68,7 +50,7 @@ public static class SeedData/// Seeds the database with test emails./// </summary>/// <param name="dbContext">The database context.</param>- /// <param name="encryptionKeyId">The encryption key ID.</param>+ /// <param name="encryptionKeyId">The delivery key ID the emails' key wraps reference.</param>/// <returns>Task.</returns>private static async Task SeedEmails(AliasServerDbContext dbContext, Guid encryptionKeyId){@@ -148,7 +130,7 @@ public static class SeedData/// </summary>/// <param name="index">The index.</param>/// <param name="daysOffset">The days offset.</param>- /// <param name="encryptionKeyId">The encryption key ID.</param>+ /// <param name="encryptionKeyId">The delivery key ID the email's key wrap references.</param>/// <param name="prefix">The prefix.</param>/// <returns>Email.</returns>private static Email CreateTestEmail(int index, int daysOffset, Guid encryptionKeyId, string prefix)@@ -167,8 +149,7 @@ public static class SeedDataMessagePlain = "Test message",MessagePreview = "Test message",MessageSource = "Test source",- EncryptedSymmetricKey = "dummy-key",- UserEncryptionKeyId = encryptionKeyId,+ Wraps = [new EmailKeyWrap { EncryptionKeyId = encryptionKeyId, EncryptedSymmetricKey = "dummy-key" }],};}diff --git a/apps/server/Tests/AliasVault.IntegrationTests/TaskRunner/TaskRunnerTests.cs b/apps/server/Tests/AliasVault.IntegrationTests/TaskRunner/TaskRunnerTests.csindex fd6271e87..46a144b2b 100644--- a/apps/server/Tests/AliasVault.IntegrationTests/TaskRunner/TaskRunnerTests.cs+++ b/apps/server/Tests/AliasVault.IntegrationTests/TaskRunner/TaskRunnerTests.cs@@ -542,13 +542,8 @@ public class TaskRunnerTestsawait using var dbContext = await _testHostBuilder.GetDbContextAsync();// Create test user- var user = new AliasVaultUser- {- UserName = "testuser",- Email = "[email protected]",- };- dbContext.AliasVaultUsers.Add(user);- await dbContext.SaveChangesAsync();+ var testUser = await TestUserSeeder.CreateTestUserAsync(dbContext, "testuser", "[email protected]");+ var user = testUser.User;// Create fulfilled-but-not-retrieved request that's old enough to be cleared (> 10 minutes)var staleRequest = new MobileLoginRequest@@ -640,15 +635,15 @@ public class TaskRunnerTests/// Creates a base email with static required fields./// </summary>/// <param name="to">The recipient email address.</param>- /// <param name="userEncryptionKey">The to be associated user encryption key.</param>+ /// <param name="deliveryKey">The delivery key the email's key wrap references.</param>/// <param name="subject">The email subject.</param>/// <param name="date">The email date.</param>/// <returns>A new Email object with static fields pre-filled.</returns>- private static Email CreateTestEmail(string to, UserEncryptionKey userEncryptionKey, string subject, DateTime date)+ private static Email CreateTestEmail(string to, VaultManifestDeliveryKey deliveryKey, string subject, DateTime date){return new Email{- UserEncryptionKeyId = userEncryptionKey.Id,+ Wraps = [new EmailKeyWrap { EncryptionKeyId = deliveryKey.Id, EncryptedSymmetricKey = "n/a" }],From = "n/a",FromLocal = "n/a",FromDomain = "n/a",@@ -660,7 +655,6 @@ public class TaskRunnerTestsMessageHtml = "n/a",MessagePreview = "n/a",Subject = subject,- EncryptedSymmetricKey = "n/a",Date = date,DateSystem = date,};@@ -716,48 +710,22 @@ public class TaskRunnerTests/// <summary>/// Sets up test data for disabled email cleanup tests./// </summary>- /// <returns>Task containing the test user and encryption key.</returns>+ /// <returns>Task.</returns>private async Task SetupDisabledEmailCleanupTest(){await using var dbContext = await _testHostBuilder.GetDbContextAsync();- // 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();+ // Create test user with personal group, manifest and primary delivery key.+ var testUser = await TestUserSeeder.CreateTestUserAsync(dbContext, "testuser", "[email protected]");+ var deliveryKey = testUser.DeliveryKey;- // Create 5 aliases- var aliases = new List<UserEmailClaim>();+ // Create 5 aliases, the first two disabled.+ var aliases = new List<EmailClaim>();for (var i = 0; i < 5; i++){- var alias = new UserEmailClaim- {- UserId = user.Id,- Address = $"alias{i}@example.tld",- AddressLocal = $"alias{i}",- AddressDomain = "example.tld",- Disabled = i < 2, // First two aliases are disabled- CreatedAt = DateTime.UtcNow.AddDays(-60),- UpdatedAt = DateTime.UtcNow.AddDays(-60),- };+ var alias = TestUserSeeder.CreateEmailClaim(testUser.Manifest.ManifestId, $"alias{i}@example.tld", disabled: i < 2, createdAt: DateTime.UtcNow.AddDays(-60));aliases.Add(alias);- dbContext.UserEmailClaims.Add(alias);+ dbContext.EmailClaims.Add(alias);}await dbContext.SaveChangesAsync();@@ -771,7 +739,7 @@ public class TaskRunnerTestsfor (int i = 0; i < 50; i++){var randomDate = DateTime.UtcNow.AddDays(-Random.Shared.Next(1, 60));- dbContext.Emails.Add(CreateTestEmail(alias.Address, encryptionKey, $"Test Email {i}", randomDate));+ dbContext.Emails.Add(CreateTestEmail(alias.Address, deliveryKey, $"Test Email {i}", randomDate));}}else@@ -781,35 +749,35 @@ public class TaskRunnerTestsvar date50DaysAgo = DateTime.UtcNow.AddDays(-50);for (int i = 0; i < 10; i++){- dbContext.Emails.Add(CreateTestEmail(alias.Address, encryptionKey, $"Old Email {i}", date50DaysAgo));+ dbContext.Emails.Add(CreateTestEmail(alias.Address, deliveryKey, $"Old Email {i}", date50DaysAgo));}// 10 emails from 40 days agovar date40DaysAgo = DateTime.UtcNow.AddDays(-40);for (int i = 0; i < 10; i++){- dbContext.Emails.Add(CreateTestEmail(alias.Address, encryptionKey, $"Old Email {i}", date40DaysAgo));+ dbContext.Emails.Add(CreateTestEmail(alias.Address, deliveryKey, $"Old Email {i}", date40DaysAgo));}// 10 emails from 30 days agovar date30DaysAgo = DateTime.UtcNow.AddDays(-30);for (int i = 0; i < 10; i++){- dbContext.Emails.Add(CreateTestEmail(alias.Address, encryptionKey, $"Old Email {i}", date30DaysAgo));+ dbContext.Emails.Add(CreateTestEmail(alias.Address, deliveryKey, $"Old Email {i}", date30DaysAgo));}// 10 emails from 20 days agovar date20DaysAgo = DateTime.UtcNow.AddDays(-20);for (int i = 0; i < 10; i++){- dbContext.Emails.Add(CreateTestEmail(alias.Address, encryptionKey, $"Recent Email {i}", date20DaysAgo));+ dbContext.Emails.Add(CreateTestEmail(alias.Address, deliveryKey, $"Recent Email {i}", date20DaysAgo));}// 10 emails from 10 days agovar date10DaysAgo = DateTime.UtcNow.AddDays(-10);for (int i = 0; i < 10; i++){- dbContext.Emails.Add(CreateTestEmail(alias.Address, encryptionKey, $"Recent Email {i}", date10DaysAgo));+ dbContext.Emails.Add(CreateTestEmail(alias.Address, deliveryKey, $"Recent Email {i}", date10DaysAgo));}}}@@ -825,59 +793,31 @@ public class TaskRunnerTests{await using var dbContext = await _testHostBuilder.GetDbContextAsync();- // Create user1 with 5 email limit- var user1 = new AliasVaultUser- {- UserName = "user1",- Email = "[email protected]",- MaxEmails = 5,- };- dbContext.AliasVaultUsers.Add(user1);+ // Create user1 with 5 email limit (the limit lives on the user's personal group)+ var user1 = await TestUserSeeder.CreateTestUserAsync(dbContext, "user1", "[email protected]", configureGroup: g => g.MaxEmails = 5);// Create user2 with 10 email limit- var user2 = new AliasVaultUser- {- UserName = "user2",- Email = "[email protected]",- MaxEmails = 10,- };- dbContext.AliasVaultUsers.Add(user2);+ var user2 = await TestUserSeeder.CreateTestUserAsync(dbContext, "user2", "[email protected]", configureGroup: g => g.MaxEmails = 10);// Create user3 with no limit (0 = unlimited)- var user3 = new AliasVaultUser- {- UserName = "user3",- Email = "[email protected]",- MaxEmails = 0,- };- dbContext.AliasVaultUsers.Add(user3);+ var user3 = await TestUserSeeder.CreateTestUserAsync(dbContext, "user3", "[email protected]", configureGroup: g => g.MaxEmails = 0);- await dbContext.SaveChangesAsync();+ // The emails' key wraps all reference user1's delivery key, mirroring the single encryption key used before.+ var deliveryKey = user1.DeliveryKey;- // Create encryption key- var encryptionKey = new UserEncryptionKey- {- Id = Guid.NewGuid(),- UserId = user1.Id,- PublicKey = "test-encryption-key",- CreatedAt = DateTime.UtcNow,- UpdatedAt = DateTime.UtcNow,- };- dbContext.UserEncryptionKeys.Add(encryptionKey);-- // Create email claims for each user- dbContext.UserEmailClaims.Add(new UserEmailClaim { UserId = user1.Id, Address = "[email protected]", AddressLocal = "user1", AddressDomain = "test.com" });- dbContext.UserEmailClaims.Add(new UserEmailClaim { UserId = user2.Id, Address = "[email protected]", AddressLocal = "user2", AddressDomain = "test.com" });- dbContext.UserEmailClaims.Add(new UserEmailClaim { UserId = user3.Id, Address = "[email protected]", AddressLocal = "user3", AddressDomain = "test.com" });+ // Create email claims for each user, linked to their personal manifest+ dbContext.EmailClaims.Add(TestUserSeeder.CreateEmailClaim(user1.Manifest.ManifestId, "[email protected]"));+ dbContext.EmailClaims.Add(TestUserSeeder.CreateEmailClaim(user2.Manifest.ManifestId, "[email protected]"));+ dbContext.EmailClaims.Add(TestUserSeeder.CreateEmailClaim(user3.Manifest.ManifestId, "[email protected]"));// Create 15 emails for each user (all will exceed user1 and user2 limits)for (int i = 0; i < 15; i++){var dateCreated = DateTime.UtcNow.AddDays(-i); // Different ages for realistic testing- dbContext.Emails.Add(CreateTestEmail("[email protected]", encryptionKey, $"User1 Email {i}", dateCreated));- dbContext.Emails.Add(CreateTestEmail("[email protected]", encryptionKey, $"User2 Email {i}", dateCreated));- dbContext.Emails.Add(CreateTestEmail("[email protected]", encryptionKey, $"User3 Email {i}", dateCreated));+ dbContext.Emails.Add(CreateTestEmail("[email protected]", deliveryKey, $"User1 Email {i}", dateCreated));+ dbContext.Emails.Add(CreateTestEmail("[email protected]", deliveryKey, $"User2 Email {i}", dateCreated));+ dbContext.Emails.Add(CreateTestEmail("[email protected]", deliveryKey, $"User3 Email {i}", dateCreated));}await dbContext.SaveChangesAsync();@@ -891,50 +831,22 @@ public class TaskRunnerTests{await using var dbContext = await _testHostBuilder.GetDbContextAsync();- // Create user1 with 7 days age limit- var user1 = new AliasVaultUser- {- UserName = "user1",- Email = "[email protected]",- MaxEmailAgeDays = 7,- };- dbContext.AliasVaultUsers.Add(user1);+ // Create user1 with 7 days age limit (the limit lives on the user's personal group)+ var user1 = await TestUserSeeder.CreateTestUserAsync(dbContext, "user1", "[email protected]", configureGroup: g => g.MaxEmailAgeDays = 7);// Create user2 with 30 days age limit- var user2 = new AliasVaultUser- {- UserName = "user2",- Email = "[email protected]",- MaxEmailAgeDays = 30,- };- dbContext.AliasVaultUsers.Add(user2);+ var user2 = await TestUserSeeder.CreateTestUserAsync(dbContext, "user2", "[email protected]", configureGroup: g => g.MaxEmailAgeDays = 30);// Create user3 with no age limit (0 = unlimited)- var user3 = new AliasVaultUser- {- UserName = "user3",- Email = "[email protected]",- MaxEmailAgeDays = 0,- };- dbContext.AliasVaultUsers.Add(user3);+ var user3 = await TestUserSeeder.CreateTestUserAsync(dbContext, "user3", "[email protected]", configureGroup: g => g.MaxEmailAgeDays = 0);- await dbContext.SaveChangesAsync();-- // Create encryption key- var encryptionKey = new UserEncryptionKey- {- Id = Guid.NewGuid(),- UserId = user1.Id,- PublicKey = "test-encryption-key",- CreatedAt = DateTime.UtcNow,- UpdatedAt = DateTime.UtcNow,- };- dbContext.UserEncryptionKeys.Add(encryptionKey);+ // The emails' key wraps all reference user1's delivery key, mirroring the single encryption key used before.+ var deliveryKey = user1.DeliveryKey;- // Create email claims for each user- dbContext.UserEmailClaims.Add(new UserEmailClaim { UserId = user1.Id, Address = "[email protected]", AddressLocal = "user1", AddressDomain = "test.com" });- dbContext.UserEmailClaims.Add(new UserEmailClaim { UserId = user2.Id, Address = "[email protected]", AddressLocal = "user2", AddressDomain = "test.com" });- dbContext.UserEmailClaims.Add(new UserEmailClaim { UserId = user3.Id, Address = "[email protected]", AddressLocal = "user3", AddressDomain = "test.com" });+ // Create email claims for each user, linked to their personal manifest+ dbContext.EmailClaims.Add(TestUserSeeder.CreateEmailClaim(user1.Manifest.ManifestId, "[email protected]"));+ dbContext.EmailClaims.Add(TestUserSeeder.CreateEmailClaim(user2.Manifest.ManifestId, "[email protected]"));+ dbContext.EmailClaims.Add(TestUserSeeder.CreateEmailClaim(user3.Manifest.ManifestId, "[email protected]"));// Create emails with various ages for each uservar testDates = new[]@@ -951,9 +863,9 @@ public class TaskRunnerTestsforeach (var date in testDates){- dbContext.Emails.Add(CreateTestEmail("[email protected]", encryptionKey, $"User1 Email {date:yyyy-MM-dd}", date));- dbContext.Emails.Add(CreateTestEmail("[email protected]", encryptionKey, $"User2 Email {date:yyyy-MM-dd}", date));- dbContext.Emails.Add(CreateTestEmail("[email protected]", encryptionKey, $"User3 Email {date:yyyy-MM-dd}", date));+ dbContext.Emails.Add(CreateTestEmail("[email protected]", deliveryKey, $"User1 Email {date:yyyy-MM-dd}", date));+ dbContext.Emails.Add(CreateTestEmail("[email protected]", deliveryKey, $"User2 Email {date:yyyy-MM-dd}", date));+ dbContext.Emails.Add(CreateTestEmail("[email protected]", deliveryKey, $"User3 Email {date:yyyy-MM-dd}", date));}await dbContext.SaveChangesAsync();@@ -967,48 +879,26 @@ public class TaskRunnerTests{await using var dbContext = await _testHostBuilder.GetDbContextAsync();- // Create user with specific limit that overrides global- var userWithLimit = new AliasVaultUser- {- UserName = "userwithLimit",- Email = "[email protected]",- MaxEmails = 5, // Lower than global limit- };- dbContext.AliasVaultUsers.Add(userWithLimit);-- // Create user without specific limit (should use global)- var userWithoutLimit = new AliasVaultUser- {- UserName = "userwithoutLimit",- Email = "[email protected]",- MaxEmails = 0, // Use global limit- };- dbContext.AliasVaultUsers.Add(userWithoutLimit);+ // Create user with specific limit that overrides global (lower than global limit, stored on the personal group)+ var userWithLimit = await TestUserSeeder.CreateTestUserAsync(dbContext, "userwithLimit", "[email protected]", configureGroup: g => g.MaxEmails = 5);- await dbContext.SaveChangesAsync();+ // Create user without specific limit (0 = use global limit)+ var userWithoutLimit = await TestUserSeeder.CreateTestUserAsync(dbContext, "userwithoutLimit", "[email protected]", configureGroup: g => g.MaxEmails = 0);- // Create encryption key- var encryptionKey = new UserEncryptionKey- {- Id = Guid.NewGuid(),- UserId = userWithLimit.Id,- PublicKey = "test-encryption-key",- CreatedAt = DateTime.UtcNow,- UpdatedAt = DateTime.UtcNow,- };- dbContext.UserEncryptionKeys.Add(encryptionKey);+ // The emails' key wraps all reference the first user's delivery key, mirroring the single encryption key used before.+ var deliveryKey = userWithLimit.DeliveryKey;- // Create email claims- dbContext.UserEmailClaims.Add(new UserEmailClaim { UserId = userWithLimit.Id, Address = "[email protected]", AddressLocal = "userwithLimit", AddressDomain = "test.com" });- dbContext.UserEmailClaims.Add(new UserEmailClaim { UserId = userWithoutLimit.Id, Address = "[email protected]", AddressLocal = "userwithoutLimit", AddressDomain = "test.com" });+ // Create email claims, linked to each user's personal manifest+ dbContext.EmailClaims.Add(TestUserSeeder.CreateEmailClaim(userWithLimit.Manifest.ManifestId, "[email protected]"));+ dbContext.EmailClaims.Add(TestUserSeeder.CreateEmailClaim(userWithoutLimit.Manifest.ManifestId, "[email protected]"));// Create 25 emails for each user (both exceed their limits)for (int i = 0; i < 25; i++){var dateCreated = DateTime.UtcNow.AddDays(-i);- dbContext.Emails.Add(CreateTestEmail("[email protected]", encryptionKey, $"Limited User Email {i}", dateCreated));- dbContext.Emails.Add(CreateTestEmail("[email protected]", encryptionKey, $"Unlimited User Email {i}", dateCreated));+ dbContext.Emails.Add(CreateTestEmail("[email protected]", deliveryKey, $"Limited User Email {i}", dateCreated));+ dbContext.Emails.Add(CreateTestEmail("[email protected]", deliveryKey, $"Unlimited User Email {i}", dateCreated));}await dbContext.SaveChangesAsync();@@ -1022,65 +912,40 @@ public class TaskRunnerTests{await using var dbContext = await _testHostBuilder.GetDbContextAsync();- // Create active user (recent activity)- var activeUser = new AliasVaultUser- {- UserName = "activeuser",- Email = "[email protected]",- LastActivityDate = DateTime.UtcNow.AddDays(-5), // Active within 30 days- };- dbContext.AliasVaultUsers.Add(activeUser);+ // Create active user (recent activity, within 30 days)+ var activeUser = await TestUserSeeder.CreateTestUserAsync(dbContext, "activeuser", "[email protected]", configureUser: u => u.LastActivityDate = DateTime.UtcNow.AddDays(-5));- // Create inactive user (no recent activity)- var inactiveUser = new AliasVaultUser- {- UserName = "inactiveuser",- Email = "[email protected]",- LastActivityDate = DateTime.UtcNow.AddDays(-45), // Inactive for 45 days- };- dbContext.AliasVaultUsers.Add(inactiveUser);+ // Create inactive user (no recent activity for 45 days)+ var inactiveUser = await TestUserSeeder.CreateTestUserAsync(dbContext, "inactiveuser", "[email protected]", configureUser: u => u.LastActivityDate = DateTime.UtcNow.AddDays(-45));- // Create old user (very old, no activity)- var oldUser = new AliasVaultUser+ // Create old user (never logged in, created 100 days ago)+ var oldUser = await TestUserSeeder.CreateTestUserAsync(dbContext, "olduser", "[email protected]", configureUser: u =>{- UserName = "olduser",- Email = "[email protected]",- LastActivityDate = null, // Never logged in- CreatedAt = DateTime.UtcNow.AddDays(-100), // Created 100 days ago- };- dbContext.AliasVaultUsers.Add(oldUser);-- await dbContext.SaveChangesAsync();+ u.LastActivityDate = null;+ u.CreatedAt = DateTime.UtcNow.AddDays(-100);+ });- // Create encryption key- var encryptionKey = new UserEncryptionKey- {- Id = Guid.NewGuid(),- UserId = activeUser.Id,- PublicKey = "test-encryption-key",- CreatedAt = DateTime.UtcNow,- UpdatedAt = DateTime.UtcNow,- };- dbContext.UserEncryptionKeys.Add(encryptionKey);+ // The emails' key wraps all reference the active user's delivery key, mirroring the single encryption key used before.+ var deliveryKey = activeUser.DeliveryKey;- // Create email claims for each user- dbContext.UserEmailClaims.Add(new UserEmailClaim { UserId = activeUser.Id, Address = "[email protected]", AddressLocal = "activeuser", AddressDomain = "test.com" });- dbContext.UserEmailClaims.Add(new UserEmailClaim { UserId = inactiveUser.Id, Address = "[email protected]", AddressLocal = "inactiveuser", AddressDomain = "test.com" });- dbContext.UserEmailClaims.Add(new UserEmailClaim { UserId = oldUser.Id, Address = "[email protected]", AddressLocal = "olduser", AddressDomain = "test.com" });+ // Create email claims for each user, linked to their personal manifest+ dbContext.EmailClaims.Add(TestUserSeeder.CreateEmailClaim(activeUser.Manifest.ManifestId, "[email protected]"));+ dbContext.EmailClaims.Add(TestUserSeeder.CreateEmailClaim(inactiveUser.Manifest.ManifestId, "[email protected]"));+ dbContext.EmailClaims.Add(TestUserSeeder.CreateEmailClaim(oldUser.Manifest.ManifestId, "[email protected]"));// Create emails for each userfor (int i = 0; i < 10; i++){var dateCreated = DateTime.UtcNow.AddDays(-i);- dbContext.Emails.Add(CreateTestEmail("[email protected]", encryptionKey, $"Active User Email {i}", dateCreated));- dbContext.Emails.Add(CreateTestEmail("[email protected]", encryptionKey, $"Inactive User Email {i}", dateCreated));+ dbContext.Emails.Add(CreateTestEmail("[email protected]", deliveryKey, $"Active User Email {i}", dateCreated));+ dbContext.Emails.Add(CreateTestEmail("[email protected]", deliveryKey, $"Inactive User Email {i}", dateCreated));}// Create 15 emails for old userfor (int i = 0; i < 15; i++){var dateCreated = DateTime.UtcNow.AddDays(-i);- dbContext.Emails.Add(CreateTestEmail("[email protected]", encryptionKey, $"Old User Email {i}", dateCreated));+ dbContext.Emails.Add(CreateTestEmail("[email protected]", deliveryKey, $"Old User Email {i}", dateCreated));}await dbContext.SaveChangesAsync();@@ -1094,53 +959,31 @@ public class TaskRunnerTests{await using var dbContext = await _testHostBuilder.GetDbContextAsync();- // Create active user (recent activity)- var activeUser = new AliasVaultUser- {- UserName = "activeuser",- Email = "[email protected]",- LastActivityDate = DateTime.UtcNow.AddDays(-5), // Active within 30 days- };- dbContext.AliasVaultUsers.Add(activeUser);+ // Create active user (recent activity, within 30 days)+ var activeUser = await TestUserSeeder.CreateTestUserAsync(dbContext, "activeuser", "[email protected]", configureUser: u => u.LastActivityDate = DateTime.UtcNow.AddDays(-5));- // Create inactive user (no recent activity)- var inactiveUser = new AliasVaultUser- {- UserName = "inactiveuser",- Email = "[email protected]",- LastActivityDate = DateTime.UtcNow.AddDays(-45), // Inactive for 45 days- };- dbContext.AliasVaultUsers.Add(inactiveUser);+ // Create inactive user (no recent activity for 45 days)+ var inactiveUser = await TestUserSeeder.CreateTestUserAsync(dbContext, "inactiveuser", "[email protected]", configureUser: u => u.LastActivityDate = DateTime.UtcNow.AddDays(-45));- await dbContext.SaveChangesAsync();-- // Create encryption key- var encryptionKey = new UserEncryptionKey- {- Id = Guid.NewGuid(),- UserId = activeUser.Id,- PublicKey = "test-encryption-key",- CreatedAt = DateTime.UtcNow,- UpdatedAt = DateTime.UtcNow,- };- dbContext.UserEncryptionKeys.Add(encryptionKey);+ // The emails' key wraps all reference the active user's delivery key, mirroring the single encryption key used before.+ var deliveryKey = activeUser.DeliveryKey;- // Create email claims for each user- dbContext.UserEmailClaims.Add(new UserEmailClaim { UserId = activeUser.Id, Address = "[email protected]", AddressLocal = "activeuser", AddressDomain = "test.com" });- dbContext.UserEmailClaims.Add(new UserEmailClaim { UserId = inactiveUser.Id, Address = "[email protected]", AddressLocal = "inactiveuser", AddressDomain = "test.com" });+ // Create email claims for each user, linked to their personal manifest+ dbContext.EmailClaims.Add(TestUserSeeder.CreateEmailClaim(activeUser.Manifest.ManifestId, "[email protected]"));+ dbContext.EmailClaims.Add(TestUserSeeder.CreateEmailClaim(inactiveUser.Manifest.ManifestId, "[email protected]"));// Create 20 emails for active userfor (int i = 0; i < 20; i++){var dateCreated = DateTime.UtcNow.AddDays(-i);- dbContext.Emails.Add(CreateTestEmail("[email protected]", encryptionKey, $"Active User Email {i}", dateCreated));+ dbContext.Emails.Add(CreateTestEmail("[email protected]", deliveryKey, $"Active User Email {i}", dateCreated));}// Create 15 emails for inactive user (should be reduced to MaxEmailsPerInactiveUser)for (int i = 0; i < 15; i++){var dateCreated = DateTime.UtcNow.AddDays(-i);- dbContext.Emails.Add(CreateTestEmail("[email protected]", encryptionKey, $"Inactive User Email {i}", dateCreated));+ dbContext.Emails.Add(CreateTestEmail("[email protected]", deliveryKey, $"Inactive User Email {i}", dateCreated));}await dbContext.SaveChangesAsync();diff --git a/apps/server/Tests/AliasVault.IntegrationTests/TestUserSeeder.cs b/apps/server/Tests/AliasVault.IntegrationTests/TestUserSeeder.csnew file mode 100644index 000000000..9f65254c6--- /dev/null+++ b/apps/server/Tests/AliasVault.IntegrationTests/TestUserSeeder.cs@@ -0,0 +1,85 @@+//-----------------------------------------------------------------------+// <copyright file="TestUserSeeder.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;++using AliasServerDb;++/// <summary>+/// Helper that seeds a test user with their personal group, owner membership, vault manifest and primary delivery key.+/// </summary>+public static class TestUserSeeder+{+ /// <summary>+ /// Creates and persists a user together with their personal group, owner membership, vault manifest and primary delivery key.+ /// </summary>+ /// <param name="dbContext">The database context to create the entities in.</param>+ /// <param name="userName">The username, also used as the personal group name.</param>+ /// <param name="email">The user's email address.</param>+ /// <param name="publicKey">The public key to publish as the manifest's primary delivery key.</param>+ /// <param name="configureUser">Optional callback to set additional user properties (e.g. activity dates) before saving.</param>+ /// <param name="configureGroup">Optional callback to set additional group properties (e.g. email limits) before saving.</param>+ /// <returns>The created entity graph.</returns>+ public static async Task<(AliasVaultUser User, Group PersonalGroup, VaultManifest Manifest, VaultManifestDeliveryKey DeliveryKey)> CreateTestUserAsync(+ AliasServerDbContext dbContext,+ string userName,+ string email,+ string publicKey = "test-encryption-key",+ Action<AliasVaultUser>? configureUser = null,+ Action<Group>? configureGroup = null)+ {+ var now = DateTime.UtcNow;++ // Every user requires a personal group that owns their vault content and email claims.+ var group = new Group { Id = Guid.NewGuid(), Name = userName, Type = GroupType.Personal, CreatedAt = now, UpdatedAt = now };+ configureGroup?.Invoke(group);+ dbContext.Groups.Add(group);+ await dbContext.SaveChangesAsync();++ var user = new AliasVaultUser { UserName = userName, Email = email, PersonalGroupId = group.Id };+ configureUser?.Invoke(user);+ dbContext.AliasVaultUsers.Add(user);+ await dbContext.SaveChangesAsync();++ dbContext.GroupMembers.Add(new GroupMember { Id = Guid.NewGuid(), GroupId = group.Id, UserId = user.Id, Role = GroupRole.Owner, CreatedAt = now, UpdatedAt = now });++ // The personal group owns a single manifest; email claims link to it and delivery keys scope to it.+ var manifest = new VaultManifest { ManifestId = Guid.NewGuid(), OwnerGroupId = group.Id, StorageFormat = "sqlite-blob", RevisionNumber = 1, CreatedAt = now, UpdatedAt = now };+ dbContext.VaultManifests.Add(manifest);++ // At most one primary delivery key exists per manifest (unique filtered index).+ var deliveryKey = new VaultManifestDeliveryKey { Id = Guid.NewGuid(), VaultManifestId = manifest.ManifestId, PublicKey = publicKey, IsPrimary = true, CreatedAt = now, UpdatedAt = now };+ dbContext.VaultManifestDeliveryKeys.Add(deliveryKey);+ await dbContext.SaveChangesAsync();++ return (user, group, manifest, deliveryKey);+ }++ /// <summary>+ /// Creates an email claim linked to the given manifest. The claim is not yet added to the database context.+ /// </summary>+ /// <param name="vaultManifestId">The manifest that claims the alias.</param>+ /// <param name="address">The full email address; the local and domain parts are derived from it.</param>+ /// <param name="disabled">Whether the claim is disabled.</param>+ /// <param name="createdAt">Optional created/updated timestamp; defaults to now.</param>+ /// <returns>The new email claim carrying a single link to the manifest.</returns>+ public static EmailClaim CreateEmailClaim(Guid vaultManifestId, string address, bool disabled = false, DateTime? createdAt = null)+ {+ var timestamp = createdAt ?? DateTime.UtcNow;+ var addressParts = address.Split('@');+ return new EmailClaim+ {+ Address = address,+ AddressLocal = addressParts[0],+ AddressDomain = addressParts[1],+ Disabled = disabled,+ CreatedAt = timestamp,+ UpdatedAt = timestamp,+ Links = [new EmailClaimLink { VaultManifestId = vaultManifestId }],+ };+ }+}