Skip to content

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

Add E2E test for merge failed situation (#186)

Leendert de Borsta1291b1951b1
diff --git a/src/AliasVault.Client/Services/Database/DbService.cs b/src/AliasVault.Client/Services/Database/DbService.cs
index 42ae84c58..efd1ae4da 100644
--- a/src/AliasVault.Client/Services/Database/DbService.cs
+++ b/src/AliasVault.Client/Services/Database/DbService.cs
@@ -29,6 +29,7 @@ public sealed class DbService : IDisposable
private readonly DbServiceState _state = new();
private readonly Config _config;
private readonly SettingsService _settingsService = new();
+ private readonly GlobalNotificationService _globalNotificationService;
private SqliteConnection _sqlConnection;
private AliasClientDbContext _dbContext;
private long _vaultRevisionNumber;
@@ -43,12 +44,14 @@ public sealed class DbService : IDisposable
/// <param name="jsInteropService">JsInteropService.</param>
/// <param name="httpClient">HttpClient.</param>
/// <param name="config">Config instance.</param>
- public DbService(AuthService authService, JsInteropService jsInteropService, HttpClient httpClient, Config config)
+ /// <param name="globalNotificationService">Global notification service.</param>
+ public DbService(AuthService authService, JsInteropService jsInteropService, HttpClient httpClient, Config config, GlobalNotificationService globalNotificationService)
{
_authService = authService;
_jsInteropService = jsInteropService;
_httpClient = httpClient;
_config = config;
+ _globalNotificationService = globalNotificationService;
// Set the initial state of the database service.
_state.UpdateState(DbServiceState.DatabaseStatus.Uninitialized);
@@ -110,10 +113,13 @@ public sealed class DbService : IDisposable
var sqlConnections = new List<SqliteConnection>();
+ Console.WriteLine("Merging databases...");
+
// Decrypt and instantiate each vault as a separate in-memory SQLite database.
foreach (var vault in vaultsToMerge.Vaults)
{
var decryptedBase64String = await _jsInteropService.SymmetricDecrypt(vault.Blob, _authService.GetEncryptionKeyAsBase64Async());
+ Console.WriteLine($"Decrypted vault {vault.UpdatedAt}.");
var connection = new SqliteConnection("Data Source=:memory:");
connection.Open();
await ImportDbContextFromBase64Async(decryptedBase64String, connection);
@@ -137,6 +143,7 @@ public sealed class DbService : IDisposable
i++;
foreach (var table in tables)
{
+ Console.WriteLine($"Merging table {table}.");
await DbMergeUtility.MergeTable(_sqlConnection, connection, table);
}
}
@@ -179,6 +186,8 @@ public sealed class DbService : IDisposable
await _settingsService.InitializeAsync(this);
_state.UpdateState(DbServiceState.DatabaseStatus.Ready);
+ Console.WriteLine("Databases merged successfully.");
+
// Save the newly merged database to the server.
await SaveDatabaseAsync();
@@ -186,6 +195,8 @@ public sealed class DbService : IDisposable
}
catch (Exception ex)
{
+ _globalNotificationService.AddErrorMessage("Error: your changes could not be saved. Your vault has been changed elsewhere since you have last opened it and the automatic merge has failed. This might be caused by a password change. Please log out and log back in again to get the latest version of your vault.");
+ Console.WriteLine($"Error merging databases: {ex.Message}");
_state.UpdateState(DbServiceState.DatabaseStatus.MergeFailed, $"Error merging databases: {ex.Message}");
return false;
}
diff --git a/src/Tests/AliasVault.E2ETests/Common/ClientPlaywrightTest.cs b/src/Tests/AliasVault.E2ETests/Common/ClientPlaywrightTest.cs
index 953bd7576..c11ea4a1c 100644
--- a/src/Tests/AliasVault.E2ETests/Common/ClientPlaywrightTest.cs
+++ b/src/Tests/AliasVault.E2ETests/Common/ClientPlaywrightTest.cs
@@ -233,6 +233,7 @@ public class ClientPlaywrightTest : PlaywrightTest
{
// Check that we are on the login page after navigating to the base URL.
// We are expecting to not be authenticated and thus to be redirected to the login page.
+ await Page.GotoAsync(AppBaseUrl);
await WaitForUrlAsync("user/login", "Your username");
// Wait for 100ms to ensure the page has loaded.
diff --git a/src/Tests/AliasVault.E2ETests/Tests/Client/DbSyncTests.cs b/src/Tests/AliasVault.E2ETests/Tests/Client/DbSyncTests.cs
index 1e53c079a..86bcb8949 100644
--- a/src/Tests/AliasVault.E2ETests/Tests/Client/DbSyncTests.cs
+++ b/src/Tests/AliasVault.E2ETests/Tests/Client/DbSyncTests.cs
@@ -65,7 +65,7 @@ public class DbSyncTests : ClientPlaywrightTest
}
/// <summary>
- /// Test that client side merge works correctly when two or more conflicting vault versions have been detected.
+ /// Test that client side merge works correctly when two or more clients have updated the same vault.
/// </summary>
/// <returns>Async task.</returns>
[Test]
@@ -82,12 +82,14 @@ public class DbSyncTests : ClientPlaywrightTest
await CreateCredentialEntry(new Dictionary<string, string> { { "service-name", "TestBaseline3" }, { "username", "user3" }, { "email", "email3" }, { "first-name", "firstname3" } });
});
+ // Client 1 updates the vault first.
var client1Vault = await SimulateClient(baselineVault, async () =>
{
await UpdateCredentialEntry("TestBaseline2", new Dictionary<string, string> { { "service-name", "TestBaseMutate2" }, { "username", "usermutate2" }, { "email", "emailmutate2" }, { "first-name", "firstnamemutate2" } });
});
- await SimulateClient(baselineVault, async () =>
+ // Then client 2 updates the same vault causing a conflict and requiring a client-side merge.
+ var client2Vault = await SimulateClient(baselineVault, async () =>
{
// Re-add client1 vault to simulate conflict when this second client updates the same vault.
client1Vault.Id = Guid.NewGuid();
@@ -97,10 +99,21 @@ public class DbSyncTests : ClientPlaywrightTest
await UpdateCredentialEntry("TestBaseline3", new Dictionary<string, string> { { "service-name", "TestBaseMutate3" }, { "username", "usermutate3" }, { "email", "emailmutate3" } });
});
+ // Then another client updates the client 1 vault again, which should also cause a conflict with the client 2 vault update.
+ await SimulateClient(client1Vault, async () =>
+ {
+ // Re-add client2 vault to simulate conflict when this third client updates the same vault.
+ client2Vault.Id = Guid.NewGuid();
+ ApiDbContext.Vaults.Add(client2Vault);
+ await ApiDbContext.SaveChangesAsync();
+
+ await UpdateCredentialEntry("TestBaseMutate2", new Dictionary<string, string> { { "service-name", "TestBaseMutate23" }, { "first-name", "firstnamemutate23" } });
+ });
+
// Assert that the two conflicting vaults have been merged and all mutated service names are found.
Dictionary<string, List<string>> expectedStrings = new()
{
- { "TestBaseMutate2", new List<string> { "usermutate2", "[email protected]", "firstnamemutate2" } },
+ { "TestBaseMutate23", new List<string> { "usermutate2", "[email protected]", "firstnamemutate23" } },
{ "TestBaseMutate3", new List<string> { "usermutate3", "[email protected]" } },
};
@@ -124,6 +137,70 @@ public class DbSyncTests : ClientPlaywrightTest
}
}
+ /// <summary>
+ /// Test that merge between two vaults where one has changed the password results in an error.
+ /// </summary>
+ /// <returns>Async task.</returns>
+ [Test]
+ [Order(3)]
+ public async Task DbSyncClientMergePasswordChangeErrorTest()
+ {
+ var baselineVault = await CreateBaselineVault(async () =>
+ {
+ await CreateCredentialEntry(new Dictionary<string, string> { { "service-name", "TestBaseline" } });
+ });
+
+ var client1Vault = await SimulateClient(baselineVault, async () =>
+ {
+ await NavigateUsingBlazorRouter("credentials");
+ await WaitForUrlAsync("credentials", "Find all of your credentials");
+
+ await CreateCredentialEntry(new Dictionary<string, string> { { "service-name", "TestA" } });
+
+ // Attempt to change password.
+ await NavigateUsingBlazorRouter("settings/security/change-password");
+ await WaitForUrlAsync("settings/security/change-password", "Current Password");
+
+ // Fill in the form.
+ var currentPasswordField = Page.Locator("input[id='currentPassword']");
+ var newPasswordField = Page.Locator("input[id='newPassword']");
+ var confirmPasswordField = Page.Locator("input[id='newPasswordConfirm']");
+
+ var newPassword = TestUserPassword + "123";
+
+ await currentPasswordField.FillAsync(TestUserPassword);
+ await newPasswordField.FillAsync(newPassword);
+ await confirmPasswordField.FillAsync(newPassword);
+
+ // Advance time by 1 second manually to ensure the new vault (encrypted with new password) is created in the future.
+ ApiTimeProvider.AdvanceBy(TimeSpan.FromSeconds(1));
+
+ // Click the change password button.
+ var changePasswordButton = Page.Locator("button:has-text('Change Password')");
+ await changePasswordButton.ClickAsync();
+
+ // Wait for success message.
+ await WaitForUrlAsync("settings/security/change-password**", "Password changed successfully.");
+ });
+
+ await SimulateClient(baselineVault, async () =>
+ {
+ // Re-add client1 vault to simulate conflict when this second client updates the same vault.
+ client1Vault.Id = Guid.NewGuid();
+ ApiDbContext.Vaults.Add(client1Vault);
+ await ApiDbContext.SaveChangesAsync();
+
+ await NavigateUsingBlazorRouter("credentials");
+ await WaitForUrlAsync("credentials", "Find all of your credentials");
+
+ await CreateCredentialEntry(new Dictionary<string, string> { { "service-name", "TestB" } });
+ });
+
+ // Assert that merge failed error message is shown.
+ var pageContent = await Page.TextContentAsync("body");
+ Assert.That(pageContent, Does.Contain("your changes could not be saved."), $"Merge failed error expected after another client changed the password but no error message found.");
+ }
+
/// <summary>
/// Create a baseline vault.
/// </summary>
@@ -155,7 +232,9 @@ public class DbSyncTests : ClientPlaywrightTest
await ApiDbContext.SaveChangesAsync();
// Simulate new client.
- await RefreshPageAndUnlockVault();
+ await Logout();
+ await Login();
+ await WaitForUrlAsync("credentials", "Find all of your credentials");
// Execute custom client actions.
await clientActions();