Skip to content

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

Add account delete E2E test (#373)

Leendert de Borstd765978e63ae
diff --git a/src/AliasVault.Client/Main/Pages/Settings/Security/DeleteAccount.razor b/src/AliasVault.Client/Main/Pages/Settings/Security/DeleteAccount.razor
index d7399495d..3cbf5941b 100644
--- a/src/AliasVault.Client/Main/Pages/Settings/Security/DeleteAccount.razor
+++ b/src/AliasVault.Client/Main/Pages/Settings/Security/DeleteAccount.razor
@@ -32,13 +32,10 @@
</ul>
</div>
- <EditForm Model="@_usernameModel" OnValidSubmit="@ConfirmUsername">
- <DataAnnotationsValidator />
- <ValidationSummary />
-
+ <EditForm Model="@_usernameModel" OnSubmit="@ConfirmUsername">
<div class="mb-4">
<label class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Confirm your username</label>
- <InputText @bind-Value="_usernameModel.Username" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-primary-500 focus:border-primary-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" />
+ <InputText id="username" @bind-Value="_usernameModel.Username" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-primary-500 focus:border-primary-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" />
</div>
<div class="flex space-x-3">
@@ -66,7 +63,7 @@
<div class="mb-4">
<label class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Enter your password</label>
- <InputText type="password" @bind-Value="_passwordModel.Password" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-primary-500 focus:border-primary-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" />
+ <InputText id="password" type="password" @bind-Value="_passwordModel.Password" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-primary-500 focus:border-primary-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" />
</div>
<div class="flex space-x-3">
@@ -79,10 +76,29 @@
</div>
@code {
+ /// <summary>
+ /// The model for the username confirmation step.
+ /// </summary>
private readonly DeleteAccountUsernameModel _usernameModel = new();
+
+ /// <summary>
+ /// The model for the password confirmation step.
+ /// </summary>
private readonly DeleteAccountPasswordModel _passwordModel = new();
+
+ /// <summary>
+ /// Whether to show the password confirmation step.
+ /// </summary>
private bool _showPasswordConfirm;
+
+ /// <summary>
+ /// The ephemeral client for SRP.
+ /// </summary>
private SrpEphemeral ClientEphemeral { get; set; } = null!;
+
+ /// <summary>
+ /// The session client for SRP.
+ /// </summary>
private SrpSession ClientSession { get; set; } = null!;
/// <inheritdoc />
@@ -92,20 +108,31 @@
BreadcrumbItems.Add(new BreadcrumbItem { DisplayName = "Security settings", Url = "/settings/security" });
BreadcrumbItems.Add(new BreadcrumbItem { DisplayName = "Delete Account" });
-
- // Set the current username for form validation.
- var username = await GetUsernameAsync();
- DeleteAccountUsernameModel.SetCurrentUsername(username);
}
/// <summary>
/// Confirms the username for account deletion.
/// </summary>
- private void ConfirmUsername()
+ private async Task ConfirmUsername()
{
- Console.WriteLine("ConfirmUsername step begin");
GlobalNotificationService.ClearMessages();
+
+ if (string.IsNullOrEmpty(_usernameModel.Username))
+ {
+ GlobalNotificationService.AddErrorMessage("Username is required", true);
+ return;
+ }
+
+ var username = await GetUsernameAsync();
+ var usernameMatches = string.Equals(_usernameModel.Username.Trim(), username.Trim(), StringComparison.OrdinalIgnoreCase);
+ if (!usernameMatches)
+ {
+ GlobalNotificationService.AddErrorMessage("The username you entered does not match your current username. Please try again.", true);
+ return;
+ }
+
_showPasswordConfirm = true;
+ StateHasChanged();
}
/// <summary>
@@ -187,45 +214,11 @@
/// </summary>
public class DeleteAccountUsernameModel
{
- private static string? _currentUsername;
-
/// <summary>
/// Gets or sets the username.
/// </summary>
[Required(ErrorMessage = "Username is required")]
- [CustomValidation(typeof(DeleteAccountUsernameModel), nameof(ValidateUsername))]
public string Username { get; set; } = string.Empty;
-
- /// <summary>
- /// Sets the current username which the form input is validated against.
- /// </summary>
- /// <param name="username"></param>
- public static void SetCurrentUsername(string username)
- {
- _currentUsername = username;
- }
-
- /// <summary>
- /// Validates the username against the current username.
- /// </summary>
- /// <param name="value">The username to validate.</param>
- /// <param name="context">The validation context.</param>
- /// <returns>A validation result.</returns>
- public static ValidationResult ValidateUsername(string? value, ValidationContext context)
- {
- if (string.IsNullOrEmpty(_currentUsername))
- {
- return new ValidationResult("Current username not set.");
- }
-
- var usernameMatches = string.Equals(value?.Trim(), _currentUsername.Trim(), StringComparison.OrdinalIgnoreCase);
- if (!usernameMatches)
- {
- return new ValidationResult("The username you entered does not match your current username. Please try again.");
- }
-
- return ValidationResult.Success!;
- }
}
/// <summary>
diff --git a/src/Shared/AliasVault.Shared/Models/WebApi/Auth/DeleteAccountRequest.cs b/src/Shared/AliasVault.Shared/Models/WebApi/Auth/DeleteAccountRequest.cs
index 5c30d9f24..de16698ea 100644
--- a/src/Shared/AliasVault.Shared/Models/WebApi/Auth/DeleteAccountRequest.cs
+++ b/src/Shared/AliasVault.Shared/Models/WebApi/Auth/DeleteAccountRequest.cs
@@ -5,6 +5,8 @@
// </copyright>
//-----------------------------------------------------------------------
+namespace AliasVault.Shared.Models.WebApi.Auth;
+
/// <summary>
/// Request model for deleting an account.
/// </summary>
diff --git a/src/Tests/AliasVault.E2ETests/Tests/Client/Shard4/AuthTests.cs b/src/Tests/AliasVault.E2ETests/Tests/Client/Shard4/AuthTests.cs
index 3c670fbee..190bd8f45 100644
--- a/src/Tests/AliasVault.E2ETests/Tests/Client/Shard4/AuthTests.cs
+++ b/src/Tests/AliasVault.E2ETests/Tests/Client/Shard4/AuthTests.cs
@@ -139,12 +139,103 @@ public class AuthTests : ClientPlaywrightTest
Assert.That(refreshToken.ExpireDate, Is.EqualTo(refreshToken.CreatedAt.AddHours(settings.RefreshTokenLifetimeLong)), "Refresh token expiration date does not match the configured long lifetime while rememberMe was checked.");
}
+ /// <summary>
+ /// Test account self-deletion with various validation scenarios.
+ /// </summary>
+ /// <returns>Async task.</returns>
+ [Test]
+ [Order(5)]
+ public async Task AccountSelfDeletionTest()
+ {
+ // Try deleting with wrong username first
+ await NavigateUsingBlazorRouter("settings/security/delete-account");
+ await WaitForUrlAsync("settings/security/delete-account", "Delete Account");
+
+ // Fill in wrong username
+ var usernameField = await WaitForAndGetElement("input[id='username']");
+ await usernameField.FillAsync("[email protected]");
+
+ var deleteButton = await WaitForAndGetElement("button[type='submit']");
+ await deleteButton.ClickAsync();
+
+ // Check for error message about wrong username
+ var warning = await Page.TextContentAsync("div[role='alert']");
+ Assert.That(warning, Does.Contain("The username you entered does not match your current username"), "No warning shown when attempting to delete account with wrong username.");
+
+ // Try with correct username
+ await usernameField.FillAsync(TestUserUsername);
+ await deleteButton.ClickAsync();
+
+ // Check that we are now on the delete account confirmation page
+ await WaitForUrlAsync("settings/security/delete-account", "Final warning");
+
+ // Fill in wrong password
+ var passwordField = await WaitForAndGetElement("input[id='password']");
+ await passwordField.FillAsync("wrongpassword");
+
+ var confirmButton = await WaitForAndGetElement("button[type='submit']");
+ await confirmButton.ClickAsync();
+
+ // Check for error message about wrong password
+ warning = await Page.TextContentAsync("div[role='alert']");
+ Assert.That(warning, Does.Contain("The provided password does not match"), "No warning shown when attempting to delete account with wrong password.");
+
+ // Fill in correct password
+ await passwordField.FillAsync(TestUserPassword);
+ await confirmButton.ClickAsync();
+
+ // Should be redirected to the start page after successful deletion
+ await WaitForUrlAsync("user/start", "Create new vault");
+
+ // Verify that the API contains auth record for deleted account
+ var authLogEntry = await ApiDbContext.AuthLogs.FirstOrDefaultAsync(x => x.Username == TestUserUsername && x.EventType == AuthEventType.AccountDeletion);
+ Assert.That(authLogEntry, Is.Not.Null, "Auth log entry not found in database after deleting account.");
+
+ // Verify that we can't login with the deleted account
+ await NavigateToLogin();
+ var loginUsernameField = await WaitForAndGetElement("input[id='email']");
+ var loginPasswordField = await WaitForAndGetElement("input[id='password']");
+ await loginUsernameField.FillAsync(TestUserUsername);
+ await loginPasswordField.FillAsync(TestUserPassword);
+ var loginButton = await WaitForAndGetElement("button[type='submit']");
+ await loginButton.ClickAsync();
+
+ warning = await Page.TextContentAsync("div[role='alert']");
+ Assert.That(warning, Does.Contain("Invalid username or password"), "No error shown when attempting to login with deleted account.");
+ }
+
+ /// <summary>
+ /// Re-register the test account after deletion.
+ /// </summary>
+ /// <returns>Async task.</returns>
+ [Test]
+ [Order(6)]
+ public async Task ReRegisterAfterDeletion()
+ {
+ // Re-register with the same credentials
+ await Register(checkForSuccess: true);
+
+ // Logout
+ await Logout();
+
+ // Verify we can log in with the new account
+ await Login();
+ await WaitForUrlAsync("**", "Credentials");
+
+ var pageContent = await Page.TextContentAsync("body");
+ Assert.That(pageContent, Does.Contain(WelcomeMessage), "No welcome message shown after re-registering deleted account.");
+
+ // Verify that a new auth log entry was created for the registration
+ var authLogEntry = await ApiDbContext.AuthLogs.FirstOrDefaultAsync(x => x.Username == TestUserUsername && x.EventType == AuthEventType.Register);
+ Assert.That(authLogEntry, Is.Not.Null, "No auth log entry found for re-registration.");
+ }
+
/// <summary>
/// Test if registering an account with the same email address as an existing account shows a warning.
/// </summary>
/// <returns>Async task.</returns>
[Test]
- [Order(5)]
+ [Order(7)]
public async Task RegisterFormWarningTest()
{
await Logout();
@@ -159,12 +250,16 @@ public class AuthTests : ClientPlaywrightTest
/// </summary>
/// <returns>Async task.</returns>
[Test]
- [Order(6)]
+ [Order(8)]
public async Task PasswordAuthLockoutTest()
{
await Logout();
await NavigateToLogin();
+ // Delete all auth records for the deleted account to clean up for the test.
+ ApiDbContext.AuthLogs.RemoveRange(ApiDbContext.AuthLogs.Where(x => x.Username == TestUserUsername));
+ await ApiDbContext.SaveChangesAsync();
+
// Fill in wrong password 11 times. After 11 times, the account should be locked.
// Note: the actual lockout happens on the 10th wrong attempt, but the lockout message is only displayed
// on the next attempt, so we need to try 11 times to see the lockout message.