Skip to content

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

Add db sync test (#186)

Leendert de Borst7ee298445938
diff --git a/src/AliasVault.Api/Controllers/AuthController.cs b/src/AliasVault.Api/Controllers/AuthController.cs
index 468bda242..6d6260456 100644
--- a/src/AliasVault.Api/Controllers/AuthController.cs
+++ b/src/AliasVault.Api/Controllers/AuthController.cs
@@ -328,6 +328,7 @@ public class AuthController(IDbContextFactory<AliasServerDbContext> dbContextFac
{
VaultBlob = string.Empty,
Version = "0.0.0",
+ RevisionNumber = 0,
Salt = model.Salt,
Verifier = model.Verifier,
EncryptionType = Defaults.EncryptionType,
diff --git a/src/AliasVault.Api/Controllers/VaultController.cs b/src/AliasVault.Api/Controllers/VaultController.cs
index c1c1a1bbc..d7feb15ea 100644
--- a/src/AliasVault.Api/Controllers/VaultController.cs
+++ b/src/AliasVault.Api/Controllers/VaultController.cs
@@ -202,12 +202,19 @@ public class VaultController(ILogger<VaultController> logger, IDbContextFactory<
return BadRequest(ServerValidationErrorResponse.Create(InvalidCurrentPassword, 400));
}
+ // Calculate the new revision number for the vault.
+ // Note: it is possible multiple clients are updating the vault at the same time which would cause
+ // multiple vaults with the same revision number. This is expected and will trigger a vault
+ // synchronize/merge process on the client side.
+ var newRevisionNumber = model.CurrentRevisionNumber + 1;
+
// Create new vault entry with salt and verifier of current vault.
var newVault = new AliasServerDb.Vault
{
UserId = user.Id,
VaultBlob = model.Blob,
Version = model.Version,
+ RevisionNumber = newRevisionNumber,
CredentialsCount = model.CredentialsCount,
EmailClaimsCount = model.EmailAddressList.Count,
FileSize = FileHelper.Base64StringToKilobytes(model.Blob),
@@ -255,6 +262,7 @@ public class VaultController(ILogger<VaultController> logger, IDbContextFactory<
UserId = x.UserId,
VaultBlob = string.Empty,
Version = x.Version,
+ RevisionNumber = x.RevisionNumber,
FileSize = x.FileSize,
CredentialsCount = x.CredentialsCount,
EmailClaimsCount = x.EmailClaimsCount,
diff --git a/src/Databases/AliasServerDb/Vault.cs b/src/Databases/AliasServerDb/Vault.cs
index aaeadfb43..e2642859d 100644
--- a/src/Databases/AliasServerDb/Vault.cs
+++ b/src/Databases/AliasServerDb/Vault.cs
@@ -51,7 +51,7 @@ public class Vault
/// update a previous version of the vault simultaneously.
/// </summary>
[Required]
- public long RevisionNumber { get; set; }
+ public required long RevisionNumber { get; set; }
/// <summary>
/// Gets or sets the vault filesize in kilobytes.
diff --git a/src/Tests/AliasVault.E2ETests/Tests/Client/DbSyncTests.cs b/src/Tests/AliasVault.E2ETests/Tests/Client/DbSyncTests.cs
new file mode 100644
index 000000000..e458c0f78
--- /dev/null
+++ b/src/Tests/AliasVault.E2ETests/Tests/Client/DbSyncTests.cs
@@ -0,0 +1,53 @@
+//-----------------------------------------------------------------------
+// <copyright file="DbSyncTests.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.E2ETests.Tests.Client;
+
+using Microsoft.EntityFrameworkCore;
+
+/// <summary>
+/// End-to-end tests for the client database synchronization when two or more databases
+/// are saved with the same revision number indicating a conflict that requires a merge.
+/// </summary>
+[TestFixture]
+[Category("ClientTests")]
+[NonParallelizable]
+public class DbSyncTests : ClientPlaywrightTest
+{
+ /// <summary>
+ /// Test if saving a new vault with the same revision number as an existing vault it results
+ /// in two vaults with the same revision number in the server database.
+ /// </summary>
+ /// <returns>Async task.</returns>
+ [Test]
+ [Order(1)]
+ public async Task DbSyncDuplicateVersionNumberTest()
+ {
+ // Advance time by 1 second manually to ensure the new vault is created in the future.
+ ApiTimeProvider.AdvanceBy(TimeSpan.FromSeconds(1));
+
+ // Create a new credential which will trigger a vault save to the server.
+ await CreateCredentialEntry();
+
+ // Assert that the vault is stored in the database with revision number 1.
+ var secondUpdateVault = await ApiDbContext.Vaults.OrderByDescending(x => x.UpdatedAt).FirstAsync();
+ Assert.That(secondUpdateVault.RevisionNumber, Is.EqualTo(1), "Vault revision number is not 1 after the first credential save.");
+
+ // Set the revision number of the vault to 2 (+1) to simulate a conflict where another client has
+ // saved a vault with the same revision number.
+ secondUpdateVault.RevisionNumber = 2;
+ await ApiDbContext.SaveChangesAsync();
+
+ // Create a new credential. The client will provide revision number 1 as the previous one, which should result
+ // in the server giving this new vault revision number 2 as well. Thus resulting in a conflict.
+ await CreateCredentialEntry();
+
+ // Assert that there are now two vaults in the database with revision number 2.
+ var revisionNumber2VaultCount = await ApiDbContext.Vaults.CountAsync(x => x.RevisionNumber == 2);
+ Assert.That(revisionNumber2VaultCount, Is.EqualTo(2), "Two vaults with revision number 2 not found in the database. Check if the server correctly handles conflicts when two clients save a vault with the same revision number.");
+ }
+}