Skip to content

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

Improve tests (#113)

Leendert de Borst670dea6924bb
diff --git a/.github/workflows/docker-compose-build.yml b/.github/workflows/docker-compose-build.yml
index 24814a2f1..a832b2b96 100644
--- a/.github/workflows/docker-compose-build.yml
+++ b/.github/workflows/docker-compose-build.yml
@@ -60,7 +60,7 @@ jobs:
- name: Test if localhost:8080 (Admin) responds
run: |
# Test if the service on localhost:8080 responds
- http_code=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8080)
+ http_code=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8080/user/login)
if [ "$http_code" -ne 200 ]; then
echo "Service did not respond with expected 200 OK. Check if admin app is configured correctly."
exit 1
diff --git a/src/AliasVault.Admin/Main/Components/RedirectToLogin.razor b/src/AliasVault.Admin/Main/Components/RedirectToLogin.razor
index 63af3de90..b0ef5c6ca 100644
--- a/src/AliasVault.Admin/Main/Components/RedirectToLogin.razor
+++ b/src/AliasVault.Admin/Main/Components/RedirectToLogin.razor
@@ -4,6 +4,14 @@
/// <inheritdoc />
protected override void OnInitialized()
{
- NavigationManager.NavigateTo($"user/login?returnUrl={Uri.EscapeDataString(NavigationManager.Uri)}", forceLoad: true);
+ var returnUrl = NavigationManager.Uri;
+ if (string.IsNullOrWhiteSpace(returnUrl) || returnUrl == "/")
+ {
+ NavigationManager.NavigateTo($"user/login", forceLoad: true);
+ }
+ else
+ {
+ NavigationManager.NavigateTo($"user/login?returnUrl={Uri.EscapeDataString(NavigationManager.Uri)}", forceLoad: true);
+ }
}
}
diff --git a/src/AliasVault.Admin/Main/Components/WorkerStatus/Services.razor b/src/AliasVault.Admin/Main/Components/WorkerStatus/Services.razor
index cbfd3fda3..e1d8e4394 100644
--- a/src/AliasVault.Admin/Main/Components/WorkerStatus/Services.razor
+++ b/src/AliasVault.Admin/Main/Components/WorkerStatus/Services.razor
@@ -15,7 +15,6 @@
@code {
private List<WorkerServiceStatus> serviceStatus = [];
- private DateTime lastRefresh = DateTime.Now;
private bool initInProgress;
private bool firstInitPageDone;
@@ -24,7 +23,22 @@
private string GetSmtpButtonClasses()
{
- return $"cursor-pointer {(smtpPending || !firstInitPageDone ? "bg-gray-600" : (smtpStatus ? "bg-green-600" : "bg-red-600"))}";
+ string buttonClass = "cursor-pointer ";
+
+ if (smtpPending || !firstInitPageDone)
+ {
+ buttonClass += "bg-gray-600";
+ }
+ else if (smtpStatus)
+ {
+ buttonClass += "bg-green-600";
+ }
+ else
+ {
+ buttonClass += "bg-red-600";
+ }
+
+ return buttonClass;
}
private async void SmtpClick()
@@ -49,18 +63,15 @@
if (!smtpPending)
{
initInProgress = true;
-
- //Console.WriteLine("Init services now: " + DateTime.Now);
serviceStatus = await GetServiceStatuses();
// Service status checks if the status is "Started" and was lastAlive
- // (so actually reported itself) in the last 5 minutes..
- var smtpEntry = serviceStatus.FirstOrDefault(x => x.ServiceName == "AliasVault.SmtpService");
+ // (so actually reported itself) in the last 5 minutes.
+ var smtpEntry = serviceStatus.Find(x => x.ServiceName == "AliasVault.SmtpService");
if (smtpEntry != null)
{
smtpStatus = (DateTime.Now <= smtpEntry.Heartbeat.AddMinutes(5) && smtpEntry.CurrentStatus == "Started");
}
- lastRefresh = DateTime.Now;
initInProgress = false;
firstInitPageDone = true;
@@ -112,9 +123,15 @@
await DbContext.SaveChangesAsync();
// Wait for service to have stopped with ACK
- // @TODO: add timeout method?
+ var timeout = DateTime.Now.AddSeconds(30);
while (true)
{
+ if (DateTime.Now > timeout)
+ {
+ // Timeout
+ return false;
+ }
+
var newdbContext = await DbContextFactory.CreateDbContextAsync();
var check = await newdbContext.WorkerServiceStatuses.Where(x => x.ServiceName == serviceName).FirstAsync();
if (check.CurrentStatus == translateStatus)
diff --git a/src/AliasVault.Api/Controllers/AuthController.cs b/src/AliasVault.Api/Controllers/AuthController.cs
index 4a7f4c59b..230183b51 100644
--- a/src/AliasVault.Api/Controllers/AuthController.cs
+++ b/src/AliasVault.Api/Controllers/AuthController.cs
@@ -132,7 +132,7 @@ public class AuthController(IDbContextFactory<AliasServerDbContext> dbContextFac
// Check if the refresh token is valid.
// Remove any existing refresh tokens for this user and device.
var deviceIdentifier = GenerateDeviceIdentifier(Request);
- var existingToken = context.AliasVaultUserRefreshTokens.Where(t => t.UserId == user.Id && t.DeviceIdentifier == deviceIdentifier).FirstOrDefault();
+ var existingToken = context.AliasVaultUserRefreshTokens.FirstOrDefault(t => t.UserId == user.Id && t.DeviceIdentifier == deviceIdentifier);
if (existingToken == null || existingToken.Value != tokenModel.RefreshToken || existingToken.ExpireDate < timeProvider.UtcNow)
{
return Unauthorized("Refresh token expired");
@@ -183,7 +183,7 @@ public class AuthController(IDbContextFactory<AliasServerDbContext> dbContextFac
// Check if the refresh token is valid.
var deviceIdentifier = GenerateDeviceIdentifier(Request);
- var existingToken = context.AliasVaultUserRefreshTokens.Where(t => t.UserId == user.Id && t.DeviceIdentifier == deviceIdentifier).FirstOrDefault();
+ var existingToken = context.AliasVaultUserRefreshTokens.FirstOrDefault(t => t.UserId == user.Id && t.DeviceIdentifier == deviceIdentifier);
if (existingToken == null || existingToken.Value != model.RefreshToken)
{
return Unauthorized("Invalid refresh token");
diff --git a/src/AliasVault.Api/Controllers/RootController.cs b/src/AliasVault.Api/Controllers/RootController.cs
index 955cd8806..9897be395 100644
--- a/src/AliasVault.Api/Controllers/RootController.cs
+++ b/src/AliasVault.Api/Controllers/RootController.cs
@@ -32,7 +32,7 @@ public class RootController(IDbContextFactory<AliasServerDbContext> dbContextFac
try
{
- var appliedMigrations = context.Database.GetAppliedMigrations();
+ var appliedMigrations = await context.Database.GetAppliedMigrationsAsync();
var allMigrations = context.Database.GetMigrations();
if (allMigrations.Except(appliedMigrations).Any())
diff --git a/src/AliasVault.Api/Controllers/TestController.cs b/src/AliasVault.Api/Controllers/TestController.cs
index c80828c32..9fdf2a3b6 100644
--- a/src/AliasVault.Api/Controllers/TestController.cs
+++ b/src/AliasVault.Api/Controllers/TestController.cs
@@ -45,6 +45,6 @@ public class TestController(UserManager<AliasVaultUser> userManager) : Authentic
public IActionResult TestCallError()
{
// Throw an exception here to test error handling.
- throw new Exception("Test error");
+ throw new ApplicationException("Test error");
}
}
diff --git a/src/AliasVault.Api/Program.cs b/src/AliasVault.Api/Program.cs
index d6d87f8c2..47478f1a6 100644
--- a/src/AliasVault.Api/Program.cs
+++ b/src/AliasVault.Api/Program.cs
@@ -181,7 +181,7 @@ app.MapControllers();
using (var scope = app.Services.CreateScope())
{
var container = scope.ServiceProvider;
- var db = container.GetRequiredService<IDbContextFactory<AliasServerDbContext>>().CreateDbContext();
+ var db = await container.GetRequiredService<IDbContextFactory<AliasServerDbContext>>().CreateDbContextAsync();
await db.Database.MigrateAsync();
}
diff --git a/src/Tests/AliasVault.E2ETests/Common/AdminPlaywrightTest.cs b/src/Tests/AliasVault.E2ETests/Common/AdminPlaywrightTest.cs
index 74af29ef2..e748600b2 100644
--- a/src/Tests/AliasVault.E2ETests/Common/AdminPlaywrightTest.cs
+++ b/src/Tests/AliasVault.E2ETests/Common/AdminPlaywrightTest.cs
@@ -96,6 +96,9 @@ public class AdminPlaywrightTest : PlaywrightTest
await submitButton.ClickAsync();
// Wait for the dashboard to load.
- await WaitForUrlAsync("**", "Welcome to the AliasVault admin portal");
+ await WaitForUrlAsync("**", "Welcome");
+
+ var pageContent = await Page.TextContentAsync("body");
+ Assert.That(pageContent, Does.Contain("Welcome to the AliasVault admin portal"), "No entry page content visible after logging in to admin app.");
}
}
diff --git a/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationAdminFactoryFixture.cs b/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationAdminFactoryFixture.cs
index 35b518e2b..71af6b8de 100644
--- a/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationAdminFactoryFixture.cs
+++ b/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationAdminFactoryFixture.cs
@@ -74,6 +74,7 @@ public class WebApplicationAdminFactoryFixture<TEntryPoint> : WebApplicationFact
public override ValueTask DisposeAsync()
{
_dbConnection.Dispose();
+ GC.SuppressFinalize(this);
return base.DisposeAsync();
}
diff --git a/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationApiFactoryFixture.cs b/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationApiFactoryFixture.cs
index 3aa2d73dc..014a5e927 100644
--- a/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationApiFactoryFixture.cs
+++ b/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationApiFactoryFixture.cs
@@ -80,6 +80,7 @@ public class WebApplicationApiFactoryFixture<TEntryPoint> : WebApplicationFactor
public override ValueTask DisposeAsync()
{
_dbConnection.Dispose();
+ GC.SuppressFinalize(this);
return base.DisposeAsync();
}
diff --git a/src/Tests/AliasVault.E2ETests/Tests/Admin/AuthTests.cs b/src/Tests/AliasVault.E2ETests/Tests/Admin/AuthTests.cs
index e20d46d4a..c0ff39567 100644
--- a/src/Tests/AliasVault.E2ETests/Tests/Admin/AuthTests.cs
+++ b/src/Tests/AliasVault.E2ETests/Tests/Admin/AuthTests.cs
@@ -35,7 +35,10 @@ public class AuthTests : AdminPlaywrightTest
await submitButton.ClickAsync();
// Wait for current page to refresh and confirm message shows.
- await WaitForUrlAsync("account/manage/change-password", "Error: incorrect password.");
+ await WaitForUrlAsync("account/manage/change-password", "Error");
+
+ var pageContent = await Page.TextContentAsync("body");
+ Assert.That(pageContent, Does.Contain("Error: Incorrect password."), "No error shown after submitting change password field with wrong old password.");
}
/// <summary>
@@ -59,7 +62,10 @@ public class AuthTests : AdminPlaywrightTest
await submitButton.ClickAsync();
// Wait for current page to refresh and confirm message shows.
- await WaitForUrlAsync("account/manage/change-password", "Your password has been changed.");
+ await WaitForUrlAsync("account/manage/change-password", "Your password");
+
+ var pageContent = await Page.TextContentAsync("body");
+ Assert.That(pageContent, Does.Contain("Your password has been changed."), "No success message shown after successfully changing password.");
// Set new password for next tests.
TestUserPassword = "newnewnew";
@@ -84,5 +90,8 @@ public class AuthTests : AdminPlaywrightTest
// Wait and check if we get redirected to /user/login.
await WaitForUrlAsync("user/login**", "Sign in to");
+
+ var pageContent = await Page.TextContentAsync("body");
+ Assert.That(pageContent, Does.Contain("Sign in to"), "No login page visible after logout.");
}
}
diff --git a/src/Tests/AliasVault.E2ETests/Tests/Admin/EmailLogTests.cs b/src/Tests/AliasVault.E2ETests/Tests/Admin/EmailLogTests.cs
index bcdeaded9..546b8f0c9 100644
--- a/src/Tests/AliasVault.E2ETests/Tests/Admin/EmailLogTests.cs
+++ b/src/Tests/AliasVault.E2ETests/Tests/Admin/EmailLogTests.cs
@@ -24,5 +24,8 @@ public class EmailLogTests : AdminPlaywrightTest
// Navigate to emails page.
await NavigateBrowser("emails");
await WaitForUrlAsync("emails", "received mails");
+
+ var pageContent = await Page.TextContentAsync("body");
+ Assert.That(pageContent, Does.Contain("received mails"), "No email page content found.");
}
}