Skip to content

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

Add E2E test for email claim disable logic (#711)

Leendert de Borst1194d54e6fd1
diff --git a/src/AliasVault.Api/Controllers/VaultController.cs b/src/AliasVault.Api/Controllers/VaultController.cs
index d595a30cd..5140df4d5 100644
--- a/src/AliasVault.Api/Controllers/VaultController.cs
+++ b/src/AliasVault.Api/Controllers/VaultController.cs
@@ -406,14 +406,17 @@ public class VaultController(ILogger<VaultController> logger, IAliasServerDbCont
// Get all existing user email claims.
var existingEmailClaims = await context.UserEmailClaims
.Where(x => x.UserId == user.Id)
- .Select(x => x.Address)
.ToListAsync();
+ // Keep track of processed and sanitized email addresses to know which ones still exist.
+ var processedEmailAddresses = new List<string>();
+
// Register new email addresses.
foreach (var email in newEmailAddresses)
{
// Sanitize email address.
var sanitizedEmail = EmailHelper.SanitizeEmail(email);
+ processedEmailAddresses.Add(sanitizedEmail);
// If email address is invalid according to the EmailAddressAttribute, skip it.
if (!new EmailAddressAttribute().IsValid(sanitizedEmail))
@@ -421,9 +424,14 @@ public class VaultController(ILogger<VaultController> logger, IAliasServerDbCont
continue;
}
+ // If email address is already claimed by current user, we don't need to claim it again.
+ if (existingEmailClaims.Any(x => x.Address == sanitizedEmail))
+ {
+ continue;
+ }
+
// Check if the email address is already claimed (by another user).
- var existingClaim = await context.UserEmailClaims
- .FirstOrDefaultAsync(x => x.Address == sanitizedEmail);
+ var existingClaim = await context.UserEmailClaims.FirstOrDefaultAsync(x => x.Address == sanitizedEmail);
if (existingClaim != null && existingClaim.UserId != user.Id)
{
@@ -432,11 +440,10 @@ public class VaultController(ILogger<VaultController> logger, IAliasServerDbCont
continue;
}
- if (!existingEmailClaims.Contains(sanitizedEmail))
+ // If we get to this point, the email address is new and not claimed by another user, so we can add it.
+ try
{
- try
- {
- context.UserEmailClaims.Add(new UserEmailClaim
+ context.UserEmailClaims.Add(new UserEmailClaim
{
UserId = user.Id,
Address = sanitizedEmail,
@@ -445,19 +452,27 @@ public class VaultController(ILogger<VaultController> logger, IAliasServerDbCont
CreatedAt = timeProvider.UtcNow,
UpdatedAt = timeProvider.UtcNow,
});
- }
- catch (DbUpdateException ex)
- {
- // Error while adding email claim. Log the error and continue.
- logger.LogWarning(ex, "Error while adding UserEmailClaim with email: {Email} for user: {UserId}.", sanitizedEmail, user.UserName);
- }
+ }
+ catch (DbUpdateException ex)
+ {
+ // Error while adding email claim. Log the error and continue.
+ logger.LogWarning(ex, "Error while adding UserEmailClaim with email: {Email} for user: {UserId}.", sanitizedEmail, user.UserName);
}
}
- // Do not delete email claims that are not in the new list
- // as they may be re-used by the user in the future. We don't want
- // to allow other users to re-use emails used by other users.
+ // Disable email claims that are no longer in the new list and have not been disabled yet.
+ // Important: we do not delete email claims ever, as they may be re-used by the user in the future.
+ // We also don't want to allow other users to re-use emails used by other users.
// Email claims are considered permanent.
+ foreach (var existingClaim in existingEmailClaims.Where(x => !x.Disabled).ToList())
+ {
+ if (!processedEmailAddresses.Contains(existingClaim.Address))
+ {
+ // Email address is no longer in the new list and has not been disabled yet, so disable it.
+ existingClaim.Disabled = true;
+ }
+ }
+
await context.SaveChangesAsync();
}
diff --git a/src/Tests/AliasVault.E2ETests/Tests/Client/Shard1/EmailDecryptionTests.cs b/src/Tests/AliasVault.E2ETests/Tests/Client/Shard1/EmailDecryptionTests.cs
index 08798ea87..21e07b6e9 100644
--- a/src/Tests/AliasVault.E2ETests/Tests/Client/Shard1/EmailDecryptionTests.cs
+++ b/src/Tests/AliasVault.E2ETests/Tests/Client/Shard1/EmailDecryptionTests.cs
@@ -343,6 +343,61 @@ public class EmailDecryptionTests : ClientPlaywrightTest
Assert.That(pageContent, Does.Contain("The current chosen email address is already in use"), "Error message not displayed on page when trying to claim email address already claimed by another user.");
}
+ /// <summary>
+ /// Test that when a user deletes an alias, the email claim gets disabled in the database.
+ /// </summary>
+ /// <returns>Async task.</returns>
+ [Test]
+ [Order(5)]
+ public async Task EmailClaimDeleteDisableTest()
+ {
+ // Create two credential which should automatically create claims on server during database sync.
+ const string serviceName1 = "Test 1 Service";
+ const string email1 = "[email protected]";
+ await CreateCredentialEntry(new Dictionary<string, string>
+ {
+ { "service-name", serviceName1 },
+ { "email", email1 },
+ });
+
+ // Create credential which should automatically create claim on server during database sync.
+ const string serviceName2 = "Test 2 Service";
+ const string email2 = "[email protected]";
+ await CreateCredentialEntry(new Dictionary<string, string>
+ {
+ { "service-name", serviceName2 },
+ { "email", email2 },
+ });
+
+ // Assert that both claims were created on the server.
+ var claim1 = await ApiDbContext.UserEmailClaims.AsNoTracking().FirstOrDefaultAsync(x => x.Address == email1);
+ var claim2 = await ApiDbContext.UserEmailClaims.AsNoTracking().FirstOrDefaultAsync(x => x.Address == email2);
+ Assert.Multiple(() =>
+ {
+ Assert.That(claim1, Is.Not.Null, "Claim for email address not found in database. Check if credential creation and claim creation are working correctly.");
+ Assert.That(claim2, Is.Not.Null, "Claim for email address not found in database. Check if credential creation and claim creation are working correctly.");
+ });
+
+ // Assert that both claims are not disabled.
+ Assert.Multiple(() =>
+ {
+ Assert.That(claim1.Disabled, Is.False, "Claim for new email address is marked disabled after creation.");
+ Assert.That(claim2.Disabled, Is.False, "Claim for new email address is marked disabled after creation.");
+ });
+
+ // Delete the first credential.
+ await DeleteCredentialEntry(serviceName1);
+
+ // Assert that the first claim is now disabled, and second still enabled.
+ claim1 = await ApiDbContext.UserEmailClaims.AsNoTracking().FirstAsync(x => x.Address == email1);
+ claim2 = await ApiDbContext.UserEmailClaims.AsNoTracking().FirstAsync(x => x.Address == email2);
+ Assert.Multiple(() =>
+ {
+ Assert.That(claim1.Disabled, Is.True, "Claim for deleted alias is not marked as disabled after deletion.");
+ Assert.That(claim2.Disabled, Is.False, "Claim for existing email address is marked disabled after deleting another claim.");
+ });
+ }
+
/// <summary>
/// Tear down logic for every test.
/// </summary>